Skip to content

Commit

Permalink
Merge pull request #235 from martinRenou/flake8
Browse files Browse the repository at this point in the history
Run flake8 on tests dir and setup.py
  • Loading branch information
martinRenou committed Jun 13, 2019
2 parents b5b386e + f9d8ba6 commit 6a1d979
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -23,7 +23,7 @@ install:
- pip install ".[test]"
- cd tests/test_template; pip install .; cd ../../;
before_script:
- flake8 voila
- flake8 voila tests setup.py
script:
- VOILA_TEST_DEBUG=1 py.test tests/
- voila --help # Making sure we can run `voila --help`
32 changes: 18 additions & 14 deletions setup.py
Expand Up @@ -8,7 +8,7 @@
from setuptools.command.bdist_egg import bdist_egg

from io import BytesIO
from subprocess import check_call
from subprocess import check_call, CalledProcessError

import os
import sys
Expand All @@ -28,12 +28,14 @@

npm_path = os.pathsep.join([
os.path.join(node_root, 'node_modules', '.bin'),
os.environ.get('PATH', os.defpath),
os.environ.get('PATH', os.defpath),
])


def in_read_the_docs():
return os.environ.get('READTHEDOCS') == 'True'


def js_first(command, strict=False):
"""decorator for building minified js/css prior to another command"""

Expand Down Expand Up @@ -61,6 +63,7 @@ def run(self):
update_package_data(self.distribution)
return DecoratedCommand


def update_package_data(distribution):
"""update package_data to catch changes during setup"""
build_py = distribution.get_command_obj('build_py')
Expand Down Expand Up @@ -91,12 +94,10 @@ def has_npm(self):
try:
check_call(['npm', '--version'])
return True
except:
except CalledProcessError:
return False

def should_run_npm_install(self):
package_json = os.path.join(node_root, 'package.json')
node_modules_exists = os.path.exists(self.node_modules)
return self.has_npm()

def run(self):
Expand Down Expand Up @@ -161,7 +162,7 @@ def _download(self, url):
except Exception as e:
if 'ssl' in str(e).lower():
try:
import pycurl
import pycurl # noqa
except ImportError:
print("Failed, try again after installing PycURL with `pip install pycurl` to avoid outdated SSL.", file=sys.stderr)
raise e
Expand All @@ -181,16 +182,15 @@ def _download_pycurl(self, url):
return buf.getvalue()

def run(self):
css_dest = os.path.join('share', 'jupyter', 'voila', 'templates', 'default', 'static', 'index.css')
theme_light_dest = os.path.join('share', 'jupyter', 'voila', 'templates', 'default', 'static', 'theme-light.css')
theme_dark_dest = os.path.join('share', 'jupyter', 'voila', 'templates', 'default', 'static', 'theme-dark.css')
css_dest = os.path.join('share', 'jupyter', 'voila', 'templates', 'default', 'static', 'index.css')
theme_light_dest = os.path.join('share', 'jupyter', 'voila', 'templates', 'default', 'static', 'theme-light.css')
theme_dark_dest = os.path.join('share', 'jupyter', 'voila', 'templates', 'default', 'static', 'theme-dark.css')

try:
css = self._download(css_url)
theme_light = self._download(theme_light_url)
theme_dark = self._download(theme_dark_url)
except Exception as e:
msg = "Failed to download CSS: %s" % e
except Exception:
if os.path.exists(css_dest) and os.path.exists(theme_light_dest) and os.path.exists(theme_dark_dest):
print("Already have CSS, moving on.")
else:
Expand All @@ -208,6 +208,7 @@ def run(self):
with open(theme_dark_dest, 'wb+') as f:
f.write(theme_dark)


def css_first(command):
class CSSFirst(command):
def run(self):
Expand All @@ -216,7 +217,7 @@ def run(self):
return CSSFirst


