xref: /aosp_15_r20/external/pigweed/pw_module/py/seed_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2023 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker"""Tests for pw_module.seed."""
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Workerfrom pathlib import Path
17*61c4878aSAndroid Build Coastguard Workerimport tempfile
18*61c4878aSAndroid Build Coastguard Workerimport unittest
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Workerfrom pw_module import seed
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker_SAMPLE_REGISTRY_FILE = '''
24*61c4878aSAndroid Build Coastguard Workerimport("//build_overrides/pigweed.gni")
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard Workerimport("seed.gni")
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Workerpw_seed("0001") {
29*61c4878aSAndroid Build Coastguard Worker  sources = [ "0001.rst" ]
30*61c4878aSAndroid Build Coastguard Worker  inputs = [ "0001/seed-index-gerrit.png" ]
31*61c4878aSAndroid Build Coastguard Worker  title = "The SEED Process"
32*61c4878aSAndroid Build Coastguard Worker  status = "Meta"
33*61c4878aSAndroid Build Coastguard Worker  author = "The Pigweed Authors"
34*61c4878aSAndroid Build Coastguard Worker  facilitator = "N/A"
35*61c4878aSAndroid Build Coastguard Worker}
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard Workerpw_seed("0002") {
38*61c4878aSAndroid Build Coastguard Worker  sources = [ "0002.rst" ]
39*61c4878aSAndroid Build Coastguard Worker  title = "SEED Template"
40*61c4878aSAndroid Build Coastguard Worker  status = "Meta"
41*61c4878aSAndroid Build Coastguard Worker  author = "The Pigweed Authors"
42*61c4878aSAndroid Build Coastguard Worker  facilitator = "N/A"
43*61c4878aSAndroid Build Coastguard Worker}
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Workerpw_seed_index("seeds") {
46*61c4878aSAndroid Build Coastguard Worker  index_file = "0000.rst"
47*61c4878aSAndroid Build Coastguard Worker  seeds = [
48*61c4878aSAndroid Build Coastguard Worker    ":0001",
49*61c4878aSAndroid Build Coastguard Worker    ":0002",
50*61c4878aSAndroid Build Coastguard Worker  ]
51*61c4878aSAndroid Build Coastguard Worker}
52*61c4878aSAndroid Build Coastguard Worker'''
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard Worker_SAMPLE_REGISTRY_FILE_WITH_ADDED_SEED = '''
56*61c4878aSAndroid Build Coastguard Workerimport("//build_overrides/pigweed.gni")
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Workerimport("seed.gni")
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Workerpw_seed("0001") {
61*61c4878aSAndroid Build Coastguard Worker  sources = [ "0001.rst" ]
62*61c4878aSAndroid Build Coastguard Worker  inputs = [ "0001/seed-index-gerrit.png" ]
63*61c4878aSAndroid Build Coastguard Worker  title = "The SEED Process"
64*61c4878aSAndroid Build Coastguard Worker  status = "Meta"
65*61c4878aSAndroid Build Coastguard Worker  author = "The Pigweed Authors"
66*61c4878aSAndroid Build Coastguard Worker  facilitator = "N/A"
67*61c4878aSAndroid Build Coastguard Worker}
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Workerpw_seed("0002") {
70*61c4878aSAndroid Build Coastguard Worker  sources = [ "0002.rst" ]
71*61c4878aSAndroid Build Coastguard Worker  title = "SEED Template"
72*61c4878aSAndroid Build Coastguard Worker  status = "Meta"
73*61c4878aSAndroid Build Coastguard Worker  author = "The Pigweed Authors"
74*61c4878aSAndroid Build Coastguard Worker  facilitator = "N/A"
75*61c4878aSAndroid Build Coastguard Worker}
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Workerpw_seed("0200") {
78*61c4878aSAndroid Build Coastguard Worker  title = "a title"
79*61c4878aSAndroid Build Coastguard Worker  author = "an author"
80*61c4878aSAndroid Build Coastguard Worker  status = "Draft"
81*61c4878aSAndroid Build Coastguard Worker  changelist = 111111
82*61c4878aSAndroid Build Coastguard Worker}
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Workerpw_seed_index("seeds") {
85*61c4878aSAndroid Build Coastguard Worker  index_file = "0000.rst"
86*61c4878aSAndroid Build Coastguard Worker  seeds = [
87*61c4878aSAndroid Build Coastguard Worker    ":0001",
88*61c4878aSAndroid Build Coastguard Worker    ":0002",
89*61c4878aSAndroid Build Coastguard Worker    ":0200",
90*61c4878aSAndroid Build Coastguard Worker  ]
91*61c4878aSAndroid Build Coastguard Worker}
92*61c4878aSAndroid Build Coastguard Worker'''
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Workerclass TestSeedMetadata(unittest.TestCase):
96*61c4878aSAndroid Build Coastguard Worker    """Tests for SeedMetadata."""
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Worker    def test_default_filename_basic(self):
99*61c4878aSAndroid Build Coastguard Worker        meta = seed.SeedMetadata(
100*61c4878aSAndroid Build Coastguard Worker            number=789,
101*61c4878aSAndroid Build Coastguard Worker            title='Simple Title 2',
102*61c4878aSAndroid Build Coastguard Worker            authors='',
103*61c4878aSAndroid Build Coastguard Worker            status=seed.SeedStatus.DRAFT,
104*61c4878aSAndroid Build Coastguard Worker        )
105*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(meta.default_filename(), '0789.rst')
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker    def test_default_filename_special_characters(self):
108*61c4878aSAndroid Build Coastguard Worker        meta = seed.SeedMetadata(
109*61c4878aSAndroid Build Coastguard Worker            number=9876,
110*61c4878aSAndroid Build Coastguard Worker            title="pw_some_module: Pigweed's newest module",
111*61c4878aSAndroid Build Coastguard Worker            authors='',
112*61c4878aSAndroid Build Coastguard Worker            status=seed.SeedStatus.DRAFT,
113*61c4878aSAndroid Build Coastguard Worker        )
114*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(
115*61c4878aSAndroid Build Coastguard Worker            meta.default_filename(),
116*61c4878aSAndroid Build Coastguard Worker            '9876.rst',
117*61c4878aSAndroid Build Coastguard Worker        )
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker
120*61c4878aSAndroid Build Coastguard Workerclass TestSeedRegistry(unittest.TestCase):
121*61c4878aSAndroid Build Coastguard Worker    """Tests for SEED registry modifications."""
122*61c4878aSAndroid Build Coastguard Worker
123*61c4878aSAndroid Build Coastguard Worker    def setUp(self):
124*61c4878aSAndroid Build Coastguard Worker        self._dir = tempfile.TemporaryDirectory()
125*61c4878aSAndroid Build Coastguard Worker        root = Path(self._dir.name)
126*61c4878aSAndroid Build Coastguard Worker        self._build_file = root / 'seed' / 'BUILD.gn'
127*61c4878aSAndroid Build Coastguard Worker        self._build_file.parent.mkdir()
128*61c4878aSAndroid Build Coastguard Worker        self._build_file.write_text(_SAMPLE_REGISTRY_FILE)
129*61c4878aSAndroid Build Coastguard Worker        self._registry = seed.SeedRegistry.parse(self._build_file)
130*61c4878aSAndroid Build Coastguard Worker
131*61c4878aSAndroid Build Coastguard Worker    def tearDown(self):
132*61c4878aSAndroid Build Coastguard Worker        self._dir.cleanup()
133*61c4878aSAndroid Build Coastguard Worker
134*61c4878aSAndroid Build Coastguard Worker    def test_basic_parsing(self):
135*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(self._registry.seed_count(), 2)
136*61c4878aSAndroid Build Coastguard Worker
137*61c4878aSAndroid Build Coastguard Worker    def test_insert_seed(self):
138*61c4878aSAndroid Build Coastguard Worker        meta = seed.SeedMetadata(
139*61c4878aSAndroid Build Coastguard Worker            number=200,
140*61c4878aSAndroid Build Coastguard Worker            title='a title',
141*61c4878aSAndroid Build Coastguard Worker            authors='an author',
142*61c4878aSAndroid Build Coastguard Worker            status=seed.SeedStatus.DRAFT,
143*61c4878aSAndroid Build Coastguard Worker            changelist=111111,
144*61c4878aSAndroid Build Coastguard Worker        )
145*61c4878aSAndroid Build Coastguard Worker        self._registry.insert(meta)
146*61c4878aSAndroid Build Coastguard Worker        self._registry.write()
147*61c4878aSAndroid Build Coastguard Worker
148*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(
149*61c4878aSAndroid Build Coastguard Worker            self._build_file.read_text(),
150*61c4878aSAndroid Build Coastguard Worker            _SAMPLE_REGISTRY_FILE_WITH_ADDED_SEED,
151*61c4878aSAndroid Build Coastguard Worker        )
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard Worker
154*61c4878aSAndroid Build Coastguard Workerif __name__ == '__main__':
155*61c4878aSAndroid Build Coastguard Worker    unittest.main()
156