Skip to content

Commit 4442d10

Browse files
authored
Support leading v in .node-version (#359)
1 parent 62ea1b7 commit 4442d10

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

nodeenv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def _load(cls, configfiles, verbose=False):
138138

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

143143
@classmethod
144144
def _dump(cls):

tests/nodeenv_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,32 @@ def test_remove_env_bin_from_path():
178178
assert (nodeenv.remove_env_bin_from_path(
179179
'//home://home/env/bin://home/bin', '//home/env/bin')
180180
== '//home://home/bin')
181+
182+
183+
@pytest.mark.parametrize(
184+
"node_version_file_content, expected_node_version",
185+
[
186+
("v22.14.0", "22.14.0"),
187+
("22.14.0", "22.14.0"),
188+
("v22.14.0\n", "22.14.0"),
189+
("v22.14.0\r\n", "22.14.0"),
190+
],
191+
)
192+
def test_node_version_file(node_version_file_content, expected_node_version):
193+
def custom_exists(path):
194+
if path == ".node-version":
195+
return True
196+
else:
197+
return os.path.exists(path)
198+
199+
def custom_open(file_path, *args, **kwargs):
200+
if file_path == ".node-version":
201+
return mock.mock_open(read_data=node_version_file_content)()
202+
else:
203+
return open(file_path, *args, **kwargs)
204+
205+
with mock.patch("os.path.exists", new=custom_exists), mock.patch(
206+
"builtins.open", new=custom_open
207+
):
208+
nodeenv.Config._load([])
209+
assert nodeenv.Config.node == expected_node_version

0 commit comments

Comments
 (0)