Back to index

python-cliapp  1.20120630
runcmd_tests.py
Go to the documentation of this file.
00001 # Copyright (C) 2011  Lars Wirzenius
00002 # 
00003 # This program is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License along
00014 # with this program; if not, write to the Free Software Foundation, Inc.,
00015 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 
00017 
00018 import optparse
00019 import os
00020 import StringIO
00021 import sys
00022 import tempfile
00023 import unittest
00024 
00025 import cliapp
00026 
00027 
00028 def devnull(msg):
00029     pass
00030 
00031 
00032 class ApplicationTests(unittest.TestCase):
00033 
00034     def test_runcmd_executes_true(self):
00035         self.assertEqual(cliapp.runcmd(['true']), '')
00036     
00037     def test_runcmd_raises_error_on_failure(self):
00038         self.assertRaises(cliapp.AppException, cliapp.runcmd, ['false'])
00039     
00040     def test_runcmd_returns_stdout_of_command(self):
00041         self.assertEqual(cliapp.runcmd(['echo', 'hello', 'world']),
00042                          'hello world\n')
00043     
00044     def test_runcmd_returns_stderr_of_command(self):
00045         exit, out, err = cliapp.runcmd_unchecked(['ls', 'notexist'])
00046         self.assertNotEqual(exit, 0)
00047         self.assertEqual(out, '')
00048         self.assertNotEqual(err, '')
00049 
00050     def test_runcmd_pipes_stdin_through_command(self):
00051         self.assertEqual(cliapp.runcmd(['cat'], feed_stdin='hello, world'),
00052                          'hello, world')
00053 
00054     def test_runcmd_pipes_stdin_through_two_commands(self):
00055         self.assertEqual(cliapp.runcmd(
00056                             ['cat'], ['cat'], feed_stdin='hello, world'),
00057                          'hello, world')
00058 
00059     def test_runcmd_pipes_stdin_through_command_with_lots_of_data(self):
00060         data = 'x' * (1024**2)
00061         self.assertEqual(cliapp.runcmd(['cat'], feed_stdin=data), data)
00062 
00063     def test_runcmd_ignores_failures_on_request(self):
00064         self.assertEqual(cliapp.runcmd(['false'], ignore_fail=True), '')
00065 
00066     def test_runcmd_obeys_cwd(self):
00067         self.assertEqual(cliapp.runcmd(['pwd'], cwd='/'), '/\n')
00068 
00069     def test_runcmd_unchecked_returns_values_on_success(self):
00070         self.assertEqual(cliapp.runcmd_unchecked(['echo', 'foo']), 
00071                          (0, 'foo\n', ''))
00072 
00073     def test_runcmd_unchecked_returns_values_on_failure(self):
00074         self.assertEqual(cliapp.runcmd_unchecked(['false']), 
00075                          (1, '', ''))
00076 
00077     def test_runcmd_unchecked_runs_simple_pipeline(self):
00078         self.assertEqual(cliapp.runcmd_unchecked(
00079                             ['echo', 'foo'], ['wc', '-c']),
00080                          (0, '4\n', ''))
00081 
00082     def test_runcmd_unchecked_runs_longer_pipeline(self):
00083         self.assertEqual(cliapp.runcmd_unchecked(['echo', 'foo'], 
00084                                                  ['cat'],
00085                                                  ['wc', '-c']),
00086                          (0, '4\n', ''))
00087 
00088     def test_runcmd_redirects_stdin_from_file(self):
00089         fd, filename = tempfile.mkstemp()
00090         os.write(fd, 'foobar')
00091         os.lseek(fd, 0, os.SEEK_SET)
00092         self.assertEqual(cliapp.runcmd_unchecked(['cat'], stdin=fd),
00093                          (0, 'foobar', ''))
00094         os.close(fd)
00095                             
00096     def test_runcmd_redirects_stdout_to_file(self):
00097         fd, filename = tempfile.mkstemp()
00098         exit, out, err = cliapp.runcmd_unchecked(['echo', 'foo'], stdout=fd)
00099         os.close(fd)
00100         with open(filename) as f:
00101             data = f.read()
00102         self.assertEqual(exit, 0)
00103         self.assertEqual(data, 'foo\n')
00104                             
00105     def test_runcmd_redirects_stderr_to_file(self):
00106         fd, filename = tempfile.mkstemp()
00107         exit, out, err = cliapp.runcmd_unchecked(['ls', 'notexist'], stderr=fd)
00108         os.close(fd)
00109         with open(filename) as f:
00110             data = f.read()
00111         self.assertNotEqual(exit, 0)
00112         self.assertNotEqual(data, '')
00113 
00114