|
7 | 7 | import tarfile
|
8 | 8 | import tempfile
|
9 | 9 | import unittest.mock
|
10 |
| -import urllib |
11 |
| -import urllib.request |
12 | 10 |
|
13 | 11 | import git
|
14 | 12 | import github
|
@@ -286,33 +284,23 @@ def test_compile_sketches(mocker, compilation_success_list, expected_success, do
|
286 | 284 | )
|
287 | 285 |
|
288 | 286 |
|
289 |
| -def test_install_arduino_cli(tmpdir, mocker): |
| 287 | +def test_install_arduino_cli(mocker): |
290 | 288 | cli_version = "1.2.3"
|
| 289 | + arduino_cli_installation_path = unittest.mock.sentinel.arduino_cli_installation_path |
291 | 290 | arduino_cli_user_directory_path = pathlib.PurePath("/foo/arduino_cli_user_directory_path")
|
292 |
| - source_file_path = test_data_path.joinpath("githubevent.json") |
293 |
| - # Create temporary folder |
294 |
| - arduino_cli_installation_path = pathlib.PurePath(tmpdir.mkdir("test_install_arduino_cli")) |
295 |
| - output_archive_path = arduino_cli_installation_path.joinpath("foo_archive.tar.gz") |
296 |
| - |
297 |
| - # Create an archive file |
298 |
| - with tarfile.open(name=output_archive_path, mode="w:gz") as tar: |
299 |
| - tar.add(name=source_file_path, arcname=source_file_path.name) |
300 | 291 |
|
301 | 292 | compile_sketches = get_compilesketches_object(cli_version=cli_version)
|
302 | 293 | compile_sketches.arduino_cli_installation_path = arduino_cli_installation_path
|
303 | 294 | compile_sketches.arduino_cli_user_directory_path = arduino_cli_user_directory_path
|
304 | 295 |
|
305 |
| - # Patch urllib.request.urlopen so that the generated archive file is opened instead of the Arduino CLI download |
306 |
| - mocker.patch("urllib.request.urlopen", |
307 |
| - return_value=urllib.request.urlopen(url="file:///" + str(output_archive_path))) |
| 296 | + mocker.patch("compilesketches.install_from_download", autospec=True) |
308 | 297 |
|
309 | 298 | compile_sketches.install_arduino_cli()
|
310 | 299 |
|
311 |
| - urllib.request.urlopen.assert_called_once_with(url="https://downloads.arduino.cc/arduino-cli/arduino-cli_" |
312 |
| - + cli_version + "_Linux_64bit.tar.gz") |
313 |
| - |
314 |
| - # Verify that the installation matches the source file |
315 |
| - assert filecmp.cmp(f1=source_file_path, f2=arduino_cli_installation_path.joinpath(source_file_path.name)) is True |
| 300 | + compilesketches.install_from_download.assert_called_once_with( |
| 301 | + url="https://downloads.arduino.cc/arduino-cli/arduino-cli_" + cli_version + "_Linux_64bit.tar.gz", |
| 302 | + source_path="arduino-cli", |
| 303 | + destination_parent_path=arduino_cli_installation_path) |
316 | 304 |
|
317 | 305 | assert os.environ["ARDUINO_DIRECTORIES_USER"] == str(arduino_cli_user_directory_path)
|
318 | 306 |
|
@@ -1288,6 +1276,62 @@ def test_list_to_string():
|
1288 | 1276 | assert compilesketches.list_to_string([42, path]) == "42 " + str(path)
|
1289 | 1277 |
|
1290 | 1278 |
|
| 1279 | +@pytest.mark.parametrize("arcname, source_path, destination_name, expected_destination_name, expected_success", |
| 1280 | + [("FooArcname", ".", None, "FooArcname", True), |
| 1281 | + ("FooArcname", "./Sketch1", "FooDestinationName", "FooDestinationName", True), |
| 1282 | + ("FooArcname", "Sketch1", None, "Sketch1", True), |
| 1283 | + (".", "Sketch1", None, "Sketch1", True), |
| 1284 | + ("FooArcname", "Nonexistent", None, "", False), ]) |
| 1285 | +def test_install_from_download(capsys, |
| 1286 | + tmp_path, |
| 1287 | + arcname, |
| 1288 | + source_path, |
| 1289 | + destination_name, |
| 1290 | + expected_destination_name, |
| 1291 | + expected_success): |
| 1292 | + url_source_path = test_data_path.joinpath("HasSketches") |
| 1293 | + |
| 1294 | + # Create temporary folder |
| 1295 | + url_path = tmp_path.joinpath("url_path") |
| 1296 | + url_path.mkdir() |
| 1297 | + url_archive_path = url_path.joinpath("foo_archive.tar.gz") |
| 1298 | + url = url_archive_path.as_uri() |
| 1299 | + |
| 1300 | + # Create an archive file |
| 1301 | + with tarfile.open(name=url_archive_path, mode="w:gz", format=tarfile.GNU_FORMAT) as tar: |
| 1302 | + tar.add(name=url_source_path, arcname=arcname) |
| 1303 | + |
| 1304 | + destination_parent_path = tmp_path.joinpath("destination_parent_path") |
| 1305 | + |
| 1306 | + if expected_success: |
| 1307 | + compilesketches.install_from_download(url=url, |
| 1308 | + source_path=source_path, |
| 1309 | + destination_parent_path=destination_parent_path, |
| 1310 | + destination_name=destination_name) |
| 1311 | + |
| 1312 | + # Verify that the installation matches the source |
| 1313 | + assert directories_are_same(left_directory=url_source_path.joinpath(source_path), |
| 1314 | + right_directory=destination_parent_path.joinpath(expected_destination_name)) |
| 1315 | + else: |
| 1316 | + with pytest.raises(expected_exception=SystemExit, match="1"): |
| 1317 | + compilesketches.install_from_download(url=url, |
| 1318 | + source_path=source_path, |
| 1319 | + destination_parent_path=destination_parent_path, |
| 1320 | + destination_name=destination_name) |
| 1321 | + assert capsys.readouterr().out.strip() == ("::error::Archive source path: " + source_path + " not found") |
| 1322 | + |
| 1323 | + |
| 1324 | +@pytest.mark.parametrize("archive_extract_path, expected_archive_root_path", |
| 1325 | + [(test_data_path.joinpath("test_get_archive_root_folder_name", "has-root"), |
| 1326 | + test_data_path.joinpath("test_get_archive_root_folder_name", "has-root", "root")), |
| 1327 | + (test_data_path.joinpath("test_get_archive_root_folder_name", "has-file"), |
| 1328 | + test_data_path.joinpath("test_get_archive_root_folder_name", "has-file")), |
| 1329 | + (test_data_path.joinpath("test_get_archive_root_folder_name", "has-folders"), |
| 1330 | + test_data_path.joinpath("test_get_archive_root_folder_name", "has-folders"))]) |
| 1331 | +def test_get_archive_root_path(archive_extract_path, expected_archive_root_path): |
| 1332 | + assert compilesketches.get_archive_root_path(archive_extract_path) == expected_archive_root_path |
| 1333 | + |
| 1334 | + |
1291 | 1335 | @pytest.mark.parametrize("url, source_path, destination_name, expected_destination_name",
|
1292 | 1336 | [("https://example.com/foo/FooRepositoryName.git", ".", None, "FooRepositoryName"),
|
1293 | 1337 | ("https://example.com/foo/FooRepositoryName.git/", "./examples", "FooDestinationName",
|
|
0 commit comments