Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 1 | # 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 7 | import os |
| 8 | |
| 9 | from chromite.lib import cros_test_lib |
| 10 | from chromite.lib import osutils |
| 11 | from chromite.lib import sysroot_lib |
Sloan Johnson | a85640f | 2021-10-01 22:32:40 +0000 | [diff] [blame] | 12 | from chromite.lib import unittest_lib |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 13 | from chromite.scripts import install_toolchain |
| 14 | |
| 15 | |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 16 | class ParseArgsTest(cros_test_lib.TempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 17 | """Tests for argument parsing and validation rules.""" |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 18 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | # 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 26 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | def testInvalidArgs(self): |
| 38 | """Test invalid argument parsing.""" |
| 39 | with self.assertRaises(SystemExit): |
| 40 | install_toolchain._ParseArgs([]) |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | 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 Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | opts = install_toolchain._ParseArgs(["--sysroot", self.sysroot_dir]) |
| 57 | self.assertEqual(self.chost_value, opts.toolchain) |