Completed
Push — fix-ci-failures ( 4b308a...3c0b05 )
by Michael
02:37
created

with_auth_token_envvar()   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
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 --local 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
    git(shlex.split('tag 0.0.1'))
83
    github_merge_commit(111)
84
85
86
def github_merge_commit(pull_request_number):
87
    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...
88
89
    branch_name = Haikunator().haikunate()
90
    commands = [
91
        'checkout -b {}'.format(branch_name),
92
        'commit --allow-empty -m "Test branch commit message"',
93
        'checkout master',
94
        'merge --no-ff {}'.format(branch_name),
95
96
        'commit --allow-empty --amend -m '
97
        '"Merge pull request #{} from test_app/{}"'.format(
98
            pull_request_number,
99
            branch_name,
100
        )
101
    ]
102
    for command in commands:
103
        git(shlex.split(command))
104
105
106
@pytest.fixture
107
def with_auth_token_prompt(mocker):
108
    _ = mocker.patch('changes.commands.init.click.launch')
109
110
    prompt = mocker.patch('changes.commands.init.click.prompt')
111
    prompt.return_value = 'foo'
112
113
    saved_token = None
114
    if os.environ.get(AUTH_TOKEN_ENVVAR):
115
        saved_token = os.environ[AUTH_TOKEN_ENVVAR]
116
        del os.environ[AUTH_TOKEN_ENVVAR]
117
118
    yield
119
120
    if saved_token:
121
        os.environ[AUTH_TOKEN_ENVVAR] = saved_token
122
123
124
@pytest.fixture
125
def with_auth_token_envvar():
126
    saved_token = None
127
    if os.environ.get(AUTH_TOKEN_ENVVAR):
128
        saved_token = os.environ[AUTH_TOKEN_ENVVAR]
129
130
    os.environ[AUTH_TOKEN_ENVVAR] = 'foo'
131
132
    yield
133
134
    if saved_token:
135
        os.environ[AUTH_TOKEN_ENVVAR] = saved_token
136