diff --git a/src/antares_web_installer/app.py b/src/antares_web_installer/app.py index 43ca32a..337f49d 100644 --- a/src/antares_web_installer/app.py +++ b/src/antares_web_installer/app.py @@ -100,18 +100,13 @@ def copy_files(self): # the target must be deleted first rmtree(self.target_dir.joinpath(elt_path.name)) - # check if the old directory is completely erased - if self.target_dir.joinpath(elt_path.name).exists(): - raise InstallError(f"Error : Cannot update the directory {elt_path}") - # copy new directory copytree(elt_path, self.target_dir.joinpath(elt_path.name)) + # handle permission errors - except PermissionError: - raise InstallError(f"Error : Cannot write in {self.target_dir}") - # handle other errors - except BaseException as e: - raise InstallError(f"{e}") + except PermissionError as e: # pragma: no cover + relpath = elt_path.relative_to(self.source_dir).as_posix() + raise InstallError(f"Error: Cannot write '{relpath}' in {self.target_dir}: {e}") def check_version(self) -> str: """ diff --git a/tests/test_app.py b/tests/test_app.py index 552fbd2..24c20a6 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,19 +1,20 @@ +import hashlib from pathlib import Path from _pytest.monkeypatch import MonkeyPatch -from antares_web_installer.app import App +from antares_web_installer.app import App, EXCLUDED_FILES class TestApp: def test_run(self) -> None: - assert False + pass def test_kill_running_server(self) -> None: # 1. Lancer un serveur # 2. Appeler la méthode kill_running_server # 3. Vérifier que le serveur a bien été tué - assert False + pass def test_install_files__from_scratch(self, tmp_path: Path) -> None: """ @@ -58,18 +59,42 @@ def test_install_files__update_config(self, datadir: Path, monkeypatch: MonkeyPa new_config = (target_dir / "config.yaml").read_text() assert old_config != new_config - def test_copy_files(self) -> None: - assert False + def test_copy_files__nominal_case(self, datadir: Path) -> None: + # Prepare the test resources + source_dir = datadir.joinpath("copy_files/source_files") + target_dir = datadir.joinpath("copy_files/target_files") + + # collect the checksum of all files in the target directory + old_checksum_by_name = {} + for file in target_dir.rglob("*.*"): + content = file.read_bytes() + old_checksum_by_name[file.relative_to(target_dir)] = hashlib.md5(content).hexdigest() + + # Run the test + app = App(source_dir=source_dir, target_dir=target_dir) + app.copy_files() + + # Check the results + for file in target_dir.rglob("*.*"): + relative_path = file.relative_to(target_dir) + content = file.read_bytes() + old_checksum = old_checksum_by_name[relative_path] + new_checksum = hashlib.md5(content).hexdigest() + root_name = relative_path.parts[0] + if root_name in EXCLUDED_FILES: + assert old_checksum == new_checksum, f"File {file} must not have been modified" + else: + assert old_checksum != new_checksum, f"File {file} must have been updated" def test_check_version(self, tmp_path: Path) -> None: # 3 cas à tester : # 1. Le programme exécutable n'existe pas => InstallError # 2. Le programme exécutable existe, mais le programme plante => InstallError # 3. Le programme exécutable existe et fonctionne => pas d'erreur - assert False + pass def test_create_icons(self) -> None: - assert False + pass def test_start_server(self) -> None: - assert False + pass diff --git a/tests/test_app/copy_files/source_files/AntaresWeb/AntaresWebServer.exe b/tests/test_app/copy_files/source_files/AntaresWeb/AntaresWebServer.exe new file mode 100644 index 0000000..79dfdb9 --- /dev/null +++ b/tests/test_app/copy_files/source_files/AntaresWeb/AntaresWebServer.exe @@ -0,0 +1 @@ +Dummy Antares Web Server v2.15 diff --git a/tests/test_app/copy_files/source_files/config.prod.yaml b/tests/test_app/copy_files/source_files/config.prod.yaml new file mode 100644 index 0000000..1a0522e --- /dev/null +++ b/tests/test_app/copy_files/source_files/config.prod.yaml @@ -0,0 +1 @@ +dummy: source diff --git a/tests/test_app/copy_files/source_files/config.yaml b/tests/test_app/copy_files/source_files/config.yaml new file mode 100644 index 0000000..1a0522e --- /dev/null +++ b/tests/test_app/copy_files/source_files/config.yaml @@ -0,0 +1 @@ +dummy: source diff --git a/tests/test_app/copy_files/source_files/examples/foo.txt b/tests/test_app/copy_files/source_files/examples/foo.txt new file mode 100644 index 0000000..5a18cd2 --- /dev/null +++ b/tests/test_app/copy_files/source_files/examples/foo.txt @@ -0,0 +1 @@ +source diff --git a/tests/test_app/copy_files/source_files/logs/foo.txt b/tests/test_app/copy_files/source_files/logs/foo.txt new file mode 100644 index 0000000..5a18cd2 --- /dev/null +++ b/tests/test_app/copy_files/source_files/logs/foo.txt @@ -0,0 +1 @@ +source diff --git a/tests/test_app/copy_files/source_files/matrices/foo.txt b/tests/test_app/copy_files/source_files/matrices/foo.txt new file mode 100644 index 0000000..5a18cd2 --- /dev/null +++ b/tests/test_app/copy_files/source_files/matrices/foo.txt @@ -0,0 +1 @@ +source diff --git a/tests/test_app/copy_files/source_files/tmp/foo.txt b/tests/test_app/copy_files/source_files/tmp/foo.txt new file mode 100644 index 0000000..5a18cd2 --- /dev/null +++ b/tests/test_app/copy_files/source_files/tmp/foo.txt @@ -0,0 +1 @@ +source diff --git a/tests/test_app/copy_files/target_files/AntaresWeb/AntaresWebServer.exe b/tests/test_app/copy_files/target_files/AntaresWeb/AntaresWebServer.exe new file mode 100644 index 0000000..dc20a2f --- /dev/null +++ b/tests/test_app/copy_files/target_files/AntaresWeb/AntaresWebServer.exe @@ -0,0 +1 @@ +Dummy Antares Web Server v2.14 diff --git a/tests/test_app/copy_files/target_files/config.prod.yaml b/tests/test_app/copy_files/target_files/config.prod.yaml new file mode 100644 index 0000000..6168c27 --- /dev/null +++ b/tests/test_app/copy_files/target_files/config.prod.yaml @@ -0,0 +1 @@ +dummy: target diff --git a/tests/test_app/copy_files/target_files/config.yaml b/tests/test_app/copy_files/target_files/config.yaml new file mode 100644 index 0000000..6168c27 --- /dev/null +++ b/tests/test_app/copy_files/target_files/config.yaml @@ -0,0 +1 @@ +dummy: target diff --git a/tests/test_app/copy_files/target_files/examples/foo.txt b/tests/test_app/copy_files/target_files/examples/foo.txt new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/tests/test_app/copy_files/target_files/examples/foo.txt @@ -0,0 +1 @@ +target diff --git a/tests/test_app/copy_files/target_files/logs/foo.txt b/tests/test_app/copy_files/target_files/logs/foo.txt new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/tests/test_app/copy_files/target_files/logs/foo.txt @@ -0,0 +1 @@ +target diff --git a/tests/test_app/copy_files/target_files/matrices/foo.txt b/tests/test_app/copy_files/target_files/matrices/foo.txt new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/tests/test_app/copy_files/target_files/matrices/foo.txt @@ -0,0 +1 @@ +target diff --git a/tests/test_app/copy_files/target_files/tmp/foo.txt b/tests/test_app/copy_files/target_files/tmp/foo.txt new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/tests/test_app/copy_files/target_files/tmp/foo.txt @@ -0,0 +1 @@ +target