Tyler Tyler - 1 month ago 23
Javascript Question

Set environment variables grunt or grunt-exec

I'm trying to use grunt-exec to run a javascript test runner with a deployed link variable passed in.

I am trying to do so by setting an environment variable

grunt.option('link')
using
exec:setLink
. In my
test_runner.js
I grab the variable with
process.env.TEST_LINK
. Unfortunately, it appears that grunt-exec won't run bash commands such as export(?)

Really, I don't care how the variable gets to my
test_runner.js
so any other ideas would be welcome.

exec: {

// DOESN'T WORK: Sets env variable with link for selenium tests
setLink: {
cmd: function () {
return "export TEST_LINK=" + "'" + grunt.option('link') + "'";
}
},
// Integration tests, needs TEST_LINK
selenium: {
cmd: function () {
return "node test/runner/jasmine_runner.js";
}
}

Answer

With grunt-exec, environment variables for the child process can be specified in the env option:

exec: {
  selenium: {
    cmd: function () {
      return "node test/runner/jasmine_runner.js";
    },
    options: {
      env: {
        'TEST_LINK': grunt.option('link')
      }
    }
  }
}

One thing to bear in mind is that if only TEST_LINK is specified in the env option, that will be the only environment variable for the child process. If you want the current process's environment variables to be passed, too, you can do something like this:

exec: {
  selenium: {
    cmd: function () {
      return "node test/runner/jasmine_runner.js";
    },
    options: {
      env: Object.assign({}, process.env, { 'TEST_LINK': grunt.option('link') })
    }
  }
}
Comments