Skip to content

Commit

Permalink
test: add unit tests for App.copy_files.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-laporte-pro committed May 31, 2024
1 parent b1212a1 commit 598edde
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 17 deletions.
13 changes: 4 additions & 9 deletions src/antares_web_installer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
41 changes: 33 additions & 8 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -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:
"""
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy Antares Web Server v2.15
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/config.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/examples/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/logs/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/matrices/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/tmp/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy Antares Web Server v2.14
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/config.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/examples/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/logs/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/matrices/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/tmp/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target

0 comments on commit 598edde

Please sign in to comment.