ara-cli 0.1.9.74__py3-none-any.whl → 0.1.9.75__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ara-cli might be problematic. Click here for more details.

@@ -1,7 +1,253 @@
1
1
  import pytest
2
2
  from unittest.mock import patch, MagicMock, mock_open, call
3
- from ara_cli.artefact_scan import check_file, find_invalid_files, show_results
4
- from pydantic import ValidationError
3
+ from ara_cli.artefact_scan import (
4
+ check_file,
5
+ find_invalid_files,
6
+ show_results,
7
+ is_contribution_valid,
8
+ is_rule_valid,
9
+ check_contribution,
10
+ )
11
+
12
+
13
+ @pytest.mark.parametrize("contribution", [None, False, 0, "", []])
14
+ def test_check_contribution_none_contribution(contribution):
15
+ # Should return (True, None) if contribution is falsey
16
+ result = check_contribution(
17
+ contribution, classified_artefact_info={}, file_path="irrelevant"
18
+ )
19
+ assert result == (True, None)
20
+
21
+
22
+ def test_check_contribution_invalid_contribution(monkeypatch):
23
+ # If is_contribution_valid returns False, should return (False, custom_reason)
24
+ contribution = MagicMock()
25
+ contribution.classifier = "some_cls"
26
+ contribution.artefact_name = "some_art"
27
+ with patch(
28
+ "ara_cli.artefact_scan.is_contribution_valid", return_value=False
29
+ ) as mock_is_contrib, patch("ara_cli.artefact_scan.is_rule_valid") as mock_is_rule:
30
+ result = check_contribution(
31
+ contribution, classified_artefact_info={}, file_path="f"
32
+ )
33
+ assert result == (
34
+ False,
35
+ "Invalid Contribution Reference: The contribution references "
36
+ "'some_cls' artefact 'some_art' which does not exist.",
37
+ )
38
+ mock_is_contrib.assert_called_once_with(contribution, {})
39
+ mock_is_rule.assert_not_called()
40
+
41
+
42
+ def test_check_contribution_invalid_rule(monkeypatch):
43
+ # is_contribution_valid returns True, is_rule_valid returns False
44
+ contribution = MagicMock()
45
+ contribution.classifier = "c"
46
+ contribution.artefact_name = "a"
47
+ contribution.rule = "r"
48
+ with patch(
49
+ "ara_cli.artefact_scan.is_contribution_valid", return_value=True
50
+ ) as mock_is_contrib, patch(
51
+ "ara_cli.artefact_scan.is_rule_valid", return_value=False
52
+ ) as mock_is_rule:
53
+ result = check_contribution(
54
+ contribution, classified_artefact_info={"x": 1}, file_path="myfile"
55
+ )
56
+ assert result == (
57
+ False,
58
+ "Rule Mismatch: The contribution references rule 'r' which the parent c 'a' does not have.",
59
+ )
60
+ mock_is_contrib.assert_called_once_with(contribution, {"x": 1})
61
+ mock_is_rule.assert_called_once_with(contribution, {"x": 1})
62
+
63
+
64
+ def test_check_contribution_all_valid(monkeypatch):
65
+ # Both is_contribution_valid and is_rule_valid return True
66
+ contribution = MagicMock()
67
+ contribution.classifier = "c"
68
+ contribution.artefact_name = "a"
69
+ contribution.rule = "r"
70
+ with patch(
71
+ "ara_cli.artefact_scan.is_contribution_valid", return_value=True
72
+ ) as mock_is_contrib, patch(
73
+ "ara_cli.artefact_scan.is_rule_valid", return_value=True
74
+ ) as mock_is_rule:
75
+ result = check_contribution(
76
+ contribution, classified_artefact_info={1: 2}, file_path="p"
77
+ )
78
+ assert result == (True, None)
79
+ mock_is_contrib.assert_called_once_with(contribution, {1: 2})
80
+ mock_is_rule.assert_called_once_with(contribution, {1: 2})
81
+
82
+
83
+ @pytest.mark.parametrize(
84
+ "contribution_attrs,expected",
85
+ [
86
+ (None, True), # contribution is None
87
+ ({"artefact_name": None, "classifier": "foo"}, True), # artefact_name is None
88
+ (
89
+ {"artefact_name": "", "classifier": "foo"},
90
+ True,
91
+ ), # artefact_name is empty string
92
+ ({"artefact_name": "bar", "classifier": None}, True), # classifier is None
93
+ (
94
+ {"artefact_name": "bar", "classifier": ""},
95
+ True,
96
+ ), # classifier is empty string
97
+ ],
98
+ )
99
+ def test_is_rule_valid_short_circuits(contribution_attrs, expected):
100
+ if contribution_attrs is None:
101
+ contribution = None
102
+ else:
103
+ contribution = MagicMock()
104
+ for k, v in contribution_attrs.items():
105
+ setattr(contribution, k, v)
106
+ with patch("ara_cli.artefact_reader.ArtefactReader.read_artefact") as mock_read:
107
+ result = is_rule_valid(contribution, classified_artefact_info={})
108
+ assert result is expected
109
+ mock_read.assert_not_called()
110
+
111
+
112
+ def test_is_rule_valid_rule_is_none():
113
+ """Should return True if contribution.rule is None or falsey."""
114
+ contribution = MagicMock()
115
+ contribution.artefact_name = "foo"
116
+ contribution.classifier = "bar"
117
+ contribution.rule = None
118
+ with patch("ara_cli.artefact_reader.ArtefactReader.read_artefact") as mock_read:
119
+ result = is_rule_valid(contribution, classified_artefact_info={})
120
+ assert result is True
121
+ mock_read.assert_not_called()
122
+
123
+ # Also test rule as empty string
124
+ contribution.rule = ""
125
+ with patch("ara_cli.artefact_reader.ArtefactReader.read_artefact") as mock_read:
126
+ result = is_rule_valid(contribution, classified_artefact_info={})
127
+ assert result is True
128
+ mock_read.assert_not_called()
129
+
130
+
131
+ @pytest.mark.parametrize(
132
+ "parent,expected",
133
+ [
134
+ (None, True), # parent is None
135
+ (MagicMock(rules=None), True), # parent.rules is None
136
+ ],
137
+ )
138
+ def test_is_rule_valid_parent_or_rules_none(parent, expected):
139
+ contribution = MagicMock()
140
+ contribution.artefact_name = "foo"
141
+ contribution.classifier = "bar"
142
+ contribution.rule = "r1"
143
+ with patch(
144
+ "ara_cli.artefact_reader.ArtefactReader.read_artefact", return_value=parent
145
+ ) as mock_read:
146
+ result = is_rule_valid(contribution, classified_artefact_info={})
147
+ assert result is expected
148
+ mock_read.assert_called_once_with("foo", "bar")
149
+
150
+
151
+ def test_is_rule_valid_rule_not_in_parent_rules():
152
+ """Should return False if rule is not in parent.rules."""
153
+ contribution = MagicMock()
154
+ contribution.artefact_name = "foo"
155
+ contribution.classifier = "bar"
156
+ contribution.rule = "missing_rule"
157
+ parent = MagicMock()
158
+ parent.rules = ["rule1", "rule2"]
159
+ with patch(
160
+ "ara_cli.artefact_reader.ArtefactReader.read_artefact", return_value=parent
161
+ ) as mock_read:
162
+ result = is_rule_valid(contribution, classified_artefact_info={})
163
+ assert result is False
164
+ mock_read.assert_called_once_with("foo", "bar")
165
+
166
+
167
+ def test_is_rule_valid_rule_in_parent_rules():
168
+ """Should return True if rule is in parent.rules."""
169
+ contribution = MagicMock()
170
+ contribution.artefact_name = "foo"
171
+ contribution.classifier = "bar"
172
+ contribution.rule = "my_rule"
173
+ parent = MagicMock()
174
+ parent.rules = ["my_rule", "other_rule"]
175
+ with patch(
176
+ "ara_cli.artefact_reader.ArtefactReader.read_artefact", return_value=parent
177
+ ) as mock_read:
178
+ result = is_rule_valid(contribution, classified_artefact_info={})
179
+ assert result is True
180
+ mock_read.assert_called_once_with("foo", "bar")
181
+
182
+
183
+ @pytest.mark.parametrize(
184
+ "contribution_attrs,expected",
185
+ [
186
+ # contribution is None
187
+ (None, True),
188
+ # artefact_name missing/None/empty
189
+ ({"artefact_name": None, "classifier": "foo"}, True),
190
+ ({"artefact_name": "", "classifier": "foo"}, True),
191
+ # classifier missing/None/empty
192
+ ({"artefact_name": "bar", "classifier": None}, True),
193
+ ({"artefact_name": "bar", "classifier": ""}, True),
194
+ ],
195
+ )
196
+ def test_is_contribution_valid_short_circuits(contribution_attrs, expected):
197
+ classified_artefact_info = {"dummy": []}
198
+ if contribution_attrs is None:
199
+ contribution = None
200
+ else:
201
+ contribution = MagicMock()
202
+ for k, v in contribution_attrs.items():
203
+ setattr(contribution, k, v)
204
+ with patch(
205
+ "ara_cli.artefact_fuzzy_search.extract_artefact_names_of_classifier"
206
+ ) as mock_extract:
207
+ result = is_contribution_valid(contribution, classified_artefact_info)
208
+ assert result is expected
209
+ # The extract function should NOT be called in these cases
210
+ mock_extract.assert_not_called()
211
+
212
+
213
+ def test_is_contribution_valid_references_existing_artefact():
214
+ """
215
+ contribution with valid artefact_name and classifier,
216
+ artefact_name IS in list returned by extract_artefact_names_of_classifier.
217
+ """
218
+ contribution = MagicMock()
219
+ contribution.artefact_name = "valid_art"
220
+ contribution.classifier = "valid_clf"
221
+ classified_artefact_info = {"valid_clf": [{"name": "valid_art"}]}
222
+ with patch(
223
+ "ara_cli.artefact_fuzzy_search.extract_artefact_names_of_classifier",
224
+ return_value=["valid_art"],
225
+ ) as mock_extract:
226
+ result = is_contribution_valid(contribution, classified_artefact_info)
227
+ assert result is True
228
+ mock_extract.assert_called_once_with(
229
+ classified_files=classified_artefact_info, classifier="valid_clf"
230
+ )
231
+
232
+
233
+ def test_is_contribution_valid_references_missing_artefact():
234
+ """
235
+ contribution with valid artefact_name and classifier,
236
+ artefact_name is NOT in list returned by extract_artefact_names_of_classifier.
237
+ """
238
+ contribution = MagicMock()
239
+ contribution.artefact_name = "missing_art"
240
+ contribution.classifier = "some_clf"
241
+ classified_artefact_info = {"some_clf": [{"name": "another"}]}
242
+ with patch(
243
+ "ara_cli.artefact_fuzzy_search.extract_artefact_names_of_classifier",
244
+ return_value=["another", "something_else"],
245
+ ) as mock_extract:
246
+ result = is_contribution_valid(contribution, classified_artefact_info)
247
+ assert result is False
248
+ mock_extract.assert_called_once_with(
249
+ classified_files=classified_artefact_info, classifier="some_clf"
250
+ )
5
251
 