class bdist_egg_disabled(bdist_egg):
class BdistEggDisabled(bdist_egg):
"""Disabled version of bdist_egg
Prevents setup.py install performing setuptools' default easy_install,
Expand All @@ -225,20 +226,22 @@ class bdist_egg_disabled(bdist_egg):
def run(self):
sys.exit("Aborting implicit building of eggs. Use `pip install .` to install from source.")


cmdclass = {
'css': FetchCSS,
'jsdeps': NPM,
'build_py': css_first(js_first(build_py)),
'egg_info': css_first(js_first(egg_info)),
'sdist': css_first(js_first(sdist, strict=True)),
'develop' : css_first(develop),
'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled
'develop': css_first(develop),
'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else BdistEggDisabled
}

version_ns = {}
with open(os.path.join(here, 'voila', '_version.py')) as f:
exec(f.read(), {}, version_ns)


def get_data_files():
"""Get the data files for the package.
"""
Expand All @@ -254,6 +257,7 @@ def get_data_files():
data_files.append((dirpath, [os.path.join(dirpath, filename) for filename in filenames]))
return data_files


setup_args = {
'name': 'voila',
'version': version_ns['__version__'],
Expand Down
8 changes: 3 additions & 5 deletions tests/app/config_paths_test.py
@@ -1,6 +1,6 @@
# test all objects that should be configurable
import pytest
import json

import os


Expand All @@ -15,17 +15,15 @@ def voila_config_file_paths_arg():

def test_config_app(voila_app):
assert voila_app.voila_configuration.template == 'test_template'

def test_config_app(voila_app):
assert voila_app.voila_configuration.enable_nbextensions == True
assert voila_app.voila_configuration.enable_nbextensions is True


def test_config_kernel_manager(voila_app):
assert voila_app.kernel_manager.cull_interval == 10


def test_config_contents_manager(voila_app):
assert voila_app.contents_manager.use_atomic_writing == False
assert voila_app.contents_manager.use_atomic_writing is False


@pytest.mark.gen_test
Expand Down
4 changes: 2 additions & 2 deletions tests/app/conftest.py
@@ -1,6 +1,7 @@
import os

import pytest

import voila.app

BASE_DIR = os.path.dirname(__file__)
Expand Down Expand Up @@ -42,8 +43,7 @@ def voila_app(voila_args, voila_config):
yield voila_app
voila_app.clear_instance()


@pytest.fixture
def app(voila_app):
return voila_app.app


6 changes: 4 additions & 2 deletions tests/app/cwd_subdir_test.py
@@ -1,17 +1,19 @@
# test serving a notebook
import pytest


@pytest.fixture
def cwd_subdir_notebook_url(base_url):
return base_url + "/voila/render/subdir/cwd_subdir.ipynb"
return base_url + "/voila/render/subdir/cwd_subdir.ipynb"


@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra


@pytest.mark.gen_test
def test_hello_world(http_client, cwd_subdir_notebook_url):
response = yield http_client.fetch(cwd_subdir_notebook_url)
html_text = response.body.decode('utf-8')
assert 'check for the cwd' in html_text

4 changes: 2 additions & 2 deletions tests/app/cwd_test.py
@@ -1,8 +1,9 @@
# tests the --template argument of voila
import pytest
import base64

import os


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, 'cwd.ipynb')
Expand All @@ -13,4 +14,3 @@ def test_template_cwd(http_client, base_url, notebook_directory):
response = yield http_client.fetch(base_url)
html_text = response.body.decode('utf-8')
assert 'check for the cwd' in html_text

4 changes: 3 additions & 1 deletion tests/app/execute_test.py
@@ -1,13 +1,15 @@
# test basics of voila running a notebook
import pytest

import tornado.web
import tornado.gen

import re
import json

try:
from unittest import mock
except:
except ImportError:
import mock


Expand Down
3 changes: 3 additions & 0 deletions tests/app/image_inlining_test.py
@@ -1,8 +1,11 @@
# tests the --template argument of voila
import pytest

import base64

import os


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, 'images.ipynb')
Expand Down
2 changes: 2 additions & 0 deletions tests/app/nbextensions_test.py
@@ -1,9 +1,11 @@
# tests programmatic config of template sytem
import pytest

import os

BASE_DIR = os.path.dirname(__file__)


@pytest.fixture
def voila_config():
def config(app):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/no_strip_sources_test.py
@@ -1,5 +1,5 @@
import pytest
import json

import os


Expand Down
2 changes: 1 addition & 1 deletion tests/app/serve_directory_test.py
@@ -1,6 +1,7 @@
# test serving a notebook
import pytest


@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
Expand All @@ -12,4 +13,3 @@ def test_hello_world(http_client, print_notebook_url):
response = yield http_client.fetch(print_notebook_url)
assert response.code == 200
assert 'Hi Voila' in response.body.decode('utf-8')

2 changes: 1 addition & 1 deletion tests/app/template_cli_test.py
@@ -1,7 +1,7 @@
# tests programmatic config of template sytem
import pytest

import os
import sys

BASE_DIR = os.path.dirname(__file__)

Expand Down
2 changes: 1 addition & 1 deletion tests/app/template_custom_test.py
@@ -1,7 +1,7 @@
# tests programmatic config of template sytem
import pytest

import os
import sys

BASE_DIR = os.path.dirname(__file__)

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
@@ -1,5 +1,6 @@
# fixtures common for app and server
import os

import pytest


Expand All @@ -13,10 +14,9 @@ def notebook_directory():

@pytest.fixture
def print_notebook_url(base_url):
return base_url + "/voila/render/print.ipynb"
return base_url + "/voila/render/print.ipynb"


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, 'print.ipynb')

2 changes: 2 additions & 0 deletions tests/execute_output_test.py
@@ -1,7 +1,9 @@
import os

from voila.execute import executenb

from nbformat import read, NO_CONVERT

from copy import deepcopy


Expand Down
8 changes: 3 additions & 5 deletions tests/server/conftest.py
@@ -1,7 +1,9 @@
import os

import pytest

from jupyter_server.serverapp import ServerApp

from tornado import httpserver


Expand All @@ -28,7 +30,7 @@ def jupyter_server_args(notebook_directory, jupyter_server_args_extra):
@pytest.fixture
def jupyter_server_app(jupyter_server_args, jupyter_server_config):
jupyter_server_app = ServerApp.instance()
# we monkey patch
# we monkey patch
old_listen = httpserver.HTTPServer.listen
httpserver.HTTPServer.listen = lambda *x, **y: None
# NOTE: in voila's conftest.py we call config after initialize
Expand All @@ -39,10 +41,6 @@ def jupyter_server_app(jupyter_server_args, jupyter_server_config):
ServerApp.clear_instance()




@pytest.fixture
def app(jupyter_server_app):
return jupyter_server_app.web_app


6 changes: 4 additions & 2 deletions tests/server/cwd_subdir_test.py
Expand Up @@ -2,17 +2,19 @@
# we might want to find a better pattern of executing test for the app and extension
import pytest


@pytest.fixture
def cwd_subdir_notebook_url(base_url):
return base_url + "/voila/render/subdir/cwd_subdir.ipynb"
return base_url + "/voila/render/subdir/cwd_subdir.ipynb"


@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra


@pytest.mark.gen_test
def test_hello_world(http_client, cwd_subdir_notebook_url):
response = yield http_client.fetch(cwd_subdir_notebook_url)
html_text = response.body.decode('utf-8')
assert 'check for the cwd' in html_text

9 changes: 0 additions & 9 deletions tests/server/execute_test.py
@@ -1,14 +1,5 @@
# test basics of voila running a notebook
import pytest
import tornado.web
import tornado.gen
import re
import json

try:
from unittest import mock
except:
import mock


@pytest.mark.gen_test
Expand Down

0 comments on commit 6a1d979

Please sign in to comment.