Skip to content

Commit

Permalink
test: check that Jira labels are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Batchelder authored and nedbat committed Nov 27, 2023
1 parent 50607ef commit 3ee0182
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/fake_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dataclasses
import itertools
import re
from dataclasses import dataclass, field
from typing import Dict, Optional, Set

Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion tests/test_fake_jira.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests of FakeJira."""

import pytest
import requests


Expand All @@ -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()
Expand Down Expand Up @@ -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")

0 comments on commit 3ee0182

Please sign in to comment.