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