Skip to content

Commit 5494cb0

Browse files
committed
test file added in data and suggestions applied
Signed-off-by: NucleonGodX <racerpro41@gmail.com>
1 parent 11a9116 commit 5494cb0

File tree

8 files changed

+32
-46
lines changed

8 files changed

+32
-46
lines changed

scanpipe/apps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
from licensedcode.models import load_licenses
3939

40-
from scanpipe.license_policies import load_policies_file
41-
from scanpipe.license_policies import make_license_policy_index
40+
from scanpipe.policies import load_policies_file
41+
from scanpipe.policies import make_license_policy_index
4242

4343
try:
4444
from importlib import metadata as importlib_metadata

scanpipe/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
from taggit.forms import TagField
3030
from taggit.forms import TagWidget
3131

32-
from scanpipe.license_policies import load_policies_yaml
33-
from scanpipe.license_policies import validate_policies
3432
from scanpipe.models import Project
3533
from scanpipe.models import Run
3634
from scanpipe.models import WebhookSubscription
3735
from scanpipe.pipelines import convert_markdown_to_html
3836
from scanpipe.pipes import fetch
37+
from scanpipe.policies import load_policies_yaml
38+
from scanpipe.policies import validate_policies
3939

4040
scanpipe_app = apps.get_app_config("scanpipe")
4141

scanpipe/models.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

9595
import scancodeio
9696
from scanpipe import humanize_time
97-
from scanpipe import license_policies
97+
from scanpipe import policies
9898
from scanpipe import tasks
9999

100100
logger = logging.getLogger(__name__)
@@ -1508,14 +1508,12 @@ def get_policy_index(self):
15081508
if policies_from_settings := self.get_env("policies"):
15091509
policies_dict = policies_from_settings
15101510
if isinstance(policies_from_settings, str):
1511-
policies_dict = license_policies.load_policies_yaml(
1512-
policies_from_settings
1513-
)
1514-
return license_policies.make_license_policy_index(policies_dict)
1511+
policies_dict = policies.load_policies_yaml(policies_from_settings)
1512+
return policies.make_license_policy_index(policies_dict)
15151513

15161514
elif policies_file := self.get_input_policies_file():
1517-
policies_dict = license_policies.load_policies_file(policies_file)
1518-
return license_policies.make_license_policy_index(policies_dict)
1515+
policies_dict = policies.load_policies_file(policies_file)
1516+
return policies.make_license_policy_index(policies_dict)
15191517

15201518
else:
15211519
return scanpipe_app.license_policies_index

scanpipe/pipes/license_clarity.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/nexB/scancode.io for support and download.
2222

23-
# clarity_thresholds.py (updated)
2423
"""
2524
License Clarity Thresholds Management
2625
@@ -40,6 +39,8 @@
4039
50: warning # Scores 50-79 get 'warning' alert
4140
"""
4241

42+
from pathlib import Path
43+
4344
from django.core.exceptions import ValidationError
4445

4546
import saneyaml
@@ -152,8 +153,6 @@ def load_clarity_thresholds_from_file(file_path):
152153
ClarityThresholdsPolicy: Configured policy object or None if file not found
153154
154155
"""
155-
from pathlib import Path
156-
157156
file_path = Path(file_path)
158157

159158
if not file_path.exists():

scanpipe/license_policies.py renamed to scanpipe/policies.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,14 @@
2525
import saneyaml
2626

2727

28-
def load_yaml_content(yaml_content):
29-
"""Load and parse YAML content into a Python dictionary."""
28+
def load_policies_yaml(policies_yaml):
29+
"""Load provided ``policies_yaml``."""
3030
try:
31-
return saneyaml.load(yaml_content)
31+
return saneyaml.load(policies_yaml)
3232
except saneyaml.YAMLError as e:
3333
raise ValidationError(f"Policies file format error: {e}")
3434

3535

36-
def load_policies_yaml(policies_yaml):
37-
"""Load provided ``policies_yaml``."""
38-
data = load_yaml_content(policies_yaml)
39-
validate_policies(data)
40-
return data
41-
42-
4336
def load_policies_file(policies_file, validate=True):
4437
"""
4538
Load provided ``policies_file`` into a Python dictionary.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
license_clarity_thresholds:
2+
90: ok
3+
70: warning
4+
40: error

scanpipe/tests/pipes/test_license_clarity.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/nexB/scancode.io for support and download.
2222

23-
import tempfile
2423
from pathlib import Path
2524

2625
from django.core.exceptions import ValidationError
2726
from django.test import TestCase
2827

2928
from scanpipe.pipes.license_clarity import ClarityThresholdsPolicy
30-
from scanpipe.pipes.license_clarity import load_clarity_thresholds_from_yaml
3129
from scanpipe.pipes.license_clarity import load_clarity_thresholds_from_file
30+
from scanpipe.pipes.license_clarity import load_clarity_thresholds_from_yaml
31+
3232

3333
class ClarityThresholdsPolicyTest(TestCase):
34+
data = Path(__file__).parent.parent / "data"
3435
"""Test ClarityThresholdsPolicy class functionality."""
3536

3637
def test_valid_thresholds_initialization(self):
@@ -149,22 +150,13 @@ def test_yaml_string_invalid_yaml(self):
149150
load_clarity_thresholds_from_yaml(yaml_content)
150151

151152
def test_load_from_existing_file(self):
152-
yaml_content = """
153-
license_clarity_thresholds:
154-
90: ok
155-
70: warning
156-
"""
157-
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f:
158-
f.write(yaml_content)
159-
temp_path = f.name
160-
161-
try:
162-
policy = load_clarity_thresholds_from_file(temp_path)
163-
self.assertIsNotNone(policy)
164-
self.assertEqual(policy.get_alert_for_score(95), "ok")
165-
finally:
166-
Path(temp_path).unlink()
153+
test_file = self.data / "license_clarity" / "sample_thresholds.yml"
154+
policy = load_clarity_thresholds_from_file(test_file)
155+
self.assertIsNotNone(policy)
156+
self.assertEqual(policy.get_alert_for_score(95), "ok")
157+
self.assertEqual(policy.get_alert_for_score(75), "warning")
158+
self.assertEqual(policy.get_alert_for_score(50), "error")
167159

168160
def test_load_from_nonexistent_file(self):
169161
policy = load_clarity_thresholds_from_file("/nonexistent/file.yml")
170-
self.assertIsNone(policy)
162+
self.assertIsNone(policy)

scanpipe/tests/test_license_policies.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
from django.core.exceptions import ValidationError
2828
from django.test import TestCase
2929

30-
from scanpipe.license_policies import load_policies_file
31-
from scanpipe.license_policies import load_policies_yaml
32-
from scanpipe.license_policies import make_license_policy_index
33-
from scanpipe.license_policies import validate_policies
3430
from scanpipe.pipes.input import copy_input
31+
from scanpipe.policies import load_policies_file
32+
from scanpipe.policies import load_policies_yaml
33+
from scanpipe.policies import make_license_policy_index
34+
from scanpipe.policies import validate_policies
3535
from scanpipe.tests import global_policies
3636
from scanpipe.tests import license_policies_index
3737
from scanpipe.tests import make_project

0 commit comments

Comments
 (0)