Javascript 在envjs中设置相对window.location.href

Javascript 在envjs中设置相对window.location.href,javascript,jenkins,jasmine,envjs,Javascript,Jenkins,Jasmine,Envjs,我正在使用java运行Envjs,以便在Jasmine中运行javascript单元测试。这将允许我在没有浏览器的情况下运行测试,并使其更容易集成到Jenkins(一个持续集成构建服务器)中 我有一个LoadSpecRunner.js文件(Envjs运行),它只加载实际的jasmine测试运行程序,使用如下代码 window.location.href = 'file:///c:/source/JasmineTest/SpecRunner.html'); 问题是,设置文件的完整url效果很好,

我正在使用java运行Envjs,以便在Jasmine中运行javascript单元测试。这将允许我在没有浏览器的情况下运行测试,并使其更容易集成到Jenkins(一个持续集成构建服务器)中

我有一个LoadSpecRunner.js文件(Envjs运行),它只加载实际的jasmine测试运行程序,使用如下代码

window.location.href = 'file:///c:/source/JasmineTest/SpecRunner.html');
问题是,设置文件的完整url效果很好,而我所有设置相对路径的尝试都失败了。下面是我在返回的输出中设置相对url的一些尝试

window.location.href = Envjs.uri('../JasmineTest/SpecRunner.html');

无法打开文件file://c/source/JasmineTest/SpecRunner.html
Java异常:Java.net.UnknownHostException:c

无法打开文件file://c/:/Source/jasmine-记者/关于:空白
JavaException:java.net.UnknownHostException:c

有人有什么想法吗

谢谢

割让

PS.进一步阅读我正在尝试做的事情:


希望我能正确理解您的查询-下面的内容看起来如何?(如果我把baseURL弄掉一点,你可能需要调整一下)

功能

function resolvePath (relativePath) {
  var protocol = "file:///c:/";
  var baseUrl = "source/JasmineTest";
  var reUpward = /\.\.\//g;
  var upwardCount = (relativePath.match(reUpward) || []).length;
  return protocol + (!upwardCount ? baseUrl : baseUrl.split("/").slice(0, -upwardCount).join("/")) + "/" + relativePath.replace(reUpward, "");
}
示例调用

resolvePath("SpecRunner.html");
// "file:///c:/source/JasmineTest/SpecRunner.html"
resolvePath("path/SpecRunner.html");
// "file:///c:/source/JasmineTest/path/SpecRunner.html"
resolvePath("../../SpecRunner.html");
// "file:///c://SpecRunner.html"
resolvePath("../SpecRunner.html");
// "file:///c:/source/SpecRunner.html"
resolvePath("SpecRunner.html");
// "file:///c:/source/JasmineTest/SpecRunner.html"
这里还有一个较长的版本,应该更容易理解,它与resolvePath相同

function longerVersionOfResolvePath (relativePath) {
  var protocol = "file:///c:/";
  var baseUrl = "source/JasmineTest";
  var reUpward = /\.\.\//g;
  var upwardCount = (relativePath.match(reUpward) || []).length;

  var walkUpwards = upwardCount > 0;
  var relativePathWithUpwardStepsRemoved = relativePath.replace(reUpward, "");
  var folderWalkedUpTo = baseUrl.split("/").slice(0, -upwardCount).join("/");

  if (walkUpwards) {
    return protocol + folderWalkedUpTo + "/" + relativePathWithUpwardStepsRemoved;
  }
  else {
    return protocol + baseUrl + "/" + relativePath;
  }
}

我遇到了同样的问题,并通过修改env.rhino.1.2.js解决了这个问题:

->


你好,谢谢你的关注。据我所知,这段代码仍然假设specrunner.html将驻留在c驱动器上,并且与c:\source\jasminetest相关。我在本地计算机上的代码是c:\source\smart\jasmine\etc,但构建服务器的代码是d:\hudsonworkspace\sharedworkspace\smart\jasmine\etc。我想解析一个与loadspecrunner.js文件恰好位于磁盘上的位置相关的路径……我认为这是不可能的,但请随意证明我错了!美好的我无法让它与“../specrunner.html”类型的路径(例如返回目录树结构的路径)一起工作,但它确实与“specrunner.html”一起工作。
resolvePath("SpecRunner.html");
// "file:///c:/source/JasmineTest/SpecRunner.html"
resolvePath("path/SpecRunner.html");
// "file:///c:/source/JasmineTest/path/SpecRunner.html"
resolvePath("../../SpecRunner.html");
// "file:///c://SpecRunner.html"
resolvePath("../SpecRunner.html");
// "file:///c:/source/SpecRunner.html"
resolvePath("SpecRunner.html");
// "file:///c:/source/JasmineTest/SpecRunner.html"
function longerVersionOfResolvePath (relativePath) {
  var protocol = "file:///c:/";
  var baseUrl = "source/JasmineTest";
  var reUpward = /\.\.\//g;
  var upwardCount = (relativePath.match(reUpward) || []).length;

  var walkUpwards = upwardCount > 0;
  var relativePathWithUpwardStepsRemoved = relativePath.replace(reUpward, "");
  var folderWalkedUpTo = baseUrl.split("/").slice(0, -upwardCount).join("/");

  if (walkUpwards) {
    return protocol + folderWalkedUpTo + "/" + relativePathWithUpwardStepsRemoved;
  }
  else {
    return protocol + baseUrl + "/" + relativePath;
  }
}
if (!base) {
    base = 'file://' +  Envjs.getcwd() + '/';
}
if (!base) {
    base = 'file:///' +  Envjs.getcwd() + '/';
}