Skip to content

Commit bfdb5da

Browse files
Update test_metadata.py to use temporary files for test cases, for easier extensibility (#33008)
* Modified the test script with the changes recommended * Removed unwanted import os * Removed path_under_test as it is using TempFile * Restyled by autopep8 * Restyled by isort --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent c065cc7 commit bfdb5da

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

scripts/tests/py/test_metadata.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/python3
21
# Copyright (c) 2024 Project CHIP Authors
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,38 +12,39 @@
1312
# See the License for the specific language governing permissions and
1413
# limitations under the License.
1514

16-
import os
15+
import tempfile
1716
import unittest
1817
from os import path
18+
from typing import List
1919

2020
from metadata import Metadata, MetadataReader
2121

2222

2323
class TestMetadataReader(unittest.TestCase):
24-
path_under_test = path_under_test = path.join(path.dirname(__file__), "simple_run_args.txt")
2524

2625
def setUp(self):
27-
2826
# build the reader object
2927
self.reader = MetadataReader(path.join(path.dirname(__file__), "env_test.yaml"))
30-
with open(self.path_under_test, 'w', encoding='utf8') as test_file:
31-
test_file.writelines('''
32-
# test-runner-runs: run1
33-
# test-runner-run/run1: app/all-clusters discriminator KVS storage-path commissioning-method discriminator passcode
34-
''')
35-
36-
def test_parse_single_run(self):
3728

38-
expected_runs_metadata = []
29+
def assertMetadataParse(self, file_content: str, expected: List[Metadata]):
30+
with tempfile.NamedTemporaryFile(mode='w', delete=False) as fp:
31+
fp.write(file_content)
32+
fp.close()
33+
for e in expected:
34+
e.py_script_path = fp.name
35+
actual = self.reader.parse_script(fp.name)
36+
self.assertEqual(actual, expected)
3937

40-
expected_runs_metadata.append(Metadata(app="out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app",
41-
discriminator=1234, py_script_path=self.path_under_test, run="run1", passcode=20202021))
42-
43-
self.assertEqual(self.reader.parse_script(self.path_under_test), expected_runs_metadata)
44-
45-
def tearDown(self):
46-
if os.path.exists(self.path_under_test):
47-
os.remove(self.path_under_test)
38+
def test_parse_single_run(self):
39+
self.assertMetadataParse('''
40+
# test-runner-runs: run1
41+
# test-runner-run/run1: app/all-clusters discriminator passcode
42+
''',
43+
[
44+
Metadata(app="out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app",
45+
discriminator=1234, run="run1", passcode=20202021)
46+
]
47+
)
4848

4949

5050
if __name__ == "__main__":

0 commit comments

Comments
 (0)