@@ -28,10 +28,6 @@ const package_json_template = {
2828 }
2929} ;
3030
31-
32- const all_commands = [ 'build' , 'clean' , 'configure' , 'info' , 'install' , 'package' , 'publish' , 'rebuild' ,
33- 'reinstall' , 'reveal' , 'testbinary' , 'testpackage' , 'unpublish' ] ;
34-
3531/**
3632 * before testing create a scratch directory to run tests in.
3733 */
@@ -67,82 +63,6 @@ test.onFinish(() => {
6763 rimraf ( scratch ) . then ( ( ) => undefined , ( ) => undefined ) ;
6864} ) ;
6965
70- test ( 'should set staging and production hosts' , ( t ) => {
71- // make sure it's good when specifying host.
72- const mock_package_json = makePackageJson ( ) ;
73-
74- let { prog } = setupTest ( dir , mock_package_json ) ;
75- t . deepEqual ( prog . package_json , mock_package_json ) ;
76- t . equal ( prog . binaryHostSet , false , 'binary host should not be flagged as set' ) ;
77-
78- // test with no s3_host option
79- all_commands . forEach ( ( cmd ) => {
80- const mpj = clone ( mock_package_json ) ;
81- mpj . binary . host = '' ;
82- const opts = { argv : [ cmd ] } ;
83- ( { prog } = setupTest ( dir , mpj , opts ) ) ;
84- mpj . binary . host = ( cmd === 'publish' || cmd === 'unpublish' ) ? mpj . binary . staging_host : mpj . binary . production_host ;
85- t . deepEqual ( prog . package_json , mpj , 'host should be correct for command: ' + cmd ) ;
86- t . equal ( prog . binaryHostSet , true , 'binary host should be flagged as set' ) ;
87- } ) ;
88-
89- // test with s3_host set to staging
90- all_commands . forEach ( ( cmd ) => {
91- const mpj = clone ( mock_package_json ) ;
92- mpj . binary . host = '' ;
93- const opts = { argv : [ cmd , '--s3_host=staging' ] } ;
94- ( { prog } = setupTest ( dir , mpj , opts ) ) ;
95- mpj . binary . host = mpj . binary . staging_host ;
96- t . deepEqual ( prog . package_json , mpj , 'host should be correct for command: ' + cmd ) ;
97- t . equal ( prog . binaryHostSet , true , 'binary host should be flagged as set' ) ;
98- } ) ;
99-
100- // test with s3_host set to production
101- all_commands . forEach ( ( cmd ) => {
102- const mpj = clone ( mock_package_json ) ;
103- mpj . binary . host = '' ;
104- const opts = { argv : [ cmd , '--s3_host=production' ] } ;
105- ( { prog } = setupTest ( dir , mpj , opts ) ) ;
106- mpj . binary . host = mpj . binary . production_host ;
107- t . deepEqual ( prog . package_json , mpj , 'host should be correct for command: ' + cmd ) ;
108- t . equal ( prog . binaryHostSet , true , 'binary host should be flagged as set' ) ;
109- } ) ;
110-
111- t . end ( ) ;
112- } ) ;
113-
114- test ( 'should execute setBinaryHostProperty() properly' , ( t ) => {
115- // it only --s3_host only takes effect if host is falsey.
116- const mock_package_json = makePackageJson ( { binary : { host : '' } } ) ;
117-
118- const opts = { argv : [ 'publish' , '--s3_host=staging' ] } ;
119-
120- let { prog, binaryHost } = setupTest ( dir , mock_package_json , opts ) ;
121- t . equal ( binaryHost , mock_package_json . binary . staging_host ) ;
122-
123- // set it again to verify that it returns the already set value
124- binaryHost = prog . setBinaryHostProperty ( 'publish' ) ;
125- t . equal ( binaryHost , mock_package_json . binary . staging_host ) ;
126-
127- // now do this again but expect an empty binary host value because
128- // staging_host is missing.
129- const mpj = clone ( mock_package_json ) ;
130- delete mpj . binary . staging_host ;
131- ( { prog, binaryHost } = setupTest ( dir , mpj , opts ) ) ;
132- t . equal ( binaryHost , '' ) ;
133-
134- // one more time but with an invalid value for s3_host
135- opts . argv = [ 'publish' , '--s3_host=bad-news' ] ;
136- try {
137- ( { prog, binaryHost } = setupTest ( dir , mock_package_json , opts ) ) ;
138- t . fail ( 'should throw with --s3_host=bad-news' ) ;
139- } catch ( e ) {
140- t . equal ( e . message , 'invalid s3_host bad-news' ) ;
141- }
142-
143- t . end ( ) ;
144- } ) ;
145-
14666test ( 'verify that the --directory option works' , ( t ) => {
14767 const initial = process . cwd ( ) ;
14868
@@ -223,6 +143,10 @@ test('verify that a non-existent package.json fails', (t) => {
223143// test helpers.
224144//
225145
146+ // helper to clone mock package.json.
147+ // // https://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript
148+ const clone = ( obj ) => JSON . parse ( JSON . stringify ( obj ) ) ;
149+
226150function makePackageJson ( options = { } ) {
227151 const package_json = clone ( package_json_template ) ;
228152 // override binary values if supplied
@@ -233,60 +157,3 @@ function makePackageJson(options = {}) {
233157 }
234158 return package_json ;
235159}
236-
237- // helper to write package.json to disk so Run() can be instantiated with it.
238- function setupTest ( directory , package_json , opts ) {
239- opts = opts || { } ;
240- let argv = [ 'node' , 'program' ] ;
241- if ( opts . argv ) {
242- argv = argv . concat ( opts . argv ) ;
243- }
244- const prev_dir = process . cwd ( ) ;
245- if ( ! opts . noChdir ) {
246- try {
247- fs . mkdirSync ( directory ) ;
248- } catch ( e ) {
249- if ( e . code !== 'EEXIST' ) {
250- throw e ;
251- }
252- }
253- process . chdir ( directory ) ;
254- }
255-
256- try {
257- fs . writeFileSync ( 'package.json' , JSON . stringify ( package_json ) ) ;
258- const prog = new npg . Run ( { package_json_path : './package.json' , argv } ) ;
259- const binaryHost = prog . setBinaryHostProperty ( prog . todo [ 0 ] && prog . todo [ 0 ] . name ) ;
260- return { prog, binaryHost } ;
261- } finally {
262- process . chdir ( prev_dir ) ;
263- }
264- }
265-
266- // helper to clone mock package.json. it's overkill for existing tests
267- // but is future-proof.
268- // https://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript
269- function clone ( obj , hash = new WeakMap ( ) ) {
270- if ( Object ( obj ) !== obj ) return obj ; // primitives
271- if ( hash . has ( obj ) ) return hash . get ( obj ) ; // cyclic reference
272- let result ;
273-
274- if ( obj instanceof Set ) {
275- result = new Set ( obj ) ; // treat set as a value
276- } else if ( obj instanceof Map ) {
277- result = new Map ( Array . from ( obj , ( [ key , val ] ) => [ key , clone ( val , hash ) ] ) ) ;
278- } else if ( obj instanceof Date ) {
279- result = new Date ( obj ) ;
280- } else if ( obj instanceof RegExp ) {
281- result = new RegExp ( obj . source , obj . flags ) ;
282- } else if ( obj . constructor ) {
283- result = new obj . constructor ( ) ;
284- } else {
285- result = Object . create ( null ) ;
286- }
287- hash . set ( obj , result ) ;
288- return Object . assign ( result , ...Object . keys ( obj ) . map ( ( key ) => {
289- return { [ key ] : clone ( obj [ key ] , hash ) } ;
290- } ) ) ;
291- }
292-
0 commit comments