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
|
|
|
# Standard library imports |
12
|
|
|
from __future__ import absolute_import |
13
|
|
|
import os |
14
|
|
|
import time |
15
|
|
|
import signal |
|
|
|
|
16
|
|
|
import sys |
17
|
|
|
from os.path import join, dirname, normpath, exists, basename |
|
|
|
|
18
|
|
|
from textwrap import dedent |
|
|
|
|
19
|
|
|
from subprocess import Popen, PIPE |
20
|
|
|
from collections import namedtuple |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
PY3 = sys.version_info.major == 3 |
24
|
|
|
PROJECT_DIR = normpath(dirname(dirname(__file__))) |
25
|
|
|
Result = namedtuple('Result', "code stdout stderr") |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def example(*paths): |
29
|
|
|
"""Get path to an example.""" |
30
|
|
|
|
31
|
|
|
return normpath(join(dirname(__file__), '..', 'examples', *paths)) |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def touch(file): |
35
|
|
|
"""Touch a file.""" |
36
|
|
|
|
37
|
|
|
with open(file, 'a') as f: |
|
|
|
|
38
|
|
|
os.utime(file, None) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def await_condition(condition, timeout=2000): |
42
|
|
|
"""Return True if a condition is met in the given timeout period""" |
43
|
|
|
|
44
|
|
|
for _ in range(timeout): |
45
|
|
|
if condition(): |
46
|
|
|
return True |
47
|
|
|
time.sleep(0.001) |
48
|
|
|
return False |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def invoke(args): |
52
|
|
|
"""Invoke qtsass cli with specified args""" |
53
|
|
|
|
54
|
|
|
kwargs = dict( |
55
|
|
|
stdout=PIPE, |
56
|
|
|
stderr=PIPE, |
57
|
|
|
cwd=PROJECT_DIR |
58
|
|
|
) |
59
|
|
|
proc = Popen(['python', '-m', 'qtsass'] + args, **kwargs) |
60
|
|
|
return proc |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def invoke_with_result(args): |
64
|
|
|
"""Invoke qtsass cli and return a Result obj""" |
65
|
|
|
|
66
|
|
|
proc = invoke(args) |
67
|
|
|
out, err = proc.communicate() |
68
|
|
|
out = out.decode('ascii', errors="ignore") |
69
|
|
|
err = err.decode('ascii', errors="ignore") |
70
|
|
|
return Result(proc.returncode, out, err) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
def test_compile_dummy_to_stdout(): |
74
|
|
|
"""CLI compile dummy example to stdout.""" |
75
|
|
|
|
76
|
|
|
args = [example('dummy.scss')] |
77
|
|
|
result = invoke_with_result(args) |
78
|
|
|
|
79
|
|
|
assert result.code == 0 |
80
|
|
|
assert result.stdout |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def test_compile_dummy_to_file(tmpdir): |
84
|
|
|
"""CLI compile dummy example to file.""" |
85
|
|
|
|
86
|
|
|
input = example('dummy.scss') |
|
|
|
|
87
|
|
|
output = tmpdir.join('dummy.css') |
88
|
|
|
args = [input, '-o', output.strpath] |
89
|
|
|
result = invoke_with_result(args) |
90
|
|
|
|
91
|
|
|
assert result.code == 0 |
92
|
|
|
assert exists(output.strpath) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_watch_dummy(tmpdir): |
96
|
|
|
"""CLI watch dummy example.""" |
97
|
|
|
|
98
|
|
|
input = example('dummy.scss') |
|
|
|
|
99
|
|
|
output = tmpdir.join('dummy.css') |
100
|
|
|
args = [input, '-o', output.strpath, '-w'] |
101
|
|
|
proc = invoke(args) |
102
|
|
|
|
103
|
|
|
# Wait for initial compile |
104
|
|
|
output_exists = lambda: exists(output.strpath) |
105
|
|
|
if not await_condition(output_exists): |
106
|
|
|
proc.terminate() |
107
|
|
|
assert False, "Failed to compile dummy.scss" |
108
|
|
|
|
109
|
|
|
# Ensure subprocess is still alive |
110
|
|
|
assert proc.poll() is None |
111
|
|
|
|
112
|
|
|
# Touch input file, triggering a recompile |
113
|
|
|
created = output.mtime() |
114
|
|
|
file_modified = lambda: output.mtime() > created |
115
|
|
|
time.sleep(0.1) |
116
|
|
|
touch(input) |
117
|
|
|
|
118
|
|
|
if not await_condition(file_modified): |
119
|
|
|
proc.terminate() |
120
|
|
|
assert False, 'Output file has not been recompiled...' |
121
|
|
|
|
122
|
|
|
proc.terminate() |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
def test_compile_complex(tmpdir): |
126
|
|
|
"""CLI compile complex example.""" |
127
|
|
|
|
128
|
|
|
input = example('complex') |
|
|
|
|
129
|
|
|
output = tmpdir.mkdir('output') |
130
|
|
|
args = [input, '-o', output.strpath] |
131
|
|
|
result = invoke_with_result(args) |
132
|
|
|
|
133
|
|
|
assert result.code == 0 |
134
|
|
|
|
135
|
|
|
expected_files = [output.join('light.css'), output.join('dark.css')] |
136
|
|
|
for file in expected_files: |
137
|
|
|
assert exists(file.strpath) |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
def test_watch_complex(tmpdir): |
141
|
|
|
"""CLI watch complex example.""" |
142
|
|
|
|
143
|
|
|
input = example('complex') |
|
|
|
|
144
|
|
|
output = tmpdir.mkdir('output') |
145
|
|
|
args = [input, '-o', output.strpath, '-w'] |
146
|
|
|
proc = invoke(args) |
147
|
|
|
|
148
|
|
|
expected_files = [output.join('light.css'), output.join('dark.css')] |
149
|
|
|
|
150
|
|
|
# Wait for initial compile |
151
|
|
|
files_created = lambda: all([exists(f.strpath) for f in expected_files]) |
152
|
|
|
if not await_condition(files_created): |
153
|
|
|
assert False, 'All expected files have not been created...' |
154
|
|
|
|
155
|
|
|
# Ensure subprocess is still alive |
156
|
|
|
assert proc.poll() is None |
157
|
|
|
|
158
|
|
|
# Input files to touch |
159
|
|
|
input_full = example('complex', 'light.scss') |
160
|
|
|
input_partial = example('complex', '_base.scss') |
161
|
|
|
input_nested = example('complex', 'widgets', '_qwidget.scss') |
162
|
|
|
|
163
|
|
|
def touch_and_wait(input_file, timeout=2000): |
164
|
|
|
"""Touch a file, triggering a recompile""" |
165
|
|
|
|
166
|
|
|
filename = basename(input_file) |
167
|
|
|
old_mtimes = [f.mtime() for f in expected_files] |
168
|
|
|
files_modified = lambda: all( |
169
|
|
|
[f.mtime() > old_mtimes[i] for i, f in enumerate(expected_files)] |
170
|
|
|
) |
171
|
|
|
time.sleep(0.1) |
172
|
|
|
touch(input_file) |
173
|
|
|
|
174
|
|
|
if not await_condition(files_modified, timeout): |
175
|
|
|
proc.terminate() |
176
|
|
|
err = 'Modifying %s did not trigger recompile.' % filename |
177
|
|
|
assert False, err |
178
|
|
|
|
179
|
|
|
return True |
180
|
|
|
|
181
|
|
|
assert touch_and_wait(input_full) |
182
|
|
|
assert touch_and_wait(input_partial) |
183
|
|
|
assert touch_and_wait(input_nested) |
184
|
|
|
|
185
|
|
|
proc.terminate() |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
def test_invalid_input(): |
189
|
|
|
"""CLI input is not a file or dir.""" |
190
|
|
|
|
191
|
|
|
proc = invoke_with_result(['file_does_not_exist.scss']) |
192
|
|
|
assert proc.code == 1 |
193
|
|
|
assert 'Error: input must be' in proc.stdout |
194
|
|
|
|
195
|
|
|
proc = invoke_with_result(['./dir/does/not/exist']) |
196
|
|
|
assert proc.code == 1 |
197
|
|
|
assert 'Error: input must be' in proc.stdout |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
def test_dir_missing_output(): |
201
|
|
|
"""CLI dir missing output option""" |
202
|
|
|
|
203
|
|
|
proc = invoke_with_result([example('complex')]) |
204
|
|
|
assert proc.code == 1 |
205
|
|
|
assert 'Error: missing required option' in proc.stdout |
206
|
|
|
|