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