• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12import common
13
14
15@unittest.skipIf(os.name == 'nt', 'Fuchsia tests not supported on Windows')
16class CommonTest(unittest.TestCase):
17    """Test common.py methods."""
18    def test_find_in_dir_returns_file_or_dir_if_searching(self) -> None:
19        """Test |find_in_dir| returns files if searching for file, or None."""
20        # Make the directory structure.
21        with tempfile.TemporaryDirectory() as tmp_dir:
22            with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_file, \
23                tempfile.TemporaryDirectory(dir=tmp_dir) as inner_tmp_dir:
24
25                # Structure is now:
26                # temp_dir/
27                # temp_dir/inner_dir1
28                # temp_dir/tempfile1
29                # File is not a dir, so returns None.
30                self.assertIsNone(
31                    common.find_in_dir(os.path.basename(tmp_file.name),
32                                       parent_dir=tmp_dir))
33
34                # Repeat for directory.
35                self.assertEqual(
36                    common.find_in_dir(inner_tmp_dir, parent_dir=tmp_dir),
37                    inner_tmp_dir)
38
39    def test_find_image_in_sdk_searches_images_in_product_bundle(self):
40        """Test |find_image_in_sdk| searches for 'images' if product-bundle."""
41        with tempfile.TemporaryDirectory() as tmp_dir:
42            os.makedirs(os.path.join(tmp_dir, 'sdk'), exist_ok=True)
43            os.makedirs(os.path.join(tmp_dir, 'images', 'workstation-product',
44                                     'images'),
45                        exist_ok=True)
46            with mock.patch('common.SDK_ROOT', os.path.join(tmp_dir, 'sdk')):
47                self.assertEqual(
48                    common.find_image_in_sdk('workstation-product'),
49                    os.path.join(tmp_dir, 'images', 'workstation-product',
50                                 'images'))
51
52
53if __name__ == '__main__':
54    unittest.main()
55