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