6
252
 
7
253
  def test_check_file_valid():
@@ -16,8 +262,10 @@ def test_check_file_valid():
16
262
 
17
263
  with patch("builtins.open", mock_open(read_data="valid content")):
18
264
  is_valid, reason = check_file("dummy_path.feature", mock_artefact_class)
19
-
20
- assert reason is None, f"Reason for invalid found, expected none to be found. The reason found: {reason}"
265
+
266
+ assert (
267
+ reason is None
268
+ ), f"Reason for invalid found, expected none to be found. The reason found: {reason}"
21
269
  assert is_valid is True, "File detected as invalid, expected to be valid"
22
270
 
23
271
 
@@ -51,8 +299,7 @@ def test_check_file_value_error():
51
299
 
52
300
  def test_check_file_assertion_error():
53
301
  mock_artefact_class = MagicMock()
54
- mock_artefact_class.deserialize.side_effect = AssertionError(
55
- "Assertion error")
302
+ mock_artefact_class.deserialize.side_effect = AssertionError("Assertion error")
56
303
 
57
304
  with patch("builtins.open", mock_open(read_data="invalid content")):
58
305
  is_valid, reason = check_file("dummy_path", mock_artefact_class)
@@ -78,6 +325,7 @@ def test_check_file_unexpected_error():
78
325
  assert is_valid is False
