Skip to content

Commit

Permalink
Merge beea193 into 30c05c5
Browse files Browse the repository at this point in the history
  • Loading branch information
mission-liao committed Dec 23, 2017
2 parents 30c05c5 + beea193 commit 41b5d31
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 43 deletions.
22 changes: 22 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
environment:
matrix:
- PYTHON: "C:\\Python27"
- PYTHON: "C:\\Python27-x64"
#- PYTHON: "C:\\Python33"
#- PYTHON: "C:\\Python33-x64"
#- PYTHON: "C:\\Python35"
#- PYTHON: "C:\\Python35-x64"
#- PYTHON: "C:\\Python36"
#- PYTHON: "C:\\Python36-x64"

install:
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
- "python --version"
- "python -c \"import struct; print(struct.calcsize('P') * 8)\""
- "pip install --disable-pip-version-check --user --upgrade pip"
- "pip install -r requirements-dev.txt"

build: off

test_script:
- "python -m pytest -s -v pyswagger/tests"
43 changes: 37 additions & 6 deletions pyswagger/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from pyswagger import utils, errs
from .utils import is_windows
from datetime import datetime
import unittest
import functools
import six
import os


class SwaggerUtilsTestCase(unittest.TestCase):
Expand Down Expand Up @@ -133,12 +135,6 @@ def test_jr_split(self):
self.assertEqual(utils.jr_split(
'#/definitions/s1'), (
'', '#/definitions/s1'))
self.assertEqual(utils.jr_split(
'/user/tmp/local/ttt'), (
'file:///user/tmp/local/ttt', '#'))
self.assertEqual(utils.jr_split(
'/user/tmp/local/ttt/'), (
'file:///user/tmp/local/ttt', '#'))
# relative path should be converted to absolute one
self.assertEqual(utils.jr_split(
'user'), (
Expand All @@ -150,11 +146,34 @@ def test_jr_split(self):
'//'), (
'', '#'))

@unittest.skipUnless(not is_windows(), 'make no sense on windows')
def test_jr_split_on_unix(self):
""" test jr_split on unix-like os """
self.assertEqual(utils.jr_split(
'/user/tmp/local/ttt'), (
'file:///user/tmp/local/ttt', '#'))
self.assertEqual(utils.jr_split(
'/user/tmp/local/ttt/'), (
'file:///user/tmp/local/ttt', '#'))

@unittest.skipUnless(is_windows(), 'make no sense on unix')
def test_jr_split_on_windows(self):
""" test jr_split on windows """
self.assertEqual(utils.jr_split(
r'C:\user\tmp\local\ttt'), (
'file:///C:/user/tmp/local/ttt', '#'))
self.assertEqual(utils.jr_split(
# check here for adding backslach at the end of raw string
# https://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/
os.path.normpath('c:/user/tmp/local/ttt/')), (
'file:///C:/user/tmp/local/ttt', '#'))

def test_cycle_guard(self):
c = utils.CycleGuard()
c.update(1)
self.assertRaises(errs.CycleDetectionError, c.update, 1)

@unittest.skipUnless(not is_windows(), 'make no sense on windows')
def test_normalize_url(self):
self.assertEqual(utils.normalize_url(None), None)
self.assertEqual(utils.normalize_url(''), '')
Expand All @@ -163,6 +182,10 @@ def test_normalize_url(self):
self.assertEqual(utils.normalize_url('/tmp/local/test'), 'file:///tmp/local/test')
self.assertEqual(utils.normalize_url('/tmp/local/test in space.txt'), 'file:///tmp/local/test%20in%20space.txt')

@unittest.skipUnless(is_windows(), 'make no sense on unix')
def test_normalize_url_on_windows(self):
self.assertEqual(utils.normalize_url(r'C:\path\to\something'), 'file:///C:/path/to/something')

def test_normalize_jr(self):
self.assertEqual(utils.normalize_jr(None), None)
self.assertEqual(utils.normalize_jr(None, 'http://test.com/api/swagger.json'), None)
Expand Down Expand Up @@ -248,6 +271,7 @@ def test_url_join(self):
self.assertEqual(utils.url_join('https://localhost/test', 'swagger.json'), 'https://localhost/test/swagger.json')
self.assertEqual(utils.url_join('https://localhost/test/', 'swagger.json'), 'https://localhost/test/swagger.json')

@unittest.skipUnless(not is_windows(), 'make no sense on windows')
def test_patch_path(self):
""" make sure patch_path works
"""
Expand All @@ -256,6 +280,13 @@ def test_patch_path(self):
'/Users/sudeep.agarwal/src/squiddy/api/v0.1/swagger.yaml',
), '/Users/sudeep.agarwal/src/squiddy/api/v0.1/swagger.yaml')

@unittest.skipUnless(is_windows(), 'make no sense on unix-like os')
def test_patch_path_on_windows(self):
self.assertEqual(utils.patch_path(
'Users/sudeep.agarwal/src/squiddy/api/v0.1',
'Users/sudeep.agarwal/src/squiddy/api/v0.1/swagger.yaml',
), 'Users/sudeep.agarwal/src/squiddy/api/v0.1/swagger.yaml')


class WalkTestCase(unittest.TestCase):
""" test for walk """
Expand Down
3 changes: 3 additions & 0 deletions pyswagger/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ def create_pet_db():
pet.create_(**pet_Sue)

return pet

def is_windows():
return os.name == 'nt'
17 changes: 6 additions & 11 deletions pyswagger/tests/v1_2/test_upgrade.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
from pyswagger import App, errs
from ..utils import get_test_data_folder
from pyswagger.utils import normalize_url
from ..utils import get_test_data_folder, is_windows
from ...spec.v2_0 import objects
import unittest
import os
import six


folder = get_test_data_folder(
folder = normalize_url(get_test_data_folder(
version='1.2',
which='wordnik'
)
))

def _pf(s):
return six.moves.urllib.parse.urlunparse((
'file',
'',
folder,
'',
'',
s))
return folder + '#' + s


class Swagger_Upgrade_TestCase(unittest.TestCase):
Expand Down Expand Up @@ -128,7 +123,7 @@ def test_parameter(self):
p = [p for p in o.parameters if getattr(p, 'in') == 'formData' and p.type == 'string'][0]
self.assertEqual(p.name, 'additionalMetadata')
self.assertEqual(p.required, False)

# file
o = self.app.root.paths['/api/pet/uploadImage'].post
p = [p for p in o.parameters if getattr(p, 'in') == 'formData' and p.type == 'file'][0]
Expand Down
28 changes: 8 additions & 20 deletions pyswagger/tests/v2_0/test_circular.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pyswagger import App, utils, primitives, errs
from ..utils import get_test_data_folder
from ...scanner import CycleDetector
from ...scanner import CycleDetector
from ...scan import Scanner
import unittest
import os
Expand All @@ -20,19 +20,13 @@ def test_path_item_prepare_with_cycle(self):
app.prepare()

def test_path_item(self):
folder = get_test_data_folder(
folder = utils.normalize_url(get_test_data_folder(
version='2.0',
which=os.path.join('circular', 'path_item')
)
))

def _pf(s):
return six.moves.urllib.parse.urlunparse((
'file',
'',
folder,
'',
'',
s))
return folder + '#' + s

app = App.create(folder)
s = Scanner(app)
Expand All @@ -47,19 +41,13 @@ def _pf(s):
]]))

def test_schema(self):
folder = get_test_data_folder(
folder = utils.normalize_url(get_test_data_folder(
version='2.0',
which=os.path.join('circular', 'schema')
)
))

def _pf(s):
return six.moves.urllib.parse.urlunparse((
'file',
'',
folder,
'',
'',
s))
return folder + '#' + s


app = App.load(folder)
Expand Down Expand Up @@ -99,4 +87,4 @@ def test_primfactory(self):

s = app.resolve('#/definitions/s1')
self.assertRaises(errs.CycleDetectionError, app.prim_factory.produce, s, {})

29 changes: 23 additions & 6 deletions pyswagger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,26 @@ def path2url(p):
""" Return file:// URL from a filename.
"""
# Python 3 is a bit different and does a better job.
if sys.version_info[0] >= 3:
if sys.version_info.major >= 3 and sys.version_info.minor >= 4:
import pathlib
return pathlib.Path(p).as_uri()
else:
return six.moves.urllib.parse.urljoin(
'file:', six.moves.urllib.request.pathname2url(p)
)

_windows_path_prefix = re.compile(r'(^[A-Za-z]:\\)')

def normalize_url(url):
""" Normalize url
"""
if not url:
return url

matched = _windows_path_prefix.match(url)
if matched:
return path2url(url)

p = six.moves.urllib.parse.urlparse(url)
if p.scheme == '':
if p.netloc == '' and p.path != '':
Expand Down Expand Up @@ -339,10 +345,18 @@ def url_join(url, path):
""" url version of os.path.join
"""
p = six.moves.urllib.parse.urlparse(url)
t = ('/'.join([p.path, path]),)

t = None
if p.path and p.path[-1] == '/':
if path and path[0] == '/':
path = path[1:]
t = ''.join([p.path, path])
else:
t = ('' if path and path[0] == '/' else '/').join([p.path, path])

return six.moves.urllib.parse.urlunparse(
p[:2]+
t+ # os.sep is different on windows, don't use it here.
(t,)+ # os.sep is different on windows, don't use it here.
p[3:]
)

Expand Down Expand Up @@ -382,6 +396,11 @@ def normalize_jr(jr, url=None):
else:
return '#' + jp

def _fullmatch(regex, chunk):
m = re.match(regex, chunk)
if m and m.span()[1] == len(chunk):
return m

def derelativise_url(url):
'''
Normalizes URLs, gets rid of .. and .
Expand All @@ -396,14 +415,12 @@ def derelativise_url(url):
newpath=newpath[:-1]
continue
# TODO: Verify this behaviour.
elif re.fullmatch(r'\.{3,}', chunk) is not None:
elif _fullmatch(r'\.{3,}', chunk) is not None:
# parent dir.
newpath=newpath[:-1]
continue
newpath += [chunk]
return six.moves.urllib.parse.urlunparse(parsed[:2]+('/'+('/'.join(newpath)),)+parsed[3:])
def is_file_url(url):
return url.startswith('file://')

def get_swagger_version(obj):
""" get swagger version from loaded json """
Expand Down

0 comments on commit 41b5d31

Please sign in to comment.