File: test_analysis_unittest.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (365 lines) | stat: -rwxr-xr-x 16,852 bytes parent folder | download | duplicates (5)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from collections import defaultdict
import csv
from io import StringIO
import os
import tempfile
from typing import List, Set
import shutil
import sys
import unittest

from file_reading import get_and_maybe_delete_tests_in_browsertest
from file_reading import read_actions_file, read_enums_file
from file_reading import read_platform_supported_actions
from file_reading import read_unprocessed_coverage_tests_file
from models import Action
from models import EnumsByType
from models import ActionsByName
from models import ActionType
from models import CoverageTest
from models import CoverageTestsByPlatform
from models import CoverageTestsByPlatformSet
from models import TestIdsTestNamesByPlatformSet
from models import TestIdTestNameTuple
from models import TestPartitionDescription
from models import TestPlatform
from test_analysis import compare_and_print_tests_to_remove_and_add
from test_analysis import expand_parameterized_tests
from test_analysis import partition_framework_tests_per_platform_combination

TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             "test_data")


def CreateDummyAction(id: str):
    return Action(id, id, id, id, ActionType.STATE_CHANGE, TestPlatform,
                  TestPlatform)


def CreateCoverageTest(id: str, platforms: Set[TestPlatform]):
    return CoverageTest([CreateDummyAction(id)], platforms)


def CreateNewDummyTestByPlatformSet(
        platforms: Set[TestPlatform]) -> CoverageTestsByPlatformSet:
    new_test_by_platform: CoverageTestsByPlatform = {}
    for platform in platforms:
        # Add the simple dummy test of an action "a" to all platforms.
        new_test_by_platform[platform] = ([
            CreateCoverageTest("a", {platform})
        ])
    return partition_framework_tests_per_platform_combination(
        new_test_by_platform)


def GetExistingTestIdsTestNamesByPlatformSet(
        filename: str, required_tests: Set[TestIdTestNameTuple],
        delete_in_place: bool) -> TestIdsTestNamesByPlatformSet:
    # Read in existing tests from a file.
    platforms = frozenset(
        TestPlatform.get_platforms_from_browsertest_filename(filename))
    existing_tests_in_file = get_and_maybe_delete_tests_in_browsertest(
        filename,
        required_tests=required_tests,
        delete_in_place=delete_in_place)
    existing_tests: TestIdsTestNamesByPlatformSet = defaultdict(lambda: set())
    for (test_id, test_name) in existing_tests_in_file.keys():
        existing_tests[platforms].add(TestIdTestNameTuple(test_id, test_name))
    return existing_tests


