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