| 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 |  |  | from qtsass.watchers.api import retry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | from . import EXAMPLES_DIR, await_condition, example, touch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class CallCounter(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         self.count = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     def __call__(self, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         self.count += 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | @pytest.mark.parametrize( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     'Watcher', (PollingWatcher, QtWatcher), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | def test_watchers(Watcher, tmpdir): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     """Stress test Watcher implementations""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     # Skip when QtWatcher is None - when Qt is not installed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     if not Watcher: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     watch_dir = tmpdir.join('src').strpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     os.makedirs(watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     shutil.copy2(example('dummy.scss'), watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     input = tmpdir.join('src/dummy.scss').strpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     output = tmpdir.join('build/dummy.css').strpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     output_exists = lambda: exists(output) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     c = CallCounter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     w = Watcher( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         watch_dir=watch_dir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         compiler=compile_filename, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         args=(input, output), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     w.connect(c) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     # Output should not yet exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     assert not exists(output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     w.start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     touch(input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     time.sleep(0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     if not await_condition(output_exists): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         assert False, 'Output file not created...' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     # Removing the watch_dir should not kill the Watcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     # simply stop dispatching callbacks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     shutil.rmtree(watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     time.sleep(0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     assert c.count == 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     # Watcher should recover once the input file is there again | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     os.makedirs(watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     shutil.copy2(example('dummy.scss'), watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     time.sleep(0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     assert c.count == 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     # Stop watcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     w.stop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     w.join() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     for _ in range(5): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         touch(input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     # Count should not change | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     assert c.count == 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | def test_qtwatcher(tmpdir): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     """Test QtWatcher implementation""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     # Skip when QtWatcher is None - When Qt is not installed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     if not QtWatcher: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     # Constructing a QApplication will cause the QtWatcher constructed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     # below to use a Signal to dispatch callbacks. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     from qtsass.watchers.qt import QApplication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     qt_app = QApplication.instance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     if not qt_app: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         qt_app = QApplication([]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     watch_dir = tmpdir.join('src').strpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     os.makedirs(watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     shutil.copy2(example('dummy.scss'), watch_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     input = tmpdir.join('src/dummy.scss').strpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     output = tmpdir.join('build/dummy.css').strpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     output_exists = lambda: exists(output) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     c = CallCounter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     w = QtWatcher( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         watch_dir=watch_dir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         compiler=compile_filename, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         args=(input, output), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     # We connect a counter directly to the Watcher's Qt Signal in order to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     # verify that the Watcher is actually using a Qt Signal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     w.qtdispatcher.signal.connect(c) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     w.start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     touch(input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     time.sleep(0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     if not await_condition(output_exists, qt_app=qt_app): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         assert False, 'Output file not created...' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     assert c.count == 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     # Stop watcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     w.stop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     w.join() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 142 |  |  | def test_retry(): | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |     """Test retry decorator""" | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |     @retry(5, interval=0) | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |     def succeeds_after(n, counter): | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |         counter() | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |         if n <= counter.count: | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |             return True | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |         raise ValueError | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |     # Succeed when attempts < retries | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |     assert succeeds_after(4, CallCounter()) | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |     # Fails when retries < attemps | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |     with pytest.raises(ValueError): | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |         assert succeeds_after(6, CallCounter()) | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |     @retry(5, interval=0) | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     def fails(): | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |         raise ValueError | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |     # Most obvious case | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |     with pytest.raises(ValueError): | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |         fails() | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |