Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 9e41194

Browse files
First pass at tests for the command-line client
1 parent ecd34fc commit 9e41194

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

coreapi/commandline.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
import sys
66

77

8-
config_path = os.path.join(os.path.expanduser('~'), '.coreapi')
8+
config_path = None
99

10-
document_path = os.path.join(config_path, 'document.json')
11-
history_path = os.path.join(config_path, 'history.json')
12-
credentials_path = os.path.join(config_path, 'credentials.json')
13-
headers_path = os.path.join(config_path, 'headers.json')
14-
bookmarks_path = os.path.join(config_path, 'bookmarks.json')
10+
document_path = None
11+
history_path = None
12+
credentials_path = None
13+
headers_path = None
14+
bookmarks_path = None
15+
16+
17+
def setup_paths():
18+
global config_path, document_path, history_path
19+
global credentials_path, headers_path, bookmarks_path
20+
21+
default_dir = os.path.join(os.path.expanduser('~'), '.coreapi')
22+
config_path = os.environ.get('COREAPI_CONFIG_DIR', default_dir)
23+
24+
document_path = os.path.join(config_path, 'document.json')
25+
history_path = os.path.join(config_path, 'history.json')
26+
credentials_path = os.path.join(config_path, 'credentials.json')
27+
headers_path = os.path.join(config_path, 'headers.json')
28+
bookmarks_path = os.path.join(config_path, 'bookmarks.json')
1529

1630

1731
def coerce_key_types(doc, keys):
@@ -82,6 +96,8 @@ def display(doc):
8296
@click.option('--version', is_flag=True, help='Display the package version number.')
8397
@click.pass_context
8498
def client(ctx, version):
99+
setup_paths()
100+
85101
if os.path.isfile(config_path):
86102
os.remove(config_path)
87103
if not os.path.isdir(config_path):

runtests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PYTEST_ARGS = ['tests', '--tb=short']
1010
FLAKE8_ARGS = ['coreapi', 'tests', '--ignore=E501']
1111
COVERAGE_OPTIONS = {
1212
'include': ['coreapi/*', 'tests/*'],
13-
'omit': ['coreapi/compat.py', 'coreapi/commandline.py']
13+
'omit': ['coreapi/compat.py']
1414
}
1515

1616

tests/test_commandline.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
from coreapi import Document, Link
2-
from coreapi.commandline import coerce_key_types
2+
from coreapi.commandline import client, coerce_key_types
3+
from click.testing import CliRunner
4+
import coreapi
5+
import pytest
6+
import os
7+
import shutil
8+
import tempfile
9+
10+
11+
@pytest.fixture(scope="function")
12+
def runner(request):
13+
"""
14+
A fixture returning a runner for the command line client.
15+
"""
16+
config_dir = tempfile.mkdtemp()
17+
os.environ['COREAPI_CONFIG_DIR'] = config_dir
18+
19+
def finalize():
20+
shutil.rmtree(config_dir)
21+
22+
runner = CliRunner()
23+
request.addfinalizer(finalize)
24+
return runner
325

426

527
# Test dotted path notation maps to list of keys correctly.
@@ -20,3 +42,11 @@ def test_dotted_path_notation_with_invalid_key():
2042
doc = Document({'rows': [Document({'edit': Link()})]})
2143
keys = coerce_key_types(doc, ['dummy', '0', 'edit'])
2244
assert keys == ['dummy', '0', 'edit']
45+
46+
47+
# Integration tests
48+
49+
def test_usage(runner):
50+
result = runner.invoke(client, ['--version'])
51+
assert result.output == 'coreapi version %s\n' % coreapi.__version__
52+
assert result.exit_code == 0

0 commit comments

Comments
 (0)