79
326
  assert "Unexpected error: Exception('Unexpected error')" in reason
80
327
 
328
+
81
329
  # Tests for find_invalid_files
82
330
 
83
331
 
@@ -86,32 +334,37 @@ def test_find_invalid_files():
86
334
  mock_artefact_class = MagicMock()
87
335
  classified_info = {
88
336
  "test_classifier": [
89
- {"file_path": "file1.txt"}, # Should be checked
90
- {"file_path": "file2.txt"}, # Should be checked
91
- {"file_path": "templates/file3.txt"}, # Should be skipped
92
- {"file_path": "some/path/file.data"} # Should be skipped
337
+ {"file_path": "file1.txt"}, # Should be checked
338
+ {"file_path": "file2.txt"}, # Should be checked
339
+ {"file_path": "templates/file3.txt"}, # Should be skipped
340
+ {"file_path": "some/path/file.data"}, # Should be skipped
93
341
  ]
94
342
  }
95
-
96
- with patch("ara_cli.artefact_models.artefact_mapping.artefact_type_mapping",
97
- {"test_classifier": mock_artefact_class}):
343
+
344
+ with patch(
345
+ "ara_cli.artefact_models.artefact_mapping.artefact_type_mapping",
346
+ {"test_classifier": mock_artefact_class},
347
+ ):
98
348
  with patch("ara_cli.artefact_scan.check_file") as mock_check_file:
