Skip to content

Commit

Permalink
Check host platform when finding node version (#363)
Browse files Browse the repository at this point in the history
See #362
  • Loading branch information
max0x53 authored Aug 25, 2024
1 parent 4442d10 commit cf0c993
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
25 changes: 22 additions & 3 deletions nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,19 +1045,38 @@ def print_node_versions():
logger.info('\t'.join(chunk))


def _get_last_node_version(lts=False):
"""
Return last node.js version matching the filter
"""
print({"x86": is_x86_64_musl(), "risc": is_riscv64(), "lts": lts})
def version_filter(v):
if lts and not v['lts']:
return False

if is_x86_64_musl() and "linux-x64-musl" not in v['files']:
return False
elif is_riscv64() and "linux-riscv64" not in v['files']:
return False

return True

return next((v['version'].lstrip('v')
for v in _get_versions_json() if version_filter(v)), None)


def get_last_stable_node_version():
"""
Return last stable node.js version
"""
return _get_versions_json()[0]['version'].lstrip('v')
return _get_last_node_version()


def get_last_lts_node_version():
"""
Return the last node.js version marked as LTS
"""
return next((v['version'].lstrip('v')
for v in _get_versions_json() if v['lts']), None)
return _get_last_node_version(lts=True)


def get_env_dir(args):
Expand Down
28 changes: 26 additions & 2 deletions tests/nodeenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ def cap_logging_info():
yield mck


@pytest.fixture
def mock_host_platform():
with mock.patch.object(nodeenv, 'is_x86_64_musl', return_value=False) as x64_mck:
with mock.patch.object(nodeenv, 'is_riscv64', return_value=False) as risc_mck:
yield


@pytest.fixture
def mock_riscv64_platform():
with mock.patch.object(nodeenv, 'is_x86_64_musl', return_value=False) as x64_mck:
with mock.patch.object(nodeenv, 'is_riscv64', return_value=True) as risc_mck:
yield


def mck_to_out(mck):
return '\n'.join(call[0][0] for call in mck.call_args_list)

Expand Down Expand Up @@ -142,16 +156,26 @@ def rewind(_):
mock_logger.assert_called()


@pytest.mark.usefixtures('mock_index_json')
@pytest.mark.usefixtures('mock_index_json', 'mock_host_platform')
def test_get_latest_node_version():
assert nodeenv.get_last_stable_node_version() == '13.5.0'


@pytest.mark.usefixtures('mock_index_json')
@pytest.mark.usefixtures('mock_index_json', 'mock_host_platform')
def test_get_lts_node_version():
assert nodeenv.get_last_lts_node_version() == '12.14.0'


@pytest.mark.usefixtures('mock_index_json', 'mock_riscv64_platform')
def test_get_latest_node_version_riscv64():
assert nodeenv.get_last_stable_node_version() == '13.4.0'


@pytest.mark.usefixtures('mock_index_json', 'mock_riscv64_platform')
def test_get_lts_node_version_riscv64():
assert nodeenv.get_last_lts_node_version() == '12.13.1'


def test__download_node_file():
with mock.patch.object(nodeenv, 'urlopen') as m_urlopen:
m_urlopen.side_effect = IncompleteRead("dummy")
Expand Down
4 changes: 2 additions & 2 deletions tests/nodejs_index.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[
{"version":"v13.5.0","date":"2019-12-18","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.13.4","v8":"7.9.317.25","uv":"1.34.0","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":false},
{"version":"v13.4.0","date":"2019-12-17","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.13.4","v8":"7.9.317.25","uv":"1.34.0","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":true},
{"version":"v13.4.0","date":"2019-12-17","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-riscv64","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.13.4","v8":"7.9.317.25","uv":"1.34.0","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":true},
{"version":"v13.3.0","date":"2019-12-03","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.13.1","v8":"7.9.317.25","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":false},
{"version":"v13.2.0","date":"2019-11-21","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.13.1","v8":"7.9.317.23","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":false},
{"version":"v13.1.0","date":"2019-11-05","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.12.1","v8":"7.8.279.17","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":false},
{"version":"v13.0.1","date":"2019-10-23","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.12.0","v8":"7.8.279.17","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":false},
{"version":"v13.0.0","date":"2019-10-10","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.12.0","v8":"7.8.279.17","uv":"1.32.0","zlib":"1.2.11","openssl":"1.1.1d","modules":"79","lts":false,"security":false},
{"version":"v12.14.0","date":"2019-12-16","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.13.4","v8":"7.7.299.13","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"72","lts":"Erbium","security":true},
{"version":"v12.13.1","date":"2019-11-19","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.12.1","v8":"7.7.299.13","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"72","lts":"Erbium","security":false},
{"version":"v12.13.1","date":"2019-11-19","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-riscv64","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.12.1","v8":"7.7.299.13","uv":"1.33.1","zlib":"1.2.11","openssl":"1.1.1d","modules":"72","lts":"Erbium","security":false},
{"version":"v12.13.0","date":"2019-10-21","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.12.0","v8":"7.7.299.13","uv":"1.32.0","zlib":"1.2.11","openssl":"1.1.1d","modules":"72","lts":"Erbium","security":false},
{"version":"v12.12.0","date":"2019-10-11","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.11.3","v8":"7.7.299.13","uv":"1.32.0","zlib":"1.2.11","openssl":"1.1.1d","modules":"72","lts":false,"security":false},
{"version":"v12.11.1","date":"2019-10-01","files":["aix-ppc64","headers","linux-arm64","linux-armv7l","linux-ppc64le","linux-s390x","linux-x64","osx-x64-pkg","osx-x64-tar","src","sunos-x64","win-x64-7z","win-x64-exe","win-x64-msi","win-x64-zip","win-x86-7z","win-x86-exe","win-x86-msi","win-x86-zip"],"npm":"6.11.3","v8":"7.7.299.11","uv":"1.32.0","zlib":"1.2.11","openssl":"1.1.1c","modules":"72","lts":false,"security":false},
Expand Down

0 comments on commit cf0c993

Please sign in to comment.