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): |
| 17 | """Tests for argument parsing and validation rules.""" |
| 18 | |
| 19 | # pylint: disable=protected-access |
| 20 | def setUp(self): |
| 21 | D = cros_test_lib.Directory |
| 22 | filesystem = ( |
| 23 | D('build', ( |
| 24 | D('board', ( |
| 25 | D('etc', ( |
| 26 | 'make.conf.board_setup', |
| 27 | )), |
| 28 | )), |
| 29 | )), |
| 30 | ) |
| 31 | cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem) |
| 32 | |
| 33 | self.chost_value = 'chost' |
| 34 | osutils.WriteFile(os.path.join(self.tempdir, |
| 35 | 'build/board/etc/make.conf.board_setup'), |
| 36 | 'CHOST="%s"' % self.chost_value) |
| 37 | |
| 38 | self.sysroot_dir = os.path.join(self.tempdir, 'build/board') |
Sloan Johnson | a85640f | 2021-10-01 22:32:40 +0000 | [diff] [blame] | 39 | # make.conf needs to exist to correctly read back config. |
| 40 | unittest_lib.create_stub_make_conf(self.sysroot_dir) |
Alex Klein | 8212d49 | 2018-08-27 07:47:23 -0600 | [diff] [blame] | 41 | |
| 42 | def testInvalidArgs(self): |
| 43 | """Test invalid argument parsing.""" |
| 44 | with self.assertRaises(SystemExit): |
| 45 | install_toolchain._ParseArgs([]) |
| 46 | |
| 47 | def testSysrootCreate(self): |
| 48 | """Test the sysroot is correctly built.""" |
| 49 | opts = install_toolchain._ParseArgs(['--sysroot', self.sysroot_dir]) |
| 50 | self.assertIsInstance(opts.sysroot, sysroot_lib.Sysroot) |
| 51 | self.assertEqual(self.sysroot_dir, opts.sysroot.path) |
| 52 | |
| 53 | def testToolchain(self): |
| 54 | """Test toolchain arg precedence/fallback.""" |
| 55 | toolchain = 'toolchain' |
| 56 | opts = install_toolchain._ParseArgs(['--sysroot', self.sysroot_dir, |
| 57 | '--toolchain', toolchain]) |
| 58 | self.assertEqual(toolchain, opts.toolchain) |
| 59 | |
| 60 | opts = install_toolchain._ParseArgs(['--sysroot', self.sysroot_dir]) |
| 61 | self.assertEqual(self.chost_value, opts.toolchain) |