diff options
author | Lars Wirzenius <liw@liw.fi> | 2012-04-12 20:59:05 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2012-04-12 20:59:05 +0100 |
commit | 59468a2002aa26808a83fd2fb2db0e4e2c83575f (patch) | |
tree | 14c6d40184d6b2996d8adb368b2e2aef2f8f5bda | |
parent | d340738d97b7dbc2b0a0a30d05c68e85cc6033b5 (diff) | |
download | cmdtest-59468a2002aa26808a83fd2fb2db0e4e2c83575f.tar.gz |
Move cat method to cmdtestlib
-rwxr-xr-x | cmdtest | 12 | ||||
-rw-r--r-- | cmdtestlib.py | 9 | ||||
-rw-r--r-- | cmdtestlib_tests.py | 24 |
3 files changed, 35 insertions, 10 deletions
@@ -190,7 +190,8 @@ class CommandTester(cliapp.Application): if stderr_diff: errors.append(TestFailure(test, 'stderr diff:\n%s' % stderr_diff)) - expected_exit = int(self.cat(test.exit or '/dev/null').strip() or '0') + contents = cmdtestlib.cat(test.exit or '/dev/null') + expected_exit = int(contents.strip() or '0') if exit != expected_exit: errors.append(TestFailure(test, 'got exit code %s, expected %s' % @@ -204,15 +205,8 @@ class CommandTester(cliapp.Application): return errors - def cat(self, filename): - if os.path.exists(filename): - with open(filename) as f: - return f.read() - else: - return '' - def lines(self, filename): - return self.cat(filename).splitlines() + return cmdtestlib.cat(filename).splitlines() def expand(self, strings): variables = { diff --git a/cmdtestlib.py b/cmdtestlib.py index 59a3879..460ab16 100644 --- a/cmdtestlib.py +++ b/cmdtestlib.py @@ -20,6 +20,15 @@ __version__ = '0.3' import os +def cat(filename): + '''Return contents of file, or empty string if it doesn't exist.''' + if os.path.exists(filename): + with open(filename) as f: + return f.read() + else: + return '' + + class TestCase(object): def __init__(self, name, path_prefix): diff --git a/cmdtestlib_tests.py b/cmdtestlib_tests.py index a49cdad..52c72cd 100644 --- a/cmdtestlib_tests.py +++ b/cmdtestlib_tests.py @@ -14,9 +14,12 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. +import os +import shutil +import tempfile import unittest -from cmdtestlib import TestDir +from cmdtestlib import TestDir, cat class TestDirTests(unittest.TestCase): @@ -62,3 +65,22 @@ class TestDirTests(unittest.TestCase): self.assertEqual(td.find_prefixes(['setup', 'foo.setup', 'bar.script']), ['bar', 'foo']) + +class CatTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_returns_empty_string_for_nonexistent_file(self): + filename = os.path.join(self.tempdir, 'file.txt') + self.assertEqual(cat(filename), '') + + def test_returns_contents_of_file(self): + filename = os.path.join(self.tempdir, 'file.txt') + with open(filename, 'w') as f: + f.write('foobar') + self.assertEqual(cat(filename), 'foobar') + |