From 03a8a6c4c6529542ef455cebc83c22d4f7f93caf Mon Sep 17 00:00:00 2001 From: ejortega <24722023+ejortega@users.noreply.github.com> Date: Fri, 28 Jul 2023 16:00:05 -0500 Subject: [PATCH] test: add test for updated package descriptor --- tests/unit/test_parse.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/unit/test_parse.py diff --git a/tests/unit/test_parse.py b/tests/unit/test_parse.py new file mode 100644 index 00000000..cf9b2350 --- /dev/null +++ b/tests/unit/test_parse.py @@ -0,0 +1,44 @@ +"""Test the lockfile current_lockfile_packages function.""" + +import json +from unittest.mock import patch +from phylum.ci.common import PackageDescriptor +from phylum.ci.lockfile import Lockfile +from pathlib import Path + +import pytest + +@patch("subprocess.run") +def test_current_lockfile_packages(mock_run): + # Prepare the mock + mock_run.return_value.stdout = json.dumps([ + { + "name": "quote", + "version": "1.0.21", + "type": "cargo", + "lockfile": "Cargo.lock" + }, + { + "name": "example", + "version": "0.1.0", + "type": "npm", + } + ]) + + lockfile = Lockfile(Path("Cargo.lock"), Path("dummy_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) == 2 + assert packages[0] == expected_cargo_package + assert packages[1] == expected_npm_package + + # 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 + )