summaryrefslogtreecommitdiff
path: root/src/tests/test_file_watcher.cc
blob: ac13afdf2b1286eb51f765f293407556a8f1e8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// test_file_watcher.cc - Unit tests for file change detection

#include "util/file_watcher.h"
#include <cstdio>
#include <fstream>
#include <unistd.h>

#if !defined(STRIP_ALL)

int main() {
  // Create a temporary test file
  const char* test_file = "/tmp/test_watcher_file.txt";
  {
    std::ofstream f(test_file);
    f << "initial content\n";
  }

  FileWatcher watcher;
  watcher.add_file(test_file);

  // Initial check - no changes yet
  bool changed = watcher.check_changes();
  if (changed) {
    fprintf(stderr, "FAIL: Expected no changes on first check\n");
    return 1;
  }

  // Sleep to ensure mtime changes (some filesystems have 1s granularity)
  sleep(1);

  // Modify the file
  {
    std::ofstream f(test_file, std::ios::app);
    f << "modified\n";
  }

  // Check for changes
  changed = watcher.check_changes();
  if (!changed) {
    fprintf(stderr, "FAIL: Expected changes after file modification\n");
    return 1;
  }

  // Reset and check again - should be no changes
  watcher.reset();
  changed = watcher.check_changes();
  if (changed) {
    fprintf(stderr, "FAIL: Expected no changes after reset\n");
    return 1;
  }

  printf("PASS: FileWatcher tests\n");
  return 0;
}

#else

int main() {
  printf("SKIP: FileWatcher tests (STRIP_ALL build)\n");
  return 0;
}

#endif