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.
5 """Tests for the cmd_helper module."""
10 from pylib
import cmd_helper
13 class CmdHelperSingleQuoteTest(unittest
.TestCase
):
15 def testSingleQuote_basic(self
):
16 self
.assertEquals('hello',
17 cmd_helper
.SingleQuote('hello'))
19 def testSingleQuote_withSpaces(self
):
20 self
.assertEquals("'hello world'",
21 cmd_helper
.SingleQuote('hello world'))
23 def testSingleQuote_withUnsafeChars(self
):
24 self
.assertEquals("""'hello'"'"'; rm -rf /'""",
25 cmd_helper
.SingleQuote("hello'; rm -rf /"))
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())
34 class CmdHelperDoubleQuoteTest(unittest
.TestCase
):
36 def testDoubleQuote_basic(self
):
37 self
.assertEquals('hello',
38 cmd_helper
.DoubleQuote('hello'))
40 def testDoubleQuote_withSpaces(self
):
41 self
.assertEquals('"hello world"',
42 cmd_helper
.DoubleQuote('hello world'))
44 def testDoubleQuote_withUnsafeChars(self
):
45 self
.assertEquals('''"hello\\"; rm -rf /"''',
46 cmd_helper
.DoubleQuote('hello"; rm -rf /'))
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())
55 class CmdHelperIterCmdOutputLinesTest(unittest
.TestCase
):
56 """Test IterCmdOutputLines with some calls to the unix 'seq' command."""
58 def testIterCmdOutputLines_success(self
):
59 for num
, line
in enumerate(
60 cmd_helper
.IterCmdOutputLines(['seq', '10']), 1):
61 self
.assertEquals(num
, int(line
))
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
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
))
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