99
349
  mock_check_file.side_effect = [
100
- (True, None), # for file1.txt
101
- (False, "Invalid content") # for file2.txt
350
+ (True, None), # for file1.txt
351
+ (False, "Invalid content"), # for file2.txt
102
352
  ]
103
353
 
104
354
  invalid_files = find_invalid_files(classified_info, "test_classifier")
105
-
355
+
106
356
  assert len(invalid_files) == 1
107
357
  assert invalid_files[0] == ("file2.txt", "Invalid content")
108
358
  assert mock_check_file.call_count == 2
109
-
359
+
110
360
  # Check that check_file was called with the correct parameters
111
- mock_check_file.assert_has_calls([
112
- call("file1.txt", mock_artefact_class, classified_info),
113
- call("file2.txt", mock_artefact_class, classified_info)
114
- ], any_order=False)
361
+ mock_check_file.assert_has_calls(
362
+ [
363
+ call("file1.txt", mock_artefact_class, classified_info),
364
+ call("file2.txt", mock_artefact_class, classified_info),
365
+ ],
366
+ any_order=False,
367
+ )
115
368
 
116
369
 
117
370
  def test_show_results_no_issues(capsys):
@@ -120,18 +373,20 @@ def test_show_results_no_issues(capsys):
120
373
  show_results(invalid_artefacts)
121
374
  captured = capsys.readouterr()
122
375
  assert captured.out == "All files are good!\n"
