3 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """This module tests the cros image command."""
12 sys.path.insert(0, os.path.abspath('%s/../../../..' % __file__))
13 from chromite.cros.commands import cros_build
14 from chromite.cros.commands import init_unittest
15 from chromite.lib import cros_build_lib
16 from chromite.lib import cros_test_lib
17 from chromite.lib import parallel_unittest
18 from chromite.lib import partial_mock
20 # TODO(build): Finish test wrapper (http://crosbug.com/37517).
21 # Until then, this has to be after the chromite imports.
25 class MockBuildCommand(init_unittest.MockCommand):
26 """Mock out the build command."""
27 TARGET = 'chromite.cros.commands.cros_build.BuildCommand'
28 TARGET_CLASS = cros_build.BuildCommand
31 packages = cros_build.GetToolchainPackages()
32 with mock.patch.object(cros_build, 'GetToolchainPackages') as tc_mock:
33 tc_mock.return_value = packages
34 with parallel_unittest.ParallelMock():
35 init_unittest.MockCommand.Run(self, inst)
38 class BuildCommandTest(cros_test_lib.TestCase):
39 """Test class for our BuildCommand class."""
41 def testSuccess(self):
42 """Test that successful commands work."""
43 cmds = [['--host', 'power_manager'],
44 ['--board=foo', 'power_manager'],
45 ['--board=foo', '--debug', 'power_manager'],
46 ['--board=foo', '--no-deps', 'power_manager'],
47 ['--board=foo', '--no-chroot-update', 'power_manager']]
49 with MockBuildCommand(cmd) as build:
51 if '--no-deps' not in cmd:
52 self.assertFalse(build.inst.chroot_update)
54 def testFailedDeps(self):
55 """Tests that failures are detected correctly."""
56 # pylint: disable=W0212
57 args = ['--board=foo', 'power_manager']
58 with MockBuildCommand(args) as build:
59 cmd = partial_mock.In('--backtrack=0')
60 build.rc_mock.AddCmdResult(cmd=cmd, returncode=1, error='error\n')
61 ex = self.assertRaises2(cros_build_lib.RunCommandError, build.inst.Run)
62 self.assertTrue(cros_build.BuildCommand._BAD_DEPEND_MSG in ex.msg)
64 def testGetToolchainPackages(self):
65 """Test GetToolchainPackages function without mocking."""
66 packages = cros_build.GetToolchainPackages()
67 self.assertTrue(packages)
70 if __name__ == '__main__':