bh@123: # Copyright (C) 2008 by Intevation GmbH bh@123: # Authors: bh@123: # Bernhard Herzog bh@123: # bh@123: # This program is free software under the GPL (>=v2) bh@123: # Read the file COPYING coming with the software for details. bh@123: bh@123: """Tests for treepkg.run""" bh@123: bh@123: import sys bh@123: import os bh@123: import unittest bh@123: bh@193: from filesupport import FileTestMixin bh@193: bh@165: from treepkg.run import call, capture_output, SubprocessError bh@123: bh@123: bh@123: bh@123: class TestCall(unittest.TestCase): bh@123: bh@123: def test_call_defaults(self): bh@123: call([sys.executable, "-c", "pass"]) bh@123: bh@123: def test_call_error(self): bh@123: """Test call with a subprocess that exits with a non-zero exit code""" bh@123: try: bh@123: call([sys.executable, "-c", "import sys; sys.exit(1)"]) bh@123: except SubprocessError, exc: bh@123: self.assertEquals(exc.returncode, 1) bh@123: else: bh@123: self.fail("call did not raise an exception") bh@124: bh@124: def test_call_extra_env(self): bh@124: """Test call with the extra_env parameter""" bh@124: subprocess_cmd = [sys.executable, "-c", bh@124: "import sys, os;" bh@124: "value = os.environ.get('TREEPKG_TEST');" bh@124: "sys.exit(int(value != 'xyzzy'))" bh@124: ] bh@124: # sanity check that the extra environment variable we use for bh@124: # the test is not set yet bh@124: self.assertRaises(SubprocessError, call, subprocess_cmd) bh@124: bh@124: # the actual test bh@124: try: bh@124: call(subprocess_cmd, extra_env=dict(TREEPKG_TEST="xyzzy")) bh@124: except SubprocessError, exc: bh@124: self.fail("the extra_env variables were not set properly") bh@124: else: bh@124: # test OK bh@124: pass bh@164: bh@164: def test_call_inputdata(self): bh@164: """Test call with inputdata passed to the subprocesses stdin""" bh@164: # FIXME: If the feature being tested is not implemented bh@164: # properly, it's likely that the subprocess will wait bh@164: # indefinitely waiting for input. bh@164: data = "1" bh@164: subprocess_cmd = [sys.executable, "-c", bh@164: "import sys; sys.exit(int(raw_input("")))"] bh@164: try: bh@164: call(subprocess_cmd, inputdata=data) bh@164: except SubprocessError, exc: bh@164: self.assertEquals(exc.returncode, 1) bh@165: bh@193: bh@193: class TestRunWithLogging(unittest.TestCase, FileTestMixin): bh@193: bh@193: def test_run_with_logfile(self): bh@193: logfilename = self.temp_file_name("logfile") bh@193: call([sys.executable, "-c", "print \"I'm a lumber jack and I'm OK\""], bh@193: logfile=logfilename) bh@195: self.check_file_contents(logfilename, "I'm a lumber jack and I'm OK\n") bh@193: bh@193: bh@165: class TestCaptureOutput(unittest.TestCase): bh@165: bh@165: def test_capture_output_stdout(self): bh@165: text = "explicit is better than implicit" bh@165: self.assertEquals(capture_output([sys.executable, "-c", bh@165: "print %r" % text]), bh@165: text + "\n") bh@165: bh@165: def test_capture_output_stderr(self): bh@165: self.assertEquals(capture_output([sys.executable, "-c", bh@165: "import sys;" bh@165: "print 'on stdout';" bh@165: " sys.stdout.flush();" bh@165: "print >>sys.stderr, 'on stderr'"]), bh@165: "on stdout\non stderr\n") bh@165: bh@165: def test_capture_output_exception(self): bh@165: try: bh@165: capture_output([sys.executable, "-c", bh@165: "import sys;" bh@165: "print 'Beautiful is better than ugly';" bh@165: " sys.exit(1)"]) bh@165: except SubprocessError, exc: bh@165: self.assertEquals(exc.returncode, 1) bh@165: self.assertEquals(exc.output, "Beautiful is better than ugly\n")