summaryrefslogtreecommitdiff
path: root/buildscripts/tests/test_todo_check.py
blob: b158c3b18fbb4959b09ba0031522052ea3def7f7 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Unit tests for todo_check.py."""

from __future__ import absolute_import

import os
import textwrap
import unittest
from tempfile import TemporaryDirectory

from typing import Iterable

import buildscripts.todo_check as under_test

# pylint: disable=missing-docstring,invalid-name,unused-argument,no-self-use,protected-access


def create_file_iterator(file_contents: str) -> Iterable[str]:
    return textwrap.dedent(file_contents.strip()).splitlines()


class TestTodo(unittest.TestCase):
    def test_line_without_a_jira_issues(self):
        content = "a line with some random text"
        todo = under_test.Todo.from_line("file", 42, f"\n{content}\n\t\n")

        self.assertEqual(todo.file_name, "file")
        self.assertEqual(todo.line_number, 42)
        self.assertEqual(todo.line, content)
        self.assertIsNone(todo.ticket)

    def test_line_with_a_server_ticket(self):
        ticket = "SERVER-12345"
        content = f"a line with {ticket} some random text"
        todo = under_test.Todo.from_line("file", 42, f"\n{content}\n\t\n")

        self.assertEqual(todo.file_name, "file")
        self.assertEqual(todo.line_number, 42)
        self.assertEqual(todo.line, content)
        self.assertEqual(todo.ticket, ticket)

    def test_line_with_a_wiredtiger_ticket(self):
        ticket = "WT-5555"
        content = f"a line with {ticket} some random text"
        todo = under_test.Todo.from_line("file", 42, f"\n{content}\n\t\n")

        self.assertEqual(todo.file_name, "file")
        self.assertEqual(todo.line_number, 42)
        self.assertEqual(todo.line, content)
        self.assertEqual(todo.ticket, ticket)


class TestTodoChecker(unittest.TestCase):
    def test_todo_checker_starts_out_empty(self):
        todos = under_test.TodoChecker()

        self.assertEqual(len(todos.found_todos.no_tickets), 0)
        self.assertEqual(len(todos.found_todos.with_tickets), 0)
        self.assertEqual(len(todos.found_todos.by_file), 0)

    def test_a_file_with_no_todos(self):
        file_contents = """
        line 0
        line 1
        this is the file contents.
        
        """
        todos = under_test.TodoChecker()

        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertEqual(len(todos.found_todos.no_tickets), 0)
        self.assertEqual(len(todos.found_todos.with_tickets), 0)
        self.assertEqual(len(todos.found_todos.by_file), 0)

    def test_a_file_with_an_untagged_todo(self):
        file_contents = """
        line 0
        line 1
        /* todo this needs some updating */
        this is the file contents.
        """
        todos = under_test.TodoChecker()

        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertEqual(len(todos.found_todos.no_tickets), 1)
        self.assertEqual(len(todos.found_todos.with_tickets), 0)
        self.assertEqual(len(todos.found_todos.by_file), 1)

        todo = todos.found_todos.no_tickets[0]
        self.assertEqual(todo.file_name, "my file")
        self.assertEqual(todo.line_number, 3)
        self.assertEqual(todo.ticket, None)

        self.assertEqual(todo, todos.found_todos.by_file["my file"][0])

    def test_a_file_with_a_tagged_todo(self):
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        """
        todos = under_test.TodoChecker()

        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertEqual(len(todos.found_todos.no_tickets), 0)
        self.assertEqual(len(todos.found_todos.with_tickets), 1)
        self.assertEqual(len(todos.found_todos.by_file), 1)

        todo = todos.found_todos.with_tickets["SERVER-1234"][0]
        self.assertEqual(todo.file_name, "my file")
        self.assertEqual(todo.line_number, 4)
        self.assertEqual(todo.ticket, "SERVER-1234")

        self.assertEqual(todo, todos.found_todos.by_file["my file"][0])

    def test_report_on_ticket_will_return_true_if_ticket_is_found(self):
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertTrue(todos.report_on_ticket("SERVER-1234"))

    def test_report_on_ticket_will_return_false_if_ticket_is_not_found(self):
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertFalse(todos.report_on_ticket("SERVER-9876"))

    def test_report_all_tickets_will_return_true_if_any_ticket_is_found(self):
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        /* TODO server-54321 this also needs some updating */
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertTrue(todos.report_on_all_tickets())

    def test_report_all_tickets_will_return_false_if_no_ticket_is_found(self):
        file_contents = """
        line 0
        line 1
        line 2
        this is the file contents.
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertFalse(todos.report_on_all_tickets())


class TestValidateCommitQueue(unittest.TestCase):
    def test_revert_commits_should_not_fail(self):
        commit_message = "Reverts commit SERVER-1234"
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        /* TODO server-54321 this also needs some updating */
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertFalse(todos.validate_commit_queue(commit_message))

    def test_todos_associated_with_commit_message_should_be_found(self):
        commit_message = "SERVER-1234 making a commit"
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        /* TODO server-54321 this also needs some updating */
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertTrue(todos.validate_commit_queue(commit_message))

    def test_commit_messages_with_multiple_commits_search_all_of_them(self):
        commit_message = """
        Making a wiredtiger drop
        
        WT-1234
        WT-4321
        WT-9876
        """
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        /* TODO WT-9876 this also needs some updating */
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertTrue(todos.validate_commit_queue(commit_message))

    def test_commit_messages_with_no_tickets_doesnt_cause_issues(self):
        commit_message = "A random commit"
        file_contents = """
        line 0
        line 1
        line 2
        /* TODO server-1234 this needs some updating */
        this is the file contents.
        /* TODO WT-9876 this also needs some updating */
        """
        todos = under_test.TodoChecker()
        todos.check_file("my file", create_file_iterator(file_contents))

        self.assertFalse(todos.validate_commit_queue(commit_message))


def write_file(path: str, contents: str):
    with open(path, "w") as fh:
        fh.write(contents)


class TestWalkFs(unittest.TestCase):
    def test_walk_fs_walks_the_fs(self):
        expected_files = {
            "file1.txt": "The contents of file 1",
            "file2.txt": "The contents of file 2",
        }
        with TemporaryDirectory() as tmpdir:
            write_file(os.path.join(tmpdir, "file1.txt"), expected_files["file1.txt"])
            os.makedirs(os.path.join(tmpdir, "dir0", "dir1"))
            write_file(
                os.path.join(tmpdir, "dir0", "dir1", "file2.txt"), expected_files["file2.txt"])

            seen_files = {}

            def visit_file(file_name, file_contents):
                base_name = os.path.basename(file_name)
                seen_files[base_name] = "\n".join(file_contents)

            under_test.walk_fs(tmpdir, visit_file)

            self.assertDictEqual(expected_files, seen_files)