Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions testtools/matchers/_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from ._impl import (
Matcher,
OverrideDescription,
)


Expand All @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions testtools/matchers/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions testtools/tests/matchers/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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):

Expand All @@ -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):

Expand Down