forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs_test.py
225 lines (191 loc) · 7.24 KB
/
nodejs_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations
import stat
from pathlib import Path
from textwrap import dedent
from typing import NoReturn
from unittest.mock import Mock
import pytest
from pants.backend.javascript.subsystems import nodejs
from pants.backend.javascript.subsystems.nodejs import (
NodeJS,
NodejsBinaries,
_BinaryPathsPerVersion,
determine_nodejs_binaries,
)
from pants.backend.javascript.target_types import JSSourcesGeneratorTarget
from pants.backend.python import target_types_rules
from pants.core.util_rules import config_files, source_files
from pants.core.util_rules.external_tool import (
DownloadedExternalTool,
ExternalToolRequest,
ExternalToolVersion,
)
from pants.core.util_rules.system_binaries import BinaryNotFoundError, BinaryPath
from pants.engine.internals.native_engine import EMPTY_DIGEST
from pants.engine.platform import Platform
from pants.engine.process import ProcessResult
from pants.testutil.rule_runner import MockGet, QueryRule, RuleRunner, run_rule_with_mocks
from pants.util.contextutil import temporary_dir
@pytest.fixture
def rule_runner() -> RuleRunner:
return RuleRunner(
rules=[
*nodejs.rules(),
*source_files.rules(),
*config_files.rules(),
*target_types_rules.rules(),
QueryRule(ProcessResult, [nodejs.NodeJSToolProcess]),
QueryRule(NodejsBinaries, ()),
],
target_types=[JSSourcesGeneratorTarget],
)
def test_npx_process(rule_runner: RuleRunner):
result = rule_runner.request(
ProcessResult,
[
nodejs.NodeJSToolProcess.npx(
npm_package="",
args=("--version",),
description="Testing NpxProcess",
)
],
)
assert result.stdout.strip() == b"8.5.5"
def test_npm_process(rule_runner: RuleRunner):
result = rule_runner.request(
ProcessResult,
[
nodejs.NodeJSToolProcess.npm(
args=("--version",),
description="Testing NpmProcess",
)
],
)
assert result.stdout.strip() == b"8.5.5"
def given_known_version(version: str) -> str:
return f"{version}|linux_x86_64|1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd|333333"
_SEMVER_1_1_0 = given_known_version("1.1.0")
_SEMVER_2_1_0 = given_known_version("2.1.0")
_SEMVER_2_2_0 = given_known_version("2.2.0")
_SEMVER_2_2_2 = given_known_version("2.2.2")
_SEMVER_3_0_0 = given_known_version("3.0.0")
@pytest.mark.parametrize(
("semver_range", "expected"),
[
pytest.param("1.x", _SEMVER_1_1_0, id="x_range"),
pytest.param("2.0 - 3.0", _SEMVER_2_1_0, id="hyphen"),
pytest.param(">2.2.0", _SEMVER_2_2_2, id="gt"),
pytest.param("2.2.x", _SEMVER_2_2_0, id="x_range_patch"),
pytest.param("~2.2.0", _SEMVER_2_2_0, id="thilde"),
pytest.param("^2.2.0", _SEMVER_2_2_0, id="caret"),
pytest.param("3.0.0", _SEMVER_3_0_0, id="exact"),
pytest.param("=3.0.0", _SEMVER_3_0_0, id="exact_equals"),
pytest.param("<3.0.0 >2.1", _SEMVER_2_2_0, id="and_range"),
pytest.param(">2.1 || <2.1", _SEMVER_1_1_0, id="or_range"),
],
)
def test_node_version_from_semver_download(semver_range: str, expected: str) -> None:
nodejs_subsystem = Mock(spec_set=NodeJS)
nodejs_subsystem.version = semver_range
nodejs_subsystem.known_versions = [
_SEMVER_1_1_0,
_SEMVER_2_1_0,
_SEMVER_2_2_0,
_SEMVER_2_2_2,
_SEMVER_3_0_0,
]
run_rule_with_mocks(
determine_nodejs_binaries,
rule_args=(nodejs_subsystem, Platform.linux_x86_64, _BinaryPathsPerVersion()),
mock_gets=[
MockGet(
DownloadedExternalTool,
(ExternalToolRequest,),
mock=lambda *_: DownloadedExternalTool(EMPTY_DIGEST, "myexe"),
),
],
)
nodejs_subsystem.download_known_version.assert_called_once_with(
ExternalToolVersion.decode(expected), Platform.linux_x86_64
)
@pytest.mark.parametrize(
("semver_range", "expected_path"),
[
pytest.param("1.x", "1/1/0", id="x_range"),
pytest.param("2.0 - 3.0", "2/1/0", id="hyphen"),
pytest.param(">2.2.0", "2/2/2", id="gt"),
pytest.param("2.2.x", "2/2/0", id="x_range_patch"),
pytest.param("~2.2.0", "2/2/0", id="thilde"),
pytest.param("^2.2.0", "2/2/0", id="caret"),
pytest.param("3.0.0", "3/0/0", id="exact"),
pytest.param("=3.0.0", "3/0/0", id="exact_equals"),
pytest.param("<3.0.0 >2.1", "2/2/0", id="and_range"),
pytest.param(">2.1 || <2.1", "1/1/0", id="or_range"),
],
)
def test_node_version_from_semver_bootstrap(semver_range: str, expected_path: str) -> None:
nodejs_subsystem = Mock(spec_set=NodeJS)
nodejs_subsystem.version = semver_range
discoverable_versions = _BinaryPathsPerVersion(
{
"1.1.0": (BinaryPath("1/1/0/node"),),
"2.1.0": (BinaryPath("2/1/0/node"),),
"2.2.0": (BinaryPath("2/2/0/node"),),
"2.2.2": (BinaryPath("2/2/2/node"),),
"3.0.0": (BinaryPath("3/0/0/node"),),
}
)
def mock_download(*_) -> NoReturn:
raise AssertionError("Should not run.")
result = run_rule_with_mocks(
determine_nodejs_binaries,
rule_args=(nodejs_subsystem, Platform.linux_x86_64, discoverable_versions),
mock_gets=[
MockGet(DownloadedExternalTool, (ExternalToolRequest,), mock=mock_download),
],
)
assert result.binary_dir == expected_path
def test_finding_no_node_version_is_an_error() -> None:
nodejs_subsystem = Mock(spec_set=NodeJS)
nodejs_subsystem.version = "*"
nodejs_subsystem.known_versions = []
discoverable_versions = _BinaryPathsPerVersion()
def mock_download(*_) -> DownloadedExternalTool:
return DownloadedExternalTool(EMPTY_DIGEST, "myexe")
with pytest.raises(BinaryNotFoundError):
run_rule_with_mocks(
determine_nodejs_binaries,
rule_args=(nodejs_subsystem, Platform.linux_x86_64, discoverable_versions),
mock_gets=[
MockGet(DownloadedExternalTool, (ExternalToolRequest,), mock=mock_download),
],
)
def mock_nodejs(version: str) -> str:
"""Return a bash script that emulates `node --version`."""
return dedent(
f"""\
#!/bin/bash
if [[ "$1" == '--version' ]]; then
echo '{version}'
fi
"""
)
def test_find_valid_binary(rule_runner: RuleRunner) -> None:
mock_binary = mock_nodejs("v3.0.0")
with temporary_dir() as tmpdir:
binary_dir = Path(tmpdir) / "bin"
binary_dir.mkdir()
binary_path = binary_dir / "node"
binary_path.write_text(mock_binary)
binary_path.chmod(binary_path.stat().st_mode | stat.S_IEXEC)
rule_runner.set_options(
[
f"--nodejs-search-path=['{binary_dir}']",
"--nodejs-version=>2",
],
env_inherit={"PATH"},
)
result = rule_runner.request(NodejsBinaries, ())
assert result.binary_dir == str(binary_dir)