1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
# Copyright (c) 2015 Yann Lanthony |
4
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
5
|
|
|
# |
6
|
|
|
# Licensed under the terms of the MIT License |
7
|
|
|
# (See LICENSE.txt for details) |
8
|
|
|
# ----------------------------------------------------------------------------- |
9
|
|
|
"""Test qtsass cli.""" |
10
|
|
|
|
11
|
|
|
from __future__ import absolute_import |
12
|
|
|
|
13
|
|
|
# Standard library imports |
14
|
|
|
import time |
15
|
|
|
import os |
16
|
|
|
import shutil |
17
|
|
|
from os.path import dirname, exists |
18
|
|
|
|
19
|
|
|
# Third party imports |
20
|
|
|
import pytest |
21
|
|
|
|
22
|
|
|
#Local imports |
23
|
|
|
from qtsass import compile_filename |
24
|
|
|
from qtsass.watchers import PollingWatcher, QtWatcher |
25
|
|
|
|
26
|
|
|
from . import EXAMPLES_DIR, example, touch, await_condition |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class CallCounter(object): |
30
|
|
|
|
31
|
|
|
def __init__(self): |
32
|
|
|
self.count = 0 |
33
|
|
|
|
34
|
|
|
def __call__(self, *args, **kwargs): |
35
|
|
|
self.count += 1 |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
@pytest.mark.parametrize( |
39
|
|
|
'Watcher', (PollingWatcher, QtWatcher), |
40
|
|
|
) |
41
|
|
|
def test_watchers(Watcher, tmpdir): |
42
|
|
|
"""Stress test Watcher implementations""" |
43
|
|
|
|
44
|
|
|
# Skip when QtWatcher is None - when Qt is not installed. |
45
|
|
|
if not Watcher: |
46
|
|
|
return |
47
|
|
|
|
48
|
|
|
watch_dir = tmpdir.join('src').strpath |
49
|
|
|
os.makedirs(watch_dir) |
50
|
|
|
shutil.copy2(example('dummy.scss'), watch_dir) |
51
|
|
|
input = tmpdir.join('src/dummy.scss').strpath |
52
|
|
|
output = tmpdir.join('build/dummy.css').strpath |
53
|
|
|
output_exists = lambda: exists(output) |
|
|
|
|
54
|
|
|
|
55
|
|
|
c = CallCounter() |
56
|
|
|
w = Watcher( |
57
|
|
|
watch_dir=watch_dir, |
58
|
|
|
compiler=compile_filename, |
59
|
|
|
args=(input, output), |
60
|
|
|
) |
61
|
|
|
w.connect(c) |
62
|
|
|
|
63
|
|
|
# Output should not yet exist |
64
|
|
|
assert not exists(output) |
65
|
|
|
|
66
|
|
|
w.start() |
67
|
|
|
|
68
|
|
|
touch(input) |
69
|
|
|
time.sleep(0.5) |
70
|
|
|
if not await_condition(output_exists): |
71
|
|
|
assert False, 'Output file not created...' |
72
|
|
|
|
73
|
|
|
# Removing the watch_dir should not kill the Watcher |
74
|
|
|
# simply stop dispatching callbacks |
75
|
|
|
shutil.rmtree(watch_dir) |
76
|
|
|
time.sleep(0.5) |
77
|
|
|
assert c.count == 1 |
78
|
|
|
|
79
|
|
|
# Watcher should recover once the input file is there again |
80
|
|
|
os.makedirs(watch_dir) |
81
|
|
|
shutil.copy2(example('dummy.scss'), watch_dir) |
82
|
|
|
time.sleep(0.5) |
83
|
|
|
assert c.count == 2 |
84
|
|
|
|
85
|
|
|
# Stop watcher |
86
|
|
|
w.stop() |
87
|
|
|
w.join() |
88
|
|
|
|
89
|
|
|
for _ in range(5): |
90
|
|
|
touch(input) |
91
|
|
|
|
92
|
|
|
# Count should not change |
93
|
|
|
assert c.count == 2 |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
def test_qtwatcher(tmpdir): |
97
|
|
|
"""Test QtWatcher implementation""" |
98
|
|
|
|
99
|
|
|
# Skip when QtWatcher is None - When Qt is not installed |
100
|
|
|
if not QtWatcher: |
101
|
|
|
return |
102
|
|
|
|
103
|
|
|
# Constructing a QApplication will cause the QtWatcher constructed |
104
|
|
|
# below to use a Signal to dispatch callbacks. |
105
|
|
|
from qtsass.watchers.qt import QApplication |
106
|
|
|
|
107
|
|
|
qt_app = QApplication.instance() |
108
|
|
|
if not qt_app: |
109
|
|
|
qt_app = QApplication([]) |
110
|
|
|
|
111
|
|
|
watch_dir = tmpdir.join('src').strpath |
112
|
|
|
os.makedirs(watch_dir) |
113
|
|
|
shutil.copy2(example('dummy.scss'), watch_dir) |
114
|
|
|
input = tmpdir.join('src/dummy.scss').strpath |
115
|
|
|
output = tmpdir.join('build/dummy.css').strpath |
116
|
|
|
output_exists = lambda: exists(output) |
|
|
|
|
117
|
|
|
|
118
|
|
|
c = CallCounter() |
119
|
|
|
w = QtWatcher( |
120
|
|
|
watch_dir=watch_dir, |
121
|
|
|
compiler=compile_filename, |
122
|
|
|
args=(input, output), |
123
|
|
|
) |
124
|
|
|
# We connect a counter directly to the Watcher's Qt Signal in order to |
125
|
|
|
# verify that the Watcher is actually using a Qt Signal. |
126
|
|
|
w.qtdispatcher.signal.connect(c) |
127
|
|
|
w.start() |
128
|
|
|
|
129
|
|
|
touch(input) |
130
|
|
|
time.sleep(0.5) |
131
|
|
|
if not await_condition(output_exists, qt_app=qt_app): |
132
|
|
|
assert False, 'Output file not created...' |
133
|
|
|
assert c.count == 1 |
134
|
|
|
|
135
|
|
|
# Stop watcher |
136
|
|
|
w.stop() |
137
|
|
|
w.join() |
138
|
|
|
|