Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
fix: Allow comments in .nvmrc/.node-version (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Apr 7, 2024
1 parent 2e1ebc0 commit 53a203b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Added a `dist-url` config setting, allowing the download host to be customized.

#### 🐞 Fixes

- Fixed `.nvmrc` and `.node-version` parsing when they contain comments.

## 0.10.1

#### 🚀 Updates
Expand Down
11 changes: 10 additions & 1 deletion crates/node/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ pub fn parse_version_file(
}
}
} else {
version = Some(UnresolvedVersionSpec::parse(input.content)?);
for line in input.content.lines() {
let line = line.trim();

if line.is_empty() || line.starts_with('#') {
continue;
} else {
version = Some(UnresolvedVersionSpec::parse(line)?);
break;
}
}
}

Ok(Json(ParseVersionFileOutput { version }))
Expand Down
64 changes: 64 additions & 0 deletions crates/node/tests/versions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,67 @@ fn parses_volta() {
}
);
}

#[test]
fn parses_nvmrc() {
let sandbox = create_empty_proto_sandbox();
let plugin = sandbox.create_plugin("node-test");

assert_eq!(
plugin.parse_version_file(ParseVersionFileInput {
content: "~20".into(),
file: ".nvmrc".into(),
}),
ParseVersionFileOutput {
version: Some(UnresolvedVersionSpec::parse("~20").unwrap()),
}
);
}

#[test]
fn parses_nvmrc_with_comment() {
let sandbox = create_empty_proto_sandbox();
let plugin = sandbox.create_plugin("node-test");

assert_eq!(
plugin.parse_version_file(ParseVersionFileInput {
content: "# comment\n^20.1".into(),
file: ".nvmrc".into(),
}),
ParseVersionFileOutput {
version: Some(UnresolvedVersionSpec::parse("^20.1").unwrap()),
}
);
}

#[test]
fn parses_node_version() {
let sandbox = create_empty_proto_sandbox();
let plugin = sandbox.create_plugin("node-test");

assert_eq!(
plugin.parse_version_file(ParseVersionFileInput {
content: "~20".into(),
file: ".node-version".into(),
}),
ParseVersionFileOutput {
version: Some(UnresolvedVersionSpec::parse("~20").unwrap()),
}
);
}

#[test]
fn parses_node_version_with_comment() {
let sandbox = create_empty_proto_sandbox();
let plugin = sandbox.create_plugin("node-test");

assert_eq!(
plugin.parse_version_file(ParseVersionFileInput {
content: "# comment\n^20.1".into(),
file: ".node-version".into(),
}),
ParseVersionFileOutput {
version: Some(UnresolvedVersionSpec::parse("^20.1").unwrap()),
}
);
}

0 comments on commit 53a203b

Please sign in to comment.