Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/tests/test_cli.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | |||||
from pathlib import Path | from pathlib import Path | ||||
from unittest.mock import Mock, call | from unittest.mock import Mock, call | ||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
import pytest | import pytest | ||||
import swh.scanner.cli as cli | import swh.scanner.cli as cli | ||||
import swh.scanner.scanner as scanner | import swh.scanner.scanner as scanner | ||||
from .data import present_swhids | |||||
DATADIR = Path(__file__).absolute().parent / "data" | DATADIR = Path(__file__).absolute().parent / "data" | ||||
CONFIG_PATH_GOOD = str(DATADIR / "global.yml") | CONFIG_PATH_GOOD = str(DATADIR / "global.yml") | ||||
CONFIG_PATH_GOOD2 = str(DATADIR / "global2.yml") # alternative to global.yml | CONFIG_PATH_GOOD2 = str(DATADIR / "global2.yml") # alternative to global.yml | ||||
ROOTPATH_GOOD = str(DATADIR) | ROOTPATH_GOOD = str(DATADIR) | ||||
@pytest.fixture(scope="function") | @pytest.fixture(scope="function") | ||||
def m_scanner(mocker): | def m_scanner(mocker): | ||||
Show All 13 Lines | |||||
@pytest.fixture(scope="function") | @pytest.fixture(scope="function") | ||||
def cli_runner(monkeypatch, tmp_path): | def cli_runner(monkeypatch, tmp_path): | ||||
"""Return a CliRunner with default environment variable SWH_CONFIG_FILE unset""" | """Return a CliRunner with default environment variable SWH_CONFIG_FILE unset""" | ||||
BAD_CONFIG_PATH = str(tmp_path / "missing") | BAD_CONFIG_PATH = str(tmp_path / "missing") | ||||
monkeypatch.setattr(cli, "DEFAULT_CONFIG_PATH", BAD_CONFIG_PATH) | monkeypatch.setattr(cli, "DEFAULT_CONFIG_PATH", BAD_CONFIG_PATH) | ||||
return CliRunner(env={"SWH_CONFIG_FILE": None}) | return CliRunner(env={"SWH_CONFIG_FILE": None}) | ||||
@pytest.fixture(scope="function") | |||||
def bad_input_file(tmp_path): | |||||
bad_input_file = Path(os.path.join(tmp_path, "input_file.txt")) | |||||
# wrong SWHID hash | |||||
bad_swhid = "swh:1:cnt:fa8eacf43d8646129ae8adfa1648f9307d999999bbbbb" | |||||
swhids = present_swhids + [bad_swhid] | |||||
with open(bad_input_file, "w") as f: | |||||
f.write("\n".join(swhid for swhid in swhids)) | |||||
assert bad_input_file.exists() | |||||
return bad_input_file | |||||
@pytest.fixture(scope="function") | |||||
def good_input_file(tmp_path): | |||||
good_input_file = Path(os.path.join(tmp_path, "input_file.txt")) | |||||
with open(good_input_file, "w") as f: | |||||
f.write("\n".join(swhid for swhid in present_swhids)) | |||||
assert good_input_file.exists() | |||||
return good_input_file | |||||
# TEST BEGIN | # TEST BEGIN | ||||
# For nominal code paths, check that the right config file is loaded | # For nominal code paths, check that the right config file is loaded | ||||
# scanner is mocked to not run actual scan, config loading is mocked to check its usage | # scanner is mocked to not run actual scan, config loading is mocked to check its usage | ||||
def test_smoke(cli_runner): | def test_smoke(cli_runner): | ||||
"""Break if basic functionality breaks""" | """Break if basic functionality breaks""" | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | |||||
def test_api_url_option(cli_runner, m_scanner): | def test_api_url_option(cli_runner, m_scanner): | ||||
"""Test no config good root good url""" | """Test no config good root good url""" | ||||
API_URL = "https://example.com/api/1" # without trailing "/" | API_URL = "https://example.com/api/1" # without trailing "/" | ||||
res = cli_runner.invoke(cli.scanner, ["scan", ROOTPATH_GOOD, "-u", API_URL]) | res = cli_runner.invoke(cli.scanner, ["scan", ROOTPATH_GOOD, "-u", API_URL]) | ||||
assert res.exit_code == 0 | assert res.exit_code == 0 | ||||
assert m_scanner.scan.call_count == 1 | assert m_scanner.scan.call_count == 1 | ||||
def test_db_option_bad_input_file(cli_runner, bad_input_file, live_server): | |||||
api_url = live_server.url() + "/" | |||||
res = cli_runner.invoke( | |||||
cli.scanner, ["db", "import", "--input", bad_input_file, "-u", api_url] | |||||
) | |||||
assert res.exit_code != 0 | |||||
def test_db_option_good_input_file(cli_runner, good_input_file, live_server): | |||||
api_url = live_server.url() + "/" | |||||
res = cli_runner.invoke( | |||||
cli.scanner, ["db", "import", "--input", good_input_file, "-u", api_url] | |||||
) | |||||
assert res.exit_code == 0 |