1+ import fs from 'fs-extra' ;
2+ import os from 'os' ;
13import test from 'ava' ;
24import path from 'path' ;
35import json from 'json-extra' ;
4- import fs from 'fs-extra' ;
56import getConfig from '../lib/getConfig' ;
67
78const cwd = process . cwd ( ) ;
9+ const homedir = os . homedir ( ) ;
810const fixtures = path . join ( cwd , 'test' , 'fixtures' ) ;
911
1012test ( 'read config from a specific path' , ( t ) => {
@@ -18,12 +20,31 @@ test('read config from a .sgcrc_default', (t) => {
1820test ( 'read config from package.json' , ( t ) => {
1921 const sgcrc = json . readToObjSync ( path . join ( fixtures , '.sgcrc' ) ) ;
2022 const packageJson = json . readToObjSync ( path . join ( cwd , 'package.json' ) ) ;
23+ const randomString = Math . random ( ) . toString ( 36 ) . replace ( / [ ^ a - z ] + / g, '' ) . substr ( 0 , 10 ) ;
2124 packageJson . sgc = sgcrc ;
2225
23- fs . copySync ( path . join ( cwd , 'package.json' ) , path . join ( cwd , 'package.json.back' ) ) ;
24- fs . unlinkSync ( path . join ( cwd , 'package.json' ) ) ;
26+ fs . renameSync ( path . join ( cwd , 'package.json' ) , path . join ( cwd , `package.json.${ randomString } .back` ) ) ;
2527 fs . writeFileSync ( path . join ( cwd , 'package.json' ) , JSON . stringify ( packageJson ) ) ;
2628 t . deepEqual ( getConfig ( ) , sgcrc ) ;
27- fs . unlinkSync ( path . join ( cwd , 'package.json' ) ) ;
28- fs . renameSync ( path . join ( cwd , 'package.json.back' ) , path . join ( cwd , 'package.json' ) ) ;
29+ fs . removeSync ( path . join ( cwd , 'package.json' ) ) ;
30+ fs . renameSync ( path . join ( cwd , `package.json.${ randomString } .back` ) , path . join ( cwd , 'package.json' ) ) ;
31+ } ) ;
32+
33+ test ( 'read global config' , ( t ) => {
34+ let globalExist = false ;
35+ const sgcrc = json . readToObjSync ( path . join ( fixtures , '.sgcrc' ) ) ;
36+ const randomString = Math . random ( ) . toString ( 36 ) . replace ( / [ ^ a - z ] + / g, '' ) . substr ( 0 , 10 ) ;
37+
38+ if ( fs . existsSync ( path . join ( homedir , '.sgcrc' ) ) ) {
39+ globalExist = true ;
40+ fs . renameSync ( path . join ( homedir , '.sgcrc' ) , path . join ( homedir , `.sgcrc.${ randomString } .back` ) ) ;
41+ }
42+
43+ fs . writeFileSync ( path . join ( homedir , '.sgcrc' ) , JSON . stringify ( sgcrc ) ) ;
44+ t . deepEqual ( getConfig ( ) , sgcrc ) ;
45+ fs . removeSync ( path . join ( homedir , '.sgcrc' ) ) ;
46+
47+ if ( globalExist ) {
48+ fs . renameSync ( path . join ( homedir , `.sgcrc.${ randomString } .back` ) , path . join ( homedir , '.sgcrc' ) ) ;
49+ }
2950} ) ;
0 commit comments