OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Unit tests for cros_build_lib.""" | 7 """Unit tests for cros_build_lib.""" |
8 | 8 |
| 9 import mox |
9 import os | 10 import os |
10 import tempfile | 11 import tempfile |
11 import unittest | 12 import unittest |
12 | 13 |
13 import cros_build_lib | 14 import cros_build_lib |
14 | 15 |
15 class CrosBuildLibTest(unittest.TestCase): | 16 class CrosBuildLibTest(mox.MoxTestBase): |
16 """Test class for cros_build_lib.""" | 17 """Test class for cros_build_lib.""" |
17 | 18 |
18 def testRunCommandSimple(self): | 19 def testRunCommandSimple(self): |
19 """Test that RunCommand can run a simple successful command.""" | 20 """Test that RunCommand can run a simple successful command.""" |
20 result = cros_build_lib.RunCommand(['ls'], | 21 result = cros_build_lib.RunCommand(['ls'], |
21 # Keep the test quiet options | 22 # Keep the test quiet options |
22 print_cmd=False, | 23 print_cmd=False, |
23 redirect_stdout=True, | 24 redirect_stdout=True, |
24 redirect_stderr=True, | 25 redirect_stderr=True, |
25 # Test specific options | 26 # Test specific options |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 # Keep the test quiet options | 97 # Keep the test quiet options |
97 print_cmd=False, | 98 print_cmd=False, |
98 # Test specific options | 99 # Test specific options |
99 log_to_file=log_file) | 100 log_to_file=log_file) |
100 log_fh = open(log_file) | 101 log_fh = open(log_file) |
101 log_data = log_fh.read() | 102 log_data = log_fh.read() |
102 self.assertEquals('Hi', log_data) | 103 self.assertEquals('Hi', log_data) |
103 log_fh.close() | 104 log_fh.close() |
104 os.remove(log_file) | 105 os.remove(log_file) |
105 | 106 |
| 107 def testGetCrosUtilsPathInChroot(self): |
| 108 """Tests whether we can get crosutils from chroot.""" |
| 109 self.mox.StubOutWithMock(cros_build_lib, 'IsInsideChroot') |
| 110 crosutils_path_src = '/home/' + os.getenv('USER') + 'trunk/src/scripts' |
| 111 crosutils_path_installed = '/usr/lib/crosutils' |
| 112 |
| 113 cros_build_lib.IsInsideChroot().MultipleTimes().AndReturn(True) |
| 114 |
| 115 self.mox.ReplayAll() |
| 116 self.assertTrue(cros_build_lib.GetCrosUtilsPath(source_dir_path=True), |
| 117 crosutils_path_src) |
| 118 self.assertTrue(cros_build_lib.GetCrosUtilsPath(source_dir_path=False), |
| 119 crosutils_path_installed) |
| 120 self.mox.VerifyAll() |
| 121 |
| 122 def testGetCrosUtilsPathOutsideChroot(self): |
| 123 """Tests whether we can get crosutils from outside chroot.""" |
| 124 self.mox.StubOutWithMock(cros_build_lib, 'IsInsideChroot') |
| 125 path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') |
| 126 cros_build_lib.IsInsideChroot().MultipleTimes().AndReturn(False) |
| 127 |
| 128 self.mox.ReplayAll() |
| 129 self.assertTrue(cros_build_lib.GetCrosUtilsPath(), path) |
| 130 self.mox.VerifyAll() |
| 131 |
| 132 def testGetCrosUtilsBinPath(self): |
| 133 """Tests whether we can get crosutilsbin correctly.""" |
| 134 self.mox.StubOutWithMock(cros_build_lib, 'IsInsideChroot') |
| 135 self.mox.StubOutWithMock(cros_build_lib, 'GetCrosUtilsPath') |
| 136 src_path = '/fake/src' |
| 137 chroot_src_path = '/chroot/fake/src' |
| 138 chroot_path = '/usr/bin' |
| 139 |
| 140 cros_build_lib.IsInsideChroot().AndReturn(False) |
| 141 cros_build_lib.GetCrosUtilsPath(True).AndReturn(src_path) |
| 142 cros_build_lib.IsInsideChroot().AndReturn(True) |
| 143 cros_build_lib.GetCrosUtilsPath(True).AndReturn(chroot_src_path) |
| 144 cros_build_lib.IsInsideChroot().AndReturn(True) |
| 145 |
| 146 self.mox.ReplayAll() |
| 147 # Outside chroot. |
| 148 self.assertTrue(cros_build_lib.GetCrosUtilsBinPath(source_dir_path=True), |
| 149 src_path + '/bin') |
| 150 # Rest inside chroot. |
| 151 self.assertTrue(cros_build_lib.GetCrosUtilsBinPath(source_dir_path=True), |
| 152 chroot_src_path + '/bin') |
| 153 self.assertTrue(cros_build_lib.GetCrosUtilsBinPath(source_dir_path=False), |
| 154 chroot_path) |
| 155 self.mox.VerifyAll() |
| 156 |
| 157 |
106 | 158 |
107 if __name__ == '__main__': | 159 if __name__ == '__main__': |
108 unittest.main() | 160 unittest.main() |
OLD | NEW |