Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/rosdoc_lite/doxygenator.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,20 @@ def prepare_tagfiles(tagfile_spec, tagfile_dir, output_subfolder):
print(tag_pair)
if tag_pair['location'].find("http://") == 0:
#We need to download the file from somewhere online
import urllib2
try:
ret = urllib2.urlopen(tag_pair['location'])
if sys.version_info.major < 3:
# python 2
from urllib2 import urlopen, URLError, HTTPError
else:
# python 3
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
except ImportError:
print("Could not import urllib, skipping", file=sys.stderr)
continue

try:
ret = urlopen(tag_pair['location'])
if ret.code != 200:
print("Could not fetch the tagfile from %s, skipping" % tag_pair['location'], file=sys.stderr)
continue
Expand All @@ -128,7 +139,7 @@ def prepare_tagfiles(tagfile_spec, tagfile_dir, output_subfolder):
tagfile.write(ret.read())
tagfile.close()
tagfile_string += "%s=%s " % (tagfile_path, get_doc_path(output_subfolder, tag_pair))
except (urllib2.URLError, urllib2.HTTPError) as e:
except (URLError, HTTPError) as e:
print("Could not fetch the tagfile from %s, skipping" % tag_pair['location'], file=sys.stderr)
continue
elif tag_pair['location'].find("file://") == 0:
Expand Down
62 changes: 62 additions & 0 deletions test/test_doxygenator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import os
import unittest
import sys
if sys.version_info >= (3, 3):
from unittest.mock import patch, mock_open
else:
# Expect the `mock` package for python 2.
# https://pypi.python.org/pypi/mock
from mock import patch, mock_open
from rosdoc_lite import doxygenator as doxy

def builtins_name():
return "__builtin__.open" if sys.version_info.major < 3 else "builtins.open"

def urlopen_name():
return "urllib2.urlopen" if sys.version_info.major < 3 else "urllib.request.urlopen"

class TestGetDocPath(unittest.TestCase):

def setUp(self):
self.tagfile_spec = "test_tagfile_spec.yaml"
self.tagfile_dir = "./test_tagfiles"
self.output_subfolder = "/path/to/"

def tearDown(self):
if os.path.exists(self.tagfile_spec):
os.remove(self.tagfile_spec)
if os.path.exists(self.tagfile_dir):
os.rmdir(self.tagfile_dir)

def test_relative_doc_path(self):
tag_pair = {'docs_url':"relative/path"}
output = doxy.get_doc_path("sub/folders", tag_pair)
Expand All @@ -21,3 +46,40 @@ def test_absolute_doc_path(self):
output = doxy.get_doc_path("sub/folders", tag_pair)
self.assertEqual("https://absolu.te/path/doxygen_output_folder", output)

def test_prepare_tagfiles_valid_http_location(self):
input_tagfile_spec = "- location: http://example.com/tagfile.yaml\n docs_url: http://example.com/docs"
expected_result = os.path.join(self.tagfile_dir, 'tagfile.yaml') + "=http://example.com/docs "
with patch(builtins_name(), mock_open(read_data=input_tagfile_spec)) as _:
with patch(urlopen_name()) as mock_urlopen:
mock_urlopen.return_value.code = 200
mock_urlopen.return_value.read.return_value = b"tagfile_content"

result = doxy.prepare_tagfiles(self.tagfile_spec, self.tagfile_dir, self.output_subfolder)

self.assertEqual(result, expected_result)

def test_prepare_tagfiles_invalid_http_location(self):
input_tagfile_spec = "- location: http://example.com/invalid_tagfile.yaml\n docs_url: http://example.com/docs"
with patch(builtins_name(), mock_open(read_data=input_tagfile_spec)) as _:
with patch(urlopen_name()) as mock_urlopen:
mock_urlopen.return_value.code = 404

result = doxy.prepare_tagfiles(self.tagfile_spec, self.tagfile_dir, self.output_subfolder)

self.assertEqual(result, "")

def test_prepare_tagfiles_valid_file_location(self):
input_tagfile_spec = "- location: file:///path/to/tagfile.yaml\n docs_url: /path/to/docs"
expected_result = "/path/to/tagfile.yaml=/path/to/docs "

with patch(builtins_name(), mock_open(read_data=input_tagfile_spec)) as _:
result = doxy.prepare_tagfiles(self.tagfile_spec, self.tagfile_dir, self.output_subfolder)

self.assertEqual(result, expected_result)

def test_prepare_tagfiles_invalid_location_prefix(self):
input_tagfile_spec = "- location: invalid_prefix://example.com/tagfile.yaml\n docs_url: http://example.com/docs"
with patch(builtins_name(), mock_open(read_data=input_tagfile_spec)) as _:
result = doxy.prepare_tagfiles(self.tagfile_spec, self.tagfile_dir, self.output_subfolder)

self.assertEqual(result, "")