Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
test: check that env vars properly passed to scripts
Browse files Browse the repository at this point in the history
* removed legacy-env-reader test
* test that regular env vars are passed down
* test that npm-prefixed vars are stripped
* test that package.json configs are added to env
* test that package.json configs are prefixed with npm_package
  and added to the env

Credit: @zkat
PR-URL: #11292
Reviewed-By: @iarna
  • Loading branch information
zkat authored and iarna committed Feb 18, 2016
1 parent 5a701e7 commit 502d7d0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 89 deletions.
87 changes: 0 additions & 87 deletions test/tap/legacy-env-reader.js

This file was deleted.

67 changes: 65 additions & 2 deletions test/tap/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ var fullyPopulated = {
'prewith-both': 'node -e "console.log(process.argv[1] || \'pre\')"',
'with-both': 'node -e "console.log(process.argv[1] || \'main\')"',
'postwith-both': 'node -e "console.log(process.argv[1] || \'post\')"',
'stop': 'node -e "console.log(process.argv[1] || \'stop\')"'
}
'stop': 'node -e "console.log(process.argv[1] || \'stop\')"',
'env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
'npm-env-vars': 'node -e "console.log(process.env.npm_run_script_foo_var)"',
'package-env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
'prefixed-package-env-vars': 'node -e "console.log(process.env.npm_package_run_script_foo_var)"'
},
'run_script_foo_var': 'run_script_test_foo_val'
}

var lifecycleOnly = {
Expand Down Expand Up @@ -180,6 +185,64 @@ test('npm run-script nonexistent-script with --if-present flag', function (t) {
})
})

test('npm run-script env vars accessible', function (t) {
process.env.run_script_foo_var = 'run_script_test_foo_val'
common.npm(['run-script', 'env-vars'], {
cwd: pkg
}, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without crashing')
t.equal(code, 0, 'exited normally')
t.equal(stderr, '', 'no error output')
t.match(stdout,
new RegExp(process.env.run_script_foo_var),
'script had env access')
t.end()
})
})

test('npm run-script package.json vars injected', function (t) {
common.npm(['run-script', 'package-env-vars'], {
cwd: pkg
}, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without crashing')
t.equal(code, 0, 'exited normally')
t.equal(stderr, '', 'no error output')
t.match(stdout,
new RegExp(fullyPopulated.run_script_foo_var),
'script injected package.json value')
t.end()
})
})

test('npm run-script package.json vars injected with prefix', function (t) {
common.npm(['run-script', 'prefixed-package-env-vars'], {
cwd: pkg
}, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without crashing')
t.equal(code, 0, 'exited normally')
t.equal(stderr, '', 'no error output')
t.match(stdout,
new RegExp(fullyPopulated.run_script_foo_var),
'script injected npm_package-prefixed package.json value')
t.end()
})
})

test('npm run-script env vars stripped npm-prefixed', function (t) {
process.env.npm_run_script_foo_var = 'run_script_test_foo_val'
common.npm(['run-script', 'npm-env-vars'], {
cwd: pkg
}, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without crashing')
t.equal(code, 0, 'exited normally')
t.equal(stderr, '', 'no error output')
t.notMatch(stdout,
new RegExp(process.env.npm_run_script_foo_var),
'script stripped npm-prefixed env var')
t.end()
})
})

test('npm run-script no-params (lifecycle only)', function (t) {
var expected = [
'Lifecycle scripts included in scripted:',
Expand Down

0 comments on commit 502d7d0

Please sign in to comment.