| Conditions | 5 |
| Total Lines | 56 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # -*- coding: utf-8 -*- |
||
| 38 | @pytest.mark.parametrize( |
||
| 39 | 'Watcher', (PollingWatcher, QtWatcher), |
||
| 40 | ) |
||
| 41 | def test_watchers(Watcher, tmpdir): |
||
| 42 | """Stress test Watcher implementations""" |
||
| 43 | |||
| 44 | # Skip when QtWatcher is None - when Qt is not installed. |
||
| 45 | if not Watcher: |
||
| 46 | return |
||
| 47 | |||
| 48 | watch_dir = tmpdir.join('src').strpath |
||
| 49 | os.makedirs(watch_dir) |
||
| 50 | shutil.copy2(example('dummy.scss'), watch_dir) |
||
| 51 | input = tmpdir.join('src/dummy.scss').strpath |
||
| 52 | output = tmpdir.join('build/dummy.css').strpath |
||
| 53 | output_exists = lambda: exists(output) |
||
|
|
|||
| 54 | |||
| 55 | c = CallCounter() |
||
| 56 | w = Watcher( |
||
| 57 | watch_dir=watch_dir, |
||
| 58 | compiler=compile_filename, |
||
| 59 | args=(input, output), |
||
| 60 | ) |
||
| 61 | w.connect(c) |
||
| 62 | |||
| 63 | # Output should not yet exist |
||
| 64 | assert not exists(output) |
||
| 65 | |||
| 66 | w.start() |
||
| 67 | |||
| 68 | touch(input) |
||
| 69 | time.sleep(0.5) |
||
| 70 | if not await_condition(output_exists): |
||
| 71 | assert False, 'Output file not created...' |
||
| 72 | |||
| 73 | # Removing the watch_dir should not kill the Watcher |
||
| 74 | # simply stop dispatching callbacks |
||
| 75 | shutil.rmtree(watch_dir) |
||
| 76 | time.sleep(0.5) |
||
| 77 | assert c.count == 1 |
||
| 78 | |||
| 79 | # Watcher should recover once the input file is there again |
||
| 80 | os.makedirs(watch_dir) |
||
| 81 | shutil.copy2(example('dummy.scss'), watch_dir) |
||
| 82 | time.sleep(0.5) |
||
| 83 | assert c.count == 2 |
||
| 84 | |||
| 85 | # Stop watcher |
||
| 86 | w.stop() |
||
| 87 | w.join() |
||
| 88 | |||
| 89 | for _ in range(5): |
||
| 90 | touch(input) |
||
| 91 | |||
| 92 | # Count should not change |
||
| 93 | assert c.count == 2 |
||
| 94 | |||
| 138 |