aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/cros_cls_test.py
blob: 45efaf31236124187d5ee091941e85b9aa8bcac1 (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
#!/usr/bin/env python3
# Copyright 2024 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for cros_cls."""

import unittest

import cros_cls


class TestChangeListURL(unittest.TestCase):
    """ChangeListURL tests."""

    def test_parsing_long_form_url(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse(
                "chromium-review.googlesource.com/c/chromiumos/overlays/"
                "chromiumos-overlay/+/123456",
            ),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )

    def test_parsing_long_form_internal_url(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse(
                "chrome-internal-review.googlesource.com/c/chromeos/"
                "manifest-internal/+/654321"
            ),
            cros_cls.ChangeListURL(cl_id=654321, patch_set=None, internal=True),
        )

    def test_parsing_short_internal_url(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/i/654321"),
            cros_cls.ChangeListURL(cl_id=654321, patch_set=None, internal=True),
        )

    def test_parsing_discards_http(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse("http://crrev.com/c/123456"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )

    def test_parsing_discards_https(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse("https://crrev.com/c/123456"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )

    def test_parsing_detects_patch_sets(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/14"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=14),
        )

    def test_parsing_is_okay_with_trailing_slash(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/14/"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=14),
        )

    def test_parsing_is_okay_with_valid_trailing_junk(self):
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456?foo=bar"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/?foo=bar"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/14/foo=bar"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=14),
        )
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/14?foo=bar"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=14),
        )

        # While these aren't well-formed, Gerrit handles them without issue.
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456&foo=bar"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=None),
        )
        self.assertEqual(
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/14&foo=bar"),
            cros_cls.ChangeListURL(cl_id=123456, patch_set=14),
        )

    def test_parsing_raises_on_invalid_trailing_jumk(self):
        with self.assertRaises(ValueError):
            cros_cls.ChangeListURL.parse("crrev.com/c/123456foo=bar")

        with self.assertRaises(ValueError):
            cros_cls.ChangeListURL.parse("crrev.com/c/123456/14foo=bar")

    def test_str_functions_properly(self):
        self.assertEqual(
            str(
                cros_cls.ChangeListURL(
                    cl_id=1234,
                    patch_set=2,
                )
            ),
            "https://crrev.com/c/1234/2",
        )

        self.assertEqual(
            str(
                cros_cls.ChangeListURL(
                    cl_id=1234,
                    patch_set=None,
                )
            ),
            "https://crrev.com/c/1234",
        )

        self.assertEqual(
            str(
                cros_cls.ChangeListURL(
                    cl_id=1234,
                    patch_set=2,
                    internal=True,
                )
            ),
            "https://crrev.com/i/1234/2",
        )


class Test(unittest.TestCase):
    """General tests for cros_cls."""

    def test_release_builder_parsing_works(self):
        self.assertEqual(
            cros_cls.parse_release_from_builder_artifacts_link(
                "gs://chromeos-image-archive/amd64-generic-asan-cq/"
                "R122-15711.0.0-59730-8761718482083052481"
            ),
            "R122-15711.0.0",
        )
        self.assertEqual(
            cros_cls.parse_release_from_builder_artifacts_link(
                "gs://chromeos-image-archive/amd64-generic-asan-cq/"
                "R122-15711.0.0-59730-8761718482083052481/some/trailing/"
                "stuff.zip"
            ),
            "R122-15711.0.0",
        )


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