aboutsummaryrefslogtreecommitdiff
path: root/build/fuchsia/test/common_unittests.py
blob: 0391f9d8510bf741b9a6ca41dbf5c11c6fff66df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env vpython3
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""File for testing common.py."""

import os
import tempfile
import unittest
import unittest.mock as mock

from types import SimpleNamespace

import common


# Tests should use their names to explain the meaning of the tests rather than
# relying on the extra docstrings.
# pylint: disable=missing-function-docstring
@unittest.skipIf(os.name == 'nt', 'Fuchsia tests not supported on Windows')
class CommonTest(unittest.TestCase):
    """Test common.py methods."""
    def test_find_in_dir_returns_file_or_dir_if_searching(self) -> None:
        """Test |find_in_dir| returns files if searching for file, or None."""
        # Make the directory structure.
        with tempfile.TemporaryDirectory() as tmp_dir:
            with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_file, \
                tempfile.TemporaryDirectory(dir=tmp_dir) as inner_tmp_dir:

                # Structure is now:
                # temp_dir/
                # temp_dir/inner_dir1
                # temp_dir/tempfile1
                # File is not a dir, so returns None.
                self.assertIsNone(
                    common.find_in_dir(os.path.basename(tmp_file.name),
                                       parent_dir=tmp_dir))

                # Repeat for directory.
                self.assertEqual(
                    common.find_in_dir(inner_tmp_dir, parent_dir=tmp_dir),
                    inner_tmp_dir)

    def test_find_image_in_sdk_searches_images_in_product_bundle(self):
        """Test |find_image_in_sdk| searches for 'images' if product-bundle."""
        with tempfile.TemporaryDirectory() as tmp_dir:
            os.makedirs(os.path.join(tmp_dir, 'sdk'), exist_ok=True)
            os.makedirs(os.path.join(tmp_dir, 'images', 'workstation-product',
                                     'images'),
                        exist_ok=True)
            with mock.patch('common.SDK_ROOT', os.path.join(tmp_dir, 'sdk')):
                self.assertEqual(
                    common.find_image_in_sdk('workstation-product'),
                    os.path.join(tmp_dir, 'images', 'workstation-product',
                                 'images'))

    def test_images_root_should_not_end_with_path_sep(self):
        """INTERNAL_IMAGES_ROOT appends -internal at the end of the IMAGES_ROOT,
        so the later one should not end with a /, otherwise the folder name will
        become 'images/-internal'."""
        # Avoid the logic being bypassed.
        self.assertIsNone(os.environ.get('FUCHSIA_INTERNAL_IMAGES_ROOT'))
        self.assertFalse(common.IMAGES_ROOT.endswith(os.path.sep))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_parse_version_and_product(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(
            returncode=0, stdout='{"build": {"version": "v", "product": "p"}}')
        self.assertEqual(common.get_system_info(), ('p', 'v'))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_parse_version_only(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(
            returncode=0, stdout='{"build": {"version": "v"}}')
        self.assertEqual(common.get_system_info(), ('', 'v'))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_ffx_error(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(returncode=100,
                                                stdout='{"build": {}}')
        self.assertEqual(common.get_system_info(), ('', ''))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_never_returns_none(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(returncode=0,
                                                stdout='{"build": {}}')
        self.assertEqual(common.get_system_info(), ('', ''))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_ignore_no_build(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(
            returncode=0, stdout='{"thisisnotbuild": {}}')
        self.assertEqual(common.get_system_info(), ('', ''))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_ignore_bad_build_type(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(returncode=0,
                                                stdout='{"build": []}')
        self.assertEqual(common.get_system_info(), ('', ''))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_ignore_bad_build_type2(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(returncode=0,
                                                stdout='{"build": "hello"}')
        self.assertEqual(common.get_system_info(), ('', ''))

    @mock.patch('common.run_ffx_command')
    def test_get_system_info_not_a_json(self, ffx_mock):
        ffx_mock.return_value = SimpleNamespace(returncode=0, stdout='hello')
        self.assertEqual(common.get_system_info(), ('', ''))

if __name__ == '__main__':
    unittest.main()