I'm building a small test framework running Mocha + WebDriverIO via Jenkins. For some reason, the relative path isn't showing up as expected. As a result, fs.js is throwing an error:
Error:
Error: ENOENT, no such file or directory 'C:\workspaces\nodejstest\jenkins\JENKINS_HOME\jobs\browse_test\workspace\login.js'
The path should be: 'C:\workspaces\nodejstest\login.js' (the absolute location of the executed script). Anybody, have any insight as to why the directory path is wrong even though I'm executing the script from the same directory, NOT the Jenkins workspace directory as the error states. When I run it from the cli, it works fine. Weird.
Test Script (run_test.js):
var common = require('./common/common.js'), // INCLUDES FS LIBRARY
scriptList = ['login', 'browse_price_ascending'];
// ITERATE THROUGH SCRIPT NAMES AND EXECUTE
for(var currentScript in scriptList){
// BUILD SCRIPT PATH
var scriptPath = './' + scriptList[currentScript] + '.js';
// EVAL IS NOT ALWAYS EVIL ;)
eval(common.fs.readFile(scriptPath) + '');
}
Start Jenkins (run.sh):
#!/bin/bash
export JENKINS_HOME=./jenkins/JENKINS_HOME
java -jar ./jenkins/jenkins.war
Jenkins Job:
mocha $JENKINS_HOME/../../run_test.js --brand=IBSD --country=UK --env=PROD
In Jenkins, the job's workspace is treated as the current directory. Irrespective of whether you called the parent ($JENKINS_HOME/../../run_test.js) script by specifying the relative path w.r.t Jenkins, any path specified within the script will be treated relative to the Jenkins' job's workspace. So, to resolve your issue, just specify the relative path to login.js in your run_test.js script.
If you do not want to move the login.js script from your existing location to Jenkins workspace, then you will have to specify the relative path to the file w.r.t Jenkins' job's workspace. However, if you want to make it flexible enough to work both ways i.e., through command line and Jenkins, then you can try any of these approach:
In your parent script, you can check whether any Jenkins environment variable (such as JOB_NAME) is set. The purpose is to check whether your script is running as part of some Jenkins job. If yes, then change the path of login.js in your parent script to relative path w.r.t job's workspace.
path_to_login.js = default_location
if (jenkins_env_variable is set) {
path_to_login.js = ../../../../../../login.js
}
Just like other arguments (--brand=IBSD --country=UK --env=PROD) that you are passing to your parent script, you can pass one (such as --jenkins=yes) for checking whether the script is running through Jenkins or command line.
path_to_login.js = default_location
if (jenkins_arg_passed is yes) {
path_to_login.js=../../../../../../login.js
}