class TestAnalysisTest(unittest.TestCase):
    def test_partition_framework_tests_per_platform_combination(self):
        tests_by_platform: CoverageTestsByPlatform = {}
        windows_tests = []
        windows_tests.append(CreateCoverageTest("a", {TestPlatform.WINDOWS}))
        windows_tests.append(CreateCoverageTest("b", {TestPlatform.WINDOWS}))
        windows_tests.append(CreateCoverageTest("c", {TestPlatform.WINDOWS}))
        tests_by_platform[TestPlatform.WINDOWS] = windows_tests
        mac_tests = []
        mac_tests.append(CreateCoverageTest("a", {TestPlatform.MAC}))
        mac_tests.append(CreateCoverageTest("c", {TestPlatform.MAC}))
        tests_by_platform[TestPlatform.MAC] = mac_tests
        linux_tests = []
        linux_tests.append(CreateCoverageTest("a", {TestPlatform.LINUX}))
        tests_by_platform[TestPlatform.LINUX] = linux_tests

        partitions = partition_framework_tests_per_platform_combination(
            tests_by_platform)
        self.assertEqual(len(partitions), 3)

        self.assertTrue(frozenset({TestPlatform.WINDOWS}) in partitions)
        windows_tests = partitions[frozenset({TestPlatform.WINDOWS})]
        self.assertEqual(len(windows_tests), 1)
        self.assertEqual(windows_tests[0].id, "b")

        self.assertTrue(
            frozenset({TestPlatform.MAC, TestPlatform.WINDOWS}) in partitions)
        mac_win_tests = partitions[frozenset(
            {TestPlatform.MAC, TestPlatform.WINDOWS})]
        self.assertEqual(len(mac_win_tests), 1)
        self.assertEqual(mac_win_tests[0].id, "c")

        mac_win_linux_key = frozenset(
            {TestPlatform.MAC, TestPlatform.WINDOWS, TestPlatform.LINUX})
        self.assertTrue(mac_win_linux_key in partitions)
        mac_win_linux_tests = partitions[mac_win_linux_key]
        self.assertEqual(len(mac_win_linux_tests), 1)
        self.assertEqual(mac_win_linux_tests[0].id, "a")

    def test_processed_coverage(self):
        actions_filename = os.path.join(TEST_DATA_DIR, "test_actions.md")
        supported_actions_filename = os.path.join(
            TEST_DATA_DIR, "framework_supported_actions.csv")
        enums_filename = os.path.join(TEST_DATA_DIR, "test_enums.md")

        actions: ActionsByName = {}
        action_base_name_to_default_param = {}
        enums: EnumsByType = {}
        with open(actions_filename, "r", encoding="utf-8") as f, \
                open(supported_actions_filename, "r", encoding="utf-8") \
                    as supported_actions_file, \
                open(enums_filename, "r", encoding="utf-8") as enums:
            supported_actions = read_platform_supported_actions(
                csv.reader(supported_actions_file, delimiter=','))
            enums = read_enums_file(enums.readlines())
            (actions, action_base_name_to_default_param) = read_actions_file(
                f.readlines(), enums, supported_actions)

        coverage_filename = os.path.join(TEST_DATA_DIR,
                                         "test_unprocessed_coverage.md")
        coverage_tests: List[CoverageTest] = []
        with open(coverage_filename, "r", encoding="utf-8") as f:
            coverage_tests = read_unprocessed_coverage_tests_file(
                f.readlines(), actions, enums,
                action_base_name_to_default_param)
        coverage_tests = expand_parameterized_tests(coverage_tests)

        # Compare with expected
        expected_processed_tests = []
        processed_filename = os.path.join(TEST_DATA_DIR,
                                          "expected_processed_coverage.md")
        with open(processed_filename, "r", encoding="utf-8") as f:
            expected_processed_tests = read_unprocessed_coverage_tests_file(
                f.readlines(), actions, enums,
                action_base_name_to_default_param)

        # Hack for easy comparison and printing: transform coverage tests into
        # a Tuple[List[str], Set[TestPlatform]].
        self.assertCountEqual([([action.name
                                 for action in test.actions], test.platforms)
                               for test in coverage_tests],
                              [([action.name
                                 for action in test.actions], test.platforms)
                               for test in expected_processed_tests])

    def test_compare_and_print_tests_to_remove_and_add_add_to_existing_file(
            self):
        with tempfile.TemporaryDirectory(dir=TEST_DATA_DIR) as tmpdirname:
            original_file = os.path.join(TEST_DATA_DIR,
                                         "tests_for_deletion_addition.cc")
            test_file = os.path.join(tmpdirname,
                                     "tests_for_deletion_addition.cc")
            shutil.copyfile(original_file, test_file)
            test_platforms: Set[TestPlatform] = {
                TestPlatform.WINDOWS,
                TestPlatform.MAC,
                TestPlatform.LINUX,
                TestPlatform.CHROME_OS,
            }
            new_test_required_by_platform_set: CoverageTestsByPlatformSet = (
                CreateNewDummyTestByPlatformSet(test_platforms))
            existing_tests: TestIdsTestNamesByPlatformSet = (
                GetExistingTestIdsTestNamesByPlatformSet(
                    filename=test_file,
                    required_tests={
                        TestIdTestNameTuple(
                            "state_change_a_Chicken_check_a_Chicken_check_b_Chicken_Green",
                            "3Chicken_1Chicken_2ChickenGreen")
                    },
                    delete_in_place=False))
            default_partition = TestPartitionDescription(
                action_name_prefixes=set(),
                browsertest_dir=tmpdirname,
                test_file_prefix="tests_for_deletion_addition",
                test_fixture="TestName")
            compare_and_print_tests_to_remove_and_add(
                existing_tests,
                new_test_required_by_platform_set,
                test_partitions=[],
                default_partition=default_partition,
                add_to_file=True)
            expected_file = os.path.join(TEST_DATA_DIR, "expected_test_txt",
                                         "tests_change_for_adding_test.cc")
            with open(expected_file, "r") as f, open(test_file, "r") as f2:
                self.assertEqual(f.read(), f2.read())

    def test_compare_and_print_tests_with_same_name_diff_check_actions_only(
            self):
        actions_filename = os.path.join(TEST_DATA_DIR, "test_actions.md")
        supported_actions_filename = os.path.join(
            TEST_DATA_DIR, "framework_supported_actions.csv")
        enums_filename = os.path.join(TEST_DATA_DIR, "test_enums.md")

        actions: ActionsByName = {}
        action_base_name_to_default_param = {}
        with open(actions_filename) as f, \
                open(supported_actions_filename, "r", encoding="utf-8") \
                    as supported_actions, \
                open(enums_filename, "r", encoding="utf-8") as enums:
            supported_actions = read_platform_supported_actions(
                csv.reader(supported_actions, delimiter=','))
            actions_tsv = f.readlines()
            enums = read_enums_file(enums.readlines())
            (actions, action_base_name_to_default_param) = read_actions_file(
                actions_tsv, enums, supported_actions)

        coverage_filename = os.path.join(TEST_DATA_DIR,
                                         "test_addition_coverage.md")
        generated_coverage_tests: List[CoverageTest] = []
        with open(coverage_filename) as f:
            coverage_tsv = f.readlines()
            generated_coverage_tests = read_unprocessed_coverage_tests_file(
                coverage_tsv, actions, enums,
                action_base_name_to_default_param)

        test_platforms: Set[TestPlatform] = {
            TestPlatform.WINDOWS,
            TestPlatform.MAC,
            TestPlatform.LINUX,
            TestPlatform.CHROME_OS,
        }
        new_tests_by_platform: CoverageTestsByPlatform = {}
        for platform in test_platforms:
            new_tests_by_platform[platform] = [generated_coverage_tests[0]]
        new_coverage_tests_by_platform_set = (
            partition_framework_tests_per_platform_combination(
                new_tests_by_platform))

        with tempfile.TemporaryDirectory(dir=TEST_DATA_DIR) as tmpdirname:
            original_file = os.path.join(
                TEST_DATA_DIR,
                "tests_change_for_replacing_test_same_test_name.cc")
            test_file = os.path.join(
                tmpdirname,
                "tests_change_for_replacing_test_same_test_name.cc")
            shutil.copyfile(original_file, test_file)
            existing_tests: TestIdsTestNamesByPlatformSet = (
                GetExistingTestIdsTestNamesByPlatformSet(
                    filename=test_file,
                    required_tests={
                        TestIdTestNameTuple(
                            "state_change_a_Chicken_state_change_a_Dog_check_a_Dog",
                            "StateChangeAChicken_StateChangeADog")
                    },
                    delete_in_place=True))

            default_partition = TestPartitionDescription(
                action_name_prefixes=set(),
                browsertest_dir=tmpdirname,
                test_file_prefix=
                "tests_change_for_replacing_test_same_test_name",
                test_fixture="TestName")
            compare_and_print_tests_to_remove_and_add(
                existing_tests,
                new_coverage_tests_by_platform_set,
                test_partitions=[],
                default_partition=default_partition,
                add_to_file=True)

            expected_file = os.path.join(
                TEST_DATA_DIR, "expected_test_txt",
                "tests_change_for_replacing_test_same_test_name.cc")
            with open(expected_file, "r") as f, open(test_file, "r") as f2:
                self.assertEqual(f.read(), f2.read())

    def test_compare_and_print_tests_to_remove_and_add_delete_and_add_to_file(
            self):
        with tempfile.TemporaryDirectory(dir=TEST_DATA_DIR) as tmpdirname:
            original_file = os.path.join(TEST_DATA_DIR,
                                         "tests_for_deletion_addition.cc")
            test_file = os.path.join(tmpdirname,
                                     "tests_for_deletion_addition.cc")
            shutil.copyfile(original_file, test_file)

            test_platforms: Set[TestPlatform] = {
                TestPlatform.WINDOWS,
                TestPlatform.MAC,
                TestPlatform.LINUX,
                TestPlatform.CHROME_OS,
            }
            new_test_required_by_platform_set: CoverageTestsByPlatformSet = (
                CreateNewDummyTestByPlatformSet(test_platforms))
            existing_tests: TestIdsByPlatformSet = (
                GetExistingTestIdsTestNamesByPlatformSet(filename=test_file,
                                                         required_tests={},
                                                         delete_in_place=True))

            default_partition = TestPartitionDescription(
                action_name_prefixes=set(),
                browsertest_dir=tmpdirname,
                test_file_prefix="tests_for_deletion_addition",
                test_fixture="TestName")
            compare_and_print_tests_to_remove_and_add(
                existing_tests,
                new_test_required_by_platform_set,
                test_partitions=[],
                default_partition=default_partition,
                add_to_file=True)

            expected_file = os.path.join(
                TEST_DATA_DIR, "expected_test_txt",
                "tests_change_for_deleting_adding_test.cc")
            with open(expected_file, "r") as f, open(test_file, "r") as f2:
                self.assertEqual(f.read(), f2.read())

    def test_compare_and_print_tests_to_remove_and_add_add_to_new_file(self):
        with tempfile.TemporaryDirectory(dir=TEST_DATA_DIR) as tmpdirname:
            original_file = os.path.join(TEST_DATA_DIR,
                                         "tests_for_deletion_addition.cc")
            test_file = os.path.join(tmpdirname,
                                     "tests_for_deletion_addition.cc")
            shutil.copyfile(original_file, test_file)

            test_platforms: Set[TestPlatform] = {
                TestPlatform.WINDOWS, TestPlatform.MAC
            }
            new_test_required_by_platform_set: CoverageTestsByPlatformSet = (
                CreateNewDummyTestByPlatformSet(test_platforms))
            existing_tests: TestIdsByPlatformSet = (
                GetExistingTestIdsTestNamesByPlatformSet(test_file, {}, True))

            default_partition = TestPartitionDescription(
                action_name_prefixes=set(),
                browsertest_dir=tmpdirname,
                test_file_prefix="tests_for_deletion_addition",
                test_fixture="WebAppIntegration")

            captured_output = StringIO()
            sys.stdout = captured_output
            compare_and_print_tests_to_remove_and_add(
                existing_tests,
                new_test_required_by_platform_set,
                test_partitions=[],
                default_partition=default_partition,
                add_to_file=True)
            console_output_str = captured_output.getvalue()
            sys.stdout = sys.__stdout__

            expected_file = os.path.join(
                TEST_DATA_DIR, "expected_test_txt",
                "tests_change_for_deletion_addition_mac_win.txt")
            test_output_file = os.path.join(
                    tmpdirname, "tests_for_deletion_addition_mac_win.cc")
            with open(expected_file, "r") as f:
                self.assertEqual(f.read() % test_output_file,
                                 console_output_str)


if __name__ == '__main__':
    unittest.main()