2020-04-15 14:44:04 +00:00
|
|
|
import pytest
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from pprint import pprint
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
from pyproject_preprocess_record import parse_record, read_record, save_parsed_record
|
2020-04-15 14:44:04 +00:00
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
from pyproject_save_files import argparser, generate_file_list, BuildrootPath
|
|
|
|
from pyproject_save_files import main as save_files_main
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
DIR = Path(__file__).parent
|
|
|
|
BINDIR = BuildrootPath("/usr/bin")
|
2020-08-04 12:49:05 +00:00
|
|
|
DATADIR = BuildrootPath("/usr/share")
|
2020-04-15 14:44:04 +00:00
|
|
|
SITELIB = BuildrootPath("/usr/lib/python3.7/site-packages")
|
|
|
|
SITEARCH = BuildrootPath("/usr/lib64/python3.7/site-packages")
|
|
|
|
|
|
|
|
yaml_file = DIR / "pyproject_save_files_test_data.yaml"
|
|
|
|
yaml_data = yaml.safe_load(yaml_file.read_text())
|
|
|
|
EXPECTED_DICT = yaml_data["classified"]
|
|
|
|
EXPECTED_FILES = yaml_data["dumped"]
|
|
|
|
TEST_RECORDS = yaml_data["records"]
|
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def tldr_root(tmp_path):
|
|
|
|
prepare_pyproject_record(tmp_path, package="tldr")
|
|
|
|
return tmp_path
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def pyproject_record(tmp_path):
|
|
|
|
return tmp_path / "pyproject-record"
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def prepare_pyproject_record(tmp_path, package=None, content=None):
|
2020-04-15 14:44:04 +00:00
|
|
|
"""
|
2020-09-23 04:08:21 +00:00
|
|
|
Creates RECORD from test data and then uses
|
|
|
|
functions from pyproject_process_record to convert
|
|
|
|
it to pyproject-record file which is then
|
|
|
|
further processed by functions from pyproject_save_files.
|
|
|
|
"""
|
|
|
|
record_file = tmp_path / "RECORD"
|
|
|
|
pyproject_record = tmp_path / "pyproject-record"
|
2020-04-15 14:44:04 +00:00
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
if package is not None:
|
|
|
|
# Get test data and write dist-info/RECORD file
|
|
|
|
record_path = BuildrootPath(TEST_RECORDS[package]["path"])
|
|
|
|
record_file.write_text(TEST_RECORDS[package]["content"])
|
|
|
|
# Parse RECORD file
|
|
|
|
parsed_record = parse_record(record_path, read_record(record_file))
|
|
|
|
# Save JSON content to pyproject-record
|
|
|
|
save_parsed_record(record_path, parsed_record, pyproject_record)
|
|
|
|
elif content is not None:
|
|
|
|
save_parsed_record(*content, output_file=pyproject_record)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def output(tmp_path):
|
|
|
|
return tmp_path / "pyproject_files"
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_record_tldr():
|
|
|
|
record_path = BuildrootPath(TEST_RECORDS["tldr"]["path"])
|
|
|
|
record_content = read_record(DIR / "test_RECORD")
|
|
|
|
output = list(parse_record(record_path, record_content))
|
|
|
|
pprint(output)
|
|
|
|
expected = [
|
2020-09-23 04:06:53 +00:00
|
|
|
str(BINDIR / "__pycache__/tldr.cpython-37.pyc"),
|
|
|
|
str(BINDIR / "tldr"),
|
|
|
|
str(BINDIR / "tldr.py"),
|
|
|
|
str(SITELIB / "__pycache__/tldr.cpython-37.pyc"),
|
|
|
|
str(SITELIB / "tldr-0.5.dist-info/INSTALLER"),
|
|
|
|
str(SITELIB / "tldr-0.5.dist-info/LICENSE"),
|
|
|
|
str(SITELIB / "tldr-0.5.dist-info/METADATA"),
|
|
|
|
str(SITELIB / "tldr-0.5.dist-info/RECORD"),
|
|
|
|
str(SITELIB / "tldr-0.5.dist-info/WHEEL"),
|
|
|
|
str(SITELIB / "tldr-0.5.dist-info/top_level.txt"),
|
|
|
|
str(SITELIB / "tldr.py"),
|
2020-04-15 14:44:04 +00:00
|
|
|
]
|
|
|
|
assert output == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_record_tensorflow():
|
|
|
|
long = "tensorflow_core/include/tensorflow/core/common_runtime/base_collective_executor.h"
|
|
|
|
record_path = SITEARCH / "tensorflow-2.1.0.dist-info/RECORD"
|
|
|
|
record_content = [
|
|
|
|
["../../../bin/toco_from_protos", "sha256=hello", "289"],
|
|
|
|
[f"../../../lib/python3.7/site-packages/{long}", "sha256=darkness", "1024"],
|
|
|
|
["tensorflow-2.1.0.dist-info/METADATA", "sha256=friend", "2859"],
|
|
|
|
]
|
|
|
|
output = list(parse_record(record_path, record_content))
|
|
|
|
pprint(output)
|
|
|
|
expected = [
|
2020-09-23 04:06:53 +00:00
|
|
|
str(BINDIR / "toco_from_protos"),
|
|
|
|
str(SITELIB / long),
|
|
|
|
str(SITEARCH / "tensorflow-2.1.0.dist-info/METADATA"),
|
2020-04-15 14:44:04 +00:00
|
|
|
]
|
|
|
|
assert output == expected
|
|
|
|
|
|
|
|
|
2020-07-02 09:46:10 +00:00
|
|
|
def remove_others(expected):
|
2020-09-02 11:27:10 +00:00
|
|
|
return [p for p in expected if not (p.startswith(str(BINDIR)) or p.endswith(".pth") or p.rpartition(' ')[-1].startswith(str(DATADIR)))]
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-07-02 09:46:10 +00:00
|
|
|
@pytest.mark.parametrize("include_auto", (True, False))
|
2020-04-15 14:44:04 +00:00
|
|
|
@pytest.mark.parametrize("package, glob, expected", EXPECTED_FILES)
|
2020-07-02 09:46:10 +00:00
|
|
|
def test_generate_file_list(package, glob, expected, include_auto):
|
2020-04-15 14:44:04 +00:00
|
|
|
paths_dict = EXPECTED_DICT[package]
|
|
|
|
modules_glob = {glob}
|
2020-07-02 09:46:10 +00:00
|
|
|
if not include_auto:
|
|
|
|
expected = remove_others(expected)
|
|
|
|
tested = generate_file_list(paths_dict, modules_glob, include_auto)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
assert tested == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_file_list_unused_glob():
|
|
|
|
paths_dict = EXPECTED_DICT["kerberos"]
|
|
|
|
modules_glob = {"kerberos", "unused_glob1", "unused_glob2", "kerb*"}
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
generate_file_list(paths_dict, modules_glob, True)
|
|
|
|
|
|
|
|
assert "unused_glob1, unused_glob2" in str(excinfo.value)
|
|
|
|
assert "kerb" not in str(excinfo.value)
|
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def default_options(output, mock_root, pyproject_record):
|
2020-04-15 14:44:04 +00:00
|
|
|
return [
|
|
|
|
"--output",
|
|
|
|
str(output),
|
|
|
|
"--buildroot",
|
|
|
|
str(mock_root),
|
|
|
|
"--sitelib",
|
|
|
|
str(SITELIB),
|
|
|
|
"--sitearch",
|
|
|
|
str(SITEARCH),
|
|
|
|
"--python-version",
|
2020-09-23 04:08:21 +00:00
|
|
|
"3.7", # test data are for 3.7,
|
|
|
|
"--pyproject-record",
|
|
|
|
str(pyproject_record)
|
2020-04-15 14:44:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-07-02 09:46:10 +00:00
|
|
|
@pytest.mark.parametrize("include_auto", (True, False))
|
2020-04-15 14:44:04 +00:00
|
|
|
@pytest.mark.parametrize("package, glob, expected", EXPECTED_FILES)
|
2020-09-23 04:08:21 +00:00
|
|
|
def test_cli(tmp_path, package, glob, expected, include_auto, pyproject_record):
|
|
|
|
prepare_pyproject_record(tmp_path, package)
|
2020-04-15 14:44:04 +00:00
|
|
|
output = tmp_path / "files"
|
2020-07-02 09:46:10 +00:00
|
|
|
globs = [glob, "+auto"] if include_auto else [glob]
|
2020-09-23 04:08:21 +00:00
|
|
|
cli_args = argparser().parse_args([*default_options(output, tmp_path, pyproject_record), *globs])
|
|
|
|
save_files_main(cli_args)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
2020-07-02 09:46:10 +00:00
|
|
|
if not include_auto:
|
|
|
|
expected = remove_others(expected)
|
2020-04-15 14:44:04 +00:00
|
|
|
tested = output.read_text()
|
|
|
|
assert tested == "\n".join(expected) + "\n"
|
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def test_cli_no_pyproject_record(tmp_path, pyproject_record):
|
2020-04-15 14:44:04 +00:00
|
|
|
output = tmp_path / "files"
|
2020-09-23 04:08:21 +00:00
|
|
|
cli_args = argparser().parse_args([*default_options(output, tmp_path, pyproject_record), "tldr*"])
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
with pytest.raises(FileNotFoundError):
|
2020-09-23 04:08:21 +00:00
|
|
|
save_files_main(cli_args)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def test_cli_too_many_RECORDS(tldr_root, output, pyproject_record):
|
|
|
|
# Two calls to simulate how %pyproject_install process more than one RECORD file
|
|
|
|
prepare_pyproject_record(tldr_root,
|
|
|
|
content=("foo/bar/dist-info/RECORD", []))
|
|
|
|
prepare_pyproject_record(tldr_root,
|
|
|
|
content=("foo/baz/dist-info/RECORD", []))
|
|
|
|
cli_args = argparser().parse_args([*default_options(output, tldr_root, pyproject_record), "tldr*"])
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
with pytest.raises(FileExistsError):
|
2020-09-23 04:08:21 +00:00
|
|
|
save_files_main(cli_args)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def test_cli_bad_argument(tldr_root, output, pyproject_record):
|
2020-04-15 14:44:04 +00:00
|
|
|
cli_args = argparser().parse_args(
|
2020-09-23 04:08:21 +00:00
|
|
|
[*default_options(output, tldr_root, pyproject_record), "tldr*", "+foodir"]
|
2020-04-15 14:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2020-09-23 04:08:21 +00:00
|
|
|
save_files_main(cli_args)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def test_cli_bad_option(tldr_root, output, pyproject_record):
|
|
|
|
prepare_pyproject_record(tldr_root.parent, content=("RECORD1", []))
|
2020-04-15 14:44:04 +00:00
|
|
|
cli_args = argparser().parse_args(
|
2020-09-23 04:08:21 +00:00
|
|
|
[*default_options(output, tldr_root, pyproject_record), "tldr*", "you_cannot_have_this"]
|
2020-04-15 14:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2020-09-23 04:08:21 +00:00
|
|
|
save_files_main(cli_args)
|
2020-04-15 14:44:04 +00:00
|
|
|
|
|
|
|
|
2020-09-23 04:08:21 +00:00
|
|
|
def test_cli_bad_namespace(tldr_root, output, pyproject_record):
|
2020-04-15 14:44:04 +00:00
|
|
|
cli_args = argparser().parse_args(
|
2020-09-23 04:08:21 +00:00
|
|
|
[*default_options(output, tldr_root, pyproject_record), "tldr.didntread"]
|
2020-04-15 14:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2020-09-23 04:08:21 +00:00
|
|
|
save_files_main(cli_args)
|