From ed64155c754ea07b260f5024c46128a82211a092 Mon Sep 17 00:00:00 2001 From: Thomi Richards Date: Fri, 15 Apr 2016 15:57:25 +1200 Subject: [PATCH 1/2] Initial proof-of-concept to improve the output of filing matchers. --- testtools/matchers/_filesystem.py | 11 +++++++---- testtools/matchers/_impl.py | 13 +++++++++++++ testtools/tests/matchers/test_filesystem.py | 3 +++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/testtools/matchers/_filesystem.py b/testtools/matchers/_filesystem.py index 54f749b1..033e2655 100644 --- a/testtools/matchers/_filesystem.py +++ b/testtools/matchers/_filesystem.py @@ -22,6 +22,7 @@ ) from ._impl import ( Matcher, + OverrideDescription, ) @@ -37,10 +38,12 @@ def PathExists(): def DirExists(): """Matches if the path exists and is a directory.""" - return MatchesAll( - PathExists(), - MatchesPredicate(os.path.isdir, "%s is not a directory."), - first_only=True) + return OverrideDescription( + 'DirExists()', + MatchesAll( + PathExists(), + MatchesPredicate(os.path.isdir, "%s is not a directory."), + first_only=True)) def FileExists(): diff --git a/testtools/matchers/_impl.py b/testtools/matchers/_impl.py index 19a93af7..b43be0ac 100644 --- a/testtools/matchers/_impl.py +++ b/testtools/matchers/_impl.py @@ -168,6 +168,19 @@ def get_details(self): return self.original.get_details() +class OverrideDescription(object): + + def __init__(self, description, wrapped): + self._description = description + self._wrapped = wrapped + + def __getattr__(self, attr): + return getattr(self._wrapped, attr) + + def __str__(self): + return self._description + + # Signal that this is part of the testing framework, and that code from this # should not normally appear in tracebacks. __unittest = True diff --git a/testtools/tests/matchers/test_filesystem.py b/testtools/tests/matchers/test_filesystem.py index 917ff2ed..0b7f0244 100644 --- a/testtools/tests/matchers/test_filesystem.py +++ b/testtools/tests/matchers/test_filesystem.py @@ -74,6 +74,9 @@ def test_not_a_directory(self): self.assertThat( "%s is not a directory." % filename, Equals(mismatch.describe())) + def test__str__(self): + self.assertEqual("DirExists()", str(DirExists())) + class TestFileExists(TestCase, PathHelpers): From 15593cf9b142d7b7879cd8ecb3cea963d847a3a9 Mon Sep 17 00:00:00 2001 From: Thomi Richards Date: Sun, 1 May 2016 12:08:53 +1200 Subject: [PATCH 2/2] Fix PathExists, DirExists & FileExists matchers to have a more helpful __str__. --- testtools/matchers/_filesystem.py | 14 +++++++++----- testtools/tests/matchers/test_filesystem.py | 6 ++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/testtools/matchers/_filesystem.py b/testtools/matchers/_filesystem.py index 033e2655..e387ff55 100644 --- a/testtools/matchers/_filesystem.py +++ b/testtools/matchers/_filesystem.py @@ -33,7 +33,9 @@ def PathExists(): assertThat('/some/path', PathExists()) """ - return MatchesPredicate(os.path.exists, "%s does not exist.") + return OverrideDescription( + 'PathExists()', + MatchesPredicate(os.path.exists, "%s does not exist.")) def DirExists(): @@ -48,10 +50,12 @@ def DirExists(): def FileExists(): """Matches if the given path exists and is a file.""" - return MatchesAll( - PathExists(), - MatchesPredicate(os.path.isfile, "%s is not a file."), - first_only=True) + return OverrideDescription( + "FileExists()", + MatchesAll( + PathExists(), + MatchesPredicate(os.path.isfile, "%s is not a file."), + first_only=True)) class DirContains(Matcher): diff --git a/testtools/tests/matchers/test_filesystem.py b/testtools/tests/matchers/test_filesystem.py index 0b7f0244..36125eaf 100644 --- a/testtools/tests/matchers/test_filesystem.py +++ b/testtools/tests/matchers/test_filesystem.py @@ -53,6 +53,9 @@ def test_not_exists(self): self.assertThat( "%s does not exist." % doesntexist, Equals(mismatch.describe())) + def test__str__(self): + self.assertEqual("PathExists()", str(PathExists())) + class TestDirExists(TestCase, PathHelpers): @@ -99,6 +102,9 @@ def test_not_a_file(self): self.assertThat( "%s is not a file." % tempdir, Equals(mismatch.describe())) + def test__str__(self): + self.assertEqual("FileExists()", str(FileExists())) + class TestDirContains(TestCase, PathHelpers):