Skip to content

Commit bb59d80

Browse files
committed
BUG: Support running --http on file paths containing relative imports
Fixes #33
1 parent 53544d8 commit bb59d80

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

pdoc/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ def main(_args=None):
296296
sys.exit(1)
297297
pdoc.tpl_lookup.directories.insert(0, args.template_dir)
298298

299+
# Support loading modules specified as python paths relative to cwd
300+
sys.path.append(os.getcwd())
301+
299302
if args.http:
300303
args.html = True
301304
args.external_links = True
@@ -330,9 +333,6 @@ def docfilter(obj, _filters=args.filter.strip().split(',')):
330333
isinstance(obj, pdoc.Class) and f in obj.doc
331334
for f in _filters)
332335

333-
# Support loading modules specified as python paths relative to cwd
334-
sys.path.append(os.getcwd())
335-
336336
modules = [pdoc.Module(pdoc.import_module(module),
337337
docfilter=docfilter)
338338
for module in args.modules]

pdoc/test/__init__.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -807,40 +807,54 @@ def _raise(*_):
807807
yield
808808
signal.alarm(0)
809809

810-
def test_http(self):
811-
host, port = 'localhost', randint(9000, 12000)
812-
args = '--http :{} pdoc {}'.format(
813-
port, os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE)).split()
810+
@contextmanager
811+
def _http(self, modules: list):
812+
port = randint(9000, 12000)
814813

815-
with self._timeout(10):
814+
with self._timeout(1000):
816815
with redirect_streams() as (stdout, stderr):
817-
t = threading.Thread(target=main, args=(parser.parse_args(args),))
816+
t = threading.Thread(
817+
target=main, args=(parser.parse_args(['--http', ':%d' % port] + modules),))
818818
t.start()
819819
sleep(.1)
820820

821821
if not t.is_alive():
822-
sys.stderr.write(stderr.getvalue())
822+
sys.__stderr__.write(stderr.getvalue())
823823
raise AssertionError
824824

825825
try:
826-
url = 'http://{}:{}/'.format(host, port)
827-
with self.subTest(url='/'):
828-
with urlopen(url, timeout=3) as resp:
829-
html = resp.read()
830-
self.assertIn(b'Python package <code>pdoc</code>', html)
831-
self.assertNotIn(b'gzip', html)
832-
with self.subTest(url='/' + EXAMPLE_MODULE):
833-
with urlopen(url + 'pdoc', timeout=3) as resp:
834-
html = resp.read()
835-
self.assertIn(b'__pdoc__', html)
836-
with self.subTest(url='/csv.ext'):
837-
with urlopen(url + 'csv.ext', timeout=3) as resp:
838-
html = resp.read()
839-
self.assertIn(b'DictReader', html)
826+
yield 'http://localhost:{}/'.format(port)
827+
except Exception:
828+
sys.__stderr__.write(stderr.getvalue())
829+
sys.__stdout__.write(stdout.getvalue())
830+
raise
840831
finally:
841-
pdoc.cli._httpd.shutdown()
832+
pdoc.cli._httpd.shutdown() # type: ignore
842833
t.join()
843834

835+
def test_http(self):
836+
with self._http(['pdoc', os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE)]) as url:
837+
with self.subTest(url='/'):
838+
with urlopen(url, timeout=3) as resp:
839+
html = resp.read()
840+
self.assertIn(b'Python package <code>pdoc</code>', html)
841+
self.assertNotIn(b'gzip', html)
842+
with self.subTest(url='/' + EXAMPLE_MODULE):
843+
with urlopen(url + 'pdoc', timeout=3) as resp:
844+
html = resp.read()
845+
self.assertIn(b'__pdoc__', html)
846+
with self.subTest(url='/csv.ext'):
847+
with urlopen(url + 'csv.ext', timeout=3) as resp:
848+
html = resp.read()
849+
self.assertIn(b'DictReader', html)
850+
851+
def test_file(self):
852+
with chdir(os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE)):
853+
with self._http(['_relative_import']) as url:
854+
with urlopen(url, timeout=3) as resp:
855+
html = resp.read()
856+
self.assertIn(b'<a href="/_relative_import">', html)
857+
844858

845859
if __name__ == '__main__':
846860
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import foo # noqa: F401

pdoc/test/example_pkg/_relative_import/foo.py

Whitespace-only changes.

0 commit comments

Comments
 (0)