diff --git a/swh/fuse/tests/common.py b/swh/fuse/tests/common.py index 3453c91..c9f9cbd 100644 --- a/swh/fuse/tests/common.py +++ b/swh/fuse/tests/common.py @@ -1,22 +1,15 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -from pathlib import Path from typing import Any from swh.fuse.tests.data.api_data import MOCK_ARCHIVE, SWHID2URL def get_data_from_archive(swhid: str, raw: bool = False) -> Any: url = SWHID2URL[swhid] if raw: url += "raw/" return MOCK_ARCHIVE[url] - - -def assert_file_content(path: Path, expected: str) -> None: - with open(path, "r") as f: - actual = f.read() - assert actual == expected diff --git a/swh/fuse/tests/conftest.py b/swh/fuse/tests/conftest.py index 5ff80d7..4f0fdb9 100644 --- a/swh/fuse/tests/conftest.py +++ b/swh/fuse/tests/conftest.py @@ -1,75 +1,69 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import json from multiprocessing import Process -from os import listdir +import os from pathlib import Path import subprocess from tempfile import NamedTemporaryFile, TemporaryDirectory import time from click.testing import CliRunner import pytest import yaml from swh.fuse import cli from swh.fuse.tests.data.api_data import API_URL, MOCK_ARCHIVE @pytest.fixture def web_api_mock(requests_mock): for api_call, data in MOCK_ARCHIVE.items(): # Convert Python dict JSON into a string (only for non-raw API call) if not api_call.endswith("raw/"): data = json.dumps(data) requests_mock.get(f"{API_URL}/{api_call}", text=data) return requests_mock @pytest.fixture def fuse_mntdir(web_api_mock): tmpdir = TemporaryDirectory(suffix=".swh-fuse-test") tmpfile = NamedTemporaryFile(suffix=".swh-fuse-test.yml") config = { "cache": {"metadata": {"in-memory": True}, "blob": {"in-memory": True}}, "web-api": {"url": API_URL, "auth-token": None}, } # Run FUSE in foreground mode but in a separate process, so it does not # block execution and remains easy to kill during teardown def fuse_process(tmpdir, tmpfile): with tmpdir as mntdir, tmpfile as config_path: config_path = Path(config_path.name) config_path.write_text(yaml.dump(config)) CliRunner().invoke( - cli.mount, - args=[ - mntdir, - "--foreground", - "--config-file", - config_path, - ], + cli.mount, args=[mntdir, "--foreground", "--config-file", config_path,], ) fuse = Process(target=fuse_process, args=[tmpdir, tmpfile]) fuse.start() # Wait max 3 seconds for the FUSE to correctly mount for i in range(30): try: - root = listdir(tmpdir.name) + root = os.listdir(tmpdir.name) if root: break except FileNotFoundError: pass time.sleep(0.1) else: raise FileNotFoundError(f"Could not mount FUSE in {tmpdir.name}") - yield tmpdir.name + yield Path(tmpdir.name) subprocess.run(["fusermount", "-u", tmpdir.name], check=True) fuse.join() diff --git a/swh/fuse/tests/test_cli.py b/swh/fuse/tests/test_cli.py index d6ceeab..f5256bc 100644 --- a/swh/fuse/tests/test_cli.py +++ b/swh/fuse/tests/test_cli.py @@ -1,19 +1,18 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os -from pathlib import Path from swh.fuse.tests.data.config import REGULAR_FILE def test_mountpoint(fuse_mntdir): - archive_dir = Path(fuse_mntdir, "archive") - meta_dir = Path(fuse_mntdir, "meta") + archive_dir = fuse_mntdir / "archive" + meta_dir = fuse_mntdir / "meta" assert os.listdir(archive_dir) == [] assert os.listdir(meta_dir) == [] # On the fly mounting file_path = archive_dir / REGULAR_FILE assert file_path.is_file() diff --git a/swh/fuse/tests/test_content.py b/swh/fuse/tests/test_content.py index 3e57174..e2ba2a9 100644 --- a/swh/fuse/tests/test_content.py +++ b/swh/fuse/tests/test_content.py @@ -1,15 +1,13 @@ -from pathlib import Path - -from swh.fuse.tests.common import assert_file_content, get_data_from_archive +from swh.fuse.tests.common import get_data_from_archive from swh.fuse.tests.data.config import REGULAR_FILE def test_access_file(fuse_mntdir): - file_path = Path(fuse_mntdir, "archive", REGULAR_FILE) + file_path = fuse_mntdir / "archive" / REGULAR_FILE assert file_path.is_file() def test_cat_file(fuse_mntdir): - file_path = Path(fuse_mntdir, "archive", REGULAR_FILE) + file_path = fuse_mntdir / "archive" / REGULAR_FILE expected = get_data_from_archive(REGULAR_FILE, raw=True) - assert_file_content(file_path, expected) + assert file_path.read_text() == expected diff --git a/swh/fuse/tests/test_directory.py b/swh/fuse/tests/test_directory.py index c28056c..4289d94 100644 --- a/swh/fuse/tests/test_directory.py +++ b/swh/fuse/tests/test_directory.py @@ -1,39 +1,38 @@ -from os import listdir, readlink -from pathlib import Path +import os from swh.fuse.tests.common import get_data_from_archive from swh.fuse.tests.data.config import DIR_WITH_SUBMODULES, ROOT_DIR def test_list_dir(fuse_mntdir): - dir_path = Path(fuse_mntdir, "archive", ROOT_DIR) + dir_path = fuse_mntdir / "archive" / ROOT_DIR dir_meta = get_data_from_archive(ROOT_DIR) expected = [x["name"] for x in dir_meta] - actual = listdir(dir_path) + actual = os.listdir(dir_path) assert set(actual) == set(expected) def test_access_file(fuse_mntdir): - file_path = Path(fuse_mntdir, "archive", ROOT_DIR, "README.md") + file_path = fuse_mntdir / "archive" / ROOT_DIR / "README.md" assert file_path.is_file() def test_access_subdir(fuse_mntdir): - dir_path = Path(fuse_mntdir, "archive", ROOT_DIR, "src") + dir_path = fuse_mntdir / "archive" / ROOT_DIR / "src" assert dir_path.is_dir() def test_access_submodule_entries(fuse_mntdir): - dir_path = Path(fuse_mntdir, "archive", DIR_WITH_SUBMODULES) + dir_path = fuse_mntdir / "archive" / DIR_WITH_SUBMODULES submodules = { "book": "swh:1:rev:87dd6843678575f8dda962f239d14ef4be14b352", "edition-guide": "swh:1:rev:1a2390247ad6d08160e0dd74f40a01a9578659c2", "embedded-book": "swh:1:rev:4d78994915af1bde9a95c04a8c27d8dca066232a", "nomicon": "swh:1:rev:3e6e1001dc6e095dbd5c88005e80969f60e384e1", "reference": "swh:1:rev:11e893fc1357bc688418ddf1087c2b7aa25d154d", "rust-by-example": "swh:1:rev:1c2bd024d13f8011307e13386cf1fea2180352b5", "rustc-guide": "swh:1:rev:92baf7293dd2d418d2ac4b141b0faa822075d9f7", } for filename, swhid in submodules.items(): target = f"../../archive/{swhid}" - assert readlink(Path(dir_path, filename)) == target + assert os.readlink(dir_path / filename) == target diff --git a/swh/fuse/tests/test_meta.py b/swh/fuse/tests/test_meta.py index 5c83de6..e27c7f8 100644 --- a/swh/fuse/tests/test_meta.py +++ b/swh/fuse/tests/test_meta.py @@ -1,18 +1,16 @@ import json -from os import listdir -from pathlib import Path -from swh.fuse.tests.common import assert_file_content, get_data_from_archive +from swh.fuse.tests.common import get_data_from_archive from swh.fuse.tests.data.config import ALL_ENTRIES def test_access_meta_file(fuse_mntdir): for swhid in ALL_ENTRIES: # On the fly mounting - file_path_archive = Path(fuse_mntdir, "archive", swhid) + file_path_archive = fuse_mntdir / "archive" / swhid file_path_archive.exists() - file_path_meta = Path(fuse_mntdir, "meta", f"{swhid}.json") + file_path_meta = fuse_mntdir / f"meta/{swhid}.json" assert file_path_meta.exists() expected = json.dumps(get_data_from_archive(swhid)) - assert_file_content(file_path_meta, expected) + assert file_path_meta.read_text() == expected diff --git a/swh/fuse/tests/test_revision.py b/swh/fuse/tests/test_revision.py index 1f68efd..ee6051b 100644 --- a/swh/fuse/tests/test_revision.py +++ b/swh/fuse/tests/test_revision.py @@ -1,36 +1,35 @@ import json -from os import listdir, readlink -from pathlib import Path +import os -from swh.fuse.tests.common import assert_file_content, get_data_from_archive +from swh.fuse.tests.common import get_data_from_archive from swh.fuse.tests.data.config import ROOT_DIR, ROOT_REV def test_access_meta(fuse_mntdir): - file_path = Path(fuse_mntdir, "archive", ROOT_REV, "meta.json") + file_path = fuse_mntdir / "archive" / ROOT_REV / "meta.json" expected = json.dumps(get_data_from_archive(ROOT_REV)) - assert_file_content(file_path, expected) + assert file_path.read_text() == expected def test_list_root(fuse_mntdir): - dir_path = Path(fuse_mntdir, "archive", ROOT_REV, "root") + dir_path = fuse_mntdir / "archive" / ROOT_REV / "root" dir_meta = get_data_from_archive(ROOT_DIR) expected = [x["name"] for x in dir_meta] - actual = listdir(dir_path) + actual = os.listdir(dir_path) assert set(actual) == set(expected) def test_list_parents(fuse_mntdir): rev_meta = get_data_from_archive(ROOT_REV) - dir_path = Path(fuse_mntdir, "archive", ROOT_REV, "parents") + dir_path = fuse_mntdir / "archive" / ROOT_REV / "parents" for i, parent in enumerate(rev_meta["parents"]): - parent_path = Path(dir_path, str(i + 1)) + parent_path = dir_path / str(i + 1) parent_swhid = f"swh:1:rev:{parent['id']}" assert parent_path.is_symlink() - assert readlink(parent_path) == f"../../../archive/{parent_swhid}" + assert os.readlink(parent_path) == f"../../../archive/{parent_swhid}" def test_list_parent(fuse_mntdir): - file_path = Path(fuse_mntdir, "archive", ROOT_REV, "parent") + file_path = fuse_mntdir / "archive" / ROOT_REV / "parent" assert file_path.is_symlink() - assert readlink(file_path) == "parents/1/" + assert os.readlink(file_path) == "parents/1/"