4

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/

cedd
  • 1,741
  • 1
  • 21
  • 34

2 Answers2

1

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() + '/';
}
卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • Nice. I couldn't get this to work with '../specrunner.html' type paths (eg paths that go back up the directory tree structure), but it did work with 'specrunner.html'. – cedd Jan 27 '14 at 17:23
0

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;
  }
}
Jamie Mason
  • 4,159
  • 2
  • 32
  • 42
  • Hi there, thanks for taking an interest. As I understand this code it still assumes that specrunner.html will reside on the c drive and will be relative to c:\source\jasminetest. I have code on my local machine at c:\source\smart\jasmine\etc but the build server has the code at d:\hudsonworkspace\sharedworkspace\smart\jasmine\etc. I want to resolve a path relative to where the loadspecrunner.js file happens to be on disk ..... – cedd Jul 18 '12 at 14:58
  • For what its worth I believe that it isn't possible, but feel free to prove me wrong! – cedd Jul 18 '12 at 14:59