From 795bdcc9a734c6aa5c2a167ed37d04f76fbd28d1 Mon Sep 17 00:00:00 2001 From: William Ting Date: Tue, 31 Dec 2013 18:02:08 -0600 Subject: [PATCH] add env vars integration tests --- bin/autojump_utils.py | 2 +- tests/autojump_utils_test.py | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/bin/autojump_utils.py b/bin/autojump_utils.py index d040e95..241a0a1 100644 --- a/bin/autojump_utils.py +++ b/bin/autojump_utils.py @@ -90,7 +90,7 @@ def get_pwd(): return os.getcwdu() except OSError: print("Current directory no longer exists.", file=sys.stderr) - sys.exit(1) + raise def has_uppercase(string): diff --git a/tests/autojump_utils_test.py b/tests/autojump_utils_test.py index ea22d40..280a180 100644 --- a/tests/autojump_utils_test.py +++ b/tests/autojump_utils_test.py @@ -1,5 +1,7 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +from shutil import rmtree +from tempfile import mkdtemp import os import mock @@ -8,10 +10,13 @@ from testify import assert_equal from testify import assert_false from testify import assert_true from testify import run +from testify import setup +from testify import teardown import autojump_utils from autojump_utils import decode from autojump_utils import first +from autojump_utils import get_pwd from autojump_utils import has_uppercase from autojump_utils import in_bash from autojump_utils import last @@ -21,7 +26,7 @@ from autojump_utils import surround_quotes from autojump_utils import take -class StringTestCase(TestCase): +class StringUnitTests(TestCase): def test_decode(self): assert_equal(decode(r'blah'), u'blah') assert_equal(decode(r'日本語'), u'日本語') @@ -45,7 +50,7 @@ class StringTestCase(TestCase): assert_equal(sanitize([r'/foo/bar/', r'/']), [u'/foo/bar', u'/']) -class IterationTestCase(TestCase): +class IterationUnitTests(TestCase): def test_first(self): assert_equal(first((0, 1)), 0) assert_equal(first(()), None) @@ -66,13 +71,37 @@ class IterationTestCase(TestCase): assert_equal(list(take(10, [])), []) -class EnvironmentalVariableTestCase(TestCase): +class EnvironmentalVariableIntegrationTests(TestCase): + @setup + def create_tmp_dir(self): + self.tmp_dir = mkdtemp() + + @teardown + def delete_tmp_dir(self): + try: + rmtree(self.tmp_dir) + except OSError: + pass + def test_in_bash(self): os.environ['SHELL'] = '/bin/bash' assert_true(in_bash()) os.environ['SHELL'] = '/usr/bin/zsh' assert_false(in_bash()) + def test_good_get_pwd(self): + os.chdir(self.tmp_dir) + assert_equal(get_pwd(), self.tmp_dir) + + def test_bad_get_pwd(self): + os.chdir(self.tmp_dir) + rmtree(self.tmp_dir) + try: + get_pwd() + except OSError: + return + + assert False, "test_bad_get_pwd() failed." if __name__ == "__main__": run()