From 112c7af6682ee44530a21169071f7571043ac6b8 Mon Sep 17 00:00:00 2001 From: Han Yeong-woo Date: Tue, 11 Jun 2024 10:26:20 +0900 Subject: [PATCH] Support leading `v` in `.node-version` --- nodeenv.py | 2 +- tests/nodeenv_test.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/nodeenv.py b/nodeenv.py index e3aaffd..2118186 100644 --- a/nodeenv.py +++ b/nodeenv.py @@ -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): diff --git a/tests/nodeenv_test.py b/tests/nodeenv_test.py index d28d42b..a70973d 100644 --- a/tests/nodeenv_test.py +++ b/tests/nodeenv_test.py @@ -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