OLD | NEW |
| 1 #!/usr/bin/env python |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 4 # found in the LICENSE file. |
4 | 5 |
5 """Tests for the cmd_helper module.""" | 6 """Tests for the cmd_helper module.""" |
6 | 7 |
7 import unittest | 8 import unittest |
8 import subprocess | 9 import subprocess |
9 | 10 |
10 from devil.utils import cmd_helper | 11 from devil.utils import cmd_helper |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 self.assertEquals('''"hello\\"; rm -rf /"''', | 46 self.assertEquals('''"hello\\"; rm -rf /"''', |
46 cmd_helper.DoubleQuote('hello"; rm -rf /')) | 47 cmd_helper.DoubleQuote('hello"; rm -rf /')) |
47 | 48 |
48 def testSingleQuote_doExpand(self): | 49 def testSingleQuote_doExpand(self): |
49 test_string = 'hello $TEST_VAR' | 50 test_string = 'hello $TEST_VAR' |
50 cmd = 'TEST_VAR=world; echo %s' % cmd_helper.DoubleQuote(test_string) | 51 cmd = 'TEST_VAR=world; echo %s' % cmd_helper.DoubleQuote(test_string) |
51 self.assertEquals('hello world', | 52 self.assertEquals('hello world', |
52 cmd_helper.GetCmdOutput(cmd, shell=True).rstrip()) | 53 cmd_helper.GetCmdOutput(cmd, shell=True).rstrip()) |
53 | 54 |
54 | 55 |
| 56 class CmdHelperShinkToSnippetTest(unittest.TestCase): |
| 57 |
| 58 def testShrinkToSnippet_noArgs(self): |
| 59 self.assertEquals('foo', |
| 60 cmd_helper.ShrinkToSnippet(['foo'], 'a', 'bar')) |
| 61 self.assertEquals("'foo foo'", |
| 62 cmd_helper.ShrinkToSnippet(['foo foo'], 'a', 'bar')) |
| 63 self.assertEquals('"$a"\' bar\'', |
| 64 cmd_helper.ShrinkToSnippet(['foo bar'], 'a', 'foo')) |
| 65 self.assertEquals('\'foo \'"$a"', |
| 66 cmd_helper.ShrinkToSnippet(['foo bar'], 'a', 'bar')) |
| 67 self.assertEquals('foo"$a"', |
| 68 cmd_helper.ShrinkToSnippet(['foobar'], 'a', 'bar')) |
| 69 |
| 70 def testShrinkToSnippet_singleArg(self): |
| 71 self.assertEquals("foo ''", |
| 72 cmd_helper.ShrinkToSnippet(['foo', ''], 'a', 'bar')) |
| 73 self.assertEquals("foo foo", |
| 74 cmd_helper.ShrinkToSnippet(['foo', 'foo'], 'a', 'bar')) |
| 75 self.assertEquals('"$a" "$a"', |
| 76 cmd_helper.ShrinkToSnippet(['foo', 'foo'], 'a', 'foo')) |
| 77 self.assertEquals('foo "$a""$a"', |
| 78 cmd_helper.ShrinkToSnippet(['foo', 'barbar'], 'a', 'bar')) |
| 79 self.assertEquals('foo "$a"\' \'"$a"', |
| 80 cmd_helper.ShrinkToSnippet(['foo', 'bar bar'], 'a', 'bar')) |
| 81 self.assertEquals('foo "$a""$a"\' \'', |
| 82 cmd_helper.ShrinkToSnippet(['foo', 'barbar '], 'a', 'bar')) |
| 83 self.assertEquals('foo \' \'"$a""$a"\' \'', |
| 84 cmd_helper.ShrinkToSnippet(['foo', ' barbar '], 'a', 'bar')) |
| 85 |
55 class CmdHelperIterCmdOutputLinesTest(unittest.TestCase): | 86 class CmdHelperIterCmdOutputLinesTest(unittest.TestCase): |
56 """Test IterCmdOutputLines with some calls to the unix 'seq' command.""" | 87 """Test IterCmdOutputLines with some calls to the unix 'seq' command.""" |
57 | 88 |
58 def testIterCmdOutputLines_success(self): | 89 def testIterCmdOutputLines_success(self): |
59 for num, line in enumerate( | 90 for num, line in enumerate( |
60 cmd_helper.IterCmdOutputLines(['seq', '10']), 1): | 91 cmd_helper.IterCmdOutputLines(['seq', '10']), 1): |
61 self.assertEquals(num, int(line)) | 92 self.assertEquals(num, int(line)) |
62 | 93 |
63 def testIterCmdOutputLines_exitStatusFail(self): | 94 def testIterCmdOutputLines_exitStatusFail(self): |
64 with self.assertRaises(subprocess.CalledProcessError): | 95 with self.assertRaises(subprocess.CalledProcessError): |
65 for num, line in enumerate( | 96 for num, line in enumerate( |
66 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): | 97 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): |
67 self.assertEquals(num, int(line)) | 98 self.assertEquals(num, int(line)) |
68 # after reading all the output we get an exit status of 1 | 99 # after reading all the output we get an exit status of 1 |
69 | 100 |
70 def testIterCmdOutputLines_exitStatusIgnored(self): | 101 def testIterCmdOutputLines_exitStatusIgnored(self): |
71 for num, line in enumerate( | 102 for num, line in enumerate( |
72 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True, | 103 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True, |
73 check_status=False), 1): | 104 check_status=False), 1): |
74 self.assertEquals(num, int(line)) | 105 self.assertEquals(num, int(line)) |
75 | 106 |
76 def testIterCmdOutputLines_exitStatusSkipped(self): | 107 def testIterCmdOutputLines_exitStatusSkipped(self): |
77 for num, line in enumerate( | 108 for num, line in enumerate( |
78 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): | 109 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): |
79 self.assertEquals(num, int(line)) | 110 self.assertEquals(num, int(line)) |
80 # no exception will be raised because we don't attempt to read past | 111 # no exception will be raised because we don't attempt to read past |
81 # the end of the output and, thus, the status never gets checked | 112 # the end of the output and, thus, the status never gets checked |
82 if num == 10: | 113 if num == 10: |
83 break | 114 break |
| 115 |
| 116 |
| 117 if __name__ == '__main__': |
| 118 unittest.main() |
OLD | NEW |