Passed
Branch master (43347c)
by Peter
56s
created

test_modern   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 53.85 %

Importance

Changes 0
Metric Value
eloc 77
dl 70
loc 130
rs 10
c 0
b 0
f 0
wmc 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""
2
Tests for the modern module
3
"""
4
import logging
5
import pytest
6
import sys
7
8
try:
9
    from pathlib import Path
10
    from unittest.mock import call, patch
11
except ImportError:  # Python 2.7, PyPy2
12
    from mock import patch
13
14
from cli_test_helpers import ArgvContext
15
16
import pyclean
17
18
19
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
20
@patch('pyclean.modern.descend_and_clean_bytecode')
21
def test_walks_tree(mock_descend):
22
    """
23
    Does pyclean walk the directory tree?
24
    """
25
    with ArgvContext('pyclean', '.'):
26
        pyclean.cli.main()
27
28
    assert mock_descend.mock_calls == [
29
        call(Path('.')),
30
    ]
31
32
33
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
34
@patch('pyclean.modern.descend_and_clean_bytecode')
35
def test_walks_all_trees(mock_descend):
36
    """
37
    Are all positional args evaluated?
38
    """
39
    with ArgvContext('pyclean', 'foo', 'bar', 'baz'):
40
        pyclean.cli.main()
41
42
    assert mock_descend.mock_calls == [
43
        call(Path('foo')),
44
        call(Path('bar')),
45
        call(Path('baz')),
46
    ]
47
48
49
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
50
@patch.object(pyclean.modern.logging, 'basicConfig')
51
@patch('pyclean.modern.descend_and_clean_bytecode')
52
def test_normal_logging(mock_descend, mock_logconfig):
53
    """
54
    Does a normal run use log level INFO?
55
    """
56
    with ArgvContext('pyclean', '.'):
57
        pyclean.cli.main()
58
59
    assert mock_logconfig.mock_calls == [
60
        call(format='%(message)s', level=logging.INFO),
61
    ]
62
63
64
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
65
@patch.object(pyclean.modern.logging, 'basicConfig')
66
@patch('pyclean.modern.descend_and_clean_bytecode')
67
def test_verbose_logging(mock_descend, mock_logconfig):
68
    """
69
    Does --verbose use log level DEBUG?
70
    """
71
    with ArgvContext('pyclean', '.', '--verbose'):
72
        pyclean.cli.main()
73
74
    assert mock_logconfig.mock_calls == [
75
        call(format='%(message)s', level=logging.DEBUG),
76
    ]
77
78
79
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
80
@patch.object(pyclean.modern.logging, 'basicConfig')
81
@patch('pyclean.modern.descend_and_clean_bytecode')
82
def test_quiet_logging(mock_descend, mock_logconfig):
83
    """
84
    Does --quiet use log level FATAL?
85
    """
86
    with ArgvContext('pyclean', '.', '--quiet'):
87
        pyclean.cli.main()
88
89
    assert mock_logconfig.mock_calls == [
90
        call(format='%(message)s', level=logging.FATAL),
91
    ]
92
93
94
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
95
@patch('pyclean.modern.print_dirname')
96
@patch('pyclean.modern.print_filename')
97
@patch('pyclean.modern.remove_directory')
98
@patch('pyclean.modern.remove_file')
99
def test_delete(mock_real_unlink, mock_real_rmdir,
100
                mock_dry_unlink, mock_dry_rmdir):
101
    """
102
    Is actual deletion attempted w/o --dry-run?
103
    """
104
    with ArgvContext('pyclean', '.'):
105
        pyclean.cli.main()
106
107
    assert mock_real_unlink.called
108
    assert mock_real_rmdir.called
109
    assert not mock_dry_unlink.called
110
    assert not mock_dry_rmdir.called
111
112
113
@pytest.mark.skipif(sys.version_info < (3,), reason="requires Python 3")
114
@patch('pyclean.modern.print_dirname')
115
@patch('pyclean.modern.print_filename')
116
@patch('pyclean.modern.remove_directory')
117
@patch('pyclean.modern.remove_file')
118
def test_dryrun(mock_real_unlink, mock_real_rmdir,
119
                mock_dry_unlink, mock_dry_rmdir):
120
    """
121
    Does --dry-run option avoid real deletion?
122
    """
123
    with ArgvContext('pyclean', '.', '--dry-run'):
124
        pyclean.cli.main()
125
126
    assert not mock_real_unlink.called
127
    assert not mock_real_rmdir.called
128
    assert mock_dry_unlink.called
129
    assert mock_dry_rmdir.called
130