1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
from subprocess import CalledProcessError
7
from helpers import command
10
class TestCommand(unittest.TestCase):
12
def test_simple_command(self):
13
# Creating a simple command (ls) works and running the command
15
ls = command('/bin/ls')
16
self.assertIsInstance(ls(), str)
18
def test_arguments(self):
19
# Arguments can be passed to commands.
20
ls = command('/bin/ls')
21
self.assertIn('Usage:', ls('--help'))
23
def test_missing_executable(self):
24
# If the command does not exist, an OSError (No such file or
25
# directory) is raised.
26
bad = command('this command does not exist')
27
with self.assertRaises(OSError) as info:
29
self.assertEqual(2, info.exception.errno)
32
# If the command returns a non-zero exit code, an exception is raised.
33
ls = command('/bin/ls')
34
with self.assertRaises(CalledProcessError):
35
ls('--not a valid switch')
37
def test_baked_in_arguments(self):
38
# Arguments can be passed when creating the command as well as when
40
ll = command('/bin/ls', '-l')
41
self.assertIn('rw', ll()) # Assumes a file is r/w in the pwd.
42
self.assertIn('Usage:', ll('--help'))
44
def test_quoting(self):
45
# There is no need to quote special shell characters in commands.
46
ls = command('/bin/ls')
50
if __name__ == '__main__':