Completed
Push — publish ( 2db8ae...f5ec56 )
by Michael
05:19
created

python_module()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
1
import os
2
import shlex
3
import textwrap
4
from pathlib import Path
5
6
import pytest
7
import sys
8
from click.testing import CliRunner
0 ignored issues
show
Configuration introduced by
The import click.testing could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
from plumbum.cmd import git
0 ignored issues
show
Configuration introduced by
The import plumbum.cmd could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
10
11
import changes
12
13
pytest_plugins = 'pytester'
0 ignored issues
show
Coding Style Naming introduced by
The name pytest_plugins does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
14
15
# TODO: textwrap.dedent.heredoc
16
INIT_CONTENT = [
17
    '"""A test app"""',
18
    '',
19
    "__version__ = '0.0.1'",
20
    "__url__ = 'https://github.com/someuser/test_app'",
21
    "__author__ = 'Some User'",
22
    "__email__ = '[email protected]'"
23
]
24
SETUP_PY = [
25
    'from setuptools import setup',
26
    "setup(name='test_app'",
27
]
28
README_MARKDOWN = [
29
    '# Test App',
30
    '',
31
    'This is the test application.'
32
]
33
34
PYTHON_MODULE = 'test_app'
35
36
FILE_CONTENT = {
37
    '%s/__init__.py' % PYTHON_MODULE: INIT_CONTENT,
38
    'setup.py': SETUP_PY,
39
    'requirements.txt': ['pytest'],
40
    'README.md': README_MARKDOWN,
41
    'CHANGELOG.md': [''],
42
}
43
44
AUTH_TOKEN_ENVVAR = 'GITHUB_AUTH_TOKEN'
45
46
ISSUE_URL = 'https://api.github.com/repos/michaeljoseph/test_app/issues/{}'
47
PULL_REQUEST_JSON = {
48
    'number': 111,
49
    'title': 'The title of the pull request',
50
    'body': 'An optional, longer description.',
51
    'user': {
52
        'login': 'someone'
53
    },
54
    'labels': [
55
        {'id': 1, 'name': 'bug'}
56
    ],
57
}
58
59
LABEL_URL = 'https://api.github.com/repos/michaeljoseph/test_app/labels'
60
BUG_LABEL_JSON = [
61
    {
62
        'id': 52048163,
63
        'url': 'https://api.github.com/repos/michaeljoseph/changes/labels/bug',
64
        'name': 'bug',
65
        'color': 'fc2929',
66
        'default': True
67
    }
68
]
69
70
71
@pytest.fixture
72
def git_repo():
73
    with CliRunner().isolated_filesystem() as tmpdir:
74
        readme_path = 'README.md'
75
        open(readme_path, 'w').write(
76
            '\n'.join(README_MARKDOWN)
77
        )
78
        version_path = 'version.txt'
79
        open(version_path, 'w').write('0.0.1')
80
81
        files_to_add = [
82
           readme_path,
83
           version_path,
84
        ]
85
86
        git('init')
87
        git(shlex.split('config --local user.email "[email protected]"'))
