xref: /aosp_15_r20/external/angle/build/fuchsia/test/common_unittests.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/env vpython3
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""File for testing common.py."""
6
7import os
8import tempfile
9import unittest
10import unittest.mock as mock
11
12from types import SimpleNamespace
13
14import common
15
16
17# Tests should use their names to explain the meaning of the tests rather than
18# relying on the extra docstrings.
19# pylint: disable=missing-function-docstring
20@unittest.skipIf(os.name == 'nt', 'Fuchsia tests not supported on Windows')
21class CommonTest(unittest.TestCase):
22    """Test common.py methods."""
23    def test_find_in_dir_returns_file_or_dir_if_searching(self) -> None:
24        """Test |find_in_dir| returns files if searching for file, or None."""
25        # Make the directory structure.
26        with tempfile.TemporaryDirectory() as tmp_dir:
27            with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_file, \
28                tempfile.TemporaryDirectory(dir=tmp_dir) as inner_tmp_dir:
29
30                # Structure is now:
31                # temp_dir/
32                # temp_dir/inner_dir1
33                # temp_dir/tempfile1
34                # File is not a dir, so returns None.
35                self.assertIsNone(
36                    common.find_in_dir(os.path.basename(tmp_file.name),
37                                       parent_dir=tmp_dir))
38
39                # Repeat for directory.
40                self.assertEqual(
41                    common.find_in_dir(inner_tmp_dir, parent_dir=tmp_dir),
42                    inner_tmp_dir)
43
44    def test_find_image_in_sdk_searches_images_in_product_bundle(self):
45        """Test |find_image_in_sdk| searches for 'images' if product-bundle."""
46        with tempfile.TemporaryDirectory() as tmp_dir:
47            os.makedirs(os.path.join(tmp_dir, 'sdk'), exist_ok=True)
48            os.makedirs(os.path.join(tmp_dir, 'images', 'workstation-product',
49                                     'images'),
50                        exist_ok=True)
51            with mock.patch('common.SDK_ROOT', os.path.join(tmp_dir, 'sdk')):
52                self.assertEqual(
53                    common.find_image_in_sdk('workstation-product'),
54                    os.path.join(tmp_dir, 'images', 'workstation-product',
55                                 'images'))
56
57    def test_images_root_should_not_end_with_path_sep(self):
58        """INTERNAL_IMAGES_ROOT appends -internal at the end of the IMAGES_ROOT,
59        so the later one should not end with a /, otherwise the folder name will
60        become 'images/-internal'."""
61        # Avoid the logic being bypassed.
62        self.assertIsNone(os.environ.get('FUCHSIA_INTERNAL_IMAGES_ROOT'))
63        self.assertFalse(common.IMAGES_ROOT.endswith(os.path.sep))
64
65    @mock.patch('common.run_ffx_command')
66    def test_get_system_info_parse_version_and_product(self, ffx_mock):
67        ffx_mock.return_value = SimpleNamespace(
68            returncode=0, stdout='{"build": {"version": "v", "product": "p"}}')
69        self.assertEqual(common.get_system_info(), ('p', 'v'))
70
71    @mock.patch('common.run_ffx_command')
72    def test_get_system_info_parse_version_only(self, ffx_mock):
73        ffx_mock.return_value = SimpleNamespace(
74            returncode=0, stdout='{"build": {"version": "v"}}')
75        self.assertEqual(common.get_system_info(), ('', 'v'))
76
77    @mock.patch('common.run_ffx_command')
78    def test_get_system_info_ffx_error(self, ffx_mock):
79        ffx_mock.return_value = SimpleNamespace(returncode=100,
80                                                stdout='{"build": {}}')
81        self.assertEqual(common.get_system_info(), ('', ''))
82
83    @mock.patch('common.run_ffx_command')
84    def test_get_system_info_never_returns_none(self, ffx_mock):
85        ffx_mock.return_value = SimpleNamespace(returncode=0,
86                                                stdout='{"build": {}}')
87        self.assertEqual(common.get_system_info(), ('', ''))
88
89    @mock.patch('common.run_ffx_command')
90    def test_get_system_info_ignore_no_build(self, ffx_mock):
91        ffx_mock.return_value = SimpleNamespace(
92            returncode=0, stdout='{"thisisnotbuild": {}}')
93        self.assertEqual(common.get_system_info(), ('', ''))
94
95    @mock.patch('common.run_ffx_command')
96    def test_get_system_info_ignore_bad_build_type(self, ffx_mock):
97        ffx_mock.return_value = SimpleNamespace(returncode=0,
98                                                stdout='{"build": []}')
99        self.assertEqual(common.get_system_info(), ('', ''))
100
101    @mock.patch('common.run_ffx_command')
102    def test_get_system_info_ignore_bad_build_type2(self, ffx_mock):
103        ffx_mock.return_value = SimpleNamespace(returncode=0,
104                                                stdout='{"build": "hello"}')
105        self.assertEqual(common.get_system_info(), ('', ''))
106
107    @mock.patch('common.run_ffx_command')
108    def test_get_system_info_not_a_json(self, ffx_mock):
109        ffx_mock.return_value = SimpleNamespace(returncode=0, stdout='hello')
110        self.assertEqual(common.get_system_info(), ('', ''))
111
112if __name__ == '__main__':
113    unittest.main()
114