xref: /aosp_15_r20/external/toolchain-utils/llvm_tools/chroot_unittest.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# Copyright 2020 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for chroot helper functions."""
7
8import subprocess
9import unittest
10from unittest import mock
11
12import chroot
13
14
15# These are unittests; protected access is OK to a point.
16# pylint: disable=protected-access
17
18
19class HelperFunctionsTest(unittest.TestCase):
20    """Test class for updating LLVM hashes of packages."""
21
22    @mock.patch.object(subprocess, "check_output")
23    def testSucceedsToGetChrootEbuildPathForPackage(self, mock_chroot_command):
24        package_chroot_path = "/chroot/path/to/package.ebuild"
25
26        # Emulate ChrootRunCommandWOutput behavior when a chroot path is found
27        # for a valid package.
28        mock_chroot_command.return_value = package_chroot_path
29
30        chroot_path = "/test/chroot/path"
31        package_list = ["new-test/package"]
32
33        self.assertEqual(
34            chroot.GetChrootEbuildPaths(chroot_path, package_list),
35            [package_chroot_path],
36        )
37
38        mock_chroot_command.assert_called_once()
39
40    def testFailedToConvertChrootPathWithInvalidPrefix(self):
41        chroot_path = "/path/to/chroot"
42        chroot_file_path = "/src/package.ebuild"
43
44        # Verify the exception is raised when a chroot path does not have the
45        # prefix '/mnt/host/source/'.
46        with self.assertRaises(ValueError) as err:
47            chroot.ConvertChrootPathsToAbsolutePaths(
48                chroot_path, [chroot_file_path]
49            )
50
51        self.assertEqual(
52            str(err.exception),
53            "Invalid prefix for the chroot path: " "%s" % chroot_file_path,
54        )
55
56    def testSucceedsToConvertChrootPathToAbsolutePath(self):
57        chroot_path = "/path/to/chroot"
58        chroot_file_paths = ["/mnt/host/source/src/package.ebuild"]
59
60        expected_abs_path = "/path/to/chroot/src/package.ebuild"
61
62        self.assertEqual(
63            chroot.ConvertChrootPathsToAbsolutePaths(
64                chroot_path, chroot_file_paths
65            ),
66            [expected_abs_path],
67        )
68
69
70if __name__ == "__main__":
71    unittest.main()
72