1- use crate :: support:: sandbox:: { sandbox, DistroMetadata , NodeFixture , YarnFixture } ;
1+ use crate :: support:: sandbox:: { sandbox, DistroMetadata , NodeFixture , NpmFixture , YarnFixture } ;
22use hamcrest2:: assert_that;
33use hamcrest2:: prelude:: * ;
44use test_support:: matchers:: execs;
55
66use volta_core:: error:: ExitCode ;
77
8+ const PACKAGE_JSON_NODE_ONLY : & str = r#"{
9+ "name": "node-only",
10+ "volta": {
11+ "node": "10.99.1040"
12+ }
13+ }"# ;
14+
15+ const PACKAGE_JSON_WITH_NPM : & str = r#"{
16+ "name": "with-npm",
17+ "volta": {
18+ "node": "10.99.1040",
19+ "npm": "4.5.6"
20+ }
21+ }"# ;
22+
823const PACKAGE_JSON_WITH_YARN : & str = r#"{
924 "name": "with-yarn",
1025 "volta": {
@@ -13,26 +28,26 @@ const PACKAGE_JSON_WITH_YARN: &str = r#"{
1328 }
1429}"# ;
1530
16- const PACKAGE_JSON_NO_YARN : & str = r#"{
17- "name": "without-yarn",
18- "volta": {
19- "node": "10.99.1040"
31+ const PLATFORM_NODE_ONLY : & str = r#"{
32+ "node":{
33+ "runtime":"9.27.6",
34+ "npm":null
2035 }
2136}"# ;
2237
23- const PLATFORM_WITH_YARN : & str = r#"{
38+ const PLATFORM_WITH_NPM : & str = r#"{
2439 "node":{
2540 "runtime":"9.27.6",
26- "npm":null
27- },
28- "yarn": "1.7.71"
41+ "npm":"1.2.3"
42+ }
2943}"# ;
3044
31- const PLATFORM_NO_YARN : & str = r#"{
45+ const PLATFORM_WITH_YARN : & str = r#"{
3246 "node":{
3347 "runtime":"9.27.6",
3448 "npm":null
35- }
49+ },
50+ "yarn": "1.7.71"
3651}"# ;
3752
3853cfg_if:: cfg_if! {
@@ -80,6 +95,19 @@ cfg_if::cfg_if! {
8095 }
8196}
8297
98+ const NPM_VERSION_FIXTURES : [ DistroMetadata ; 2 ] = [
99+ DistroMetadata {
100+ version : "1.2.3" ,
101+ compressed_size : 239 ,
102+ uncompressed_size : Some ( 0x0028_0000 ) ,
103+ } ,
104+ DistroMetadata {
105+ version : "4.5.6" ,
106+ compressed_size : 239 ,
107+ uncompressed_size : Some ( 0x0028_0000 ) ,
108+ } ,
109+ ] ;
110+
83111const YARN_VERSION_FIXTURES : [ DistroMetadata ; 2 ] = [
84112 DistroMetadata {
85113 version : "1.12.99" ,
@@ -93,6 +121,80 @@ const YARN_VERSION_FIXTURES: [DistroMetadata; 2] = [
93121 } ,
94122] ;
95123
124+ #[ test]
125+ fn uses_project_npm_if_available ( ) {
126+ let s = sandbox ( )
127+ . platform ( PLATFORM_WITH_NPM )
128+ . package_json ( PACKAGE_JSON_WITH_NPM )
129+ . distro_mocks :: < NodeFixture > ( & NODE_VERSION_FIXTURES )
130+ . distro_mocks :: < NpmFixture > ( & NPM_VERSION_FIXTURES )
131+ . env ( "VOLTA_LOGLEVEL" , "debug" )
132+ . build ( ) ;
133+
134+ assert_that ! (
135+ s. npm( "--version" ) ,
136+ execs( )
137+ . with_status( ExitCode :: Success as i32 )
138+ . with_stderr_contains( "[..]Node: 10.99.1040 from project configuration" )
139+ . with_stderr_contains( "[..]npm: 4.5.6 from project configuration" )
140+ ) ;
141+ }
142+
143+ #[ test]
144+ fn uses_bundled_npm_in_project_without_npm ( ) {
145+ let s = sandbox ( )
146+ . platform ( PLATFORM_WITH_NPM )
147+ . package_json ( PACKAGE_JSON_NODE_ONLY )
148+ . distro_mocks :: < NodeFixture > ( & NODE_VERSION_FIXTURES )
149+ . distro_mocks :: < NpmFixture > ( & NPM_VERSION_FIXTURES )
150+ . env ( "VOLTA_LOGLEVEL" , "debug" )
151+ . build ( ) ;
152+
153+ assert_that ! (
154+ s. npm( "--version" ) ,
155+ execs( )
156+ . with_status( ExitCode :: Success as i32 )
157+ . with_stderr_contains( "[..]Node: 10.99.1040 from project configuration" )
158+ . with_stderr_contains( "[..]npm: 6.2.26 from project configuration" )
159+ ) ;
160+ }
161+
162+ #[ test]
163+ fn uses_default_npm_outside_project ( ) {
164+ let s = sandbox ( )
165+ . platform ( PLATFORM_WITH_NPM )
166+ . distro_mocks :: < NodeFixture > ( & NODE_VERSION_FIXTURES )
167+ . distro_mocks :: < NpmFixture > ( & NPM_VERSION_FIXTURES )
168+ . env ( "VOLTA_LOGLEVEL" , "debug" )
169+ . build ( ) ;
170+
171+ assert_that ! (
172+ s. npm( "--version" ) ,
173+ execs( )
174+ . with_status( ExitCode :: Success as i32 )
175+ . with_stderr_contains( "[..]Node: 9.27.6 from default configuration" )
176+ . with_stderr_contains( "[..]npm: 1.2.3 from default configuration" )
177+ ) ;
178+ }
179+
180+ #[ test]
181+ fn uses_bundled_npm_outside_project ( ) {
182+ let s = sandbox ( )
183+ . platform ( PLATFORM_NODE_ONLY )
184+ . distro_mocks :: < NodeFixture > ( & NODE_VERSION_FIXTURES )
185+ . distro_mocks :: < NpmFixture > ( & NPM_VERSION_FIXTURES )
186+ . env ( "VOLTA_LOGLEVEL" , "debug" )
187+ . build ( ) ;
188+
189+ assert_that ! (
190+ s. npm( "--version" ) ,
191+ execs( )
192+ . with_status( ExitCode :: Success as i32 )
193+ . with_stderr_contains( "[..]Node: 9.27.6 from default configuration" )
194+ . with_stderr_contains( "[..]npm: 5.6.17 from default configuration" )
195+ ) ;
196+ }
197+
96198#[ test]
97199fn uses_project_yarn_if_available ( ) {
98200 let s = sandbox ( )
@@ -117,7 +219,7 @@ fn uses_project_yarn_if_available() {
117219fn uses_default_yarn_in_project_without_yarn ( ) {
118220 let s = sandbox ( )
119221 . platform ( PLATFORM_WITH_YARN )
120- . package_json ( PACKAGE_JSON_NO_YARN )
222+ . package_json ( PACKAGE_JSON_NODE_ONLY )
121223 . distro_mocks :: < NodeFixture > ( & NODE_VERSION_FIXTURES )
122224 . distro_mocks :: < YarnFixture > ( & YARN_VERSION_FIXTURES )
123225 . env ( "VOLTA_LOGLEVEL" , "debug" )
@@ -155,8 +257,8 @@ fn uses_default_yarn_outside_project() {
155257#[ test]
156258fn throws_project_error_in_project ( ) {
157259 let s = sandbox ( )
158- . platform ( PLATFORM_NO_YARN )
159- . package_json ( PACKAGE_JSON_NO_YARN )
260+ . platform ( PLATFORM_NODE_ONLY )
261+ . package_json ( PACKAGE_JSON_NODE_ONLY )
160262 . build ( ) ;
161263
162264 assert_that ! (
@@ -169,7 +271,7 @@ fn throws_project_error_in_project() {
169271
170272#[ test]
171273fn throws_default_error_outside_project ( ) {
172- let s = sandbox ( ) . platform ( PLATFORM_NO_YARN ) . build ( ) ;
274+ let s = sandbox ( ) . platform ( PLATFORM_NODE_ONLY ) . build ( ) ;
173275
174276 assert_that ! (
175277 s. yarn( "--version" ) ,
0 commit comments