Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-pots-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": minor
---

support comments in .nvmrc files
247 changes: 247 additions & 0 deletions e2e/__snapshots__/nvmrc-comments.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Bash .node-version should not strip comments: Bash 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash .nvmrc with comment on first line: Bash 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash .nvmrc with empty lines and comments: Bash 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash .nvmrc with inline comment after version: Bash 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash .nvmrc with multiple comment lines: Bash 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash .nvmrc with whitespace and inline comments: Bash 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Fish .node-version should not strip comments: Fish 1`] = `
"fnm env | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`Fish .nvmrc with comment on first line: Fish 1`] = `
"fnm env | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`Fish .nvmrc with empty lines and comments: Fish 1`] = `
"fnm env | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`Fish .nvmrc with inline comment after version: Fish 1`] = `
"fnm env | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`Fish .nvmrc with multiple comment lines: Fish 1`] = `
"fnm env | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`Fish .nvmrc with whitespace and inline comments: Fish 1`] = `
"fnm env | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`PowerShell .node-version should not strip comments: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell .nvmrc with comment on first line: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell .nvmrc with empty lines and comments: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell .nvmrc with inline comment after version: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell .nvmrc with multiple comment lines: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell .nvmrc with whitespace and inline comments: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`Zsh .node-version should not strip comments: Zsh 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh .nvmrc with comment on first line: Zsh 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh .nvmrc with empty lines and comments: Zsh 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh .nvmrc with inline comment after version: Zsh 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh .nvmrc with multiple comment lines: Zsh 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh .nvmrc with whitespace and inline comments: Zsh 1`] = `
"set -e
eval "$(fnm env)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;
86 changes: 86 additions & 0 deletions e2e/nvmrc-comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { writeFile } from "node:fs/promises"
import { join } from "node:path"
import { script } from "./shellcode/script.js"
import { Bash, Fish, PowerShell, WinCmd, Zsh } from "./shellcode/shells.js"
import testCwd from "./shellcode/test-cwd.js"
import testNodeVersion from "./shellcode/test-node-version.js"
import describe from "./describe.js"

for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
describe(shell, () => {
test(`.nvmrc with comment on first line`, async () => {
await writeFile(join(testCwd(), ".nvmrc"), "# comment\nv8.11.3")
await script(shell)
.then(shell.env({}))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})

test(`.nvmrc with inline comment after version`, async () => {
await writeFile(join(testCwd(), ".nvmrc"), "v8.11.3 # this is Node 8 LTS")
await script(shell)
.then(shell.env({}))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})

test(`.nvmrc with multiple comment lines`, async () => {
await writeFile(
join(testCwd(), ".nvmrc"),
"# First comment\n# Second comment\n\nv8.11.3\n# Trailing comment",
)
await script(shell)
.then(shell.env({}))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})

test(`.nvmrc with empty lines and comments`, async () => {
await writeFile(
join(testCwd(), ".nvmrc"),
"\n\n# comment\n\nv8.11.3\n\n# another comment",
)
await script(shell)
.then(shell.env({}))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})

test(`.nvmrc with whitespace and inline comments`, async () => {
await writeFile(
join(testCwd(), ".nvmrc"),
" # comment with spaces \n v8.11.3 # inline comment ",
)
await script(shell)
.then(shell.env({}))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})

test(`.node-version should not strip comments`, async () => {
await writeFile(join(testCwd(), ".node-version"), "v8.11.3")
await script(shell)
.then(shell.env({}))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})
})
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod fs;
mod http;
mod installed_versions;
mod lts;
mod nvmrc;
mod package_json;
mod path_ext;
mod progress;
Expand All @@ -30,6 +31,7 @@ mod system_version;
mod user_version;
mod user_version_reader;
mod version;
mod version_file_format;
mod version_file_strategy;
mod version_files;

Expand Down
Loading