Setting relative window.location.href in envjs

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

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

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

The problem is that setting the full URL of the file to the file works fine, while all my attempts to set the relative path failed. Below are some of my attempts to set the relative url on the return output

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

or

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

could not open file file: //c/source/JasmineTest/SpecRunner.html
Java Exception: java.net.UnknownHostException: c

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

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

Does anyone have any ideas?

thanks

Cedd

PS. Further reading on what I'm trying to do:

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

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

+4
source share
2 answers

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

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

->

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

I hope I understood your request correctly - what does it look like below? (You may need to configure baseURL if I fail a bit from this).

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

Call examples;

 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 is also a longer version, which should be easier to understand, it is 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; } } 
0
source

All Articles