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@123: from treepkg.run import call, 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