Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add lockfile to PackageDescriptor #282

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/phylum/ci/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import dataclasses
from enum import IntEnum
import json
from typing import List
from typing import List, Optional


@dataclasses.dataclass(order=True, frozen=True)
Expand All @@ -12,6 +12,7 @@ class PackageDescriptor:
name: str
version: str
type: str # noqa: A003 ; shadowing built-in `type` is okay since renaming here would be more confusing
lockfile: Optional[str] = None


# Type alias
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/test_lockfile_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Test the lockfile current_lockfile_packages function."""

from pathlib import Path
from unittest.mock import patch

from phylum.ci.common import PackageDescriptor
from phylum.ci.lockfile import Lockfile

EXPECTED_NUM_PACKAGES = 2


@patch("subprocess.run")
def test_current_lockfile_packages(mock_run):
"""Test the `current_lockfile_packages` function of the Lockfile class."""
# Prepare the mock
mock_run.return_value.stdout = """
[
{
"name": "quote",
"version": "1.0.21",
"type": "cargo",
"lockfile": "Cargo.lock"
},
{
"name": "example",
"version": "0.1.0",
"type": "npm"
}
]
"""

lockfile_path = Path("Cargo.lock")
cli_path = Path("dummy_cli_path")
lockfile = Lockfile(lockfile_path, cli_path, None)

# Test the current_lockfile_packages method
packages = lockfile.current_lockfile_packages()
expected_cargo_package = PackageDescriptor("quote", "1.0.21", "cargo", "Cargo.lock")
expected_npm_package = PackageDescriptor("example", "0.1.0", "npm")

assert len(packages) == EXPECTED_NUM_PACKAGES
assert expected_cargo_package in packages
assert expected_npm_package in packages

# Ensure the mock was called correctly
mock_run.assert_called_once_with(
[str(lockfile.cli_path), "parse", str(lockfile.path)],
check=True,
capture_output=True,
text=True,
)