OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Unit tests for cros_build_lib.""" |
| 8 |
| 9 import unittest |
| 10 |
| 11 import cros_build_lib |
| 12 |
| 13 class CrosBuildLibTest(unittest.TestCase): |
| 14 """Test class for cros_build_lib.""" |
| 15 |
| 16 def testRunCommandSimple(self): |
| 17 """Test that RunCommand can run a simple successful command.""" |
| 18 result = cros_build_lib.RunCommand(['ls'], |
| 19 # Keep the test quiet options |
| 20 print_cmd=False, |
| 21 redirect_stdout=True, |
| 22 redirect_stderr=True, |
| 23 # Test specific options |
| 24 exit_code=True) |
| 25 self.assertEqual(result, 0) |
| 26 |
| 27 def testRunCommandError(self): |
| 28 """Test that RunCommand can return an error code for a failed command.""" |
| 29 result = cros_build_lib.RunCommand(['ls', '/nosuchdir'], |
| 30 # Keep the test quiet options |
| 31 print_cmd=False, |
| 32 redirect_stdout=True, |
| 33 redirect_stderr=True, |
| 34 # Test specific options |
| 35 error_ok=True, |
| 36 exit_code=True) |
| 37 self.assertNotEqual(result, 0) |
| 38 |
| 39 def testRunCommandErrorRetries(self): |
| 40 """Test that RunCommand can retry a failed command that always fails.""" |
| 41 |
| 42 # We don't actually check that it's retrying, just exercise the code path. |
| 43 result = cros_build_lib.RunCommand(['ls', '/nosuchdir'], |
| 44 # Keep the test quiet options |
| 45 print_cmd=False, |
| 46 redirect_stdout=True, |
| 47 redirect_stderr=True, |
| 48 # Test specific options |
| 49 num_retries=2, |
| 50 error_ok=True, |
| 51 exit_code=True) |
| 52 self.assertNotEqual(result, 0) |
| 53 |
| 54 def testRunCommandErrorException(self): |
| 55 """Test that RunCommand can throw an exception when a command fails.""" |
| 56 |
| 57 function = lambda : cros_build_lib.RunCommand(['ls', '/nosuchdir'], |
| 58 # Keep the test quiet options |
| 59 print_cmd=False, |
| 60 redirect_stdout=True, |
| 61 redirect_stderr=True) |
| 62 self.assertRaises(cros_build_lib.RunCommandException, function) |
| 63 |
| 64 def testRunCommandCaptureOutput(self): |
| 65 """Test that RunCommand can capture stdout if a command succeeds.""" |
| 66 |
| 67 result = cros_build_lib.RunCommand(['echo', '-n', 'Hi'], |
| 68 # Keep the test quiet options |
| 69 print_cmd=False, |
| 70 redirect_stdout=True, |
| 71 redirect_stderr=True) |
| 72 self.assertEqual(result, 'Hi') |
| 73 |
| 74 |
| 75 if __name__ == '__main__': |
| 76 unittest.main() |
OLD | NEW |