Skip to content

Commit 32f2657

Browse files
committed
test(nodeenv): make the git-bash probe report why it failed
The first CI run of the new job failed with a bare exit 1 and no diagnostic: the probe ran through `bash -c` with the script and the environment path as arguments, so the quoting rules of two command line parsers sat between the test and what the shell ran, and stderr was not captured at all. It now writes the probe to a file, traces it with `set -x` and puts both streams into the assertion message. The direct call guard matches on $0, which a shell reports in the form it was given; the test was passing the native `C:\...` path, which no posix shell would produce.
1 parent e7c7092 commit 32f2657

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

tests/nodeenv_test.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,29 @@ def test_smoke_git_bash(tmpdir):
104104
'-m', 'nodeenv', '--prebuilt', nenv_path,
105105
])
106106

107-
# node.exe and npm report native paths, so both answers can be
108-
# compared with the environment directory as python knows it
109-
script = (
110-
'set -e\n'
111-
'env_dir="$(cygpath "$1")"\n'
112-
'. "$env_dir/Scripts/activate"\n'
107+
# bash reads the script from a file: passing it inline would put the
108+
# quoting rules of two command line parsers between the test and what
109+
# the shell ends up running. `set -x` sends a trace to stderr, which
110+
# is only reported when the probe fails.
111+
# node.exe and npm answer with native paths, so both can be compared
112+
# with the environment directory as python knows it.
113+
probe = tmpdir.join('probe.sh')
114+
probe.write(
115+
'set -ex\n'
116+
'. "%s/Scripts/activate"\n'
113117
'node -p "process.execPath"\n'
114-
'npm root -g\n'
118+
'npm root -g\n' % nenv_path.replace(os.sep, '/')
115119
)
116-
out = subprocess.check_output(['bash', '-c', script, 'bash', nenv_path])
117-
node_exe, npm_root = out.decode('utf-8').splitlines()
118-
120+
proc = subprocess.run(
121+
['bash', probe.strpath.replace(os.sep, '/')],
122+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
123+
report = 'exit %s\n--- stdout ---\n%s\n--- stderr ---\n%s' % (
124+
proc.returncode,
125+
proc.stdout.decode('utf-8', 'replace'),
126+
proc.stderr.decode('utf-8', 'replace'))
127+
128+
assert proc.returncode == 0, report
129+
node_exe, npm_root = proc.stdout.decode('utf-8').splitlines()[-2:]
119130
assert _inside(node_exe, nenv_path), \
120131
'node resolved to %s, outside %s' % (node_exe, nenv_path)
121132
# npm would answer with a path outside the environment if activate

tests/test_install_activate.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,9 @@ def test_win_activate_is_valid_sh(tmpdir, fake_win):
496496

497497

498498
def test_win_activate_refuses_to_be_run_directly(tmpdir, fake_win):
499-
activate = str(_install_win(tmpdir).join('activate'))
499+
# the guard matches on $0, and a shell reports the path it was given:
500+
# from a posix shell on Windows that is the forward slash form
501+
activate = str(_install_win(tmpdir).join('activate')).replace(os.sep, '/')
500502

501503
proc = subprocess.Popen(
502504
['sh', activate], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

0 commit comments

Comments
 (0)