Setting a relative window.location.href in envjs

1.6k Views Asked by At

I am using java to run Envjs in order to run javascript unit tests in Jasmine. This will allow me to run the tests without a browser and make it easier to integrate into Jenkins (a continuous integration build server).

I have a LoadSpecRunner.js file (that Envjs runs) that just loads the actual jasmine test runner, using code like below.

window.location.href = 'file:///c:/source/JasmineTest/SpecRunner.html');

The problem is that setting the full url to the file works fine, whereas all my attempts at setting a relative path have failed. Following are some of my attempts at setting a relative url at the output returned

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

or

window.location.href = '../JasmineTest/SpecRunner.html';

failed to open file file://c/source/JasmineTest/SpecRunner.html
Java Exception: java.net.UnknownHostException: c

window.location.href = window.location.href;

failed to open file file://c/:/Source/jasmine-reporters/about:blank
JavaException: java.net.UnknownHostException: c

Does anybody have any ideas?

Thanks

Cedd

PS. Further reading on what I am trying to do:

http://skaug.no/ingvald/2010/10/javascript_unit_testing/

http://www.build-doctor.com/2010/12/08/javascript-bdd-jasmine/

2

There are 2 best solutions below

1
On BEST ANSWER

I met same problem and resolved it by modify env.rhino.1.2.js:

if (!base) {
    base = 'file://' +  Envjs.getcwd() + '/';
}

->

if (!base) {
    base = 'file:///' +  Envjs.getcwd() + '/';
}
2
On

Hopefully I understood your query properly - how does the below look? (You may need to tweak the baseURL if I got that a little off).

Function;

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, "");
}

Example Calls;

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"

Here's also a longer version which should be easier to understand, it's the same as 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;
  }
}