diff --git a/testtools/matchers/_filesystem.py b/testtools/matchers/_filesystem.py index 54f749b1..e387ff55 100644 --- a/testtools/matchers/_filesystem.py +++ b/testtools/matchers/_filesystem.py @@ -22,6 +22,7 @@ ) from ._impl import ( Matcher, + OverrideDescription, ) @@ -32,23 +33,29 @@ 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(): """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(): """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/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..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): @@ -74,6 +77,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): @@ -96,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):