3
3
load " $BATS_PATH /load.bash"
4
4
teardown () {
5
5
rm -f .npmrc
6
+ unset BUILDKITE_PLUGIN_PRIVATE_NPM_ENV
7
+ unset BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN
8
+ unset BUILDKITE_PLUGIN_PRIVATE_NPM_FILE
9
+ unset BUILDKITE_PLUGIN_PRIVATE_NPM_REGISTRY
10
+ unset MY_ENV_VAR
11
+ rm -fr my_token_file
12
+ rm -fr my_empty_file
6
13
}
7
14
8
15
@@ -16,7 +23,65 @@ teardown() {
16
23
assert_equal " $( head -n1 .npmrc) " ' //registry.npmjs.org/:_authToken=abc123'
17
24
}
18
25
19
- @test " crates a npmrc file with supplied registry path and token" {
26
+ @test " reads the token from a file if the file parameter is used" {
27
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_token_file'
28
+ echo ' abc123' > my_token_file
29
+
30
+ run $PWD /hooks/pre-command
31
+
32
+ assert_success
33
+ assert [ -e ' .npmrc' ]
34
+ assert_equal " $( head -n1 .npmrc) " ' //registry.npmjs.org/:_authToken=abc123'
35
+ }
36
+
37
+ @test " fails if the file parameter is used but no file exists" {
38
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_missing_file'
39
+
40
+ run $PWD /hooks/pre-command
41
+
42
+ assert_failure
43
+ }
44
+
45
+ @test " fails if the file parameter is used and the file exists but is empty" {
46
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_empty_file'
47
+
48
+ touch my_empty_file
49
+
50
+ run $PWD /hooks/pre-command
51
+
52
+ assert_failure
53
+ }
54
+
55
+ @test " reads the token from the environment if the env parameter is used" {
56
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_ENV_VAR'
57
+ export MY_ENV_VAR=' abc123'
58
+
59
+ run $PWD /hooks/pre-command
60
+
61
+ assert_success
62
+ assert [ -e ' .npmrc' ]
63
+ assert_equal " $( head -n1 .npmrc) " ' //registry.npmjs.org/:_authToken=abc123'
64
+ }
65
+
66
+ @test " fails if the env parameter is used but no such variable is defined" {
67
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_MISSING_VAR'
68
+
69
+ run $PWD /hooks/pre-command
70
+
71
+ assert_failure
72
+ }
73
+
74
+ @test " fails if the env parameter is used but the value of the variable is empty" {
75
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_EMPTY_VAR'
76
+
77
+ export MY_EMPTY_VAR=" "
78
+
79
+ run $PWD /hooks/pre-command
80
+
81
+ assert_failure
82
+ }
83
+
84
+ @test " creates a npmrc file with supplied registry path and token" {
20
85
export BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN=' abc123'
21
86
export BUILDKITE_PLUGIN_PRIVATE_NPM_REGISTRY=' //myprivateregistry.org/'
22
87
@@ -27,9 +92,80 @@ teardown() {
27
92
assert_equal " $( head -n1 .npmrc) " ' //myprivateregistry.org/:_authToken=abc123'
28
93
}
29
94
30
- @test " the command fails if the token is not set" {
95
+ @test " creates a npmrc file with supplied registry path and env" {
96
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_ENV_VAR'
97
+ export MY_ENV_VAR=' abc123'
98
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_REGISTRY=' //myprivateregistry.org/'
99
+
100
+ run $PWD /hooks/pre-command
101
+
102
+ assert_success
103
+ assert [ -e ' .npmrc' ]
104
+ assert_equal " $( head -n1 .npmrc) " ' //myprivateregistry.org/:_authToken=abc123'
105
+ }
106
+
107
+ @test " creates a npmrc file with supplied registry path and file" {
108
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_token_file'
109
+ echo ' abc123' > my_token_file
110
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_REGISTRY=' //myprivateregistry.org/'
111
+
112
+ run $PWD /hooks/pre-command
113
+
114
+ assert_success
115
+ assert [ -e ' .npmrc' ]
116
+ assert_equal " $( head -n1 .npmrc) " ' //myprivateregistry.org/:_authToken=abc123'
117
+ }
118
+
119
+ @test " the command fails if none of the fields are not set" {
120
+ run $PWD /hooks/pre-command
121
+
122
+ assert_failure
123
+ refute [ -e ' .npmrc' ]
124
+ }
125
+
126
+ # There is an exclusive relationship between file, env, and token. These tests ensure only value is set and fail with
127
+ # a meaninful message otherwise
128
+ @test " fails if env and file are both set" {
129
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_token_file'
130
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_ENV_VAR'
131
+
132
+ run $PWD /hooks/pre-command
133
+
134
+ assert_failure
135
+ assert_output ' :no_entry_sign: :npm: :package: Failed! Only one of file, env or token parameters may be set'
136
+ refute [ -e ' .npmrc' ]
137
+ }
138
+
139
+ @test " fails if token and file are both set" {
140
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_token_file'
141
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN=' abc123'
142
+
143
+ run $PWD /hooks/pre-command
144
+
145
+ assert_failure
146
+ assert_output ' :no_entry_sign: :npm: :package: Failed! Only one of file, env or token parameters may be set'
147
+ refute [ -e ' .npmrc' ]
148
+ }
149
+
150
+ @test " fails if env and token are both set" {
151
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN=' abc123'
152
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_ENV_VAR'
153
+
154
+ run $PWD /hooks/pre-command
155
+
156
+ assert_failure
157
+ assert_output ' :no_entry_sign: :npm: :package: Failed! Only one of file, env or token parameters may be set'
158
+ refute [ -e ' .npmrc' ]
159
+ }
160
+
161
+ @test " fails if env, file and token are all set" {
162
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_FILE=' my_token_file'
163
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_ENV=' MY_ENV_VAR'
164
+ export BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN=' abc123'
165
+
31
166
run $PWD /hooks/pre-command
32
167
33
168
assert_failure
169
+ assert_output ' :no_entry_sign: :npm: :package: Failed! Only one of file, env or token parameters may be set'
34
170
refute [ -e ' .npmrc' ]
35
171
}
0 commit comments