1
1
/* global describe it */
2
2
3
3
require ( 'babel-polyfill' )
4
+ const fs = require ( 'fs' )
4
5
const node = require ( '../lib/node' )
5
6
const sinon = require ( 'sinon' )
6
- const fs = require ( 'fs-extra' )
7
7
const assert = require ( 'assert' )
8
8
9
9
let packageJsonStub = `{
@@ -49,11 +49,17 @@ let packageJsonStub = `{
49
49
}
50
50
}`
51
51
52
- describe ( 'Node.js module' , async function ( ) {
53
- describe ( 'Get packages' , async function ( ) {
54
- it ( 'Should extract packages from package.json' , async function ( ) {
52
+ describe ( 'Node.js module' , async function ( ) {
53
+ describe ( 'Get packages' , async function ( ) {
54
+ it ( 'Should extract packages from package.json' , async function ( ) {
55
55
try {
56
- let deps = [ 'dependencies' , 'devDependencies' , 'bundledDependencies' , 'peerDependencies' , 'optionalDependencies' ]
56
+ let deps = [
57
+ 'dependencies' ,
58
+ 'devDependencies' ,
59
+ 'bundledDependencies' ,
60
+ 'peerDependencies' ,
61
+ 'optionalDependencies'
62
+ ]
57
63
let packages = await node . getPackages ( JSON . parse ( packageJsonStub ) )
58
64
59
65
for ( let dep of deps ) {
@@ -65,8 +71,8 @@ describe('Node.js module', async function () {
65
71
} )
66
72
} )
67
73
68
- it ( 'Should check if packageJson exists' , function ( ) {
69
- let stub = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( false )
74
+ it ( 'Should check if packageJson exists' , function ( ) {
75
+ let stub = sinon . stub ( fs , 'existsSync ' ) . returns ( false )
70
76
let errmsg = 'should throw an error, package.json does not exist!'
71
77
72
78
try {
@@ -83,9 +89,11 @@ describe('Node.js module', async function () {
83
89
}
84
90
} )
85
91
86
- it ( 'Should read the package.json according to package and module name' , function ( ) {
92
+ it ( 'Should read the package.json according to package and module name' , function ( ) {
87
93
let packagename = node . NODEJS_PACKAGE_FILENAME
88
- let stub = sinon . stub ( fs , 'readJSONSync' ) . returns ( { package : 'json' } )
94
+ let stub = sinon
95
+ . stub ( fs , 'readFileSync' )
96
+ . returns ( JSON . stringify ( { package : 'json' } ) )
89
97
let path = '/fake/path'
90
98
try {
91
99
node . readPackageJson ( path )
@@ -97,9 +105,11 @@ describe('Node.js module', async function () {
97
105
}
98
106
} )
99
107
100
- describe ( 'extractPackageInfo' , async function ( ) {
101
- it ( 'should return an empty object of package json is not found' , async function ( ) {
102
- let stub = sinon . stub ( node , 'packageJsonExists' ) . throws ( 'no package.json found' )
108
+ describe ( 'extractPackageInfo' , async function ( ) {
109
+ it ( 'should return an empty object of package json is not found' , async function ( ) {
110
+ let stub = sinon
111
+ . stub ( node , 'packageJsonExists' )
112
+ . throws ( 'no package.json found' )
103
113
104
114
try {
105
115
let res = await node . extractPackageInfo ( '/fake/path' )
@@ -111,33 +121,40 @@ describe('Node.js module', async function () {
111
121
}
112
122
} )
113
123
114
- it ( 'Should extract data from package.json' , async function ( ) {
124
+ it ( 'Should extract data from package.json' , async function ( ) {
115
125
let packages = {
116
- devDependencies :
117
- { 'babel-cli' : '^6.24.1' ,
126
+ devDependencies : {
127
+ 'babel-cli' : '^6.24.1' ,
118
128
'babel-core' : '^6.25.0' ,
119
129
'babel-preset-env' : '^1.6.0' ,
120
130
'babel-polyfill' : '^6.23.0' ,
121
131
mocha : '^3.4.2' ,
122
132
nyc : '^11.0.3' ,
123
133
sinon : '^2.3.8' ,
124
134
snazzy : '^7.0.0' ,
125
- standard : '^10.0.2' } ,
126
- dependencies :
127
- { 'fs-extra' : '^4.0.0' ,
135
+ standard : '^10.0.2'
136
+ } ,
137
+ dependencies : {
138
+ 'fs-extra' : '^4.0.0' ,
128
139
request : '^2.81.0' ,
129
- 'request-promise-native' : '^1.0.2' }
140
+ 'request-promise-native' : '^1.0.2'
141
+ }
130
142
}
131
143
132
- let stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync' ) . returns ( true )
133
- let stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( JSON . parse ( packageJsonStub ) )
144
+ let stubPackageJsonExists = sinon . stub ( fs , 'existsSync' ) . returns ( true )
145
+ let stubReadPackageJson = sinon
146
+ . stub ( fs , 'readFileSync' )
147
+ . returns ( packageJsonStub )
134
148
let stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( packages )
135
149
136
150
try {
137
151
let res = await node . extractPackageInfo ( '/fake/path' )
138
152
assert . ok ( 'packages' in res , 'packages not in package info' )
139
153
assert . ok ( 'project_name' in res , 'project_name not in package info' )
140
- assert . ok ( 'node_package_index' in res , 'node_package_index not in package info' )
154
+ assert . ok (
155
+ 'node_package_index' in res ,
156
+ 'node_package_index not in package info'
157
+ )
141
158
} catch ( err ) {
142
159
assert . ifError ( err )
143
160
} finally {
@@ -147,11 +164,13 @@ describe('Node.js module', async function () {
147
164
}
148
165
} )
149
166
150
- it ( 'extracts repository name from a shorthand name' , async function ( ) {
151
- const stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( true )
167
+ it ( 'extracts repository name from a shorthand name' , async function ( ) {
168
+ const stubPackageJsonExists = sinon . stub ( fs , 'existsSync ' ) . returns ( true )
152
169
const alteredPackageJsonStub = JSON . parse ( packageJsonStub )
153
170
alteredPackageJsonStub . repository = 'bla:org/repo'
154
- const stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( alteredPackageJsonStub )
171
+ const stubReadPackageJson = sinon
172
+ . stub ( fs , 'readFileSync' )
173
+ . returns ( JSON . stringify ( alteredPackageJsonStub ) )
155
174
const stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( { } )
156
175
try {
157
176
const res = await node . extractPackageInfo ( '/fake/path' )
@@ -165,11 +184,13 @@ describe('Node.js module', async function () {
165
184
}
166
185
} )
167
186
168
- it ( 'extracts repository name from a shorthand name with a branch or tag (#)' , async function ( ) {
169
- const stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( true )
187
+ it ( 'extracts repository name from a shorthand name with a branch or tag (#)' , async function ( ) {
188
+ const stubPackageJsonExists = sinon . stub ( fs , 'existsSync ' ) . returns ( true )
170
189
const alteredPackageJsonStub = JSON . parse ( packageJsonStub )
171
190
alteredPackageJsonStub . repository = 'bla:org/repo#branch'
172
- const stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( alteredPackageJsonStub )
191
+ const stubReadPackageJson = sinon
192
+ . stub ( fs , 'readFileSync' )
193
+ . returns ( JSON . stringify ( alteredPackageJsonStub ) )
173
194
const stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( { } )
174
195
try {
175
196
const res = await node . extractPackageInfo ( '/fake/path' )
@@ -183,11 +204,13 @@ describe('Node.js module', async function () {
183
204
}
184
205
} )
185
206
186
- it ( 'extracts repository name from a shorthand simple name' , function ( ) {
187
- const stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( true )
207
+ it ( 'extracts repository name from a shorthand simple name' , function ( ) {
208
+ const stubPackageJsonExists = sinon . stub ( fs , 'existsSync ' ) . returns ( true )
188
209
const alteredPackageJsonStub = JSON . parse ( packageJsonStub )
189
210
alteredPackageJsonStub . repository = 'repo'
190
- const stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( alteredPackageJsonStub )
211
+ const stubReadPackageJson = sinon
212
+ . stub ( fs , 'readFileSync' )
213
+ . returns ( JSON . stringify ( alteredPackageJsonStub ) )
191
214
const stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( { } )
192
215
try {
193
216
const res = node . extractPackageInfo ( '/fake/path' )
@@ -201,14 +224,16 @@ describe('Node.js module', async function () {
201
224
}
202
225
} )
203
226
204
- it ( 'extracts repository name from git url' , function ( ) {
205
- const stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( true )
227
+ it ( 'extracts repository name from git url' , function ( ) {
228
+ const stubPackageJsonExists = sinon . stub ( fs , 'existsSync ' ) . returns ( true )
206
229
const alteredPackageJsonStub = JSON . parse ( packageJsonStub )
207
230
alteredPackageJsonStub . repository = {
208
231
type : 'git' ,
209
232
url : 'https://github.com/npm/repo.git'
210
233
}
211
- const stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( alteredPackageJsonStub )
234
+ const stubReadPackageJson = sinon
235
+ . stub ( fs , 'readFileSync' )
236
+ . returns ( JSON . stringify ( alteredPackageJsonStub ) )
212
237
const stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( { } )
213
238
try {
214
239
const res = node . extractPackageInfo ( '/fake/path' )
@@ -222,14 +247,16 @@ describe('Node.js module', async function () {
222
247
}
223
248
} )
224
249
225
- it ( 'extracts repository name from svn url' , function ( ) {
226
- const stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( true )
250
+ it ( 'extracts repository name from svn url' , function ( ) {
251
+ const stubPackageJsonExists = sinon . stub ( fs , 'existsSync ' ) . returns ( true )
227
252
const alteredPackageJsonStub = JSON . parse ( packageJsonStub )
228
253
alteredPackageJsonStub . repository = {
229
254
type : 'svn' ,
230
255
url : 'https://svn.com/repo'
231
256
}
232
- const stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( alteredPackageJsonStub )
257
+ const stubReadPackageJson = sinon
258
+ . stub ( fs , 'readFileSync' )
259
+ . returns ( JSON . stringify ( alteredPackageJsonStub ) )
233
260
const stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( { } )
234
261
try {
235
262
const res = node . extractPackageInfo ( '/fake/path' )
@@ -243,14 +270,16 @@ describe('Node.js module', async function () {
243
270
}
244
271
} )
245
272
246
- it ( 'returns an empty string for an unknown url type' , function ( ) {
247
- const stubPackageJsonExists = sinon . stub ( fs , 'pathExistsSync ' ) . returns ( true )
273
+ it ( 'returns an empty string for an unknown url type' , function ( ) {
274
+ const stubPackageJsonExists = sinon . stub ( fs , 'existsSync ' ) . returns ( true )
248
275
const alteredPackageJsonStub = JSON . parse ( packageJsonStub )
249
276
alteredPackageJsonStub . repository = {
250
277
type : 'mercurial' ,
251
278
url : 'https://svn.com/repo'
252
279
}
253
- const stubReadPackageJson = sinon . stub ( fs , 'readJSONSync' ) . returns ( alteredPackageJsonStub )
280
+ const stubReadPackageJson = sinon
281
+ . stub ( fs , 'readFileSync' )
282
+ . returns ( JSON . stringify ( alteredPackageJsonStub ) )
254
283
const stubGetPackages = sinon . stub ( node , 'getPackages' ) . returns ( { } )
255
284
try {
256
285
const res = node . extractPackageInfo ( '/fake/path' )
0 commit comments