Skip to content

Commit

Permalink
Support leading v in .node-version (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
nix6839 authored Aug 25, 2024
1 parent 62ea1b7 commit 4442d10
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _load(cls, configfiles, verbose=False):

if os.path.exists(".node-version"):
with open(".node-version", "r") as v_file:
setattr(cls, "node", v_file.readlines(1)[0].strip())
setattr(cls, "node", v_file.readline().strip().lstrip("v"))

@classmethod
def _dump(cls):
Expand Down
29 changes: 29 additions & 0 deletions tests/nodeenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,32 @@ def test_remove_env_bin_from_path():
assert (nodeenv.remove_env_bin_from_path(
'//home://home/env/bin://home/bin', '//home/env/bin')
== '//home://home/bin')


@pytest.mark.parametrize(
"node_version_file_content, expected_node_version",
[
("v22.14.0", "22.14.0"),
("22.14.0", "22.14.0"),
("v22.14.0\n", "22.14.0"),
("v22.14.0\r\n", "22.14.0"),
],
)
def test_node_version_file(node_version_file_content, expected_node_version):
def custom_exists(path):
if path == ".node-version":
return True
else:
return os.path.exists(path)

def custom_open(file_path, *args, **kwargs):
if file_path == ".node-version":
return mock.mock_open(read_data=node_version_file_content)()
else:
return open(file_path, *args, **kwargs)

with mock.patch("os.path.exists", new=custom_exists), mock.patch(
"builtins.open", new=custom_open
):
nodeenv.Config._load([])
assert nodeenv.Config.node == expected_node_version

0 comments on commit 4442d10

Please sign in to comment.