Commits

Olof Bjarnason committed d9ee4be

Added Monitor class + unit test, not yet integrated into pytddmon

Comments (0)

Files changed (2)

         log = self.test_logger.getlog(self.log_level)
         return log
 
+class Monitor:
+    def __init__(self, file_finder, get_file_size, get_file_modtime):
+        self.file_finder = file_finder
+        self.get_file_size = get_file_size
+        self.get_file_modtime = get_file_modtime
+        self.snapshot = self.get_snapshot()
+
+    def get_snapshot(self):
+        snapshot = {}
+        for file in self.file_finder():
+            file_size = self.get_file_size(file)
+            file_modtime = self.get_file_modtime(file)
+            snapshot[file] = (file_size, file_modtime)
+        return snapshot
+
+    def look_for_changes(self):
+        print "\nused to be: " + str(self.snapshot)
+        new_snapshot = self.get_snapshot()
+        change_detected = new_snapshot != self.snapshot
+        self.snapshot = new_snapshot
+        print "now is: " + str(new_snapshot)
+        return change_detected
+
 class DefaultLogger(object):
     """class that handels accumulation of logs. It also take care of tagging
     logs so that you can query for specific log messages."""

tests/test_monitor.py

+# coding: utf-8
+import unittest
+from pytddmon import Monitor
+
+class test_change_detection(unittest.TestCase):
+
+    def test_modification_time_changed(self):
+        def file_finder():
+            return ['file']
+        def get_file_size(file):
+            return 1
+        modtime = [1]
+        def get_file_modification_time(file):
+            return modtime[0]
+        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
+        modtime[0] = 2
+        change_detected = monitor.look_for_changes()
+        assert change_detected
+
+    def test_nothing_changed(self):
+        def file_finder():
+            return ['file']
+        def get_file_size(file):
+            return 1
+        def get_file_modification_time(file):
+            return 1
+        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
+        change_detected = monitor.look_for_changes()
+        assert not change_detected
+
+    def test_adding_file(self):
+        files = ['file']
+        def file_finder():
+            return files
+        def get_file_size(file):
+            return 1
+        def get_file_modification_time(file):
+            return 1
+        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
+        files.append('file2')
+        change_detected = monitor.look_for_changes()
+        assert change_detected
+
+    def test_renaming_file(self):
+        files = ['file']
+        def file_finder():
+            return files
+        def get_file_size(file):
+            return 1
+        def get_file_modification_time(file):
+            return 1
+        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
+        files[0] = 'renamed'
+        change_detected = monitor.look_for_changes()
+        assert change_detected
+
+    def test_change_is_only_detected_once(self):
+        files = ['file']
+        def file_finder():
+            return files
+        def get_file_size(file):
+            return 1
+        def get_file_modification_time(file):
+            return 1
+        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
+        files[0] = 'changed'
+        change_detected = monitor.look_for_changes()
+        change_detected = monitor.look_for_changes()
+        assert not change_detected
+
+    def test_file_size_changed(self):
+        files = ['file']
+        filesize = [1]
+        def file_finder():
+            return files
+        def get_file_size(file):
+            return filesize[0]
+        def get_file_modification_time(file):
+            return 1
+        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
+        filesize[0] = 5
+        change_detected = monitor.look_for_changes()
+        assert change_detected
+
+if __name__ == '__main__':
+    unittest.main()