github.com/anchore/syft@v1.38.2/.github/scripts/labeler_test.py (about) 1 #!/usr/bin/env python3 2 3 import unittest 4 from unittest.mock import patch 5 import subprocess 6 7 import labeler 8 9 class Labeler(unittest.TestCase): 10 11 def test_major_version(self): 12 self.assertEqual(labeler.major_version("1.2.3"), 1) 13 self.assertEqual(labeler.major_version("2.0.0"), 2) 14 15 def test_is_breaking_change(self): 16 new_schema_files = ["schema/json/schema-2.0.0.json"] 17 latest_schema_file = "schema/json/schema-1.2.0.json" 18 self.assertTrue(labeler.is_breaking_change(new_schema_files, latest_schema_file)) 19 20 new_schema_files = ["schema/json/schema-1.3.0.json"] 21 latest_schema_file = "schema/json/schema-1.2.0.json" 22 self.assertFalse(labeler.is_breaking_change(new_schema_files, latest_schema_file)) 23 24 def test_summarize_schema_files(self): 25 files = ["schema/json/schema-1.0.0.json", "schema/json/schema-2.0.0.json"] 26 expected = ["1.0.0", "2.0.0"] 27 self.assertEqual(labeler.summarize_schema_files(files), expected) 28 29 def test_is_ci(self): 30 # Mock os.environ to simulate CI environment 31 with patch.dict("os.environ", {"CI": "true"}): 32 self.assertTrue(labeler.is_ci()) 33 34 def test_get_pr_changed_files(self): 35 expected_command = "gh pr view 123 --json files --jq '.files.[].path'" 36 expected_output = "file1.json\nfile2.json\n" 37 38 subprocess.CompletedProcess.returncode = 0 39 subprocess.CompletedProcess.stdout = expected_output 40 with patch("labeler.run", return_value=subprocess.CompletedProcess) as mock_run: 41 result = labeler.get_pr_changed_files("123") 42 mock_run.assert_called_with(expected_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) 43 self.assertEqual(result, ["file1.json", "file2.json"]) 44 45 def test_filter_to_schema_files(self): 46 input_files = ["schema/json/schema-1.0.0.json", "not_schema.txt", "schema/json/schema-2.0.0.json"] 47 expected_files = ["schema/json/schema-1.0.0.json", "schema/json/schema-2.0.0.json"] 48 self.assertEqual(labeler.filter_to_schema_files(input_files), expected_files) 49 50 # we should be strict about what files are allowed to be processed 51 input_files = ["schema/json/schema-1.0.0extracontent.json", "schema/json/schema-1.0.0.md", "schema/json/schema-1.0.0.json.extracontent"] 52 expected_files = [] 53 self.assertEqual(labeler.filter_to_schema_files(input_files), expected_files) 54 55 def test_get_semver(self): 56 input_file = "schema/json/schema-1.0.0.json" 57 expected_semver = "1.0.0" 58 self.assertEqual(labeler.get_semver(input_file), expected_semver) 59 60 def test_sort_json_schema_files(self): 61 files = ["schema/json/schema-1.12.1.json", "schema/json/schema-1.2.1.json"] 62 expected_sorted_files = ["schema/json/schema-1.2.1.json", "schema/json/schema-1.12.1.json"] 63 self.assertEqual(labeler.sort_json_schema_files(files), expected_sorted_files) 64 65 # ensure that "latest" doesn't cause a problem and is ultimately ignored 66 files = ["schema/json/schema-1.12.1.json", "schema/json/schema-_bogus.json"] 67 expected_sorted_files = ["schema/json/schema-_bogus.json", "schema/json/schema-1.12.1.json"] 68 self.assertEqual(labeler.sort_json_schema_files(files), expected_sorted_files) 69 70 71 if __name__ == "__main__": 72 unittest.main()