Skip to content

Commit 7d47421

Browse files
authored
Use "WRAPPER_ENABLED" environment variable to control wrapper enablement (#46)
1 parent 39ff80f commit 7d47421

2 files changed

Lines changed: 76 additions & 10 deletions

File tree

nodejs/wrapper.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ type nodeContext struct {
4545
}
4646

4747
func main() {
48-
logrus.SetLevel(logrusLevel())
48+
env := envToMap(os.Environ())
49+
logrus.SetLevel(logrusLevel(env))
50+
4951
logrus.Debugln("Launched: ", os.Args)
5052

51-
env := envToMap(os.Environ())
5253
// suppress npm warnings when node on PATH isn't the node used for npm
5354
env["npm_config_scripts_prepend_node_path"] = "false"
5455
nc := nodeContext{program: os.Args[0], args: os.Args[1:], env: env}
@@ -57,8 +58,13 @@ func main() {
5758
}
5859
}
5960

60-
func logrusLevel() logrus.Level {
61-
v := os.Getenv("WRAPPER_VERBOSE")
61+
func isEnabled(env map[string]string) bool {
62+
v, found := env["WRAPPER_ENABLED"]
63+
return !found || (v != "0" && v != "false" && v != "no")
64+
}
65+
66+
func logrusLevel(env map[string]string) logrus.Level {
67+
v := env["WRAPPER_VERBOSE"]
6268
if v != "" {
6369
if l, err := logrus.ParseLevel(v); err == nil {
6470
return l
@@ -73,6 +79,11 @@ func run(nc *nodeContext, stdin io.Reader, stdout, stderr io.Writer) error {
7379
return fmt.Errorf("could not unwrap: %w", err)
7480
}
7581
logrus.Debugln("unwrapped: ", nc.program)
82+
83+
if !isEnabled(nc.env) {
84+
logrus.Info("wrapper disabled")
85+
return nc.exec(stdin, stdout, stderr)
86+
}
7687

7788
// Use an absolute path in case we're being run within a node_modules directory
7889
// If there's an error, then hand off immediately to the real node.

nodejs/wrapper_test.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"bytes"
21+
"fmt"
2122
"io/ioutil"
2223
"os"
2324
"path/filepath"
@@ -28,6 +29,54 @@ import (
2829
"testing"
2930
)
3031

32+
func TestIsEnabled(t *testing.T) {
33+
tests := []struct {
34+
env map[string]string
35+
expected bool
36+
}{
37+
{
38+
env: nil,
39+
expected: true,
40+
},
41+
{
42+
env: map[string]string{"WRAPPER_ENABLED": "1"},
43+
expected: true,
44+
},
45+
{
46+
env: map[string]string{"WRAPPER_ENABLED": "true"},
47+
expected: true,
48+
},
49+
{
50+
env: map[string]string{"WRAPPER_ENABLED": "yes"},
51+
expected: true,
52+
},
53+
{
54+
env: map[string]string{"WRAPPER_ENABLED": ""},
55+
expected: true,
56+
},
57+
{
58+
env: map[string]string{"WRAPPER_ENABLED": "0"},
59+
expected: false,
60+
},
61+
{
62+
env: map[string]string{"WRAPPER_ENABLED": "no"},
63+
expected: false,
64+
},
65+
{
66+
env: map[string]string{"WRAPPER_ENABLED": "false"},
67+
expected: false,
68+
},
69+
}
70+
for _, test := range tests {
71+
t.Run(fmt.Sprintf("env: %v", test.env), func(t *testing.T) {
72+
result := isEnabled(test.env)
73+
if test.expected != result {
74+
t.Errorf("expected %v but got %v", test.expected, result)
75+
}
76+
})
77+
}
78+
}
79+
3180
func TestFindScript(t *testing.T) {
3281
tests := []struct {
3382
description string
@@ -240,37 +289,37 @@ func TestNodeContext_unwrap(t *testing.T) {
240289
tests := []struct {
241290
description string
242291
input nodeContext
243-
noErr bool
292+
noErr bool
244293
expected string
245294
}{
246295
{
247296
description: "no PATH leaves unchanged",
248297
input: nodeContext{program: originalNode},
249-
noErr: false,
298+
noErr: false,
250299
expected: originalNode,
251300
},
252301
{
253302
description: "empty PATH leaves unchanged",
254303
input: nodeContext{program: originalNode, env: map[string]string{"PATH": ""}},
255-
noErr: false,
304+
noErr: false,
256305
expected: originalNode,
257306
},
258307
{
259308
description: "no other node leaves unchanged",
260309
input: nodeContext{program: originalNode, env: map[string]string{"PATH": root}},
261-
noErr: false,
310+
noErr: false,
262311
expected: originalNode,
263312
},
264313
{
265314
description: "first other node wins",
266315
input: nodeContext{program: originalNode, env: map[string]string{"PATH": root + string(os.PathListSeparator) + binPath + string(os.PathListSeparator) + sbinPath}},
267-
noErr: true,
316+
noErr: true,
268317
expected: binNode,
269318
},
270319
{
271320
description: "first node wins when original not found",
272321
input: nodeContext{program: name, env: map[string]string{"PATH": root + string(os.PathListSeparator) + binPath + string(os.PathListSeparator) + sbinPath}},
273-
noErr: true,
322+
noErr: true,
274323
expected: binNode,
275324
},
276325
}
@@ -497,6 +546,12 @@ done
497546
args: []string{"node_modules/script.js", "--inspect"},
498547
expected: "node_modules/script.js\n--inspect\n",
499548
},
549+
{
550+
description: "node_modules script: inspect left alone with WRAPPER_ENABLED=0",
551+
args: []string{"--inspect=9229", "./node_nodules/script.js"},
552+
env: map[string]string{"WRAPPER_ENABLED": "0"},
553+
expected: "--inspect=9229\n./node_nodules/script.js\n",
554+
},
500555
{
501556
description: "node_modules script with inspect: seeds NODE_DEBUG",
502557
args: []string{"--inspect", "node_modules/script.js"},

0 commit comments

Comments
 (0)