From 5f9e9061f1170fe81e60b4544dd87787ec6c464f Mon Sep 17 00:00:00 2001 From: Vatsal Ghelani <152916324+vatsalghelani-csa@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:51:54 -0400 Subject: [PATCH] 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 --- scripts/tests/py/test_metadata.py | 40 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/scripts/tests/py/test_metadata.py b/scripts/tests/py/test_metadata.py index 7c2594d08134c2..8707d483026bfb 100644 --- a/scripts/tests/py/test_metadata.py +++ b/scripts/tests/py/test_metadata.py @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # Copyright (c) 2024 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,38 +12,39 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os +import tempfile import unittest from os import path +from typing import List from metadata import Metadata, MetadataReader class TestMetadataReader(unittest.TestCase): - path_under_test = path_under_test = path.join(path.dirname(__file__), "simple_run_args.txt") def setUp(self): - # build the reader object self.reader = MetadataReader(path.join(path.dirname(__file__), "env_test.yaml")) - with open(self.path_under_test, 'w', encoding='utf8') as test_file: - test_file.writelines(''' - # test-runner-runs: run1 - # test-runner-run/run1: app/all-clusters discriminator KVS storage-path commissioning-method discriminator passcode - ''') - - def test_parse_single_run(self): - expected_runs_metadata = [] + def assertMetadataParse(self, file_content: str, expected: List[Metadata]): + with tempfile.NamedTemporaryFile(mode='w', delete=False) as fp: + fp.write(file_content) + fp.close() + for e in expected: + e.py_script_path = fp.name + actual = self.reader.parse_script(fp.name) + self.assertEqual(actual, expected) - expected_runs_metadata.append(Metadata(app="out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app", - discriminator=1234, py_script_path=self.path_under_test, run="run1", passcode=20202021)) - - self.assertEqual(self.reader.parse_script(self.path_under_test), expected_runs_metadata) - - def tearDown(self): - if os.path.exists(self.path_under_test): - os.remove(self.path_under_test) + def test_parse_single_run(self): + self.assertMetadataParse(''' + # test-runner-runs: run1 + # test-runner-run/run1: app/all-clusters discriminator passcode + ''', + [ + Metadata(app="out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app", + discriminator=1234, run="run1", passcode=20202021) + ] + ) if __name__ == "__main__":