Completed
Push — status ( 924687...ef6f36 )
by Michael
17:16 queued 07:15
created

with_auth_token_envvar()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
1
import os
2
import shlex
3
4
import pytest
5
import responses
0 ignored issues
show
Configuration introduced by
The import responses 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...
6
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...
7
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...
8
9
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...
10
11
# TODO: textwrap.dedent.heredoc
12
INIT_CONTENT = [
13
    '"""A test app"""',
14
    '',
15
    "__version__ = '0.0.1'",
16
    "__url__ = 'https://github.com/someuser/test_app'",
17
    "__author__ = 'Some User'",
18
    "__email__ = '[email protected]'"
19
]
20
SETUP_PY = [
21
    'from setuptools import setup',
22
    "setup(name='test_app'",
23
]
24
README_MARKDOWN = [
25
    '# Test App',
26
    '',
27
    'This is the test application.'
28
]
29
30
PYTHON_MODULE = 'test_app'
31
32
FILE_CONTENT = {
33
    '%s/__init__.py' % PYTHON_MODULE: INIT_CONTENT,
34
    'setup.py': SETUP_PY,
35
    'requirements.txt': ['pytest'],
36
    'README.md': README_MARKDOWN,
37
    'CHANGELOG.md': [''],
38
}
39
ISSUE_URL = 'https://api.github.com/repos/michaeljoseph/test_app/issues/{}'
40
AUTH_TOKEN_ENVVAR = 'GITHUB_AUTH_TOKEN'
41
42
43
@pytest.fixture
44
def python_module():
45
    with CliRunner().isolated_filesystem():
46
        os.mkdir(PYTHON_MODULE)
47
48
        for file_path, content in FILE_CONTENT.items():
49
            open(file_path, 'w').write(
50
                '\n'.join(content)
51
            )
52
53
        git_init(FILE_CONTENT.keys())
54
55
        yield
56
57
58
@pytest.fixture
59
def git_repo():
60
    with CliRunner().isolated_filesystem():
61
        readme_path = 'README.md'
62
        open(readme_path, 'w').write(
63
            '\n'.join(README_MARKDOWN)
64
        )
65
        git_init([readme_path])
66
67
        yield
68
69
70
def git_init(files_to_add):
71
    git('init')
72
    git(shlex.split('config --global user.email "[email protected]"'))
73
    git('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 (83/79).

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

Loading history...
74
    for file_to_add in files_to_add:
75
        git('add', file_to_add)
76
    git('commit', '-m', 'Initial commit')
77
78
79
@pytest.fixture
80
@responses.activate
81
def git_repo_with_merge_commit(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 59).

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...
82
    pull_request_number = '111'
83
    github_merge_commit(pull_request_number)
84
85
    responses.add(
86
        responses.GET,
87
        ISSUE_URL.format(pull_request_number),
88
        json={
89
            'number': int(pull_request_number),
90
            'title': 'The title of the pull request',
91
            'body': 'An optional, longer description.',
92
            'user': {
93
                'login': 'someone'
94
            },
95
            'labels': [
96
                {'id': 1, 'name': 'feature'}
97
            ],
98
        },
99
        status=200,
100
        content_type='application/json'
101
    )
102
103
104
def github_merge_commit(pull_request_number):
105
    commands = [
106
        'tag 0.0.1',
107
        'checkout -b test-branch',
108
        'commit --allow-empty -m "Test branch commit message"',
109
        'checkout master',
110
        'merge --no-ff test-branch',
111
112
        'commit --allow-empty --amend -m '
113
        '"Merge pull request #{} from test_app/test-branch"'.format(
114
            pull_request_number
115
        )
116
    ]
117
    for command in commands:
118
        git(shlex.split(command))
119
120
121
@pytest.fixture
122
def with_auth_token_prompt(mocker):
123
    _ = mocker.patch('changes.commands.init.click.launch')
124
125
    prompt = mocker.patch('changes.commands.init.click.prompt')
126
    prompt.return_value = 'foo'
127
128
    if os.environ.get(AUTH_TOKEN_ENVVAR):
129
        saved_token = os.environ[AUTH_TOKEN_ENVVAR]
130
        del os.environ[AUTH_TOKEN_ENVVAR]
131
132
    yield
133
134
    os.environ[AUTH_TOKEN_ENVVAR] = saved_token
135
136
137
@pytest.fixture
138
def with_auth_token_envvar():
139
    if os.environ.get(AUTH_TOKEN_ENVVAR):
140
        saved_token = os.environ[AUTH_TOKEN_ENVVAR]
141
        os.environ[AUTH_TOKEN_ENVVAR] = 'foo'
142
143
    yield
144
145
    os.environ[AUTH_TOKEN_ENVVAR] = saved_token
146