Skip to content

Commit

Permalink
Merge pull request #433 from afshin/open-browser
Browse files Browse the repository at this point in the history
More complex handling of open_browser from extension applications
  • Loading branch information
Zsailer authored Mar 4, 2021
2 parents d07853e + d18194f commit 36218db
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ jobs:
- name: Run the tests
if: ${{ matrix.python-version != 'pypy3' }}
run: |
pytest -vv --cov jupyter_server --cov-branch --cov-report term-missing:skip-covered
pytest -vv jupyter_server --cov jupyter_server --cov-branch --cov-report term-missing:skip-covered
- name: Run the tests on pypy
if: ${{ matrix.python-version == 'pypy3' }}
run: |
pytest -vv
pytest -vv jupyter_server
- name: Install the Python dependencies for the examples
run: |
cd examples/simple && pip install -e .
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ jobs:
- name: Run the tests
if: ${{ matrix.python-version != 'pypy3' }}
run: |
pytest -vv --cov jupyter_server --cov-branch --cov-report term-missing:skip-covered
pytest -vv jupyter_server --cov jupyter_server --cov-branch --cov-report term-missing:skip-covered
- name: Run the tests on pypy
if: ${{ matrix.python-version == 'pypy3' }}
run: |
pytest -vv
pytest -vv jupyter_server
- name: Install the Python dependencies for the examples
run: |
cd examples/simple && pip install -e .
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
# the file descriptions opened by the asyncio IOLoop.
# This leads to a nasty, flaky race condition that we haven't
# been able to solve.
pytest -vv -s
pytest -vv jupyter_server -s
- name: Install the Python dependencies for the examples
run: |
cd examples/simple && pip install -e .
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Launch with:
To test an installed `jupyter_server`, run the following:

$ pip install jupyter_server[test]
$ pytest --pyargs jupyter_server
$ pytest jupyter_server

## Contributing

Expand Down
5 changes: 4 additions & 1 deletion jupyter_server/extension/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ class method. This method can be set as a entry_point in
# the default value to False if they don't offer a browser
# based frontend.
open_browser = Bool(
True,
help="""Whether to open in a browser after starting.
The specific browser used is platform dependent and
determined by the python standard library `webbrowser`
Expand All @@ -163,6 +162,10 @@ class method. This method can be set as a entry_point in
"""
).tag(config=True)

@default('open_browser')
def _default_open_browser(self):
return self.serverapp.config["ServerApp"].get("open_browser", True)

# The extension name used to name the jupyter config
# file, jupyter_{name}_config.
# This should also match the jupyter subcommand used to launch
Expand Down
6 changes: 6 additions & 0 deletions jupyter_server/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,9 @@ def inner(nbpath):
nbtext = nbformat.writes(nb, version=4)
nbpath.write_text(nbtext)
return inner


@pytest.fixture(autouse=True)
def jp_server_cleanup():
yield
ServerApp.clear_instance()
9 changes: 8 additions & 1 deletion jupyter_server/tests/extension/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from .mockextensions.app import MockExtensionApp


mock_html = """
Expand Down Expand Up @@ -41,4 +42,10 @@ def config_file(jp_config_dir):
""""""
f = jp_config_dir.joinpath("jupyter_mockextension_config.py")
f.write_text("c.MockExtensionApp.mock_trait ='config from file'")
return f
return f


@pytest.fixture(autouse=True)
def jp_mockextension_cleanup():
yield
MockExtensionApp.clear_instance()
4 changes: 4 additions & 0 deletions jupyter_server/tests/extension/mockextensions/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class MockExtensionApp(ExtensionAppJinjaMixin, ExtensionApp):
mock_trait = Unicode('mock trait', config=True)
loaded = False

@staticmethod
def get_extension_package():
return "jupyter_server.tests.extension.mockextensions"

def initialize_handlers(self):
self.handlers.append(('/mock', MockExtensionHandler))
self.handlers.append(('/mock_template', MockExtensionTemplateHandler))
Expand Down
22 changes: 22 additions & 0 deletions jupyter_server/tests/extension/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from traitlets.config import Config
from jupyter_server.serverapp import ServerApp
from .mockextensions.app import MockExtensionApp


@pytest.fixture
Expand Down Expand Up @@ -67,3 +69,23 @@ def test_extensionapp_load_config_file(
assert mock_extension.config_file_name == 'jupyter_mockextension_config'
# Assert that the trait is updated by config file
assert mock_extension.mock_trait == 'config from file'


OPEN_BROWSER_COMBINATIONS = (
(True, {}),
(True, {'ServerApp': {'open_browser': True}}),
(False, {'ServerApp': {'open_browser': False}}),
(True, {'MockExtensionApp': {'open_browser': True}}),
(False, {'MockExtensionApp': {'open_browser': False}}),
(True, {'ServerApp': {'open_browser': True}, 'MockExtensionApp': {'open_browser': True}}),
(False, {'ServerApp': {'open_browser': True}, 'MockExtensionApp': {'open_browser': False}}),
(True, {'ServerApp': {'open_browser': False}, 'MockExtensionApp': {'open_browser': True}}),
(False, {'ServerApp': {'open_browser': False}, 'MockExtensionApp': {'open_browser': False}}),
)

@pytest.mark.parametrize(
'expected_value, config', OPEN_BROWSER_COMBINATIONS
)
def test_browser_open(monkeypatch, jp_environ, config, expected_value):
serverapp = MockExtensionApp.initialize_server(config=Config(config))
assert serverapp.open_browser == expected_value
2 changes: 0 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[pytest]
# Exclude the example tests.
norecursedirs = examples/*
# Work around for https://github.com/pytest-dev/pytest/issues/4039
addopts = --pyargs jupyter_server

0 comments on commit 36218db

Please sign in to comment.