Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added class EmptyDirectorySnapshot to fix issue #612. #613

Merged
merged 7 commits into from
Jan 19, 2020
Merged
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
4 changes: 2 additions & 2 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Changelog
Other Changes
=============

-
- Thanks to our beloved contributors:
- [snapshot] Added EmptyDirectorySnapshot (`#613 <https://github.com/gorakhargosh/watchdog/pull/613>`__)
- Thanks to our beloved contributors: @Ajordat


0.10.0
Expand Down
31 changes: 31 additions & 0 deletions src/watchdog/utils/dirsnapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
:members:
:show-inheritance:

.. autoclass:: DirectorySnapshotEmpty
:members:
:show-inheritance:

"""

import errno
Expand Down Expand Up @@ -347,3 +351,30 @@ def __str__(self):

def __repr__(self):
return str(self._stat_info)


class EmptyDirectorySnapshot(object):
"""Class to implement an empty snapshot. This is used together with
DirectorySnapshot and DirectorySnapshotDiff in order to get all the files/folders
in the directory as created.
"""

@staticmethod
def path(_):
"""Mock up method to return the path of the received inode. As the snapshot
is intended to be empty, it always returns None.

:returns:
None.
"""
return None

@property
def paths(self):
"""Mock up method to return a set of file/directory paths in the snapshot. As
the snapshot is intended to be empty, it always returns an empty set.

:returns:
An empty set.
"""
return set()
17 changes: 17 additions & 0 deletions tests/test_snapshot_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from watchdog.utils.dirsnapshot import DirectorySnapshot
from watchdog.utils.dirsnapshot import DirectorySnapshotDiff
from watchdog.utils.dirsnapshot import EmptyDirectorySnapshot
from watchdog.utils import platform

from .shell import mkdir, touch, mv, rm
Expand Down Expand Up @@ -200,3 +201,19 @@ def inode(self, path):
diff_without_device = DirectorySnapshotDiff(ref, snapshot, ignore_device=True)
assert diff_without_device.files_deleted == []
assert diff_without_device.files_created == []


def test_empty_snapshot(p):
# Create a file and declare a DirectorySnapshot and a DirectorySnapshotEmpty.
# When we make the diff, although both objects were declared with the same items on
# the directory, the file and directories created BEFORE the DirectorySnapshot will
# be detected as newly created.

touch(p('a'))
mkdir(p('b', 'c'), parents=True)
ref = DirectorySnapshot(p(''))
empty = EmptyDirectorySnapshot()

diff = DirectorySnapshotDiff(empty, ref)
assert diff.files_created == [p('a')]
assert sorted(diff.dirs_created) == sorted([p(''), p('b'), p('b', 'c')])