blob: d503c72a0e936f968a15ed735b3decb972d79cad [file] [log] [blame]
Alex Klein8212d492018-08-27 07:47:23 -06001# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Test install_toolchain."""
6
Alex Klein8212d492018-08-27 07:47:23 -06007import os
8
9from chromite.lib import cros_test_lib
10from chromite.lib import osutils
11from chromite.lib import sysroot_lib
Sloan Johnsona85640f2021-10-01 22:32:40 +000012from chromite.lib import unittest_lib
Alex Klein8212d492018-08-27 07:47:23 -060013from chromite.scripts import install_toolchain
14
15
Alex Klein8212d492018-08-27 07:47:23 -060016class ParseArgsTest(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060017 """Tests for argument parsing and validation rules."""
Alex Klein8212d492018-08-27 07:47:23 -060018
Alex Klein1699fab2022-09-08 08:46:06 -060019 # pylint: disable=protected-access
20 def setUp(self):
21 D = cros_test_lib.Directory
22 filesystem = (
23 D("build", (D("board", (D("etc", ("make.conf.board_setup",)),)),)),
24 )
25 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem)
Alex Klein8212d492018-08-27 07:47:23 -060026
Alex Klein1699fab2022-09-08 08:46:06 -060027 self.chost_value = "chost"
28 osutils.WriteFile(
29 os.path.join(self.tempdir, "build/board/etc/make.conf.board_setup"),
30 'CHOST="%s"' % self.chost_value,
31 )
Alex Klein8212d492018-08-27 07:47:23 -060032
Alex Klein1699fab2022-09-08 08:46:06 -060033 self.sysroot_dir = os.path.join(self.tempdir, "build/board")
34 # make.conf needs to exist to correctly read back config.
35 unittest_lib.create_stub_make_conf(self.sysroot_dir)
Alex Klein8212d492018-08-27 07:47:23 -060036
Alex Klein1699fab2022-09-08 08:46:06 -060037 def testInvalidArgs(self):
38 """Test invalid argument parsing."""
39 with self.assertRaises(SystemExit):
40 install_toolchain._ParseArgs([])
Alex Klein8212d492018-08-27 07:47:23 -060041
Alex Klein1699fab2022-09-08 08:46:06 -060042 def testSysrootCreate(self):
43 """Test the sysroot is correctly built."""
44 opts = install_toolchain._ParseArgs(["--sysroot", self.sysroot_dir])
45 self.assertIsInstance(opts.sysroot, sysroot_lib.Sysroot)
46 self.assertEqual(self.sysroot_dir, opts.sysroot.path)
Alex Klein8212d492018-08-27 07:47:23 -060047
Alex Klein1699fab2022-09-08 08:46:06 -060048 def testToolchain(self):
49 """Test toolchain arg precedence/fallback."""
50 toolchain = "toolchain"
51 opts = install_toolchain._ParseArgs(
52 ["--sysroot", self.sysroot_dir, "--toolchain", toolchain]
53 )
54 self.assertEqual(toolchain, opts.toolchain)
Alex Klein8212d492018-08-27 07:47:23 -060055
Alex Klein1699fab2022-09-08 08:46:06 -060056 opts = install_toolchain._ParseArgs(["--sysroot", self.sysroot_dir])
57 self.assertEqual(self.chost_value, opts.toolchain)