|
|
7ef706 |
import pytest
|
|
|
7ef706 |
import yaml
|
|
|
7ef706 |
|
|
|
7ef706 |
from pathlib import Path
|
|
|
7ef706 |
from pprint import pprint
|
|
|
7ef706 |
|
|
|
7ef706 |
from pyproject_preprocess_record import parse_record, read_record, save_parsed_record
|
|
|
7ef706 |
|
|
|
7ef706 |
from pyproject_save_files import argparser, generate_file_list, BuildrootPath
|
|
|
7ef706 |
from pyproject_save_files import main as save_files_main
|
|
|
7ef706 |
from pyproject_save_files import module_names_from_path
|
|
|
7ef706 |
|
|
|
7ef706 |
DIR = Path(__file__).parent
|
|
|
7ef706 |
PREFIX = Path("/usr")
|
|
|
7ef706 |
BINDIR = BuildrootPath("/usr/bin")
|
|
|
7ef706 |
DATADIR = BuildrootPath("/usr/share")
|
|
|
7ef706 |
SITELIB = BuildrootPath("/usr/lib/python3.7/site-packages")
|
|
|
7ef706 |
SITEARCH = BuildrootPath("/usr/lib64/python3.7/site-packages")
|
|
|
7ef706 |
|
|
|
7ef706 |
yaml_file = DIR / "pyproject_save_files_test_data.yaml"
|
|
|
7ef706 |
yaml_data = yaml.safe_load(yaml_file.read_text())
|
|
|
7ef706 |
EXPECTED_DICT = yaml_data["classified"]
|
|
|
7ef706 |
EXPECTED_FILES = yaml_data["dumped"]
|
|
|
7ef706 |
TEST_RECORDS = yaml_data["records"]
|
|
|
7ef706 |
TEST_METADATAS = yaml_data["metadata"]
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.fixture
|
|
|
7ef706 |
def tldr_root(tmp_path):
|
|
|
7ef706 |
prepare_pyproject_record(tmp_path, package="tldr")
|
|
|
7ef706 |
return tmp_path
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.fixture
|
|
|
7ef706 |
def pyproject_record(tmp_path):
|
|
|
7ef706 |
return tmp_path / "pyproject-record"
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def prepare_pyproject_record(tmp_path, package=None, content=None):
|
|
|
7ef706 |
"""
|
|
|
7ef706 |
Creates RECORD from test data and then uses
|
|
|
7ef706 |
functions from pyproject_process_record to convert
|
|
|
7ef706 |
it to pyproject-record file which is then
|
|
|
7ef706 |
further processed by functions from pyproject_save_files.
|
|
|
7ef706 |
"""
|
|
|
7ef706 |
record_file = tmp_path / "RECORD"
|
|
|
7ef706 |
pyproject_record = tmp_path / "pyproject-record"
|
|
|
7ef706 |
|
|
|
7ef706 |
if package is not None:
|
|
|
7ef706 |
# Get test data and write dist-info/RECORD file
|
|
|
7ef706 |
record_path = BuildrootPath(TEST_RECORDS[package]["path"])
|
|
|
7ef706 |
record_file.write_text(TEST_RECORDS[package]["content"])
|
|
|
7ef706 |
if package in TEST_METADATAS:
|
|
|
7ef706 |
metadata_path = BuildrootPath(TEST_METADATAS[package]["path"]).to_real(tmp_path)
|
|
|
7ef706 |
metadata_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
7ef706 |
metadata_path.write_text(TEST_METADATAS[package]["content"])
|
|
|
7ef706 |
# Parse RECORD file
|
|
|
7ef706 |
parsed_record = parse_record(record_path, read_record(record_file))
|
|
|
7ef706 |
# Save JSON content to pyproject-record
|
|
|
7ef706 |
save_parsed_record(record_path, parsed_record, pyproject_record)
|
|
|
7ef706 |
elif content is not None:
|
|
|
7ef706 |
save_parsed_record(*content, output_file=pyproject_record)
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.fixture
|
|
|
7ef706 |
def output_files(tmp_path):
|
|
|
7ef706 |
return tmp_path / "pyproject_files"
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.fixture
|
|
|
7ef706 |
def output_modules(tmp_path):
|
|
|
7ef706 |
return tmp_path / "pyproject_modules"
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_parse_record_tldr():
|
|
|
7ef706 |
record_path = BuildrootPath(TEST_RECORDS["tldr"]["path"])
|
|
|
7ef706 |
record_content = read_record(DIR / "test_RECORD")
|
|
|
7ef706 |
output = list(parse_record(record_path, record_content))
|
|
|
7ef706 |
pprint(output)
|
|
|
7ef706 |
expected = [
|
|
|
7ef706 |
str(BINDIR / "__pycache__/tldr.cpython-37.pyc"),
|
|
|
7ef706 |
str(BINDIR / "tldr"),
|
|
|
7ef706 |
str(BINDIR / "tldr.py"),
|
|
|
7ef706 |
str(SITELIB / "__pycache__/tldr.cpython-37.pyc"),
|
|
|
7ef706 |
str(SITELIB / "tldr-0.5.dist-info/INSTALLER"),
|
|
|
7ef706 |
str(SITELIB / "tldr-0.5.dist-info/LICENSE"),
|
|
|
7ef706 |
str(SITELIB / "tldr-0.5.dist-info/METADATA"),
|
|
|
7ef706 |
str(SITELIB / "tldr-0.5.dist-info/RECORD"),
|
|
|
7ef706 |
str(SITELIB / "tldr-0.5.dist-info/WHEEL"),
|
|
|
7ef706 |
str(SITELIB / "tldr-0.5.dist-info/top_level.txt"),
|
|
|
7ef706 |
str(SITELIB / "tldr.py"),
|
|
|
7ef706 |
]
|
|
|
7ef706 |
assert output == expected
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_parse_record_tensorflow():
|
|
|
7ef706 |
long = "tensorflow_core/include/tensorflow/core/common_runtime/base_collective_executor.h"
|
|
|
7ef706 |
record_path = SITEARCH / "tensorflow-2.1.0.dist-info/RECORD"
|
|
|
7ef706 |
record_content = [
|
|
|
7ef706 |
["../../../bin/toco_from_protos", "sha256=hello", "289"],
|
|
|
7ef706 |
[f"../../../lib/python3.7/site-packages/{long}", "sha256=darkness", "1024"],
|
|
|
7ef706 |
["tensorflow-2.1.0.dist-info/METADATA", "sha256=friend", "2859"],
|
|
|
7ef706 |
]
|
|
|
7ef706 |
output = list(parse_record(record_path, record_content))
|
|
|
7ef706 |
pprint(output)
|
|
|
7ef706 |
expected = [
|
|
|
7ef706 |
str(BINDIR / "toco_from_protos"),
|
|
|
7ef706 |
str(SITELIB / long),
|
|
|
7ef706 |
str(SITEARCH / "tensorflow-2.1.0.dist-info/METADATA"),
|
|
|
7ef706 |
]
|
|
|
7ef706 |
assert output == expected
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def remove_others(expected):
|
|
|
7ef706 |
return [
|
|
|
7ef706 |
p for p in expected
|
|
|
7ef706 |
if not (
|
|
|
7ef706 |
p.startswith(str(BINDIR)) or
|
|
|
7ef706 |
p.endswith(".pth") or
|
|
|
7ef706 |
p.endswith("*") or
|
|
|
7ef706 |
p.rpartition(' ')[-1].startswith(str(DATADIR))
|
|
|
7ef706 |
)
|
|
|
7ef706 |
]
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.mark.parametrize("include_auto", (True, False))
|
|
|
7ef706 |
@pytest.mark.parametrize("package, glob, expected_files, expected_modules", EXPECTED_FILES)
|
|
|
7ef706 |
def test_generate_file_list(package, glob, expected_files, include_auto, expected_modules):
|
|
|
7ef706 |
paths_dict = EXPECTED_DICT[package]
|
|
|
7ef706 |
modules_glob = {glob}
|
|
|
7ef706 |
if not include_auto:
|
|
|
7ef706 |
expected_files = remove_others(expected_files)
|
|
|
7ef706 |
tested = generate_file_list(paths_dict, modules_glob, include_auto)
|
|
|
7ef706 |
|
|
|
7ef706 |
assert tested == expected_files
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_generate_file_list_unused_glob():
|
|
|
7ef706 |
paths_dict = EXPECTED_DICT["kerberos"]
|
|
|
7ef706 |
modules_glob = {"kerberos", "unused_glob1", "unused_glob2", "kerb*"}
|
|
|
7ef706 |
with pytest.raises(ValueError) as excinfo:
|
|
|
7ef706 |
generate_file_list(paths_dict, modules_glob, True)
|
|
|
7ef706 |
|
|
|
7ef706 |
assert "unused_glob1, unused_glob2" in str(excinfo.value)
|
|
|
7ef706 |
assert "kerb" not in str(excinfo.value)
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.mark.parametrize(
|
|
|
7ef706 |
"path, expected",
|
|
|
7ef706 |
[
|
|
|
7ef706 |
("foo/bar/baz.py", {"foo", "foo.bar", "foo.bar.baz"}),
|
|
|
7ef706 |
("foo/bar.py", {"foo", "foo.bar"}),
|
|
|
7ef706 |
("foo.py", {"foo"}),
|
|
|
7ef706 |
("foo/bar.so.2", set()),
|
|
|
7ef706 |
("foo.cpython-37m-x86_64-linux-gnu.so", {"foo"}),
|
|
|
7ef706 |
("foo/_api/v2/__init__.py", set()),
|
|
|
7ef706 |
("foo/__init__.py", {"foo"}),
|
|
|
7ef706 |
("foo/_priv.py", set()),
|
|
|
7ef706 |
("foo/_bar/lib.so", set()),
|
|
|
7ef706 |
("foo/bar/baz.so", {"foo", "foo.bar", "foo.bar.baz"}),
|
|
|
7ef706 |
("foo/bar/baz.pth", set()),
|
|
|
7ef706 |
("foo/bar/baz.pyc", set()),
|
|
|
7ef706 |
("def.py", set()),
|
|
|
7ef706 |
("foo-bar/baz.py", set()),
|
|
|
7ef706 |
("foobar/12baz.py", set()),
|
|
|
7ef706 |
("foo/\nbar/baz.py", set()),
|
|
|
7ef706 |
("foo/+bar/baz.py", set()),
|
|
|
7ef706 |
("foo/__init__.cpython-39-x86_64-linux-gnu.so", {"foo"}),
|
|
|
7ef706 |
("foo/bar/__pycache__/abc.cpython-37.pyc", set()),
|
|
|
7ef706 |
],
|
|
|
7ef706 |
)
|
|
|
7ef706 |
def test_module_names_from_path(path, expected):
|
|
|
7ef706 |
tested = Path(path)
|
|
|
7ef706 |
assert module_names_from_path(tested) == expected
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def default_options(output_files, output_modules, mock_root, pyproject_record):
|
|
|
7ef706 |
return [
|
|
|
7ef706 |
"--output-files",
|
|
|
7ef706 |
str(output_files),
|
|
|
7ef706 |
"--output-modules",
|
|
|
7ef706 |
str(output_modules),
|
|
|
7ef706 |
"--buildroot",
|
|
|
7ef706 |
str(mock_root),
|
|
|
7ef706 |
"--sitelib",
|
|
|
7ef706 |
str(SITELIB),
|
|
|
7ef706 |
"--sitearch",
|
|
|
7ef706 |
str(SITEARCH),
|
|
|
7ef706 |
"--python-version",
|
|
|
7ef706 |
"3.7", # test data are for 3.7,
|
|
|
7ef706 |
"--pyproject-record",
|
|
|
7ef706 |
str(pyproject_record),
|
|
|
7ef706 |
"--prefix",
|
|
|
7ef706 |
str(PREFIX),
|
|
|
7ef706 |
]
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
@pytest.mark.parametrize("include_auto", (True, False))
|
|
|
7ef706 |
@pytest.mark.parametrize("package, glob, expected_files, expected_modules", EXPECTED_FILES)
|
|
|
7ef706 |
def test_cli(tmp_path, package, glob, expected_files, expected_modules, include_auto, pyproject_record):
|
|
|
7ef706 |
prepare_pyproject_record(tmp_path, package)
|
|
|
7ef706 |
output_files = tmp_path / "files"
|
|
|
7ef706 |
output_modules = tmp_path / "modules"
|
|
|
7ef706 |
globs = [glob, "+auto"] if include_auto else [glob]
|
|
|
7ef706 |
cli_args = argparser().parse_args([*default_options(output_files, output_modules, tmp_path, pyproject_record), *globs])
|
|
|
7ef706 |
save_files_main(cli_args)
|
|
|
7ef706 |
|
|
|
7ef706 |
if not include_auto:
|
|
|
7ef706 |
expected_files = remove_others(expected_files)
|
|
|
7ef706 |
tested_files = output_files.read_text()
|
|
|
7ef706 |
assert tested_files == "\n".join(expected_files) + "\n"
|
|
|
7ef706 |
|
|
|
7ef706 |
tested_modules = output_modules.read_text().split()
|
|
|
7ef706 |
|
|
|
7ef706 |
assert tested_modules == expected_modules
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_cli_no_pyproject_record(tmp_path, pyproject_record):
|
|
|
7ef706 |
output_files = tmp_path / "files"
|
|
|
7ef706 |
output_modules = tmp_path / "modules"
|
|
|
7ef706 |
cli_args = argparser().parse_args([*default_options(output_files, output_modules, tmp_path, pyproject_record), "tldr*"])
|
|
|
7ef706 |
|
|
|
7ef706 |
with pytest.raises(FileNotFoundError):
|
|
|
7ef706 |
save_files_main(cli_args)
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_cli_too_many_RECORDS(tldr_root, output_files, output_modules, pyproject_record):
|
|
|
7ef706 |
# Two calls to simulate how %pyproject_install process more than one RECORD file
|
|
|
7ef706 |
prepare_pyproject_record(tldr_root,
|
|
|
7ef706 |
content=("foo/bar/dist-info/RECORD", []))
|
|
|
7ef706 |
prepare_pyproject_record(tldr_root,
|
|
|
7ef706 |
content=("foo/baz/dist-info/RECORD", []))
|
|
|
7ef706 |
cli_args = argparser().parse_args([*default_options(output_files, output_modules, tldr_root, pyproject_record), "tldr*"])
|
|
|
7ef706 |
|
|
|
7ef706 |
with pytest.raises(FileExistsError):
|
|
|
7ef706 |
save_files_main(cli_args)
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_cli_bad_argument(tldr_root, output_files, output_modules, pyproject_record):
|
|
|
7ef706 |
cli_args = argparser().parse_args(
|
|
|
7ef706 |
[*default_options(output_files, output_modules, tldr_root, pyproject_record), "tldr*", "+foodir"]
|
|
|
7ef706 |
)
|
|
|
7ef706 |
|
|
|
7ef706 |
with pytest.raises(ValueError):
|
|
|
7ef706 |
save_files_main(cli_args)
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_cli_bad_option(tldr_root, output_files, output_modules, pyproject_record):
|
|
|
7ef706 |
prepare_pyproject_record(tldr_root.parent, content=("RECORD1", []))
|
|
|
7ef706 |
cli_args = argparser().parse_args(
|
|
|
7ef706 |
[*default_options(output_files, output_modules, tldr_root, pyproject_record), "tldr*", "you_cannot_have_this"]
|
|
|
7ef706 |
)
|
|
|
7ef706 |
|
|
|
7ef706 |
with pytest.raises(ValueError):
|
|
|
7ef706 |
save_files_main(cli_args)
|
|
|
7ef706 |
|
|
|
7ef706 |
|
|
|
7ef706 |
def test_cli_bad_namespace(tldr_root, output_files, output_modules, pyproject_record):
|
|
|
7ef706 |
cli_args = argparser().parse_args(
|
|
|
7ef706 |
[*default_options(output_files, output_modules, tldr_root, pyproject_record), "tldr.didntread"]
|
|
|
7ef706 |
)
|
|
|
7ef706 |
|
|
|
7ef706 |
with pytest.raises(ValueError):
|
|
|
7ef706 |
save_files_main(cli_args)
|