-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_check_nodejs.py
146 lines (110 loc) · 4.48 KB
/
test_check_nodejs.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
import json
from unittest.mock import MagicMock, mock_open, patch
import pytest
import responses
from bs4 import BeautifulSoup
from check_nodejs import get_nodejs_versions, main
def mock_previous_releases_html():
return """
<table>
<tr>
<td data-label="Version">Node.js v16.20.0</td>
</tr>
<tr>
<td data-label="Version">Node.js v14.21.3</td>
</tr>
<tr>
<td data-label="Version">Node.js v12.22.12</td>
</tr>
<tr>
<td data-label="Version">Node.js v10.24.1</td>
</tr>
</table>
"""
def test_get_nodejs_versions():
"""Test fetching Node.js versions with valid major versions."""
with patch("check_nodejs.requests.get") as mock_get:
mock_get.return_value.content = mock_previous_releases_html()
result = get_nodejs_versions([16, 14])
expected = ["v16.20.0", "v14.21.3"]
assert result == expected
def test_get_nodejs_versions_no_match():
"""Test fetching Node.js versions with no matching major versions."""
with patch("check_nodejs.requests.get") as mock_get:
mock_get.return_value.content = mock_previous_releases_html()
result = get_nodejs_versions([8])
expected = []
assert result == expected
def test_get_nodejs_versions_empty_page():
"""Test fetching Node.js versions when the HTML page is empty."""
with patch("check_nodejs.requests.get") as mock_get:
mock_get.return_value.content = "<html></html>"
result = get_nodejs_versions([16])
expected = []
assert result == expected
def test_main(monkeypatch):
"""Test the main function including JSON handling and updates."""
test_args = ["check_nodejs.py", "16,14"]
monkeypatch.setattr("sys.argv", test_args)
# Mock open to simulate versions.json content
mock_json = json.dumps(
{
"nodejs": {
"16": {"version": "v16.19.0"},
"14": {"version": "v14.20.0"},
}
}
)
with patch("builtins.open", mock_open(read_data=mock_json)) as mock_file:
with patch("check_nodejs.requests.get") as mock_get:
mock_get.return_value.content = mock_previous_releases_html()
with patch("builtins.print") as mock_print:
main()
# Check that the print statement output matches the updated versions
mock_print.assert_called_once_with("16.20.0,14.21.3")
# Ensure the file was read
mock_file.assert_called_once_with("versions.json", encoding="utf-8")
def test_main_no_updates(monkeypatch):
"""Test the main function when there are no updates to versions."""
test_args = ["check_nodejs.py", "16,14"]
monkeypatch.setattr("sys.argv", test_args)
# Mock open to simulate versions.json content
mock_json = json.dumps(
{
"nodejs": {
"16": {"version": "v16.20.0"},
"14": {"version": "v14.21.3"},
}
}
)
with patch("builtins.open", mock_open(read_data=mock_json)) as mock_file:
with patch("check_nodejs.requests.get") as mock_get:
mock_get.return_value.content = mock_previous_releases_html()
with patch("builtins.print") as mock_print:
main()
# Ensure nothing was printed (no updates)
mock_print.assert_not_called()
# Ensure the file was read
mock_file.assert_called_once_with("versions.json", encoding="utf-8")
@pytest.mark.skip
def test_main_missing_major_version(monkeypatch):
"""Test the main function with a missing major version in the JSON file."""
test_args = ["check_nodejs.py", "12"]
monkeypatch.setattr("sys.argv", test_args)
# Mock open to simulate versions.json content
mock_json = json.dumps(
{
"nodejs": {
"16": {"version": "v16.19.0"},
}
}
)
with patch("builtins.open", mock_open(read_data=mock_json)) as mock_file:
with patch("check_nodejs.requests.get") as mock_get:
mock_get.return_value.content = mock_previous_releases_html()
with patch("builtins.print") as mock_print: # Patch print instead of stderr
main()
# Ensure error message for missing major version is printed
mock_print.assert_any_call(
"Error: Major version 12 not found in current versions."
)