File: test_watcher.py

package info (click to toggle)
python-livereload 2.7.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 348 kB
  • sloc: javascript: 1,041; python: 823; makefile: 169
file content (156 lines) | stat: -rw-r--r-- 4,632 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python

import os
import time
import shutil
import unittest
from livereload.watcher import get_watcher_class

Watcher = get_watcher_class()

tmpdir = os.path.join(os.path.dirname(__file__), 'tmp')


class TestWatcher(unittest.TestCase):

    def setUp(self):
        if os.path.isdir(tmpdir):
            shutil.rmtree(tmpdir)
        os.mkdir(tmpdir)

    def tearDown(self):
        shutil.rmtree(tmpdir)

    def test_watch_dir(self):
        os.mkdir(os.path.join(tmpdir, '.git'))
        os.mkdir(os.path.join(tmpdir, '.hg'))
        os.mkdir(os.path.join(tmpdir, '.svn'))
        os.mkdir(os.path.join(tmpdir, '.cvs'))

        watcher = Watcher()
        watcher.watch(tmpdir)
        assert watcher.is_changed(tmpdir) is False

        # sleep 1 second so that mtime will be different
        # TODO: This doesn't seem necessary; test passes without it
        time.sleep(1)

        filepath = os.path.join(tmpdir, 'foo')

        with open(filepath, 'w') as f:
            f.write('')

        assert watcher.is_changed(tmpdir)
        assert watcher.is_changed(tmpdir) is False

        os.remove(filepath)
        assert watcher.is_changed(tmpdir)
        assert watcher.is_changed(tmpdir) is False

    def test_watch_file(self):
        watcher = Watcher()
        watcher.count = 0

        # sleep 1 second so that mtime will be different
        # TODO: This doesn't seem necessary; test passes without it
        time.sleep(1)

        filepath = os.path.join(tmpdir, 'foo')
        with open(filepath, 'w') as f:
            f.write('')

        def add_count():
            watcher.count += 1

        add_count.repr_str = "add_count"

        watcher.watch(filepath, add_count)
        assert watcher.is_changed(filepath)
        assert watcher.is_changed(filepath) is False

        # sleep 1 second so that mtime will be different
        # TODO: This doesn't seem necessary; test passes without it
        time.sleep(1)

        with open(filepath, 'w') as f:
            f.write('')

        abs_filepath = os.path.abspath(filepath)
        assert watcher.examine() == (abs_filepath, None)
        assert watcher.examine() == (None, None)
        assert watcher.count == 1

        os.remove(filepath)
        assert watcher.examine() == (abs_filepath, None)
        assert watcher.examine() == (None, None)
        assert watcher.count == 2

    def test_watch_glob(self):
        watcher = Watcher()
        watcher.watch(tmpdir + '/*')
        assert watcher.examine() == (None, None)

        with open(os.path.join(tmpdir, 'foo.pyc'), 'w') as f:
            f.write('')

        assert watcher.examine() == (None, None)

        filepath = os.path.join(tmpdir, 'foo')

        with open(filepath, 'w') as f:
            f.write('')

        abs_filepath = os.path.abspath(filepath)
        assert watcher.examine() == (abs_filepath, None)
        assert watcher.examine() == (None, None)

        os.remove(filepath)
        assert watcher.examine() == (abs_filepath, None)
        assert watcher.examine() == (None, None)

    def test_watch_ignore(self):
        watcher = Watcher()
        watcher.watch(tmpdir + '/*', ignore=lambda o: o.endswith('.ignore'))
        assert watcher.examine() == (None, None)

        with open(os.path.join(tmpdir, 'foo.ignore'), 'w') as f:
            f.write('')

        assert watcher.examine() == (None, None)

    def test_watch_multiple_dirs(self):
        first_dir = os.path.join(tmpdir, 'first')
        second_dir = os.path.join(tmpdir, 'second')

        watcher = Watcher()

        os.mkdir(first_dir)
        watcher.watch(first_dir)
        assert watcher.examine() == (None, None)

        first_path = os.path.join(first_dir, 'foo')
        with open(first_path, 'w') as f:
            f.write('')
        assert watcher.examine() == (first_path, None)
        assert watcher.examine() == (None, None)

        os.mkdir(second_dir)
        watcher.watch(second_dir)
        assert watcher.examine() == (None, None)

        second_path = os.path.join(second_dir, 'bar')
        with open(second_path, 'w') as f:
            f.write('')
        assert watcher.examine() == (second_path, None)
        assert watcher.examine() == (None, None)

        # Without a sufficient wait, the mtime isn't updated...
        time.sleep(1)
        with open(first_path, 'a') as f:
            f.write('foo')
        assert watcher.examine() == (first_path, None)
        assert watcher.examine() == (None, None)

        os.remove(second_path)
        assert watcher.examine() == (second_path, None)
        assert watcher.examine() == (None, None)