1
1
'use strict' ;
2
2
3
+ const Cp = require ( 'child_process' ) ;
3
4
const Fs = require ( 'fs' ) ;
4
5
const Fixtures = require ( './fixtures' ) ;
5
6
const Path = require ( 'path' ) ;
@@ -38,9 +39,11 @@ describe('allow-scripts', () => {
38
39
39
40
await Allow . run ( { } ) ;
40
41
41
- expect ( fixture . getActualResult ( ) ) . to . equal ( Fs . readFileSync ( Path . join ( __dirname , 'fixtures' , 'basic.full.txt' ) ) . toString ( ) . trim ( ) ) ;
42
+ expect ( fixture . getActualResult ( ) ) . to . equal ( Fixtures . expectedResults . basicFull ) ;
42
43
expect ( fixture . getLog ( ) ) . not . to . contain ( 'without-scripts' ) ;
43
44
expect ( fixture . getLog ( ) ) . not . to . contain ( 'without-install-scripts' ) ;
45
+
46
+ expect ( Fs . existsSync ( Path . join ( fixture . cwd , 'npm-shrinkwrap.json' ) ) ) . to . equal ( false ) ;
44
47
} ) ;
45
48
46
49
it ( 'dry run: reports allowed scripts' , async ( ) => {
@@ -56,7 +59,7 @@ describe('allow-scripts', () => {
56
59
await Allow . run ( { dryRun : true } ) ;
57
60
58
61
expect ( fixture . getActualResult ( ) ) . to . equal ( '' ) ;
59
- expect ( fixture . getLog ( ) ) . to . equal ( Fs . readFileSync ( Path . join ( __dirname , 'fixtures' , 'basic.dry-run.txt' ) ) . toString ( ) . trim ( ) ) ;
62
+ expect ( fixture . getLog ( ) ) . to . equal ( Fixtures . expectedResults . basicDryRun ) ;
60
63
} ) ;
61
64
62
65
it ( 'executes allowed scripts (subdeps)' , async ( ) => {
@@ -72,7 +75,7 @@ describe('allow-scripts', () => {
72
75
73
76
await Allow . run ( { } ) ;
74
77
75
- expect ( fixture . getActualResult ( ) ) . to . equal ( Fs . readFileSync ( Path . join ( __dirname , 'fixtures' , ' deep.txt' ) ) . toString ( ) . trim ( ) ) ;
78
+ expect ( fixture . getActualResult ( ) ) . to . equal ( Fixtures . expectedResults . deep ) ;
76
79
expect ( fixture . getLog ( ) ) . not . to . contain ( 'without-scripts' ) ;
77
80
expect ( fixture . getLog ( ) ) . not . to . contain ( 'without-install-scripts' ) ;
78
81
} ) ;
@@ -92,6 +95,46 @@ describe('allow-scripts', () => {
92
95
expect ( fixture . getLog ( ) ) . to . contain ( 'skip node_modules/@example/cycle-b (because it has a cycle in dependencies)' ) ;
93
96
} ) ;
94
97
98
+ it ( 'executes allowed scripts (existing shrinkwrap)' , async ( ) => {
99
+
100
+ const fixture = Fixtures . setup ( 'basic' , [
101
+ 'with-preinstall-script' ,
102
+ 'with-install-script' ,
103
+ 'with-postinstall-script' ,
104
+ 'without-scripts' ,
105
+ 'without-install-scripts'
106
+ ] ) ;
107
+
108
+ Cp . execSync ( 'npm shrinkwrap' , { cwd : fixture . cwd } ) ;
109
+
110
+ await Allow . run ( { } ) ;
111
+
112
+ expect ( fixture . getActualResult ( ) ) . to . equal ( Fixtures . expectedResults . basicFull ) ;
113
+
114
+ expect ( Fs . existsSync ( Path . join ( fixture . cwd , 'npm-shrinkwrap.json' ) ) ) . to . equal ( true ) ;
115
+ } ) ;
116
+
117
+ it ( 'executes allowed scripts (existing package-lock)' , async ( ) => {
118
+
119
+ const fixture = Fixtures . setup ( 'basic' , [
120
+ 'with-preinstall-script' ,
121
+ 'with-install-script' ,
122
+ 'with-postinstall-script' ,
123
+ 'without-scripts' ,
124
+ 'without-install-scripts'
125
+ ] ) ;
126
+
127
+ Cp . execSync ( 'npm shrinkwrap' , { cwd : fixture . cwd } ) ;
128
+ Cp . execSync ( 'mv npm-shrinkwrap.json package-lock.json' , { cwd : fixture . cwd } ) ;
129
+
130
+ await Allow . run ( { } ) ;
131
+
132
+ expect ( fixture . getActualResult ( ) ) . to . equal ( Fixtures . expectedResults . basicFull ) ;
133
+
134
+ expect ( Fs . existsSync ( Path . join ( fixture . cwd , 'package-lock.json' ) ) ) . to . equal ( true ) ;
135
+ expect ( Fs . existsSync ( Path . join ( fixture . cwd , 'npm-shrinkwrap.json' ) ) ) . to . equal ( false ) ;
136
+ } ) ;
137
+
95
138
it ( 'crashes on script not in allowed list' , async ( ) => {
96
139
97
140
const fixture = Fixtures . setup ( 'not-in-allowed' , [
0 commit comments