@@ -807,40 +807,54 @@ def _raise(*_):
807
807
yield
808
808
signal .alarm (0 )
809
809
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 )
814
813
815
- with self ._timeout (10 ):
814
+ with self ._timeout (1000 ):
816
815
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 ),))
818
818
t .start ()
819
819
sleep (.1 )
820
820
821
821
if not t .is_alive ():
822
- sys .stderr .write (stderr .getvalue ())
822
+ sys .__stderr__ .write (stderr .getvalue ())
823
823
raise AssertionError
824
824
825
825
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
840
831
finally :
841
- pdoc .cli ._httpd .shutdown ()
832
+ pdoc .cli ._httpd .shutdown () # type: ignore
842
833
t .join ()
843
834
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
+
844
858
845
859
if __name__ == '__main__' :
846
860
unittest .main ()
0 commit comments