Skip to content

Commit

Permalink
Merge pull request #10 from econchick/master
Browse files Browse the repository at this point in the history
Temp fix for py3 builds; mock out writes to file
  • Loading branch information
econchick committed May 14, 2015
2 parents 5ce220e + ff44947 commit 268e85d
Show file tree
Hide file tree
Showing 21 changed files with 339 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ramlfications/data/supported_mime_types.json

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion ramlfications/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ def _yaml_include(self, loader, node):
# Get the path out of the yaml file
file_name = os.path.join(os.path.dirname(loader.name), node.value)

file_ext = os.path.splitext(file_name)[1]
parsable_ext = [".yaml", ".yml", ".raml", ".json"]

if file_ext not in parsable_ext:
with open(file_name) as inputfile:
return inputfile.read()

with open(file_name) as inputfile:
return yaml.load(inputfile)
try:
return yaml.load(inputfile)
except yaml.loader.ConstructorError:
msg = "Recursively-including files not supported."
raise LoadRAMLError(msg)

def _ordered_load(self, stream, loader=yaml.Loader):
"""
Expand Down Expand Up @@ -61,3 +72,6 @@ def load(self, raml):
except yaml.parser.ParserError as e:
msg = "Error parsing RAML: {0}".format(e)
raise LoadRAMLError(msg)
except yaml.constructor.ConstructorError as e:
msg = "Error parsing RAML: {0}".format(e)
raise LoadRAMLError(msg)
9 changes: 8 additions & 1 deletion ramlfications/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ def docs():
return docs or None

def schemas():
return raml.get("schemas")
_schemas = raml.get("schemas")
if not _schemas:
return None
schemas = []
for schema in _schemas:
value = load_schema(list(itervalues(schema))[0])
schemas.append({list(iterkeys(schema))[0]: value})
return schemas or None

def secured_by():
return raml.get("securedBy")
Expand Down
13 changes: 7 additions & 6 deletions ramlfications/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ def parse_xml_data(xml_data):
return all_mime_types


def save_data(mime_types):
current_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(current_dir, "data")
output_file = os.path.join(data_dir, "supported_mime_types.json")

def save_data(output_file, mime_types):
with open(output_file, "w") as f:
json.dump(mime_types, f)

Expand Down Expand Up @@ -140,6 +136,11 @@ def setup_logger():
log.debug("Data received; parsing...")
xml_data = xml_to_dict(raw_data)
mime_types = parse_xml_data(xml_data)
save_data(mime_types)

current_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(current_dir, "data")
output_file = os.path.join(data_dir, "supported_mime_types.json")

save_data(output_file, mime_types)

log.debug("Done! Supported IANA MIME media types have been updated.")
4 changes: 4 additions & 0 deletions tests/data/examples/cyclic_includes.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#%RAML 0.8
title: Sample API Demo - Cyclic Includes
version: v1
first: !include includes/first-level-includes.raml
4 changes: 4 additions & 0 deletions tests/data/examples/include_has_invalid_tag.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#%RAML 0.8
title: Sample API Demo - Invalid Tag in Includes
version: v1
incorrect: !include: includes/invalid_yaml_tag.raml
4 changes: 4 additions & 0 deletions tests/data/examples/includes/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"false": true
}
9 changes: 9 additions & 0 deletions tests/data/examples/includes/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Foo

*Bacon ipsum dolor* amet pork belly _doner_ rump brisket. [Cupim jerky shoulder][0] ball tip, jowl bacon meatloaf shank kielbasa turducken corned beef beef turkey porchetta.

### Doner meatball pork belly andouille drumstick sirloin

Porchetta picanha tail sirloin kielbasa, pig meatball short ribs drumstick jowl. Brisket swine spare ribs picanha t-bone. Ball tip beef tenderloin jowl doner andouille cupim meatball. Porchetta hamburger filet mignon jerky flank, meatball salami beef cow venison tail ball tip pork belly.

[0]: https://baconipsum.com/?paras=1&type=all-meat&start-with-lorem=1
5 changes: 5 additions & 0 deletions tests/data/examples/includes/example.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<false>true</false>
<name>foo</name>
</root>
4 changes: 4 additions & 0 deletions tests/data/examples/includes/first-level-includes.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#%RAML 0.8
foo: bar
baz: blah
second: !include second_level_includes/second-includes.raml
3 changes: 3 additions & 0 deletions tests/data/examples/includes/invalid_yaml_tag.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#%RAML 0.8
blah: fizz
foo: !foo: bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#%RAML 0.8

bar: foo
blah: baz
4 changes: 4 additions & 0 deletions tests/data/examples/invalid_yaml_tag.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#%RAML 0.8
title: Sample API Demo - Invalid YAML Tag
version: v1
incorrect: !foo: baz.raml
8 changes: 8 additions & 0 deletions tests/data/examples/json_includes.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#%RAML 0.8
title: Sample API Demo - JSON Includes
version: v1
schemas:
- json: !include includes/example.json
baseUri: http://foo-json.bar
/foo:
displayName: foo resource
9 changes: 9 additions & 0 deletions tests/data/examples/md_includes.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#%RAML 0.8
title: Sample API Demo - Markdown Includes
version: v1
documentation:
- title: example
content: !include includes/example.md
baseUri: http://foo-markdown.bar
/foo:
displayName: foo resource
8 changes: 8 additions & 0 deletions tests/data/examples/xsd_includes.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#%RAML 0.8
title: Sample API Demo - XSD Includes
version: v1
schemas:
- xml: !include includes/example.xsd
baseUri: http://foo-xml.bar
/foo:
displayName: foo resource
113 changes: 112 additions & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def dict_equal(dict1, dict2):
for k, v1 in list(iteritems(dict1)):
assert k in dict2
v2 = dict2[k]
assert v1, v2
assert v1 == v2
return True


Expand Down Expand Up @@ -60,3 +60,114 @@ def test_yaml_parser_error():
loader.RAMLLoader().load(open(raml_obj))
msg = "Error parsing RAML:"
assert msg in e.value.args[0]


def test_cyclic_includes_raises_error():
raml_file = os.path.join(EXAMPLES + "cyclic_includes.raml")
with pytest.raises(LoadRAMLError) as e:
loader.RAMLLoader().load(open(raml_file))
msg = "Recursively-including files not supported."
assert msg in e.value.args[0]


def test_include_json():
raml_file = os.path.join(EXAMPLES + "json_includes.raml")
with open(raml_file) as f:
raml = loader.RAMLLoader().load(f)

expected_data = {
"title": "Sample API Demo - JSON Includes",
"version": "v1",
"baseUri": "http://foo-json.bar",
"schemas": [{
"json": {
"name": "foo",
"false": True
},
}],
"/foo": {
"displayName": "foo resource"
}
}
assert dict_equal(raml, expected_data)


def test_include_xsd():
raml_file = os.path.join(EXAMPLES + "xsd_includes.raml")
with open(raml_file) as f:
raml = loader.RAMLLoader().load(f)

xml_raw = """<?xml version="1.0" encoding="UTF-8"?>
<root>
<false>true</false>
<name>foo</name>
</root>
"""

expected_data = {
"title": "Sample API Demo - XSD Includes",
"version": "v1",
"baseUri": "http://foo-xml.bar",
"schemas": [{
"xml": xml_raw,
}],
"/foo": {
"displayName": "foo resource",
},
}
assert dict_equal(raml, expected_data)


def test_include_markdown():
raml_file = os.path.join(EXAMPLES + "md_includes.raml")
with open(raml_file) as f:
raml = loader.RAMLLoader().load(f)

markdown_raw = """## Foo
*Bacon ipsum dolor* amet pork belly _doner_ rump brisket. [Cupim jerky \
shoulder][0] ball tip, jowl bacon meatloaf shank kielbasa turducken corned \
beef beef turkey porchetta.
### Doner meatball pork belly andouille drumstick sirloin
Porchetta picanha tail sirloin kielbasa, pig meatball short ribs drumstick \
jowl. Brisket swine spare ribs picanha t-bone. Ball tip beef tenderloin jowl \
doner andouille cupim meatball. Porchetta hamburger filet mignon jerky flank, \
meatball salami beef cow venison tail ball tip pork belly.
[0]: https://baconipsum.com/?paras=1&type=all-meat&start-with-lorem=1
"""

expected_data = {
"title": "Sample API Demo - Markdown Includes",
"version": "v1",
"baseUri": "http://foo-markdown.bar",
"/foo": {
"displayName": "foo resource"
},
"documentation": [{
"title": "example",
"content": markdown_raw,
}],
}
print(raml.get("markdown"))
assert dict_equal(raml, expected_data)


def test_invalid_yaml_tag():
raml_file = os.path.join(EXAMPLES, "invalid_yaml_tag.raml")
with pytest.raises(LoadRAMLError) as e:
loader.RAMLLoader().load(open(raml_file))

msg = "Error parsing RAML:"
assert msg in e.value.args[0]


def test_includes_has_invalid_tag():
raml_file = os.path.join(EXAMPLES, "include_has_invalid_tag.raml")
with pytest.raises(LoadRAMLError) as e:
loader.RAMLLoader().load(open(raml_file))

msg = "Error parsing RAML:"
assert msg in e.value.args[0]
6 changes: 5 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ def test_tree_invalid(runner):
check_result(exp_code, exp_msg, result)


def test_update(runner):
def test_update(runner, monkeypatch):
"""
Successfully update supported mime types
"""
monkeypatch.setattr(
"ramlfications.utils.save_data", lambda x, y: ["foo/bar"]
)
exp_code = 0
exp_msg = None

result = runner.invoke(main.update)
check_result(exp_code, exp_msg, result)
91 changes: 91 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,94 @@ def test_resource_security_scheme(resources):
def test_resource_inherit_parent(resources):
res = resources[2]
assert len(res.uri_params) == 4


#####
# Test Includes parsing
#####
@pytest.fixture(scope="session")
def xml_includes():
raml_file = os.path.join(EXAMPLES + "xsd_includes.raml")
loaded_raml_file = load_file(raml_file)
config = setup_config(EXAMPLES + "test-config.ini")
return pw.parse_raml(loaded_raml_file, config)


def test_includes_xml(xml_includes):
api = xml_includes
assert api.title == "Sample API Demo - XSD Includes"
assert api.version == "v1"
assert api.schemas == [{
"xml": {
"root": {
"false": "true",
"name": "foo",
}
},
}]


@pytest.fixture(scope="session")
def json_includes():
raml_file = os.path.join(EXAMPLES + "json_includes.raml")
loaded_raml_file = load_file(raml_file)
config = setup_config(EXAMPLES + "test-config.ini")
return pw.parse_raml(loaded_raml_file, config)


def test_includes_json(json_includes):
api = json_includes
assert api.title == "Sample API Demo - JSON Includes"
assert api.version == "v1"
assert api.schemas == [{
"json": {
"false": True,
"name": "foo",
},
}]


@pytest.fixture(scope="session")
def md_includes():
raml_file = os.path.join(EXAMPLES + "md_includes.raml")
loaded_raml_file = load_file(raml_file)
config = setup_config(EXAMPLES + "test-config.ini")
return pw.parse_raml(loaded_raml_file, config)


def test_includes_md(md_includes):
api = md_includes
assert api.title == "Sample API Demo - Markdown Includes"
assert api.version == "v1"
markdown_raw = """## Foo
*Bacon ipsum dolor* amet pork belly _doner_ rump brisket. [Cupim jerky \
shoulder][0] ball tip, jowl bacon meatloaf shank kielbasa turducken corned \
beef beef turkey porchetta.
### Doner meatball pork belly andouille drumstick sirloin
Porchetta picanha tail sirloin kielbasa, pig meatball short ribs drumstick \
jowl. Brisket swine spare ribs picanha t-bone. Ball tip beef tenderloin jowl \
doner andouille cupim meatball. Porchetta hamburger filet mignon jerky flank, \
meatball salami beef cow venison tail ball tip pork belly.
[0]: https://baconipsum.com/?paras=1&type=all-meat&start-with-lorem=1
"""
assert api.documentation[0].content.raw == markdown_raw

markdown_html = """<h2>Foo</h2>
<p><em>Bacon ipsum dolor</em> amet pork belly <em>doner</em> rump brisket. \
<a href="https://baconipsum.com/?paras=1&amp;type=all-meat&amp;start-with-\
lorem=1">Cupim jerky shoulder</a> ball tip, jowl bacon meatloaf shank \
kielbasa turducken corned beef beef turkey porchetta.</p>
<h3>Doner meatball pork belly andouille drumstick sirloin</h3>
<p>Porchetta picanha tail sirloin kielbasa, pig meatball short ribs drumstick \
jowl. Brisket swine spare ribs picanha t-bone. Ball tip beef tenderloin jowl \
doner andouille cupim meatball. Porchetta hamburger filet mignon jerky flank, \
meatball salami beef cow venison tail ball tip pork belly.</p>
"""
assert api.documentation[0].content.html == markdown_html
Loading

0 comments on commit 268e85d

Please sign in to comment.