|
| 1 | +from checkbox_support.helpers.file_watcher import FileWatcher, InotifyEvent |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | +import unittest |
| 4 | + |
| 5 | + |
| 6 | +class TestInotifyEvent(unittest.TestCase): |
| 7 | + """ |
| 8 | + Test the InotifyEvent data class. |
| 9 | + """ |
| 10 | + |
| 11 | + def test_inotify_event_creation(self): |
| 12 | + event = InotifyEvent( |
| 13 | + wd=1, event_type="modify", cookie=0, name="test_file.txt" |
| 14 | + ) |
| 15 | + self.assertEqual(event.wd, 1) |
| 16 | + self.assertEqual(event.event_type, "modify") |
| 17 | + self.assertEqual(event.cookie, 0) |
| 18 | + self.assertEqual(event.name, "test_file.txt") |
| 19 | + |
| 20 | + |
| 21 | +class TestFileWatcher(unittest.TestCase): |
| 22 | + """ |
| 23 | + Test the FileWatcher class by mocking system-level calls. |
| 24 | + """ |
| 25 | + |
| 26 | + @patch.object(FileWatcher, "libc") |
| 27 | + def test_initialization_success(self, mock_libc): |
| 28 | + mock_libc.inotify_init.return_value = 123 |
| 29 | + watcher = FileWatcher() |
| 30 | + self.assertEqual(watcher.fd, 123) |
| 31 | + |
| 32 | + @patch.object(FileWatcher, "libc") |
| 33 | + def test_initialization_failure(self, mock_libc): |
| 34 | + mock_libc.inotify_init.return_value = -1 |
| 35 | + with self.assertRaises(SystemExit) as cm: |
| 36 | + FileWatcher() |
| 37 | + self.assertEqual(cm.exception.code, "Failed to initialize inotify") |
| 38 | + |
| 39 | + @patch.object(FileWatcher, "libc") |
| 40 | + def test_watch_directory_with_single_event(self, mock_libc): |
| 41 | + mock_libc.inotify_init.return_value = 123 |
| 42 | + watcher = FileWatcher() |
| 43 | + |
| 44 | + test_path = "/tmp/test_dir" |
| 45 | + mock_watch_desc = 456 |
| 46 | + mock_libc.inotify_add_watch.return_value = mock_watch_desc |
| 47 | + |
| 48 | + wd = watcher.watch_directory(test_path, "m") |
| 49 | + mock_libc.inotify_add_watch.assert_called_with( |
| 50 | + 123, test_path.encode("utf-8"), 0x00000002 |
| 51 | + ) |
| 52 | + self.assertEqual(wd, mock_watch_desc) |
| 53 | + |
| 54 | + @patch.object(FileWatcher, "libc") |
| 55 | + def test_watch_directory_with_multiple_events(self, mock_libc): |
| 56 | + mock_libc.inotify_init.return_value = 123 |
| 57 | + watcher = FileWatcher() |
| 58 | + |
| 59 | + test_path = "/tmp/test_dir" |
| 60 | + mock_watch_desc = 789 |
| 61 | + mock_libc.inotify_add_watch.return_value = mock_watch_desc |
| 62 | + |
| 63 | + wd = watcher.watch_directory(test_path, "dmc") |
| 64 | + expected_mask = 0x00000200 | 0x00000002 | 0x00000100 |
| 65 | + mock_libc.inotify_add_watch.assert_called_with( |
| 66 | + 123, test_path.encode("utf-8"), expected_mask |
| 67 | + ) |
| 68 | + self.assertEqual(wd, mock_watch_desc) |
| 69 | + |
| 70 | + @patch.object(FileWatcher, "libc") |
| 71 | + def test_stop_watch(self, mock_libc): |
| 72 | + mock_libc.inotify_init.return_value = 123 |
| 73 | + watcher = FileWatcher() |
| 74 | + |
| 75 | + mock_watch_desc = 1011 |
| 76 | + watcher.stop_watch(mock_watch_desc) |
| 77 | + mock_libc.inotify_rm_watch.assert_called_with(123, mock_watch_desc) |
| 78 | + |
| 79 | + def test_mask2event(self): |
| 80 | + watcher = FileWatcher() |
| 81 | + self.assertEqual(watcher._mask2event(0x00000002), "modify") |
| 82 | + self.assertEqual(watcher._mask2event(0x00000100), "create") |
| 83 | + self.assertEqual(watcher._mask2event(0x00000200), "delete") |
| 84 | + self.assertEqual(watcher._mask2event(0x0000FFFF), "unknown") |
| 85 | + |
| 86 | + @patch("checkbox_support.helpers.file_watcher.os.read") |
| 87 | + @patch("checkbox_support.helpers.file_watcher.InotifyEvent") |
| 88 | + @patch.object(FileWatcher, "libc") |
| 89 | + def test_read_events_single_event( |
| 90 | + self, mock_libc, mock_inotify_event, mock_os_read |
| 91 | + ): |
| 92 | + mock_libc.inotify_init.return_value = 123 |
| 93 | + watcher = FileWatcher() |
| 94 | + |
| 95 | + mock_os_read.return_value = b"\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00filename\x00" |
| 96 | + |
| 97 | + mock_event_instance = MagicMock() |
| 98 | + mock_inotify_event.return_value = mock_event_instance |
| 99 | + |
| 100 | + events = watcher.read_events(1024) |
| 101 | + |
| 102 | + mock_os_read.assert_called_with(watcher.fd, 1024) |
| 103 | + |
| 104 | + self.assertEqual(len(events), 1) |
| 105 | + |
| 106 | + mock_inotify_event.assert_called_with(1, "modify", 0, "filename") |
| 107 | + |
| 108 | + self.assertEqual(events[0], mock_event_instance) |
| 109 | + |
| 110 | + @patch("checkbox_support.helpers.file_watcher.os.read") |
| 111 | + @patch("checkbox_support.helpers.file_watcher.InotifyEvent") |
| 112 | + @patch.object(FileWatcher, "libc") |
| 113 | + def test_read_events_multiple_events( |
| 114 | + self, mock_libc, mock_inotify_event, mock_os_read |
| 115 | + ): |
| 116 | + # Mock multiple events in a single read |
| 117 | + mock_libc.inotify_init.return_value = 123 |
| 118 | + watcher = FileWatcher() |
| 119 | + |
| 120 | + # Mock data for two events |
| 121 | + mock_os_read.return_value = ( |
| 122 | + b"\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00file_one\x00" |
| 123 | + + b"\x02\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00file_two\x00" |
| 124 | + ) |
| 125 | + |
| 126 | + events = watcher.read_events(1024) |
| 127 | + |
| 128 | + self.assertEqual(len(events), 2) |
| 129 | + mock_inotify_event.assert_any_call(1, "modify", 0, "file_one") |
| 130 | + mock_inotify_event.assert_any_call(2, "delete", 0, "file_two") |
| 131 | + |
| 132 | + @patch( |
| 133 | + "checkbox_support.helpers.file_watcher.os.read", |
| 134 | + side_effect=KeyboardInterrupt, |
| 135 | + ) |
| 136 | + @patch.object(FileWatcher, "stop_watch") |
| 137 | + @patch.object(FileWatcher, "libc") |
| 138 | + def test_read_events_keyboard_interrupt( |
| 139 | + self, mock_libc, mock_stop_watch, mock_os_read |
| 140 | + ): |
| 141 | + mock_libc.inotify_init.return_value = 123 |
| 142 | + watcher = FileWatcher() |
| 143 | + |
| 144 | + watcher.read_events(1024) |
| 145 | + mock_stop_watch.assert_called_with(watcher.fd) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + unittest.main() |
0 commit comments