Skip to content

Commit 1000914

Browse files
skirstenSchniz
andauthored
Fix --resolve-engines in combination with --use-on-cd (#1259)
* Fix --resolve-engines with --use-on-cd * Add clippy suggestions * Create hot-mice-camp.md * apply tests --------- Co-authored-by: Gal Schlezinger <gal@spitfire.co.il>
1 parent 574e090 commit 1000914

9 files changed

Lines changed: 148 additions & 87 deletions

File tree

‎.changeset/hot-mice-camp.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fnm": patch
3+
---
4+
5+
Fix `--resolve-engines` in combination with `--use-on-cd`

‎e2e/__snapshots__/basic.test.ts.snap‎

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,6 @@ if [ "$(node --version)" != "v6.17.1" ]; then
7575
fi"
7676
`;
7777

78-
exports[`Bash use on cd: Bash 1`] = `
79-
"set -e
80-
eval "$(fnm env --use-on-cd)"
81-
fnm install v8.11.3
82-
fnm install v12.22.12
83-
cd subdir
84-
if [ "$(node --version)" != "v12.22.12" ]; then
85-
echo "Expected node version to be v12.22.12. Got $(node --version)"
86-
exit 1
87-
fi"
88-
`;
89-
9078
exports[`Bash when .node-version and .nvmrc are in sync, it throws no error: Bash 1`] = `
9179
"set -e
9280
eval "$(fnm env)"
@@ -173,18 +161,6 @@ if test "$____test____" != "v6.17.1"
173161
end"
174162
`;
175163

176-
exports[`Fish use on cd: Fish 1`] = `
177-
"fnm env --use-on-cd | source
178-
fnm install v8.11.3
179-
fnm install v12.22.12
180-
cd subdir
181-
set ____test____ (node --version)
182-
if test "$____test____" != "v12.22.12"
183-
echo "Expected node version to be v12.22.12. Got $____test____"
184-
exit 1
185-
end"
186-
`;
187-
188164
exports[`Fish when .node-version and .nvmrc are in sync, it throws no error: Fish 1`] = `
189165
"fnm env | source
190166
fnm install
@@ -250,15 +226,6 @@ fnm use 6
250226
if ( "$(node --version)" -ne "v6.17.1" ) { echo "Expected node version to be v6.17.1. Got $(node --version)"; exit 1 }"
251227
`;
252228

253-
exports[`PowerShell use on cd: PowerShell 1`] = `
254-
"$ErrorActionPreference = "Stop"
255-
fnm env --use-on-cd | Out-String | Invoke-Expression
256-
fnm install v8.11.3
257-
fnm install v12.22.12
258-
cd subdir
259-
if ( "$(node --version)" -ne "v12.22.12" ) { echo "Expected node version to be v12.22.12. Got $(node --version)"; exit 1 }"
260-
`;
261-
262229
exports[`PowerShell when .node-version and .nvmrc are in sync, it throws no error: PowerShell 1`] = `
263230
"$ErrorActionPreference = "Stop"
264231
fnm env | Out-String | Invoke-Expression
@@ -342,18 +309,6 @@ if [ "$(node --version)" != "v6.17.1" ]; then
342309
fi"
343310
`;
344311

345-
exports[`Zsh use on cd: Zsh 1`] = `
346-
"set -e
347-
eval "$(fnm env --use-on-cd)"
348-
fnm install v8.11.3
349-
fnm install v12.22.12
350-
cd subdir
351-
if [ "$(node --version)" != "v12.22.12" ]; then
352-
echo "Expected node version to be v12.22.12. Got $(node --version)"
353-
exit 1
354-
fi"
355-
`;
356-
357312
exports[`Zsh when .node-version and .nvmrc are in sync, it throws no error: Zsh 1`] = `
358313
"set -e
359314
eval "$(fnm env)"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Bash use on cd: Bash 1`] = `
4+
"set -e
5+
eval "$(fnm env --use-on-cd)"
6+
fnm install v8.11.3
7+
fnm install v12.22.12
8+
cd subdir
9+
if [ "$(node --version)" != "v12.22.12" ]; then
10+
echo "Expected node version to be v12.22.12. Got $(node --version)"
11+
exit 1
12+
fi"
13+
`;
14+
15+
exports[`Fish use on cd: Fish 1`] = `
16+
"fnm env --use-on-cd | source
17+
fnm install v8.11.3
18+
fnm install v12.22.12
19+
cd subdir
20+
set ____test____ (node --version)
21+
if test "$____test____" != "v12.22.12"
22+
echo "Expected node version to be v12.22.12. Got $____test____"
23+
exit 1
24+
end"
25+
`;
26+
27+
exports[`PowerShell use on cd: PowerShell 1`] = `
28+
"$ErrorActionPreference = "Stop"
29+
fnm env --use-on-cd | Out-String | Invoke-Expression
30+
fnm install v8.11.3
31+
fnm install v12.22.12
32+
cd subdir
33+
if ( "$(node --version)" -ne "v12.22.12" ) { echo "Expected node version to be v12.22.12. Got $(node --version)"; exit 1 }"
34+
`;
35+
36+
exports[`Zsh use on cd: Zsh 1`] = `
37+
"set -e
38+
eval "$(fnm env --use-on-cd)"
39+
fnm install v8.11.3
40+
fnm install v12.22.12
41+
cd subdir
42+
if [ "$(node --version)" != "v12.22.12" ]; then
43+
echo "Expected node version to be v12.22.12. Got $(node --version)"
44+
exit 1
45+
fi"
46+
`;

‎e2e/basic.test.ts‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { writeFile, mkdir } from "node:fs/promises"
1+
import { writeFile } from "node:fs/promises"
22
import { join } from "node:path"
33
import { script } from "./shellcode/script.js"
44
import { Bash, Fish, PowerShell, WinCmd, Zsh } from "./shellcode/shells.js"
@@ -43,7 +43,7 @@ for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
4343
test(`package.json engines.node`, async () => {
4444
await writeFile(
4545
join(testCwd(), "package.json"),
46-
JSON.stringify({ engines: { node: "8.11.3" } })
46+
JSON.stringify({ engines: { node: "8.11.3" } }),
4747
)
4848
await script(shell)
4949
.then(shell.env({ resolveEngines: true }))
@@ -57,7 +57,7 @@ for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
5757
test(`package.json engines.node with semver range`, async () => {
5858
await writeFile(
5959
join(testCwd(), "package.json"),
60-
JSON.stringify({ engines: { node: "^6 < 6.17.1" } })
60+
JSON.stringify({ engines: { node: "^6 < 6.17.1" } }),
6161
)
6262
await script(shell)
6363
.then(shell.env({ resolveEngines: true }))
@@ -68,19 +68,6 @@ for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
6868
.execute(shell)
6969
})
7070

71-
test(`use on cd`, async () => {
72-
await mkdir(join(testCwd(), "subdir"), { recursive: true })
73-
await writeFile(join(testCwd(), "subdir", ".node-version"), "v12.22.12")
74-
await script(shell)
75-
.then(shell.env({ useOnCd: true }))
76-
.then(shell.call("fnm", ["install", "v8.11.3"]))
77-
.then(shell.call("fnm", ["install", "v12.22.12"]))
78-
.then(shell.call("cd", ["subdir"]))
79-
.then(testNodeVersion(shell, "v12.22.12"))
80-
.takeSnapshot(shell)
81-
.execute(shell)
82-
})
83-
8471
test(`resolves partial semver`, async () => {
8572
await script(shell)
8673
.then(shell.env({}))
@@ -98,8 +85,8 @@ for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
9885
shell.hasCommandOutput(
9986
shell.call("fnm", ["ls"]),
10087
"* system",
101-
"fnm ls"
102-
)
88+
"fnm ls",
89+
),
10390
)
10491
.takeSnapshot(shell)
10592
.execute(shell)

‎e2e/use-on-cd.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { writeFile, mkdir } from "node:fs/promises"
2+
import { join } from "node:path"
3+
import { script } from "./shellcode/script.js"
4+
import { Bash, Fish, PowerShell, WinCmd, Zsh } from "./shellcode/shells.js"
5+
import testCwd from "./shellcode/test-cwd.js"
6+
import testNodeVersion from "./shellcode/test-node-version.js"
7+
import describe from "./describe.js"
8+
9+
for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
10+
describe(shell, () => {
11+
test(`use on cd`, async () => {
12+
await mkdir(join(testCwd(), "subdir"), { recursive: true })
13+
await writeFile(join(testCwd(), "subdir", ".node-version"), "v12.22.12")
14+
await script(shell)
15+
.then(shell.env({ useOnCd: true }))
16+
.then(shell.call("fnm", ["install", "v8.11.3"]))
17+
.then(shell.call("fnm", ["install", "v12.22.12"]))
18+
.then(shell.call("cd", ["subdir"]))
19+
.then(testNodeVersion(shell, "v12.22.12"))
20+
.takeSnapshot(shell)
21+
.execute(shell)
22+
})
23+
24+
test(`with resolve-engines`, async () => {
25+
await mkdir(join(testCwd(), "subdir"), { recursive: true })
26+
await writeFile(
27+
join(testCwd(), "subdir", "package.json"),
28+
JSON.stringify({
29+
name: "hello",
30+
engines: {
31+
node: "v12.22.12",
32+
},
33+
}),
34+
)
35+
await script(shell)
36+
.then(shell.env({ useOnCd: true, resolveEngines: true }))
37+
.then(shell.call("fnm", ["install", "v8.11.3"]))
38+
.then(shell.call("fnm", ["install", "v12.22.12"]))
39+
.then(shell.call("cd", ["subdir"]))
40+
.then(testNodeVersion(shell, "v12.22.12"))
41+
.execute(shell)
42+
})
43+
})
44+
}

‎src/shell/bash.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::version_file_strategy::VersionFileStrategy;
22

33
use super::shell::Shell;
4-
use indoc::{formatdoc, indoc};
4+
use indoc::formatdoc;
55
use std::path::Path;
66

77
#[derive(Debug)]
@@ -26,15 +26,21 @@ impl Shell for Bash {
2626
}
2727

2828
fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {
29+
let version_file_exists_condition = if config.resolve_engines() {
30+
"-f .node-version || -f .nvmrc || -f package.json"
31+
} else {
32+
"-f .node-version || -f .nvmrc"
33+
};
2934
let autoload_hook = match config.version_file_strategy() {
30-
VersionFileStrategy::Local => indoc!(
31-
r"
32-
if [[ -f .node-version || -f .nvmrc ]]; then
35+
VersionFileStrategy::Local => formatdoc!(
36+
r#"
37+
if [[ {version_file_exists_condition} ]]; then
3338
fnm use --silent-if-unchanged
3439
fi
35-
"
40+
"#,
41+
version_file_exists_condition = version_file_exists_condition,
3642
),
37-
VersionFileStrategy::Recursive => r"fnm use --silent-if-unchanged",
43+
VersionFileStrategy::Recursive => String::from(r"fnm use --silent-if-unchanged"),
3844
};
3945
Ok(formatdoc!(
4046
r#"

‎src/shell/fish.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::version_file_strategy::VersionFileStrategy;
22

33
use super::shell::Shell;
4-
use indoc::{formatdoc, indoc};
4+
use indoc::formatdoc;
55
use std::path::Path;
66

77
#[derive(Debug)]
@@ -26,15 +26,21 @@ impl Shell for Fish {
2626
}
2727

2828
fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {
29+
let version_file_exists_condition = if config.resolve_engines() {
30+
"test -f .node-version -o -f .nvmrc -o -f package.json"
31+
} else {
32+
"test -f .node-version -o -f .nvmrc"
33+
};
2934
let autoload_hook = match config.version_file_strategy() {
30-
VersionFileStrategy::Local => indoc!(
31-
r"
32-
if test -f .node-version -o -f .nvmrc
35+
VersionFileStrategy::Local => formatdoc!(
36+
r#"
37+
if {version_file_exists_condition}
3338
fnm use --silent-if-unchanged
3439
end
35-
"
40+
"#,
41+
version_file_exists_condition = version_file_exists_condition,
3642
),
37-
VersionFileStrategy::Recursive => r"fnm use --silent-if-unchanged",
43+
VersionFileStrategy::Recursive => String::from(r"fnm use --silent-if-unchanged"),
3844
};
3945
Ok(formatdoc!(
4046
r#"

‎src/shell/powershell.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::version_file_strategy::VersionFileStrategy;
22

33
use super::Shell;
4-
use indoc::{formatdoc, indoc};
4+
use indoc::formatdoc;
55
use std::path::Path;
66

77
#[derive(Debug)]
@@ -26,13 +26,19 @@ impl Shell for PowerShell {
2626
}
2727

2828
fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {
29+
let version_file_exists_condition = if config.resolve_engines() {
30+
"(Test-Path .nvmrc) -Or (Test-Path .node-version) -Or (Test-Path package.json)"
31+
} else {
32+
"(Test-Path .nvmrc) -Or (Test-Path .node-version)"
33+
};
2934
let autoload_hook = match config.version_file_strategy() {
30-
VersionFileStrategy::Local => indoc!(
31-
r"
32-
If ((Test-Path .nvmrc) -Or (Test-Path .node-version)) { & fnm use --silent-if-unchanged }
33-
"
35+
VersionFileStrategy::Local => formatdoc!(
36+
r#"
37+
If ({version_file_exists_condition}) {{ & fnm use --silent-if-unchanged }}
38+
"#,
39+
version_file_exists_condition = version_file_exists_condition,
3440
),
35-
VersionFileStrategy::Recursive => r"fnm use --silent-if-unchanged",
41+
VersionFileStrategy::Recursive => String::from(r"fnm use --silent-if-unchanged"),
3642
};
3743
Ok(formatdoc!(
3844
r#"

‎src/shell/zsh.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::version_file_strategy::VersionFileStrategy;
22

33
use super::shell::Shell;
4-
use indoc::{formatdoc, indoc};
4+
use indoc::formatdoc;
55
use std::path::Path;
66

77
#[derive(Debug)]
@@ -30,15 +30,21 @@ impl Shell for Zsh {
3030
}
3131

3232
fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {
33+
let version_file_exists_condition = if config.resolve_engines() {
34+
"-f .node-version || -f .nvmrc || -f package.json"
35+
} else {
36+
"-f .node-version || -f .nvmrc"
37+
};
3338
let autoload_hook = match config.version_file_strategy() {
34-
VersionFileStrategy::Local => indoc!(
35-
r"
36-
if [[ -f .node-version || -f .nvmrc ]]; then
39+
VersionFileStrategy::Local => formatdoc!(
40+
r#"
41+
if [[ {version_file_exists_condition} ]]; then
3742
fnm use --silent-if-unchanged
3843
fi
39-
"
44+
"#,
45+
version_file_exists_condition = version_file_exists_condition,
4046
),
41-
VersionFileStrategy::Recursive => r"fnm use --silent-if-unchanged",
47+
VersionFileStrategy::Recursive => String::from(r"fnm use --silent-if-unchanged"),
4248
};
4349
Ok(formatdoc!(
4450
r#"

0 commit comments

Comments
 (0)