-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_pr_best_practices.py
63 lines (55 loc) · 2.31 KB
/
test_pr_best_practices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import unittest
from unittest.mock import patch
import io
import sys
import os
import requests
# Import the functions from the script
from pr_best_practices import (
check_pr_title_contains_jira,
check_commits_contain_jira,
check_pr_description_not_empty,
add_best_practice_label
)
class TestPRChecks(unittest.TestCase):
def test_check_pr_title_contains_jira(self):
"""
Test whether the function correctly identifies Jira ticket references in PR titles.
"""
self.assertTrue(check_pr_title_contains_jira("PR-123: Fix some issues"))
self.assertFalse(check_pr_title_contains_jira("Fix some issues"))
def test_check_pr_title_contains_jira_empty(self):
"""
Test whether the function correctly handles an empty PR title.
"""
self.assertFalse(check_pr_title_contains_jira(""))
@patch('os.popen')
def test_check_commits_contain_jira(self, mock_popen):
"""
Test whether the function correctly identifies commits lacking Jira ticket references.
"""
mock_popen.return_value.read.return_value = "PR-123: Fix some issues\nTest commit\n"
with patch('sys.stdout', new=io.StringIO()) as fake_stdout:
check_commits_contain_jira()
output = fake_stdout.getvalue().strip()
self.assertEqual(output, "Commit message 'Test commit' should contain a Jira.")
def test_check_pr_description_not_empty(self):
"""
Test whether the function correctly identifies an empty PR description.
"""
self.assertTrue(check_pr_description_not_empty("This is a PR description"))
self.assertFalse(check_pr_description_not_empty(""))
@patch('requests.post')
def test_add_best_practice_label(self, mock_post):
"""
Test whether the function adds the 'best-practice' label to a PR successfully.
"""
mock_response = requests.Response()
mock_response.status_code = 200
mock_post.return_value = mock_response
with patch('sys.stdout', new=io.StringIO()) as fake_stdout:
add_best_practice_label("mock_token", "mock_repo", 123)
output = fake_stdout.getvalue().strip()
self.assertEqual(output, "Label 'best-practice' added to PR successfully.")
if __name__ == '__main__':
unittest.main()