aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/chroot_unittest.py
blob: f1a6a626d999744198d9faeb3d95947dfed68f42 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for chroot helper functions."""


import subprocess
import unittest
import unittest.mock as mock

import chroot


# These are unittests; protected access is OK to a point.
# pylint: disable=protected-access


class HelperFunctionsTest(unittest.TestCase):
    """Test class for updating LLVM hashes of packages."""

    @mock.patch.object(subprocess, "check_output")
    def testSucceedsToGetChrootEbuildPathForPackage(self, mock_chroot_command):
        package_chroot_path = "/chroot/path/to/package.ebuild"

        # Emulate ChrootRunCommandWOutput behavior when a chroot path is found for
        # a valid package.
        mock_chroot_command.return_value = package_chroot_path

        chroot_path = "/test/chroot/path"
        package_list = ["new-test/package"]

        self.assertEqual(
            chroot.GetChrootEbuildPaths(chroot_path, package_list),
            [package_chroot_path],
        )

        mock_chroot_command.assert_called_once()

    def testFailedToConvertChrootPathWithInvalidPrefix(self):
        chroot_path = "/path/to/chroot"
        chroot_file_path = "/src/package.ebuild"

        # Verify the exception is raised when a chroot path does not have the prefix
        # '/mnt/host/source/'.
        with self.assertRaises(ValueError) as err:
            chroot.ConvertChrootPathsToAbsolutePaths(
                chroot_path, [chroot_file_path]
            )

        self.assertEqual(
            str(err.exception),
            "Invalid prefix for the chroot path: " "%s" % chroot_file_path,
        )

    def testSucceedsToConvertChrootPathToAbsolutePath(self):
        chroot_path = "/path/to/chroot"
        chroot_file_paths = ["/mnt/host/source/src/package.ebuild"]

        expected_abs_path = "/path/to/chroot/src/package.ebuild"

        self.assertEqual(
            chroot.ConvertChrootPathsToAbsolutePaths(
                chroot_path, chroot_file_paths
            ),
            [expected_abs_path],
        )


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