Skip to content

Commit 72fc9da

Browse files
committed
Modified tests to avoid 'bad config encountered during initialization: Unrecognized flag: '-v'' error from nosetests, by using/modifying subprocess.Popen based tests from JupyterHub and earlier versions of NBViewer
1 parent ce29e8b commit 72fc9da

File tree

8 files changed

+33
-72
lines changed

8 files changed

+33
-72
lines changed

nbviewer/providers/github/tests/test_github.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import requests
1010

11-
from unittest import SkipTest
12-
1311
from ....tests.base import NBViewerTestCase, FormatHTMLMixin, skip_unless_github_auth
1412

1513
class GitHubTestCase(NBViewerTestCase):

nbviewer/providers/local/tests/test_localfile.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
#-----------------------------------------------------------------------------
77

88
import requests
9-
import sys
10-
from nbviewer.utils import url_path_join
119

1210
from ....tests.base import NBViewerTestCase, FormatHTMLMixin
1311

1412
class LocalFileDefaultTestCase(NBViewerTestCase):
1513
@classmethod
16-
def get_server_args(cls):
17-
return [
18-
'--localfiles=.',
19-
]
14+
def get_server_cmd(cls):
15+
return super().get_server_cmd() + [ '--localfiles=.' ]
2016

2117
def test_url(self):
2218
## assumes being run from base of this repo
@@ -32,10 +28,8 @@ class FormatHTMLLocalFileDefaultTestCase(LocalFileDefaultTestCase,
3228

3329
class LocalFileRelativePathTestCase(NBViewerTestCase):
3430
@classmethod
35-
def get_server_args(cls):
36-
return [
37-
'--localfiles=nbviewer',
38-
]
31+
def get_server_cmd(cls):
32+
return super().get_server_cmd() + [ '--localfiles=nbviewer' ]
3933

4034
def test_url(self):
4135
## assumes being run from base of this repo

nbviewer/tests/base.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313
import time
1414
import requests
1515
from contextlib import contextmanager
16-
from threading import Thread, Event
1716
from unittest import TestCase, skipIf
1817

1918
from tornado.escape import to_unicode
20-
from tornado.ioloop import IOLoop
2119
from tornado.log import app_log
22-
import tornado.options
2320

2421
from nbviewer.utils import url_path_join
25-
from nbviewer.app import main
2622
from nbviewer.providers.github.client import AsyncGitHubClient
2723

24+
from subprocess import Popen
25+
from subprocess import DEVNULL as devnull
26+
import os
27+
import sys
2828

2929
class NBViewerTestCase(TestCase):
3030
"""A base class for tests that need a running nbviewer server."""
3131

3232
port = 12341
3333

34+
environment_variables = {}
35+
3436
def assertIn(self, observed, expected, *args, **kwargs):
3537
return super().assertIn(
3638
to_unicode(observed),
@@ -69,30 +71,22 @@ def wait_until_dead(cls):
6971
else:
7072
time.sleep(.1)
7173

74+
@classmethod
75+
def get_server_cmd(cls):
76+
return [ sys.executable, '-m', 'nbviewer', '--port=%d' % cls.port, ]
77+
7278
@classmethod
7379
def setup_class(cls):
74-
cls._start_evt = Event()
75-
cls.server = Thread(target=cls._server_main)
76-
cls.server.start()
77-
cls._start_evt.wait()
80+
server_cmd = cls.get_server_cmd()
81+
cls.server = Popen(server_cmd,
82+
stdout=devnull, stderr=devnull,
83+
# Set environment variables if any
84+
env=dict(os.environ, **cls.environment_variables))
7885
cls.wait_until_alive()
7986

80-
@classmethod
81-
def get_server_args(cls):
82-
return []
83-
84-
@classmethod
85-
def _server_main(cls):
86-
cls._server_loop = loop = IOLoop()
87-
loop.make_current()
88-
cls._server_loop.add_callback(cls._start_evt.set)
89-
main(['', '--port=%d' % cls.port] + cls.get_server_args())
90-
loop.close(all_fds=True)
91-
9287
@classmethod
9388
def teardown_class(cls):
94-
cls._server_loop.add_callback(cls._server_loop.stop)
95-
cls.server.join()
89+
cls.server.terminate()
9690
cls.wait_until_dead()
9791

9892
@classmethod

nbviewer/tests/test_app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ def test_generate_config():
8080
assert 'NBViewer.statsd_port' in cfg_text
8181
assert 'NBViewer.statsd_prefix' in cfg_text
8282
assert 'NBViewer.template_path' in cfg_text
83-
assert 'NBViewer.cache' not in cfg_text # Shouldn't be configurable, is a property
8483
assert 'NBViewer.default_endpoint' not in cfg_text # Shouldn't be configurable, is a property
85-
assert 'NBViewer.env' not in cfg_text # Ditto the above
84+
assert 'NBViewer.env' not in cfg_text # Shouldn't be configurable, is a property
8685
assert 'NBViewer.fetch_kwargs' not in cfg_text
8786
assert 'NBViewer.formats' not in cfg_text
8887
assert 'NBViewer.frontpage_setup' not in cfg_text

nbviewer/tests/test_config.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import os
22
from subprocess import Popen
33
from .base import NBViewerTestCase
4-
import sys
54
import requests
6-
from tornado.ioloop import IOLoop
7-
from nbviewer.app import main
85

96
tmpl_fixture = "nbviewer/tests/templates"
107

@@ -19,22 +16,12 @@ def test_used_custom_template(self):
1916

2017
class TemplatePathCLITestCase(NBViewerTestCase, CustomTemplateStub):
2118
@classmethod
22-
def get_server_args(cls):
23-
return super().get_server_args() + [
24-
'--template_path={}'.format(tmpl_fixture),
25-
]
19+
def get_server_cmd(cls):
20+
return super().get_server_cmd() + [
21+
'--template-path={}'.format(tmpl_fixture), ]
2622

2723

28-
class TemplatePathEnvTestCase(NBViewerTestCase, CustomTemplateStub):
29-
30-
@classmethod
31-
def _server_main(cls):
32-
cls._server_loop = loop = IOLoop()
33-
loop.make_current()
34-
cls._server_loop.add_callback(cls._start_evt.set)
3524

36-
# Set environment variable
37-
os.environ['NBVIEWER_TEMPLATE_PATH'] = tmpl_fixture
25+
class TemplatePathEnvTestCase(NBViewerTestCase, CustomTemplateStub):
3826

39-
main(['', '--port=%d' % cls.port] + cls.get_server_args())
40-
loop.close(all_fds=True)
27+
environment_variables = {'NBVIEWER_TEMPLATE_PATH': tmpl_fixture}

nbviewer/tests/test_index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from unittest import SkipTest
2-
31
from .base import NBViewerTestCase
42

3+
from unittest import skip
54
from nbviewer import index
65

76
class ElasticSearchTestCase(NBViewerTestCase):
7+
@skip
8+
@classmethod
89
def test_finish_notebook(self):
910
pass

nbviewer/tests/test_security.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,11 @@ class JupyterHubServiceTestCase(NBViewerTestCase):
6161
'JUPYTERHUB_SERVICE_PREFIX': '/services/nbviewer-test'
6262
}
6363

64+
environment_variables = HUB_SETTINGS
65+
6466
@classmethod
65-
def get_server_args(cls):
66-
return [
67-
'--localfiles=.'
68-
]
69-
70-
@classmethod
71-
def setup_class(cls):
72-
os.environ.update(cls.HUB_SETTINGS)
73-
super().setup_class()
74-
75-
@classmethod
76-
def teardown_class(cls):
77-
for key in cls.HUB_SETTINGS.keys():
78-
del os.environ[key]
79-
super().teardown_class()
67+
def get_server_cmd(cls):
68+
return super().get_server_cmd() + [ '--localfiles=.' ]
8069

8170
def test_login_redirect(self):
8271
url = self.url('/services/nbviewer-test/github/jupyter')

nbviewer/tests/test_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Distributed under the terms of the BSD License. The full license is in
66
# the file COPYING, distributed as part of this software.
77
#-----------------------------------------------------------------------------
8-
from collections import OrderedDict
98

109
import nose.tools as nt
1110

0 commit comments

Comments
 (0)