123
- m.assert_called_once_with("incompatible_artefacts_report.md", "w", encoding="utf-8")
376
+ m.assert_called_once_with(
377
+ "incompatible_artefacts_report.md", "w", encoding="utf-8"
378
+ )
124
379
  handle = m()
125
- handle.write.assert_has_calls([
126
- call("# Artefact Check Report\n\n"),
127
- call("No problems found.\n")
128
- ], any_order=False)
380
+ handle.write.assert_has_calls(
381
+ [call("# Artefact Check Report\n\n"), call("No problems found.\n")],
382
+ any_order=False,
383
+ )
129
384
 
130
385
 
131
386
  def test_show_results_with_issues(capsys):
132
387
  invalid_artefacts = {
133
388
  "classifier1": [("file1.txt", "reason1"), ("file2.txt", "reason2")],
134
- "classifier2": [("file3.txt", "reason3")]
389
+ "classifier2": [("file3.txt", "reason3")],
135
390
  }
136
391
  with patch("builtins.open", mock_open()) as m:
137
392
  show_results(invalid_artefacts)
@@ -147,7 +402,9 @@ def test_show_results_with_issues(capsys):
147
402
  "\t\treason3\n"
148
403
  )
149
404
  assert captured.out == expected_output
150
- m.assert_called_once_with("incompatible_artefacts_report.md", "w", encoding="utf-8")
405
+ m.assert_called_once_with(
406
+ "incompatible_artefacts_report.md", "w", encoding="utf-8"
407
+ )
151
408
  handle = m()
152
409
  expected_writes = [
153
410
  call("# Artefact Check Report\n\n"),
@@ -157,7 +414,7 @@ def test_show_results_with_issues(capsys):
157
414
  call("\n"),
158
415
  call("## classifier2\n"),
159
416
  call("- `file3.txt`: reason3\n"),
160
- call("\n")
417
+ call("\n"),
161
418
  ]
162
419
  handle.write.assert_has_calls(expected_writes, any_order=False)
163
420
 
@@ -166,7 +423,7 @@ def test_check_file_with_invalid_contribution():
166
423
  """Tests file with invalid contribution reference."""
167
424
  mock_artefact_instance = MagicMock()
168
425
  mock_artefact_instance.title = "dummy_path"
169
-
426
+
170
427
  # Set up invalid contribution
171
428
  mock_contribution = MagicMock()
172
429
  mock_contribution.classifier = "test_classifier"
@@ -178,12 +435,16 @@ def test_check_file_with_invalid_contribution():
178
435
 
179
436
  # Mock classified_artefact_info
180
437
  classified_info = {"test_classifier": [{"name": "existing_artefact"}]}
181
-
438
+
182
439
  # Mock extract_artefact_names_of_classifier to return a list without the referenced artefact
183
440
  with patch("builtins.open", mock_open(read_data="valid content")):
184
- with patch("ara_cli.artefact_fuzzy_search.extract_artefact_names_of_classifier",
185
- return_value=["existing_artefact"]):
186
- is_valid, reason = check_file("dummy_path.feature", mock_artefact_class, classified_info)
187
-
441
+ with patch(
442
+ "ara_cli.artefact_fuzzy_search.extract_artefact_names_of_classifier",
443
+ return_value=["existing_artefact"],
444
+ ):
445
+ is_valid, reason = check_file(
446
+ "dummy_path.feature", mock_artefact_class, classified_info
447
+ )
448
+
188
449
  assert is_valid is False
189
450
  assert "Invalid Contribution Reference" in reason
tests/test_chat.py CHANGED
@@ -13,8 +13,8 @@ from ara_cli.ara_config import ConfigManager
13
13
  def get_default_config():
14
14
  return SimpleNamespace(
15
15
  ext_code_dirs=[
16
- {"source_dir_1": "./src"},
17
- {"source_dir_2": "./tests"},
16
+ {"source_dir": "./src"},
17
+ {"source_dir": "./tests"},
18
18
  ],
19
19
  glossary_dir="./glossary",
20
20
  doc_dir="./docs",