Skip to content

Commit

Permalink
Add the EmptyDirectorySnapshot class (fixes #612)
Browse files Browse the repository at this point in the history
* Added DirectorySnapshotEmpty (#612).

* Added test to show (and test) the usage of DirectorySnapshotEmpty (#612).

* Added sphinx class for DirectorySnapshotEmpty.

* Changed class name from DirectorySnapshotEmpty to EmptyDirectorySnapshot.

* Added documentation.

* Small doc fix.

* Updated changelog.rst.
  • Loading branch information
Àlex Jordà Triginer authored and BoboTiG committed Jan 19, 2020
1 parent 28b75d0 commit 5ee9a38
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
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')])

0 comments on commit 5ee9a38

Please sign in to comment.