@@ -68,7 +68,103 @@ test("adds 'renovate' entry to package.json if it did not exist", async () => {
68
68
} ) ;
69
69
} ) ;
70
70
71
- test ( "adds 'extends' entry to 'renovate' entry if it did not exist" , async ( ) => {
71
+ test ( "adds 'renovate' entry ONLY to package.json files" , async ( ) => {
72
+ const path = "renovate.json" ;
73
+
74
+ const originalPackageJson = {
75
+ name : "octoherd-cli" ,
76
+ version : "0.0.0" ,
77
+ description : "" ,
78
+ main : "index.js" ,
79
+ scripts : {
80
+ test : 'echo "Error: no test specified" && exit 1' ,
81
+ } ,
82
+ author : "" ,
83
+ license : "ISC" ,
84
+ } ;
85
+
86
+ nock ( "https://api.github.com" )
87
+ . get ( `/repos/${ repository . owner . login } /${ repository . name } /contents/${ path } ` )
88
+ . reply ( 200 , {
89
+ type : "file" ,
90
+ sha : "randomSha" ,
91
+ content : Buffer . from ( JSON . stringify ( originalPackageJson ) ) . toString (
92
+ "base64"
93
+ ) ,
94
+ } )
95
+ . put (
96
+ `/repos/${ repository . owner . login } /${ repository . name } /contents/${ path } ` ,
97
+ ( body ) => {
98
+ const pkg = JSON . parse ( Buffer . from ( body . content , "base64" ) . toString ( ) ) ;
99
+
100
+ equal ( pkg , {
101
+ ...originalPackageJson ,
102
+ extends : [ "github>octoherd/.github" ] ,
103
+ } ) ;
104
+
105
+ return true ;
106
+ }
107
+ )
108
+ . reply ( 200 , { commit : { html_url : "link to commit" } } ) ;
109
+
110
+ await script ( getOctokitForTests ( ) , repository , {
111
+ extends : "github>octoherd/.github" ,
112
+ path,
113
+ } ) ;
114
+ } ) ;
115
+
116
+ test ( "'path' option recognizes paths containing package.json as package.json files" , async ( ) => {
117
+ const path = "my/random/path/package.json" ;
118
+
119
+ const originalPackageJson = {
120
+ name : "octoherd-cli" ,
121
+ version : "0.0.0" ,
122
+ description : "" ,
123
+ main : "index.js" ,
124
+ scripts : {
125
+ test : 'echo "Error: no test specified" && exit 1' ,
126
+ } ,
127
+ author : "" ,
128
+ license : "ISC" ,
129
+ } ;
130
+
131
+ nock ( "https://api.github.com" )
132
+ . get (
133
+ `/repos/${ repository . owner . login } /${
134
+ repository . name
135
+ } /contents/${ encodeURIComponent ( path ) } `
136
+ )
137
+ . reply ( 200 , {
138
+ type : "file" ,
139
+ sha : "randomSha" ,
140
+ content : Buffer . from ( JSON . stringify ( originalPackageJson ) ) . toString (
141
+ "base64"
142
+ ) ,
143
+ } )
144
+ . put (
145
+ `/repos/${ repository . owner . login } /${
146
+ repository . name
147
+ } /contents/${ encodeURIComponent ( path ) } `,
148
+ ( body ) => {
149
+ const pkg = JSON . parse ( Buffer . from ( body . content , "base64" ) . toString ( ) ) ;
150
+
151
+ equal ( pkg , {
152
+ ...originalPackageJson ,
153
+ renovate : { extends : [ "github>octoherd/.github" ] } ,
154
+ } ) ;
155
+
156
+ return true ;
157
+ }
158
+ )
159
+ . reply ( 200 , { commit : { html_url : "link to commit" } } ) ;
160
+
161
+ await script ( getOctokitForTests ( ) , repository , {
162
+ extends : "github>octoherd/.github" ,
163
+ path,
164
+ } ) ;
165
+ } ) ;
166
+
167
+ test ( "adds 'extends' entry if it did not exist" , async ( ) => {
72
168
const originalPackageJson = {
73
169
name : "octoherd-cli" ,
74
170
version : "0.0.0" ,
@@ -113,7 +209,7 @@ test("adds 'extends' entry to 'renovate' entry if it did not exist", async () =>
113
209
} ) ;
114
210
} ) ;
115
211
116
- test ( "replaces 'extends' entry if renovate. extends already existed" , async ( ) => {
212
+ test ( "replaces 'extends' entry if extends already existed" , async ( ) => {
117
213
const originalPackageJson = {
118
214
name : "octoherd-cli" ,
119
215
version : "0.0.0" ,
@@ -217,7 +313,7 @@ test("throws if --extends option is NOT provided", async () => {
217
313
equal ( nock . pendingMocks ( ) . length , 0 ) ;
218
314
} ) ;
219
315
220
- test ( "throws if package.json is NOT a file" , async ( ) => {
316
+ test ( "throws if JSON file provided is NOT a file" , async ( ) => {
221
317
nock ( "https://api.github.com" )
222
318
. get (
223
319
`/repos/${ repository . owner . login } /${ repository . name } /contents/package.json`
@@ -240,7 +336,7 @@ test("throws if package.json is NOT a file", async () => {
240
336
}
241
337
} ) ;
242
338
243
- test ( "throws if server fails when retrieving package.json " , async ( ) => {
339
+ test ( "throws if server fails when retrieving the JSON file " , async ( ) => {
244
340
nock ( "https://api.github.com" )
245
341
. get (
246
342
`/repos/${ repository . owner . login } /${ repository . name } /contents/package.json`
@@ -287,7 +383,26 @@ test("returns if repository is archived", async () => {
287
383
equal ( nock . pendingMocks ( ) . length , 0 ) ;
288
384
} ) ;
289
385
290
- test ( "creates package.json if file does NOT exist in the repository" , async ( ) => {
386
+ test ( "creates JSON file if file does NOT exist in the repository" , async ( ) => {
387
+ const path = "renovate.json" ;
388
+
389
+ nock ( "https://api.github.com" )
390
+ . get ( `/repos/${ repository . owner . login } /${ repository . name } /contents/${ path } ` )
391
+ . reply ( 404 )
392
+ . put ( `/repos/${ repository . owner . login } /${ repository . name } /contents/${ path } ` )
393
+ . reply ( 201 , { commit : { html_url : "link to commit" } } ) ;
394
+
395
+ try {
396
+ await script ( getOctokitForTests ( ) , repository , {
397
+ extends : "github>octoherd/.github" ,
398
+ path,
399
+ } ) ;
400
+ } catch ( error ) {
401
+ unreachable ( "should have NOT thrown" ) ;
402
+ }
403
+ } ) ;
404
+
405
+ test ( "creates package.json file if file no 'path' option is provided" , async ( ) => {
291
406
nock ( "https://api.github.com" )
292
407
. get (
293
408
`/repos/${ repository . owner . login } /${ repository . name } /contents/package.json`
0 commit comments