Skip to content

Commit 3ed1a09

Browse files
committed
🚒 Fixed issues with conftest
* Added support for token/private_key/privatekey config setting ; * Added support for non static services list (will help when we'll support more services) ; * made betamax configuration dynamic
1 parent 8077d35 commit 3ed1a09

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

tests/conftest.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,52 @@
77

88
record_mode = 'once'
99

10+
from git_repo.services.service import RepositoryService
11+
services = list(RepositoryService.service_map.keys())
12+
1013
if os.environ.get('TRAVIS_GH3'):
1114
# create default bogus values for tokens and namespaces if missing for pytest
1215
# to run without environment values
1316
# also if an environment variable is not set, then we don't want to record cassettes
1417
record_mode = 'never'
15-
for service_name in ('github', 'gitlab', 'bitbucket'):
16-
token_name = 'PRIVATE_KEY_{}'.format(service_name.upper())
17-
namespace_name = '{}_NAMESPACE'.format(service_name.upper())
18+
for service in services:
19+
token_name = 'PRIVATE_KEY_{}'.format(service.upper())
20+
namespace_name = '{}_NAMESPACE'.format(service.upper())
1821
if token_name not in os.environ:
19-
os.environ[token_name] = 'not_configured:test' # using a : for bitbucket's case
22+
os.environ[token_name] = '_token_{}_:_private_'.format(s) # using a : for bitbucket's case
2023
if namespace_name not in os.environ:
21-
os.environ[namespace_name] = 'not_configured'
24+
os.environ[namespace_name] = '_not_a_namespace_'
2225
else:
26+
# if running tests "locally" and not in travis, let's try to extract the keys from
27+
# the local configuration if there is some local configuration. And exposes them as
28+
# environment variables.
2329
import git, getpass
2430
config = git.config.GitConfigParser(os.path.join(os.environ['HOME'], '.gitconfig'))
25-
conf_section = list(filter(lambda n: 'gitrepo' in n, config.sections()))
26-
key_dict = {section.split('"')[1] :config.get_value(section, 'privatekey') for section in conf_section}
27-
for service, key in key_dict.items():
28-
token_name = 'PRIVATE_KEY_{}'.format(service.upper())
29-
namespace_name = '{}_NAMESPACE'.format(service.upper())
30-
os.environ[token_name] = key
31-
os.environ[namespace_name] = os.environ.get('GITREPO_NAMESPACE', getpass.getuser())
3231

33-
api_token_github = os.environ['PRIVATE_KEY_GITHUB']
34-
api_token_gitlab = os.environ['PRIVATE_KEY_GITLAB']
35-
api_token_bitbucket = os.environ['PRIVATE_KEY_BITBUCKET']
32+
# handle the different forms of token configuration item (yup, technical debt bites here)
33+
get_section = lambda s: 'gitrepo "{}"'.format(s)
34+
get_token = lambda s: config.get_value(get_section(s), 'token',
35+
config.get_value(get_section(s), 'private_token',
36+
config.get_value(get_section(s), 'privatekey',
37+
'_token_{}_:_private_'.format(s) # using a : for bitbucket's case
38+
)))
3639

37-
github_namespace = os.environ['GITHUB_NAMESPACE']
38-
gitlab_namespace = os.environ['GITLAB_NAMESPACE']
39-
bitbucket_namespace = os.environ['BITBUCKET_NAMESPACE']
40+
for service in services:
41+
token_name = 'PRIVATE_KEY_{}'.format(service.upper())
42+
namespace_name = '{}_NAMESPACE'.format(service.upper())
43+
if token_name not in os.environ:
44+
os.environ[token_name] = get_token(service)
45+
if namespace_name not in os.environ:
46+
os.environ[namespace_name] = os.environ.get('GITREPO_NAMESPACE', getpass.getuser())
4047

4148
betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
4249

4350
with betamax.Betamax.configure() as config:
4451
config.default_cassette_options['record_mode'] = record_mode
4552
config.cassette_library_dir = 'tests/integration/cassettes'
4653
config.default_cassette_options['serialize_with'] = 'prettyjson'
47-
config.define_cassette_placeholder('<PRIVATE_KEY_GITHUB>', api_token_github)
48-
config.define_cassette_placeholder('<PRIVATE_KEY_GITLAB>', api_token_gitlab)
49-
config.define_cassette_placeholder('<PRIVATE_KEY_BITBUCKET>', api_token_bitbucket)
50-
config.define_cassette_placeholder('<GITHUB_NAMESPACE>', github_namespace)
51-
config.define_cassette_placeholder('<GITLAB_NAMESPACE>', gitlab_namespace)
52-
config.define_cassette_placeholder('<BITBUCKET_NAMESPACE>', bitbucket_namespace)
53-
54-
54+
# generating placeholders in betamax configuration for each service's key and default namespace
55+
for service in services:
56+
config.define_cassette_placeholder('<PRIVATE_KEY_{}>'.format(service.upper()), os.environ.get('PRIVATE_KEY_{}'.format(service.upper())))
57+
config.define_cassette_placeholder('<{}_NAMESPACE>'.format(service.upper()), os.environ.get('{}_NAMESPACE'.format(service.upper())))
5558

0 commit comments

Comments
 (0)