OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2013 The Chromium 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 """Tests for the cmd_helper module.""" | |
7 | |
8 import unittest | |
9 import subprocess | |
10 | |
11 from devil.utils import cmd_helper | |
12 | |
13 | |
14 class CmdHelperSingleQuoteTest(unittest.TestCase): | |
15 | |
16 def testSingleQuote_basic(self): | |
17 self.assertEquals('hello', | |
18 cmd_helper.SingleQuote('hello')) | |
19 | |
20 def testSingleQuote_withSpaces(self): | |
21 self.assertEquals("'hello world'", | |
22 cmd_helper.SingleQuote('hello world')) | |
23 | |
24 def testSingleQuote_withUnsafeChars(self): | |
25 self.assertEquals("""'hello'"'"'; rm -rf /'""", | |
26 cmd_helper.SingleQuote("hello'; rm -rf /")) | |
27 | |
28 def testSingleQuote_dontExpand(self): | |
29 test_string = 'hello $TEST_VAR' | |
30 cmd = 'TEST_VAR=world; echo %s' % cmd_helper.SingleQuote(test_string) | |
31 self.assertEquals(test_string, | |
32 cmd_helper.GetCmdOutput(cmd, shell=True).rstrip()) | |
33 | |
34 | |
35 class CmdHelperDoubleQuoteTest(unittest.TestCase): | |
36 | |
37 def testDoubleQuote_basic(self): | |
38 self.assertEquals('hello', | |
39 cmd_helper.DoubleQuote('hello')) | |
40 | |
41 def testDoubleQuote_withSpaces(self): | |
42 self.assertEquals('"hello world"', | |
43 cmd_helper.DoubleQuote('hello world')) | |
44 | |
45 def testDoubleQuote_withUnsafeChars(self): | |
46 self.assertEquals('''"hello\\"; rm -rf /"''', | |
47 cmd_helper.DoubleQuote('hello"; rm -rf /')) | |
48 | |
49 def testSingleQuote_doExpand(self): | |
50 test_string = 'hello $TEST_VAR' | |
51 cmd = 'TEST_VAR=world; echo %s' % cmd_helper.DoubleQuote(test_string) | |
52 self.assertEquals('hello world', | |
53 cmd_helper.GetCmdOutput(cmd, shell=True).rstrip()) | |
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 | |
86 class CmdHelperIterCmdOutputLinesTest(unittest.TestCase): | |
87 """Test IterCmdOutputLines with some calls to the unix 'seq' command.""" | |
88 | |
89 def testIterCmdOutputLines_success(self): | |
90 for num, line in enumerate( | |
91 cmd_helper.IterCmdOutputLines(['seq', '10']), 1): | |
92 self.assertEquals(num, int(line)) | |
93 | |
94 def testIterCmdOutputLines_exitStatusFail(self): | |
95 with self.assertRaises(subprocess.CalledProcessError): | |
96 for num, line in enumerate( | |
97 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): | |
98 self.assertEquals(num, int(line)) | |
99 # after reading all the output we get an exit status of 1 | |
100 | |
101 def testIterCmdOutputLines_exitStatusIgnored(self): | |
102 for num, line in enumerate( | |
103 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True, | |
104 check_status=False), 1): | |
105 self.assertEquals(num, int(line)) | |
106 | |
107 def testIterCmdOutputLines_exitStatusSkipped(self): | |
108 for num, line in enumerate( | |
109 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): | |
110 self.assertEquals(num, int(line)) | |
111 # no exception will be raised because we don't attempt to read past | |
112 # the end of the output and, thus, the status never gets checked | |
113 if num == 10: | |
114 break | |
115 | |
116 | |
117 if __name__ == '__main__': | |
118 unittest.main() | |
OLD | NEW |