OLD | NEW |
| (Empty) |
1 # 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 # found in the LICENSE file. | |
4 | |
5 """Tests for the cmd_helper module.""" | |
6 | |
7 import unittest | |
8 import subprocess | |
9 | |
10 from pylib import cmd_helper | |
11 | |
12 | |
13 class CmdHelperSingleQuoteTest(unittest.TestCase): | |
14 | |
15 def testSingleQuote_basic(self): | |
16 self.assertEquals('hello', | |
17 cmd_helper.SingleQuote('hello')) | |
18 | |
19 def testSingleQuote_withSpaces(self): | |
20 self.assertEquals("'hello world'", | |
21 cmd_helper.SingleQuote('hello world')) | |
22 | |
23 def testSingleQuote_withUnsafeChars(self): | |
24 self.assertEquals("""'hello'"'"'; rm -rf /'""", | |
25 cmd_helper.SingleQuote("hello'; rm -rf /")) | |
26 | |
27 def testSingleQuote_dontExpand(self): | |
28 test_string = 'hello $TEST_VAR' | |
29 cmd = 'TEST_VAR=world; echo %s' % cmd_helper.SingleQuote(test_string) | |
30 self.assertEquals(test_string, | |
31 cmd_helper.GetCmdOutput(cmd, shell=True).rstrip()) | |
32 | |
33 | |
34 class CmdHelperDoubleQuoteTest(unittest.TestCase): | |
35 | |
36 def testDoubleQuote_basic(self): | |
37 self.assertEquals('hello', | |
38 cmd_helper.DoubleQuote('hello')) | |
39 | |
40 def testDoubleQuote_withSpaces(self): | |
41 self.assertEquals('"hello world"', | |
42 cmd_helper.DoubleQuote('hello world')) | |
43 | |
44 def testDoubleQuote_withUnsafeChars(self): | |
45 self.assertEquals('''"hello\\"; rm -rf /"''', | |
46 cmd_helper.DoubleQuote('hello"; rm -rf /')) | |
47 | |
48 def testSingleQuote_doExpand(self): | |
49 test_string = 'hello $TEST_VAR' | |
50 cmd = 'TEST_VAR=world; echo %s' % cmd_helper.DoubleQuote(test_string) | |
51 self.assertEquals('hello world', | |
52 cmd_helper.GetCmdOutput(cmd, shell=True).rstrip()) | |
53 | |
54 | |
55 class CmdHelperIterCmdOutputLinesTest(unittest.TestCase): | |
56 """Test IterCmdOutputLines with some calls to the unix 'seq' command.""" | |
57 | |
58 def testIterCmdOutputLines_success(self): | |
59 for num, line in enumerate( | |
60 cmd_helper.IterCmdOutputLines(['seq', '10']), 1): | |
61 self.assertEquals(num, int(line)) | |
62 | |
63 def testIterCmdOutputLines_exitStatusFail(self): | |
64 with self.assertRaises(subprocess.CalledProcessError): | |
65 for num, line in enumerate( | |
66 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): | |
67 self.assertEquals(num, int(line)) | |
68 # after reading all the output we get an exit status of 1 | |
69 | |
70 def testIterCmdOutputLines_exitStatusIgnored(self): | |
71 for num, line in enumerate( | |
72 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True, | |
73 check_status=False), 1): | |
74 self.assertEquals(num, int(line)) | |
75 | |
76 def testIterCmdOutputLines_exitStatusSkipped(self): | |
77 for num, line in enumerate( | |
78 cmd_helper.IterCmdOutputLines('seq 10 && false', shell=True), 1): | |
79 self.assertEquals(num, int(line)) | |
80 # 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 | |
82 if num == 10: | |
83 break | |
OLD | NEW |