@@ -3,86 +3,145 @@ import test from 'ava';
33import { readFile , appendFile } from 'fs-extra' ;
44import { stub } from 'sinon' ;
55import 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 ( ) ;
710
811test . beforeEach ( t => {
912// Stub the logger
1013t . context . log = stub ( ) ;
1114t . 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 ) ;
1223} ) ;
1324
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 ( ) ;
1527const cwd = tempy . directory ( ) ;
28+ process . chdir ( cwd ) ;
1629const npmrc = tempy . file ( { name :'.npmrc' } ) ;
1730const env = { NPM_TOKEN :'npm_token' } ;
1831
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 } ) ;
2033
2134t . 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 \} / ) ;
2235t . deepEqual ( t . context . log . args [ 1 ] , [ `Wrote NPM_TOKEN to${ npmrc } ` ] ) ;
2336} ) ;
2437
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 ( ) ;
2640const cwd = tempy . directory ( ) ;
41+ process . chdir ( cwd ) ;
2742const npmrc = tempy . file ( { name :'.npmrc' } ) ;
2843const env = { NPM_USERNAME :'npm_username' , NPM_PASSWORD :'npm_pasword' , NPM_EMAIL :'npm_email' } ;
2944
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 } ) ;
3146
3247t . is ( ( await readFile ( npmrc ) ) . toString ( ) , `\n_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}` ) ;
3348t . deepEqual ( t . context . log . args [ 1 ] , [ `Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to${ npmrc } ` ] ) ;
3449} ) ;
3550
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 ( ) ;
3768const 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 ) ;
3889const npmrc = tempy . file ( { name :'.npmrc' } ) ;
3990
4091await appendFile ( path . resolve ( cwd , '.npmrc' ) , `//custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
92+ await appendFile ( path . resolve ( process . env . HOME , '.npmrc' ) , 'home_config = test' ) ;
4193
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 } ) ;
4395
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}` ) ;
4597t . is ( t . context . log . callCount , 1 ) ;
4698} ) ;
4799
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 ( ) ;
49102const cwd = tempy . directory ( ) ;
103+ process . chdir ( cwd ) ;
50104const npmrc = tempy . file ( { name :'.npmrc' } ) ;
51105
52106await appendFile (
53107path . resolve ( cwd , '.npmrc' ) ,
54108`@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
55109) ;
110+ await appendFile ( path . resolve ( process . env . HOME , '.npmrc' ) , 'home_config = test' ) ;
56111
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 } ) ;
58113
59114t . is (
60115( 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}`
62117) ;
63118t . is ( t . context . log . callCount , 1 ) ;
64119} ) ;
65120
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 ( ) ;
67123const cwd = tempy . directory ( ) ;
124+ process . chdir ( cwd ) ;
68125const npmrc = tempy . file ( { name :'.npmrc' } ) ;
69126
70127const [ 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 } )
72129) ;
73130
74131t . is ( error . name , 'SemanticReleaseError' ) ;
75132t . is ( error . message , 'No npm token specified.' ) ;
76133t . is ( error . code , 'ENONPMTOKEN' ) ;
77134} ) ;
78135
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 ( ) ;
80138const cwd = tempy . directory ( ) ;
139+ process . chdir ( cwd ) ;
81140const npmrc = tempy . file ( { name :'.npmrc' } ) ;
82141
83142await appendFile ( path . resolve ( cwd , '.custom-npmrc' ) , `//custom.registry.com/:_authToken = \${NPM_TOKEN}` ) ;
84143
85- await setNpmrcAuth ( npmrc , 'http://custom.registry.com' , {
144+ await require ( '../lib/set-npmrc-auth' ) ( npmrc , 'http://custom.registry.com' , {
86145 cwd,
87146env :{ NPM_CONFIG_USERCONFIG :path . resolve ( cwd , '.custom-npmrc' ) } ,
88147logger :t . context . logger ,
@@ -92,41 +151,47 @@ test('Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set', async t
92151t . is ( t . context . log . callCount , 1 ) ;
93152} ) ;
94153
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 ( ) ;
96156const cwd = tempy . directory ( ) ;
157+ process . chdir ( cwd ) ;
97158const npmrc = tempy . file ( { name :'.npmrc' } ) ;
98159const env = { NPM_PASSWORD :'npm_pasword' , NPM_EMAIL :'npm_email' } ;
99160
100161const [ 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 } )
102163) ;
103164
104165t . is ( error . name , 'SemanticReleaseError' ) ;
105166t . is ( error . message , 'No npm token specified.' ) ;
106167t . is ( error . code , 'ENONPMTOKEN' ) ;
107168} ) ;
108169
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 ( ) ;
110172const cwd = tempy . directory ( ) ;
173+ process . chdir ( cwd ) ;
111174const npmrc = tempy . file ( { name :'.npmrc' } ) ;
112175const env = { NPM_USERNAME :'npm_username' , NPM_EMAIL :'npm_email' } ;
113176
114177const [ 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 } )
116179) ;
117180
118181t . is ( error . name , 'SemanticReleaseError' ) ;
119182t . is ( error . message , 'No npm token specified.' ) ;
120183t . is ( error . code , 'ENONPMTOKEN' ) ;
121184} ) ;
122185
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 ( ) ;
124188const cwd = tempy . directory ( ) ;
189+ process . chdir ( cwd ) ;
125190const npmrc = tempy . file ( { name :'.npmrc' } ) ;
126191const env = { NPM_USERNAME :'npm_username' , NPM_PASSWORD :'npm_password' } ;
127192
128193const [ 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 } )
130195) ;
131196
132197t . is ( error . name , 'SemanticReleaseError' ) ;