Skip to content

Commit 2b8d899

Browse files
author
Frederic Collonval
committed
fix browser_check
1 parent 4fabf93 commit 2b8d899

File tree

3 files changed

+40
-96
lines changed

3 files changed

+40
-96
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ cache:
55
pip: true
66
directories:
77
- /home/travis/.yarn-cache/
8-
install: pip install pytest jupyterlab
8+
install: pip install pytest "jupyterlab>=1.0.3"
99
script:
1010
- pytest tests/unit
1111
- yarn
1212
- yarn build
1313
- yarn test
1414
- pip install .
15+
- jupyter serverextension enable --py jupyterlab_git
1516
- jupyter labextension install .
1617
- python -m jupyterlab.browser_check
1718
- yarn run lint

tests/test-browser/chrome-test.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
const assert = require('assert');
22
const puppeteer = require('puppeteer');
33
const inspect = require('util').inspect;
4+
const URL = process.argv[2];
45

5-
function getUrl() {
6-
return process.argv[2];
6+
function testGitExtension(html) {
7+
// Test git icons are present
8+
assert(html.includes('--jp-icon-git-clone'), 'Could not find git clone icon.');
9+
assert(html.includes('--jp-icon-git-pull'), 'Could not find git pull icon.');
10+
assert(html.includes('--jp-icon-git-push'), 'Could not find git push icon.');
711
}
812

9-
function testJupyterLabPage(html) {
13+
async function main() {
14+
/* eslint-disable no-console */
15+
console.info('Starting Chrome Headless');
16+
17+
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
18+
const page = await browser.newPage();
19+
20+
console.info('Navigating to page:', URL);
21+
await page.goto(URL);
22+
console.info('Waiting for page to load...');
23+
24+
// Wait for the local file to redirect on notebook >= 6.0
25+
await page.waitForNavigation();
26+
27+
const html = await page.content();
1028
if (inspect(html).indexOf('jupyter-config-data') === -1) {
1129
console.error('Error loading JupyterLab page:');
1230
console.error(html);
1331
}
14-
}
1532

16-
async function testApplication(page) {
1733
const el = await page.waitForSelector('#browserTest', { timeout: 100000 });
1834
console.log('Waiting for application to start...');
35+
let testError = null;
1936

20-
await page.waitForSelector('.completed');
21-
37+
try {
38+
await page.waitForSelector('.completed');
39+
} catch (e) {
40+
testError = e;
41+
}
2242
const textContent = await el.getProperty('textContent');
2343
const errors = JSON.parse(await textContent.jsonValue());
2444

2545
for (let error of errors) {
2646
console.error(`Parsed an error from text content: ${error.message}`, error);
27-
throw error;
2847
}
29-
}
3048

31-
function testGitExtension(html) {
32-
// Test git icons are present
33-
assert(html.includes('--jp-icon-git-clone'), 'Could not find git clone icon.');
34-
assert(html.includes('--jp-icon-git-pull'), 'Could not find git pull icon.');
35-
assert(html.includes('--jp-icon-git-push'), 'Could not find git push icon.');
36-
}
37-
38-
async function main() {
39-
console.info('Starting Chrome Headless');
40-
41-
const URL = getUrl();
42-
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
43-
const page = await browser.newPage();
44-
45-
console.info('Navigating to page:', URL);
46-
await page.goto(URL);
47-
console.info('Waiting for page to load...');
48-
49-
const html = await page.content();
50-
testJupyterLabPage(html);
51-
52-
await testApplication(page);
5349
testGitExtension(html);
5450

5551
await browser.close();
52+
53+
if (testError) {
54+
throw testError;
55+
}
5656
console.info('Chrome test complete');
5757
}
5858

@@ -61,4 +61,4 @@ process.on('unhandledRejection', up => {
6161
throw up;
6262
});
6363

64-
main();
64+
main();
Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,14 @@
11
# -*- coding: utf-8 -*-
2-
# This file is copied from https://github.com/jupyterlab/jupyterlab/blob/master/jupyterlab/browser_check.py
3-
from concurrent.futures import ThreadPoolExecutor
2+
# This file reproduces https://github.com/jupyterlab/jupyterlab/blob/master/jupyterlab/browser_check.py
3+
# but patch the global `here` to use the local chrome-test.js
44
import os
5-
import shutil
6-
import sys
7-
import subprocess
8-
9-
from jupyterlab.labapp import LabApp, get_app_dir
10-
from notebook.notebookapp import flags, aliases
11-
from tornado.ioloop import IOLoop
12-
from traitlets import Bool
5+
from unittest.mock import patch
136

7+
from jupyterlab.browser_check import BrowserApp
148

159
here = os.path.abspath(os.path.dirname(__file__))
16-
test_flags = dict(flags)
17-
test_flags['core-mode'] = (
18-
{'BrowserApp': {'core_mode': True}},
19-
"Start the app in core mode."
20-
)
21-
test_flags['dev-mode'] = (
22-
{'BrowserApp': {'dev_mode': True}},
23-
"Start the app in dev mode."
24-
)
25-
26-
27-
test_aliases = dict(aliases)
28-
test_aliases['app-dir'] = 'BrowserApp.app_dir'
29-
30-
31-
class BrowserApp(LabApp):
32-
33-
open_browser = Bool(False)
34-
base_url = '/lab/'
35-
ip = '127.0.0.1'
36-
flags = test_flags
37-
aliases = test_aliases
38-
39-
def start(self):
40-
web_app = self.web_app
41-
web_app.settings.setdefault('page_config_data', dict())
42-
web_app.settings['page_config_data']['browserTest'] = True
43-
web_app.settings['page_config_data']['buildAvailable'] = False
44-
45-
pool = ThreadPoolExecutor()
46-
future = pool.submit(run_browser, self.display_url)
47-
IOLoop.current().add_future(future, self._browser_finished)
48-
super(BrowserApp, self).start()
49-
50-
def _browser_finished(self, future):
51-
try:
52-
sys.exit(future.result())
53-
except Exception as e:
54-
self.log.error(str(e))
55-
sys.exit(1)
56-
57-
58-
def run_browser(url):
59-
"""Run the browser test and return an exit code.
60-
"""
61-
target = os.path.join(get_app_dir(), 'browser_test')
62-
if not os.path.exists(os.path.join(target, 'node_modules')):
63-
os.makedirs(target)
64-
subprocess.call(["yarn"], cwd=target)
65-
subprocess.call(["yarn", "add", "puppeteer"], cwd=target)
66-
shutil.copy(os.path.join(here, 'chrome-test.js'), os.path.join(target, 'chrome-test.js'))
67-
return subprocess.check_call(["node", "chrome-test.js", url], cwd=target)
6810

6911

7012
if __name__ == '__main__':
71-
BrowserApp.launch_instance()
13+
with patch('jupyterlab.browser_check.here', here):
14+
BrowserApp.launch_instance()

0 commit comments

Comments
 (0)