|
| 1 | +import json |
| 2 | +from importlib.util import module_from_spec, spec_from_file_location |
| 3 | +from pathlib import Path |
| 4 | +from typing import Tuple |
| 5 | +from unittest import TestCase, mock |
| 6 | + |
| 7 | + |
| 8 | +REPO_ROOT = Path.cwd() |
| 9 | +VALIDATOR_PATH = REPO_ROOT / ".github/scripts/validate_pr.py" |
| 10 | +POLICY_PATH = REPO_ROOT / ".github/policy/pr_title.json" |
| 11 | + |
| 12 | + |
| 13 | +def load_validator(): |
| 14 | + spec = spec_from_file_location("validate_pr", str(VALIDATOR_PATH)) |
| 15 | + assert spec and spec.loader |
| 16 | + mod = module_from_spec(spec) |
| 17 | + spec.loader.exec_module(mod) # type: ignore[attr-defined] |
| 18 | + return mod |
| 19 | + |
| 20 | + |
| 21 | +class TestValidateTitle(TestCase): |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls) -> None: |
| 24 | + assert VALIDATOR_PATH.exists(), "Validator script not found" |
| 25 | + assert POLICY_PATH.exists(), "Policy file not found" |
| 26 | + cls.validator = load_validator() |
| 27 | + with open(POLICY_PATH, "r", encoding="utf-8") as f: |
| 28 | + cls.policy = json.load(f) |
| 29 | + |
| 30 | + def test_valid_task_ru(self) -> None: |
| 31 | + title = ( |
| 32 | + "[TASK] 2-12. Иванов Иван Иванович. 2341-а234. OMP. Вычисление суммы элементов вектора." |
| 33 | + ) |
| 34 | + errs = self.validator.validate_title(title) |
| 35 | + self.assertEqual(errs, []) |
| 36 | + |
| 37 | + def test_valid_task_en(self) -> None: |
| 38 | + title = ( |
| 39 | + "[TASK] 3-4. Ivanov Ivan Ivanovich. 2341-a234. MPI. Vector elements sum calculation." |
| 40 | + ) |
| 41 | + errs = self.validator.validate_title(title) |
| 42 | + self.assertEqual(errs, []) |
| 43 | + |
| 44 | + def test_invalid_task_number_out_of_range(self) -> None: |
| 45 | + title = ( |
| 46 | + "[TASK] 6-1. Иванов Иван Иванович. 2341-а234. SEQ. Вычисление суммы элементов вектора." |
| 47 | + ) |
| 48 | + errs = self.validator.validate_title(title) |
| 49 | + self.assertTrue(errs, "Expected errors for out-of-range task number") |
| 50 | + |
| 51 | + def test_valid_dev_when_allowed(self) -> None: |
| 52 | + title = "[DEV] Update docs for lab 2" |
| 53 | + errs = self.validator.validate_title(title) |
| 54 | + self.assertEqual(errs, []) |
| 55 | + |
| 56 | + def test_dev_disallowed_by_policy(self) -> None: |
| 57 | + cfg = dict(self.policy) |
| 58 | + cfg["allow_dev"] = False |
| 59 | + |
| 60 | + def fake_load_title_config() -> Tuple[dict, list]: |
| 61 | + return cfg, [str(POLICY_PATH)] |
| 62 | + |
| 63 | + with mock.patch.object(self.validator, "_load_title_config", fake_load_title_config): |
| 64 | + errs = self.validator.validate_title("[DEV] Working WIP") |
| 65 | + self.assertTrue(errs, "Expected errors when allow_dev is False") |
| 66 | + |
| 67 | + def test_missing_policy_file(self) -> None: |
| 68 | + def fake_load_title_config_missing(): |
| 69 | + return None, [str(POLICY_PATH)] |
| 70 | + |
| 71 | + with mock.patch.object(self.validator, "_load_title_config", fake_load_title_config_missing): |
| 72 | + errs = self.validator.validate_title("[TASK] 2-1. X Y Z. G. OMP. Title.") |
| 73 | + self.assertTrue(errs, "Expected error for missing policy config") |
| 74 | + self.assertIn("policy config not found", " ".join(errs).lower()) |
| 75 | + |
| 76 | + def test_missing_technology_block(self) -> None: |
| 77 | + title = ( |
| 78 | + "[TASK] 2-12. Иванов Иван Иванович. 2341-а234. Вычисление суммы элементов вектора." |
| 79 | + ) |
| 80 | + errs = self.validator.validate_title(title) |
| 81 | + self.assertTrue(errs, "Expected error when technology block is missing") |
| 82 | + |
| 83 | + def test_invalid_technology_token(self) -> None: |
| 84 | + title = ( |
| 85 | + "[TASK] 2-12. Иванов Иван Иванович. 2341-а234. CUDA. Вычисление суммы элементов вектора." |
| 86 | + ) |
| 87 | + errs = self.validator.validate_title(title) |
| 88 | + self.assertTrue(errs, "Expected error for unsupported technology token") |
| 89 | + |
0 commit comments