Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cros / commands / init_unittest.py
1 #!/usr/bin/python
2 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """This module tests the init module."""
7
8 from __future__ import print_function
9
10 import glob
11 import imp
12 import mox
13 import os
14 import sys
15
16 sys.path.insert(0, os.path.abspath('%s/../../..' % os.path.dirname(__file__)))
17
18 from chromite.lib import commandline
19 from chromite.lib import cros_build_lib_unittest
20 from chromite.lib import cros_test_lib
21 from chromite.lib import partial_mock
22 from chromite.cros import commands
23
24
25 class MockCommand(partial_mock.PartialMock):
26   """Mock class for a generic cros command."""
27   ATTRS = ('Run',)
28   COMMAND = None
29   TARGET_CLASS = None
30
31   def __init__(self, args, base_args=None):
32     partial_mock.PartialMock.__init__(self)
33     self.args = args
34     self.rc_mock = cros_build_lib_unittest.RunCommandMock()
35     self.rc_mock.SetDefaultCmdResult()
36     parser = commandline.ArgumentParser(caching=True)
37     subparsers = parser.add_subparsers()
38     subparser = subparsers.add_parser(self.COMMAND, caching=True)
39     self.TARGET_CLASS.AddParser(subparser)
40
41     args = base_args if base_args else []
42     args += [self.COMMAND] + self.args
43     options =  parser.parse_args(args)
44     self.inst = options.cros_class(options)
45
46   def Run(self, inst):
47     with self.rc_mock:
48       self.backup['Run'](inst)
49
50
51 # pylint: disable=W0212
52 class CommandTest(cros_test_lib.MoxTestCase):
53   """This test class tests that we can load modules correctly."""
54
55   def testFindModules(self):
56     """Tests that we can return modules correctly when mocking out glob."""
57     self.mox.StubOutWithMock(glob, 'glob')
58     fake_command_file = 'cros_command_test.py'
59     filtered_file = 'cros_command_unittest.py'
60     mydir = 'mydir'
61
62     glob.glob(mox.StrContains(mydir)).AndReturn([fake_command_file,
63                                                  filtered_file])
64
65     self.mox.ReplayAll()
66     self.assertEqual(commands._FindModules(mydir), [fake_command_file])
67     self.mox.VerifyAll()
68
69   def testLoadCommands(self):
70     """Tests import commands correctly."""
71     self.mox.StubOutWithMock(commands, '_FindModules')
72     self.mox.StubOutWithMock(imp, 'load_module')
73     self.mox.StubOutWithMock(imp, 'find_module')
74     fake_command_file = 'cros_command_test.py'
75     fake_module = 'cros_command_test'
76     module_tuple = 'file', 'pathname', 'description'
77     commands._FindModules(mox.IgnoreArg()).AndReturn([fake_command_file])
78     imp.find_module(fake_module, mox.IgnoreArg()).AndReturn(module_tuple)
79     imp.load_module(fake_module, *module_tuple)
80
81     self.mox.ReplayAll()
82     commands._ImportCommands()
83     self.mox.VerifyAll()
84
85
86 if __name__ == '__main__':
87   cros_test_lib.main()