GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#44)
by
unknown
01:12
created

tests.test_watchers   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 93
dl 0
loc 166
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A CallCounter.__init__() 0 2 1
A CallCounter.__call__() 0 2 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
B test_watchers() 0 56 5
B test_qtwatcher() 0 42 5
A test_retry() 0 24 4
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)
0 ignored issues
show
introduced by
The variable output does not seem to be defined for all execution paths.
Loading history...
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)
0 ignored issues
show
introduced by
The variable output does not seem to be defined for all execution paths.
Loading history...
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