88
        git(shlex.split('remote add origin https://github.com/michaeljoseph/test_app.git'))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (91/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
89
        git(shlex.split('remote set-url --push origin {}'.format(str(tmpdir))))
90
91
        for file_to_add in files_to_add:
92
            git('add', file_to_add)
93
        git('commit', '-m', 'Initial commit')
94
        git(shlex.split('tag 0.0.1'))
95
96
        yield tmpdir
97
98
99
@pytest.fixture
100
def python_module(git_repo):
0 ignored issues
show
Comprehensibility Bug introduced by
git_repo is re-defining a name which is already available in the outer-scope (previously defined on line 72).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
101
    os.mkdir(PYTHON_MODULE)
102
103
    for file_path, content in FILE_CONTENT.items():
104
        open(file_path, 'w').write(
105
            '\n'.join(content)
106
        )
107
108
    git('add', [file for file in FILE_CONTENT.keys()])
109
110
    yield
111
112
113
def github_merge_commit(pull_request_number):
114
    from haikunator import Haikunator
0 ignored issues
show
Configuration introduced by
The import haikunator could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
115
116
    branch_name = Haikunator().haikunate()
117
    commands = [
118
        'checkout -b {}'.format(branch_name),
119
        'commit --allow-empty -m "Test branch commit message"',
120
        'checkout master',
121
        'merge --no-ff {}'.format(branch_name),
122
123
        'commit --allow-empty --amend -m '
124
        '"Merge pull request #{} from test_app/{}"'.format(
125
            pull_request_number,
126
            branch_name,
127
        )
128
    ]
129
    for command in commands:
130
        git(shlex.split(command))
131
132
133
@pytest.fixture
134
def with_releases_directory_and_bumpversion_file_prompt(mocker):
0 ignored issues
show
Coding Style Naming introduced by
The name with_releases_directory_...bumpversion_file_prompt does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
135
    prompt = mocker.patch(
136
        'changes.config.click.prompt',
137
        autospec=True
138
    )
139
    prompt.side_effect = [
140
        # release_directory
141
        'docs/releases',
142
        # bumpversion files
143
        'version.txt',
144
        # quit prompt
145
        '.',
146
        # label descriptions
147
        # 'Features',
148
        # 'Bug Fixes'
149
    ]
150
151
    prompt = mocker.patch(
152
        'changes.config.choose_labels',
153
        autospec=True
154
    )
155
    prompt.return_value = ['bug']
156
157
158
@pytest.fixture
159
def with_auth_token_prompt(mocker):
160
    _ = mocker.patch('changes.config.click.launch')
161
162
    prompt = mocker.patch('changes.config.click.prompt')
163
    prompt.return_value = 'foo'
164
165
    saved_token = None
166
    if os.environ.get(AUTH_TOKEN_ENVVAR):
167
        saved_token = os.environ[AUTH_TOKEN_ENVVAR]
168
        del os.environ[AUTH_TOKEN_ENVVAR]
169
170
    yield
171
172
    if saved_token:
173
        os.environ[AUTH_TOKEN_ENVVAR] = saved_token
174
175
176
@pytest.fixture
177
def with_auth_token_envvar():
178
    saved_token = None
179
    if os.environ.get(AUTH_TOKEN_ENVVAR):
180
        saved_token = os.environ[AUTH_TOKEN_ENVVAR]
181
182
    os.environ[AUTH_TOKEN_ENVVAR] = 'foo'
183
184
    yield
185
186
    if saved_token:
187
        os.environ[AUTH_TOKEN_ENVVAR] = saved_token
188
    else:
189
        del os.environ[AUTH_TOKEN_ENVVAR]
190
191
192
@pytest.fixture
193
def changes_config_in_tmpdir(monkeypatch, tmpdir):
194
    IS_WINDOWS = 'win32' in str(sys.platform).lower()
0 ignored issues
show
Coding Style Naming introduced by
The name IS_WINDOWS does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
195
196
    changes_config_file = Path(str(tmpdir.join('.changes')))
197
    monkeypatch.setattr(
198
        changes.config,
199
        'expandvars' if IS_WINDOWS else 'expanduser',
200
        lambda x: str(changes_config_file)
201
    )
202
    assert not changes_config_file.exists()
203
    return changes_config_file
204
205
206
@pytest.fixture
207
def configured(git_repo, changes_config_in_tmpdir):
0 ignored issues
show
Comprehensibility Bug introduced by
git_repo is re-defining a name which is already available in the outer-scope (previously defined on line 72).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Comprehensibility Bug introduced by
changes_config_in_tmpdir is re-defining a name which is already available in the outer-scope (previously defined on line 193).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
208
    changes_config_in_tmpdir.write_text(textwrap.dedent(
209
        """\
210
        [changes]
211
        auth_token = "foo"
212
        """
213
    ))
214
215
    Path('.changes.toml').write_text(textwrap.dedent(
0 ignored issues
show
Bug introduced by
The Instance of Path does not seem to have a member named write_text.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
216
        """\
217
        [changes]
218
        releases_directory = "docs/releases"
219
220
        [changes.labels.bug]
221
        default = true
222
        id = 208045946
223
        url = "https://api.github.com/repos/michaeljoseph/test_app/labels/bug"
224
        name = "bug"
225
        description = "Bug"
226
        color = "f29513"
227
        """
228
    ))
229
230
    Path('.bumpversion.cfg').write_text(textwrap.dedent(
0 ignored issues
show
Bug introduced by
The Instance of Path does not seem to have a member named write_text.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
231
        """\
232
        [bumpversion]
233
        current_version = 0.0.1
234
235
        [bumpversion:file:version.txt]
236
        """
237
    ))
238
239
    for file_to_add in ['.changes.toml', '.bumpversion.cfg']:
240
        git('add', file_to_add)
241
    git('commit', '-m', 'Add changes configuration files')
242
243
    return str(changes_config_in_tmpdir)
244