Skip to content

Commit 1ccaefc

Browse files
committed
chore: add integration test for provenance extractor; add json_tools script.
Signed-off-by: Ben Selwyn-Smith <[email protected]>
1 parent 9eaccbc commit 1ccaefc

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

scripts/dev_scripts/integration_tests.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ if [[ -z "$NO_NPM_TEST" ]]; then
9898
$RUN_MACARON analyze -purl pkg:npm/@sigstore/[email protected] -rp https://github.com/sigstore/sigstore-js -b main -d ebdcfdfbdfeb9c9aeee6df53674ef230613629f5 --skip-deps || log_fail
9999

100100
check_or_update_expected_output $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
101+
102+
echo -e "\n----------------------------------------------------------------------------------"
103+
echo "[email protected]: Extracting repository URL and commit from provenance while Repo Finder is disabled."
104+
echo -e "----------------------------------------------------------------------------------\n"
105+
JSON_EXPECTED=$WORKSPACE/tests/e2e/expected_results/purl/npm/semver/semver.json
106+
JSON_RESULT=$WORKSPACE/output/reports/npm/semver/semver.json
107+
$RUN_MACARON -dp tests/e2e/defaults/disable_repo_finder.ini analyze -purl pkg:npm/[email protected] || log_fail
108+
109+
check_or_update_expected_output $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
101110
fi
102111

103112
echo -e "\n----------------------------------------------------------------------------------"

src/macaron/json_tools.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module provides utility functions for JSON data."""
5+
6+
from typing import TypeVar
7+
8+
from macaron.errors import JsonError
9+
from macaron.util import JsonType
10+
11+
T = TypeVar("T", bound=JsonType)
12+
13+
14+
def json_extract(entry: JsonType, keys: list[str], type_: type[T]) -> T:
15+
"""Return the value found by following the list of depth-sequential keys inside the passed JSON dictionary.
16+
17+
The value must be of the passed type.
18+
19+
Parameters
20+
----------
21+
entry: JsonType
22+
An entry point into a JSON structure.
23+
keys: list[str]
24+
The list of depth-sequential keys within the JSON.
25+
type: type[T]
26+
The type to check the value against and return it as.
27+
28+
Returns
29+
-------
30+
T:
31+
The found value as the type of the type parameter.
32+
33+
Raises
34+
------
35+
JsonError
36+
Raised if an error occurs while searching for or validating the value.
37+
"""
38+
target = entry
39+
40+
for index, key in enumerate(keys):
41+
if not isinstance(target, dict):
42+
raise JsonError(f"Expect the value .{'.'.join(keys[:index])} to be a dict.")
43+
if key not in target:
44+
raise JsonError(f"JSON key '{key}' not found in .{'.'.join(keys[:index])}.")
45+
target = target[key]
46+
47+
if isinstance(target, type_):
48+
return target
49+
50+
raise JsonError(f"Expect the value .{'.'.join(keys)} to be of type '{type_}'.")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
[repofinder]
5+
find_repos = False

0 commit comments

Comments
 (0)