From 3ee01825c80d0af076afde05fde994e7ba4c37c4 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 27 Nov 2023 11:41:29 -0500 Subject: [PATCH] test: check that Jira labels are valid --- tests/fake_jira.py | 10 ++++++++++ tests/test_fake_jira.py | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/fake_jira.py b/tests/fake_jira.py index efd555ef..c0e99170 100644 --- a/tests/fake_jira.py +++ b/tests/fake_jira.py @@ -2,6 +2,7 @@ import dataclasses import itertools +import re from dataclasses import dataclass, field from typing import Dict, Optional, Set @@ -26,6 +27,15 @@ class Issue: summary: Optional[str] = None labels: Set[str] = field(default_factory=set) + def __post_init__(self) -> None: + # Jira labels can't have spaces in them. Check that they are only + # letters, numbers, dashes. + for label in self.labels: + if re.search(r"[^a-zA-Z0-9-]", label): + raise ValueError(f"Label {label!r} has invalid characters") + if len(label) < 3: + raise ValueError(f"Label {label!r} is too short") + def as_json(self) -> Dict: return { "key": self.key, diff --git a/tests/test_fake_jira.py b/tests/test_fake_jira.py index eec14c0a..e79f0344 100644 --- a/tests/test_fake_jira.py +++ b/tests/test_fake_jira.py @@ -1,5 +1,6 @@ """Tests of FakeJira.""" +import pytest import requests @@ -10,7 +11,7 @@ class TestIssues: Tests of the correct behavior of issuees. """ def test_get_issue(self, fake_jira): - fake_jira.make_issue(key="HELLO-123", summary="This is a bad bug!") + fake_jira.make_issue(key="HELLO-123", summary="This is a bad bug!", labels=["bad-bug"]) resp = requests.get("https://test.atlassian.net/rest/api/2/issue/HELLO-123") assert resp.status_code == 200 issue = resp.json() @@ -74,3 +75,9 @@ class TestBadRequests: def test_no_such_put(self, fake_jira): resp = requests.put("https://test.atlassian.net/rest/api/2/issue/XYZ-999") assert resp.status_code == 404 + + def test_bad_label(self, fake_jira): + with pytest.raises(ValueError, match="Label 'a bug' has invalid characters"): + fake_jira.make_issue(key="HELLO-123", summary="a bug!", labels=["a bug"]) + with pytest.raises(ValueError, match="Label 'a' is too short"): + fake_jira.make_issue(key="HELLO-123", summary="a bug!", labels="a bug")