@@ -3,86 +3,145 @@ import test from 'ava';
3
3
import { readFile , appendFile } from 'fs-extra' ;
4
4
import { stub } from 'sinon' ;
5
5
import tempy from 'tempy' ;
6
- import setNpmrcAuth from '../lib/set-npmrc-auth' ;
6
+ import clearModule from 'clear-module' ;
7
+
8
+ const { HOME } = process . env ;
9
+ const cwd = process . cwd ( ) ;
7
10
8
11
test . beforeEach ( t => {
9
12
// Stub the logger
10
13
t . context . log = stub ( ) ;
11
14
t . context . logger = { log : t . context . log } ;
15
+
16
+ clearModule ( 'rc' ) ;
17
+ clearModule ( '../lib/set-npmrc-auth' ) ;
18
+ } ) ;
19
+
20
+ test . afterEach . always ( ( ) => {
21
+ process . env . HOME = HOME ;
22
+ process . chdir ( cwd ) ;
12
23
} ) ;
13
24
14
- test ( 'Set auth with "NPM_TOKEN"' , async t => {
25
+ test . serial ( 'Set auth with "NPM_TOKEN"' , async t => {
26
+ process . env . HOME = tempy . directory ( ) ;
15
27
const cwd = tempy . directory ( ) ;
28
+ process . chdir ( cwd ) ;
16
29
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
17
30
const env = { NPM_TOKEN : 'npm_token' } ;
18
31
19
- await setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } ) ;
32
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } ) ;
20
33
21
34
t . regex ( ( await readFile ( npmrc ) ) . toString ( ) , / \/ \/ c u s t o m .r e g i s t r y .c o m \/ : _ a u t h T o k e n = \$ \{ N P M _ T O K E N \} / ) ;
22
35
t . deepEqual ( t . context . log . args [ 1 ] , [ `Wrote NPM_TOKEN to ${ npmrc } ` ] ) ;
23
36
} ) ;
24
37
25
- test ( 'Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"' , async t => {
38
+ test . serial ( 'Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"' , async t => {
39
+ process . env . HOME = tempy . directory ( ) ;
26
40
const cwd = tempy . directory ( ) ;
41
+ process . chdir ( cwd ) ;
27
42
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
28
43
const env = { NPM_USERNAME : 'npm_username' , NPM_PASSWORD : 'npm_pasword' , NPM_EMAIL : 'npm_email' } ;
29
44
30
- await setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } ) ;
45
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } ) ;
31
46
32
47
t . is ( ( await readFile ( npmrc ) ) . toString ( ) , `\n_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}` ) ;
33
48
t . deepEqual ( t . context . log . args [ 1 ] , [ `Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${ npmrc } ` ] ) ;
34
49
} ) ;
35
50
36
- test ( 'Copy ".npmrc" if auth is already configured' , async t => {
51
+ test . serial ( 'Preserve home ".npmrc"' , async t => {
52
+ process . env . HOME = tempy . directory ( ) ;
53
+ const cwd = tempy . directory ( ) ;
54
+ process . chdir ( cwd ) ;
55
+ const npmrc = tempy . file ( { name : '.npmrc' } ) ;
56
+ const env = { NPM_TOKEN : 'npm_token' } ;
57
+
58
+ await appendFile ( path . resolve ( process . env . HOME , '.npmrc' ) , 'home_config = test' ) ;
59
+
60
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } ) ;
61
+
62
+ t . is ( ( await readFile ( npmrc ) ) . toString ( ) , `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
63
+ t . deepEqual ( t . context . log . args [ 1 ] , [ `Wrote NPM_TOKEN to ${ npmrc } ` ] ) ;
64
+ } ) ;
65
+
66
+ test . serial ( 'Preserve home and local ".npmrc"' , async t => {
67
+ process . env . HOME = tempy . directory ( ) ;
37
68
const cwd = tempy . directory ( ) ;
69
+ process . chdir ( cwd ) ;
70
+ const npmrc = tempy . file ( { name : '.npmrc' } ) ;
71
+ const env = { NPM_TOKEN : 'npm_token' } ;
72
+
73
+ await appendFile ( path . resolve ( cwd , '.npmrc' ) , 'cwd_config = test' ) ;
74
+ await appendFile ( path . resolve ( process . env . HOME , '.npmrc' ) , 'home_config = test' ) ;
75
+
76
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } ) ;
77
+
78
+ t . is (
79
+ ( await readFile ( npmrc ) ) . toString ( ) ,
80
+ `home_config = test\ncwd_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
81
+ ) ;
82
+ t . deepEqual ( t . context . log . args [ 1 ] , [ `Wrote NPM_TOKEN to ${ npmrc } ` ] ) ;
83
+ } ) ;
84
+
85
+ test . serial ( 'Preserve all ".npmrc" if auth is already configured' , async t => {
86
+ process . env . HOME = tempy . directory ( ) ;
87
+ const cwd = tempy . directory ( ) ;
88
+ process . chdir ( cwd ) ;
38
89
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
39
90
40
91
await appendFile ( path . resolve ( cwd , '.npmrc' ) , `//custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
92
+ await appendFile ( path . resolve ( process . env . HOME , '.npmrc' ) , 'home_config = test' ) ;
41
93
42
- await setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env : { } , logger : t . context . logger } ) ;
94
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env : { } , logger : t . context . logger } ) ;
43
95
44
- t . is ( ( await readFile ( npmrc ) ) . toString ( ) , `//custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
96
+ t . is ( ( await readFile ( npmrc ) ) . toString ( ) , `home_config = test\n //custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
45
97
t . is ( t . context . log . callCount , 1 ) ;
46
98
} ) ;
47
99
48
- test ( 'Copy ".npmrc" if auth is already configured for a scoped package' , async t => {
100
+ test . serial ( 'Preserve ".npmrc" if auth is already configured for a scoped package' , async t => {
101
+ process . env . HOME = tempy . directory ( ) ;
49
102
const cwd = tempy . directory ( ) ;
103
+ process . chdir ( cwd ) ;
50
104
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
51
105
52
106
await appendFile (
53
107
path . resolve ( cwd , '.npmrc' ) ,
54
108
`@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
55
109
) ;
110
+ await appendFile ( path . resolve ( process . env . HOME , '.npmrc' ) , 'home_config = test' ) ;
56
111
57
- await setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env : { } , logger : t . context . logger } ) ;
112
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env : { } , logger : t . context . logger } ) ;
58
113
59
114
t . is (
60
115
( await readFile ( npmrc ) ) . toString ( ) ,
61
- `@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
116
+ `home_config = test\n @scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
62
117
) ;
63
118
t . is ( t . context . log . callCount , 1 ) ;
64
119
} ) ;
65
120
66
- test ( 'Throw error if "NPM_TOKEN" is missing' , async t => {
121
+ test . serial ( 'Throw error if "NPM_TOKEN" is missing' , async t => {
122
+ process . env . HOME = tempy . directory ( ) ;
67
123
const cwd = tempy . directory ( ) ;
124
+ process . chdir ( cwd ) ;
68
125
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
69
126
70
127
const [ error ] = await t . throwsAsync (
71
- setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env : { } , logger : t . context . logger } )
128
+ require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env : { } , logger : t . context . logger } )
72
129
) ;
73
130
74
131
t . is ( error . name , 'SemanticReleaseError' ) ;
75
132
t . is ( error . message , 'No npm token specified.' ) ;
76
133
t . is ( error . code , 'ENONPMTOKEN' ) ;
77
134
} ) ;
78
135
79
- test ( 'Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set' , async t => {
136
+ test . serial ( 'Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set' , async t => {
137
+ process . env . HOME = tempy . directory ( ) ;
80
138
const cwd = tempy . directory ( ) ;
139
+ process . chdir ( cwd ) ;
81
140
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
82
141
83
142
await appendFile ( path . resolve ( cwd , '.custom-npmrc' ) , `//custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
84
143
85
- await setNpmrcAuth ( npmrc , 'http://custom.registry.com' , {
144
+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , {
86
145
cwd,
87
146
env : { NPM_CONFIG_USERCONFIG : path . resolve ( cwd , '.custom-npmrc' ) } ,
88
147
logger : t . context . logger ,
@@ -92,41 +151,47 @@ test('Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set', async t
92
151
t . is ( t . context . log . callCount , 1 ) ;
93
152
} ) ;
94
153
95
- test ( 'Throw error if "NPM_USERNAME" is missing' , async t => {
154
+ test . serial ( 'Throw error if "NPM_USERNAME" is missing' , async t => {
155
+ process . env . HOME = tempy . directory ( ) ;
96
156
const cwd = tempy . directory ( ) ;
157
+ process . chdir ( cwd ) ;
97
158
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
98
159
const env = { NPM_PASSWORD : 'npm_pasword' , NPM_EMAIL : 'npm_email' } ;
99
160
100
161
const [ error ] = await t . throwsAsync (
101
- setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } )
162
+ require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } )
102
163
) ;
103
164
104
165
t . is ( error . name , 'SemanticReleaseError' ) ;
105
166
t . is ( error . message , 'No npm token specified.' ) ;
106
167
t . is ( error . code , 'ENONPMTOKEN' ) ;
107
168
} ) ;
108
169
109
- test ( 'Throw error if "NPM_PASSWORD" is missing' , async t => {
170
+ test . serial ( 'Throw error if "NPM_PASSWORD" is missing' , async t => {
171
+ process . env . HOME = tempy . directory ( ) ;
110
172
const cwd = tempy . directory ( ) ;
173
+ process . chdir ( cwd ) ;
111
174
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
112
175
const env = { NPM_USERNAME : 'npm_username' , NPM_EMAIL : 'npm_email' } ;
113
176
114
177
const [ error ] = await t . throwsAsync (
115
- setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } )
178
+ require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } )
116
179
) ;
117
180
118
181
t . is ( error . name , 'SemanticReleaseError' ) ;
119
182
t . is ( error . message , 'No npm token specified.' ) ;
120
183
t . is ( error . code , 'ENONPMTOKEN' ) ;
121
184
} ) ;
122
185
123
- test ( 'Throw error if "NPM_EMAIL" is missing' , async t => {
186
+ test . serial ( 'Throw error if "NPM_EMAIL" is missing' , async t => {
187
+ process . env . HOME = tempy . directory ( ) ;
124
188
const cwd = tempy . directory ( ) ;
189
+ process . chdir ( cwd ) ;
125
190
const npmrc = tempy . file ( { name : '.npmrc' } ) ;
126
191
const env = { NPM_USERNAME : 'npm_username' , NPM_PASSWORD : 'npm_password' } ;
127
192
128
193
const [ error ] = await t . throwsAsync (
129
- setNpmrcAuth ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } )
194
+ require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , { cwd, env, logger : t . context . logger } )
130
195
) ;
131
196
132
197
t . is ( error . name , 'SemanticReleaseError' ) ;
0 commit comments