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

Commit b079c7f

Browse files
Fixed python3 bugs and more cli tests
1 parent 11ad5d7 commit b079c7f

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

coreapi/commandline.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from coreapi.compat import json_load_bytes, force_bytes
12
import click
23
import coreapi
34
import json
@@ -277,14 +278,14 @@ def get_credentials():
277278
if not os.path.isfile(credentials_path):
278279
return {}
279280
store = open(credentials_path, 'rb')
280-
credentials = json.loads(store.read())
281+
credentials = json_load_bytes(store.read())
281282
store.close()
282283
return credentials
283284

284285

285286
def set_credentials(credentials):
286287
store = open(credentials_path, 'wb')
287-
store.write(json.dumps(credentials))
288+
store.write(force_bytes(json.dumps(credentials)))
288289
store.close
289290

290291

@@ -334,14 +335,14 @@ def get_headers():
334335
if not os.path.isfile(headers_path):
335336
return {}
336337
headers_file = open(headers_path, 'rb')
337-
headers = json.loads(headers_file.read())
338+
headers = json_load_bytes(headers_file.read())
338339
headers_file.close()
339340
return headers
340341

341342

342343
def set_headers(headers):
343344
headers_file = open(headers_path, 'wb')
344-
headers_file.write(json.dumps(headers))
345+
headers_file.write(force_bytes(json.dumps(headers)))
345346
headers_file.close()
346347

347348

@@ -394,14 +395,14 @@ def get_bookmarks():
394395
if not os.path.isfile(bookmarks_path):
395396
return {}
396397
bookmarks_file = open(bookmarks_path, 'rb')
397-
bookmarks = json.loads(bookmarks_file.read())
398+
bookmarks = json_load_bytes(bookmarks_file.read())
398399
bookmarks_file.close()
399400
return bookmarks
400401

401402

402403
def set_bookmarks(bookmarks):
403404
bookmarks_file = open(bookmarks_path, 'wb')
404-
bookmarks_file.write(json.dumps(bookmarks))
405+
bookmarks_file.write(force_bytes(json.dumps(bookmarks)))
405406
bookmarks_file.close()
406407

407408

coreapi/compat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# coding: utf-8
22

3+
import json
4+
35
__all__ = [
46
'urlparse', 'string_types',
57
'COMPACT_SEPARATORS', 'VERBOSE_SEPARATORS'
@@ -25,3 +27,7 @@ def force_bytes(string):
2527
if isinstance(string, string_types):
2628
return string.encode('utf-8')
2729
return string
30+
31+
32+
def json_load_bytes(bytes):
33+
return json.loads(bytes.decode('utf-8') or '{}')

tests/test_commandline.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,57 @@ def test_cli_history(cli):
127127
assert result.output == '<Document "http://2.com">\n'
128128

129129

130+
# Credentials
131+
132+
def test_cli_credentials(cli):
133+
result = cli('credentials', 'show')
134+
assert result.output == 'Credentials\n'
135+
136+
result = cli('credentials', 'add', 'http://1.com', 'Token 123cat')
137+
assert result.output == 'Added credentials\nhttp://1.com "Token 123cat"\n'
138+
139+
result = cli('credentials', 'show')
140+
assert result.output == 'Credentials\nhttp://1.com "Token 123cat"\n'
141+
142+
143+
# Bookmarks
144+
145+
def test_cli_bookmarks(cli):
146+
set_response(Document('http://example.com', 'Example'))
147+
cli('get', 'http://example.com')
148+
149+
result = cli('bookmarks', 'add', 'example')
150+
assert result.output == 'Added bookmark\nexample\n'
151+
152+
result = cli('bookmarks', 'show')
153+
assert result.output == 'Bookmarks\nexample <Example "http://example.com">\n'
154+
155+
result = cli('bookmarks', 'get', 'example')
156+
assert result.output == '<Example "http://example.com">\n'
157+
158+
result = cli('bookmarks', 'remove', 'example')
159+
assert result.output == 'Removed bookmark\nexample\n'
160+
161+
result = cli('bookmarks', 'show')
162+
assert result.output == 'Bookmarks\n'
163+
164+
165+
# Headers
166+
167+
def test_cli_headers(cli):
168+
result = cli('headers', 'add', 'Cache-Control', 'public')
169+
assert result.output == 'Added header\nCache-Control: public\n'
170+
171+
result = cli('headers', 'show')
172+
assert result.output == 'Headers\nCache-Control: public\n'
173+
174+
result = cli('headers', 'remove', 'Cache-Control')
175+
assert result.output == 'Removed header\nCache-Control\n'
176+
177+
result = cli('headers', 'show')
178+
assert result.output == 'Headers\n'
179+
180+
130181
# Test dotted path notation maps to list of keys correctly.
131182

132183
def test_dotted_path_notation():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy