Skip to content

Commit 9cea7e3

Browse files
Merge pull request #957 from Sh4rK/pin-bundled
Fix pinning of npm@bundled
2 parents 9606139 + a0b2bbc commit 9cea7e3

File tree

2 files changed

+127
-42
lines changed

2 files changed

+127
-42
lines changed

crates/volta-core/src/platform/mod.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -244,34 +244,17 @@ impl Platform {
244244
/// pulling Yarn from the user default platform, if available
245245
/// - If there is no Project platform, then we use the user Default Platform
246246
pub fn current(session: &mut Session) -> Fallible<Option<Self>> {
247-
match session.project_platform()? {
248-
Some(platform) => {
249-
if platform.yarn.is_none() || platform.npm.is_none() {
250-
if let Some(default) = session.default_platform()? {
251-
let npm = platform
252-
.npm
253-
.clone()
254-
.map(Sourced::with_project)
255-
.or_else(|| default.npm.clone().map(Sourced::with_default));
256-
let yarn = platform
257-
.yarn
258-
.clone()
259-
.map(Sourced::with_project)
260-
.or_else(|| default.yarn.clone().map(Sourced::with_default));
261-
262-
return Ok(Some(Platform {
263-
node: Sourced::with_project(platform.node.clone()),
264-
npm,
265-
yarn,
266-
}));
267-
}
268-
}
269-
Ok(Some(platform.as_project()))
247+
if let Some(mut platform) = session.project_platform()?.map(PlatformSpec::as_project) {
248+
if platform.yarn.is_none() {
249+
platform.yarn = session
250+
.default_platform()?
251+
.and_then(|default_platform| default_platform.yarn.clone())
252+
.map(Sourced::with_default);
270253
}
271-
None => match session.default_platform()? {
272-
Some(platform) => Ok(Some(platform.as_default())),
273-
None => Ok(None),
274-
},
254+
255+
Ok(Some(platform))
256+
} else {
257+
Ok(session.default_platform()?.map(PlatformSpec::as_default))
275258
}
276259
}
277260

tests/acceptance/merged_platform.rs

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
use crate::support::sandbox::{sandbox, DistroMetadata, NodeFixture, YarnFixture};
1+
use crate::support::sandbox::{sandbox, DistroMetadata, NodeFixture, NpmFixture, YarnFixture};
22
use hamcrest2::assert_that;
33
use hamcrest2::prelude::*;
44
use test_support::matchers::execs;
55

66
use 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+
823
const 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

3853
cfg_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+
83111
const 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]
97199
fn uses_project_yarn_if_available() {
98200
let s = sandbox()
@@ -117,7 +219,7 @@ fn uses_project_yarn_if_available() {
117219
fn 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]
156258
fn 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]
171273
fn 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

Comments
 (0)