Skip to content

Commit

Permalink
Integrate will remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaMaul committed Jun 25, 2024
1 parent 9b16045 commit e5686a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
12 changes: 10 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ name = "pypi_attestation_models"

[tool.coverage.run]
# don't attempt code coverage for the CLI entrypoints
omit = ["src/pypi_attestation_models/_cli.py"]
omit = [
"src/pypi_attestation_models/_cli.py",
"src/pypi_attestation_models/__main__.py"
]

[tool.mypy]
mypy_path = "src"
Expand Down Expand Up @@ -93,6 +96,11 @@ ignore = ["ANN101", "ANN102", "D203", "D213", "COM812", "ISC001"]
[tool.interrogate]
# don't enforce documentation coverage for packaging, testing, the virtual
# environment, or the CLI (which is documented separately).
exclude = ["env", "test", "src/pypi_attestation_models/_cli.py"]
exclude = [
"env",
"test",
"src/pypi_attestation_models/_cli.py",
"src/pypi_attestation_models/__main__.py"
]
ignore-semiprivate = true
fail-under = 100
17 changes: 8 additions & 9 deletions src/pypi_attestation_models/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ def _die(message: str) -> NoReturn:
raise SystemExit(1)


def _validate_files(files: list[Path] | Iterable[Path], should_exists: bool = True) -> None:
def _validate_files(files: Iterable[Path], should_exist: bool = True) -> None:
"""Validate that the list of files exists or not.
This function exits the program if the condition is not met.
"""
for file_path in files:
if file_path.is_file() != should_exists:
if should_exists:
if file_path.is_file() != should_exist:
if should_exist:
_die(f"{file_path} is not a file.")
else:
_die(f"{file_path} already exists.")
Expand All @@ -151,7 +151,6 @@ def get_identity_token(args: argparse.Namespace) -> IdentityToken:
"""Generate an Identity Token.
This method uses the following order of precedence:
- A token passed as an argument
- An ambient credential
- An OAuth-2 flow
"""
Expand All @@ -175,10 +174,10 @@ def _sign(args: argparse.Namespace) -> None:
signing_ctx = SigningContext.staging() if args.staging else SigningContext.production()

# Validates that every file we want to sign exist but none of their attestations
_validate_files(args.files, should_exists=True)
_validate_files(args.files, should_exist=True)
_validate_files(
(Path(f"{file_path}.publish.attestation") for file_path in args.files),
should_exists=False,
should_exist=False,
)

with signing_ctx.signer(identity, cache=True) as signer:
Expand All @@ -198,7 +197,7 @@ def _inspect(args: argparse.Namespace) -> None:
Warning: The information displayed from the attestations are not verified.
"""
_validate_files(args.files, should_exists=True)
_validate_files(args.files, should_exist=True)
for file_path in args.files:
try:
attestation = Attestation.model_validate_json(file_path.read_text())
Expand Down Expand Up @@ -251,10 +250,10 @@ def _verify(args: argparse.Namespace) -> None:
pol = policy.Identity(identity=args.identity)

# Validate that both the attestations and files exists
_validate_files(args.files, should_exists=True)
_validate_files(args.files, should_exist=True)
_validate_files(
(Path(f"{file_path}.publish.attestation") for file_path in args.files),
should_exists=True,
should_exist=True,
)

for file_path in args.files:
Expand Down
20 changes: 13 additions & 7 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def test_sign_command(tmp_path: Path) -> None:
copied_artifact_attestation = Path(f"{copied_artifact}.publish.attestation")
assert copied_artifact_attestation.is_file()

attestation = Attestation.model_validate_json(copied_artifact_attestation.read_text())
attestation = Attestation.model_validate_json(
copied_artifact_attestation.read_text()
)
assert attestation.version


Expand Down Expand Up @@ -145,7 +147,9 @@ def return_invalid_token() -> str:
assert "Failed to detect identity" in caplog.text


def test_inspect_command(caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
def test_inspect_command(
caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
# Happy path
run_main_with_command(["inspect", attestation_path.as_posix()])
assert attestation_path.as_posix() in caplog.text
Expand Down Expand Up @@ -175,7 +179,9 @@ def test_inspect_command(caplog: pytest.LogCaptureFixture, monkeypatch: pytest.M
assert "not_a_file.txt is not a file." in caplog.text


def test_verify_command(caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
def test_verify_command(
caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
# Happy path
run_main_with_command(
[
Expand Down Expand Up @@ -264,22 +270,22 @@ def test_validate_files(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> Non
file_2_exist = tmp_path / "file2"
file_2_exist.touch()

_validate_files([file_1_exist, file_2_exist], should_exists=True)
_validate_files([file_1_exist, file_2_exist], should_exist=True)
assert True # No exception raised

file_1_missing = tmp_path / "file3"
file_2_missing = tmp_path / "file4"
_validate_files([file_1_missing, file_2_missing], should_exists=False)
_validate_files([file_1_missing, file_2_missing], should_exist=False)
assert True

# Failure paths
with pytest.raises(SystemExit):
_validate_files([file_1_missing, file_2_exist], should_exists=True)
_validate_files([file_1_missing, file_2_exist], should_exist=True)

assert f"{file_1_missing} is not a file." in caplog.text

caplog.clear()
with pytest.raises(SystemExit):
_validate_files([file_1_missing, file_2_exist], should_exists=False)
_validate_files([file_1_missing, file_2_exist], should_exist=False)

assert f"{file_2_exist} already exists." in caplog.text

0 comments on commit e5686a1

Please sign in to comment.