Skip to content

Commit 7a77f08

Browse files
authored
Merge pull request #26 from timkpaine/master
Setting up testing/a few cleanup items
2 parents af54dc3 + 4387a02 commit 7a77f08

File tree

8 files changed

+197
-4
lines changed

8 files changed

+197
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# nbresuse
2+
[![Build Status](https://dev.azure.com/tpaine154/jupyter/_apis/build/status/timkpaine.nbresuse?branchName=master)](https://dev.azure.com/tpaine154/jupyter/_build/latest?definitionId=17&branchName=master)
3+
[![Coverage](https://img.shields.io/azure-devops/coverage/tpaine154/jupyter/17)](https://dev.azure.com/tpaine154/jupyter/_build?definitionId=17&_a=summary)
4+
[![PyPI](https://img.shields.io/pypi/l/nbresuse.svg)](https://pypi.python.org/pypi/nbresuse)
5+
[![PyPI](https://img.shields.io/pypi/v/nbresuse.svg)](https://pypi.python.org/pypi/nbresuse)
6+
7+
28

39
![Screenshot with memory limit](screenshot.png)
410

azure-pipelines.yml

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
trigger:
2+
- master
3+
4+
jobs:
5+
- job: 'Linux'
6+
pool:
7+
vmImage: 'ubuntu-latest'
8+
9+
strategy:
10+
matrix:
11+
Python37:
12+
python.version: '3.7'
13+
14+
steps:
15+
- task: UsePythonVersion@0
16+
inputs:
17+
versionSpec: '$(python.version)'
18+
displayName: 'Use Python $(python.version)'
19+
20+
- script: |
21+
python -m pip install --upgrade pip
22+
pip install -e .[dev]
23+
displayName: 'Install dependencies'
24+
25+
- script: |
26+
python -m flake8 nbresuse
27+
displayName: 'Lint'
28+
29+
- script:
30+
python -m pytest -vvv nbresuse --cov=nbresuse --junitxml=python_junit.xml --cov-report=xml --cov-branch
31+
displayName: 'Test'
32+
33+
- task: PublishTestResults@2
34+
condition: succeededOrFailed()
35+
inputs:
36+
testResultsFiles: 'python_junit.xml'
37+
testRunTitle: 'Publish test results for Python $(python.version) $(manylinux_flag)'
38+
39+
- task: PublishCodeCoverageResults@1
40+
inputs:
41+
codeCoverageTool: Cobertura
42+
summaryFileLocation: '$(System.DefaultWorkingDirectory)/*coverage.xml'
43+
44+
- job: 'Mac'
45+
pool:
46+
vmImage: 'macos-10.14'
47+
48+
strategy:
49+
matrix:
50+
Python37:
51+
python.version: '3.7'
52+
53+
steps:
54+
- task: UsePythonVersion@0
55+
inputs:
56+
versionSpec: '$(python.version)'
57+
displayName: 'Use Python $(python.version)'
58+
59+
- script: |
60+
python -m pip install --upgrade pip
61+
pip install -e .[dev]
62+
displayName: 'Install dependencies'
63+
64+
- script: |
65+
python -m flake8 nbresuse
66+
displayName: 'Lint'
67+
68+
- script: |
69+
python -m pytest -vvv nbresuse --cov=nbresuse --junitxml=python_junit.xml --cov-report=xml --cov-branch
70+
displayName: 'Test'
71+
72+
- task: PublishTestResults@2
73+
condition: succeededOrFailed()
74+
inputs:
75+
testResultsFiles: 'python_junit.xml'
76+
testRunTitle: 'Publish test results for Python $(python.version) $(manylinux_flag)'
77+
78+
- task: PublishCodeCoverageResults@1
79+
inputs:
80+
codeCoverageTool: Cobertura
81+
summaryFileLocation: '$(System.DefaultWorkingDirectory)/*coverage.xml'
82+
83+
- job: 'Windows'
84+
pool:
85+
vmImage: 'vs2017-win2016'
86+
87+
strategy:
88+
matrix:
89+
Python37:
90+
python.version: '3.7'
91+
92+
steps:
93+
- task: UsePythonVersion@0
94+
inputs:
95+
versionSpec: '$(python.version)'
96+
displayName: 'Use Python $(python.version)'
97+
98+
- script: |
99+
which python > python.txt
100+
set /p PYTHON=<python.txt
101+
ln -s %PYTHON% %PYTHON%$(python.version)
102+
python --version
103+
which python$(python.version)
104+
displayName: "Which python"
105+
106+
- script: |
107+
python -m pip install --upgrade pip
108+
pip install -e .[dev]
109+
displayName: 'Install dependencies'
110+
111+
- script: |
112+
python -m flake8 nbresuse
113+
displayName: 'Lint'
114+
115+
- script: |
116+
python -m pytest -vvv nbresuse --cov=nbresuse --junitxml=python_junit.xml --cov-report=xml --cov-branch
117+
displayName: 'Test'
118+
119+
- task: PublishTestResults@2
120+
condition: succeededOrFailed()
121+
inputs:
122+
testResultsFiles: 'python_junit.xml'
123+
testRunTitle: 'Publish test results for Python $(python.version) $(manylinux_flag)'
124+
125+
- task: PublishCodeCoverageResults@1
126+
inputs:
127+
codeCoverageTool: Cobertura
128+
summaryFileLocation: '$(System.DefaultWorkingDirectory)/*coverage.xml'

nbresuse/metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_cpu_percent(p):
3737
return p.cpu_percent(interval=0.05)
3838
# Avoid littering logs with stack traces complaining
3939
# about dead processes having no CPU usage
40-
except:
40+
except BaseException:
4141
return 0
4242
cpu_percent = sum([get_cpu_percent(p) for p in all_processes])
4343

nbresuse/tests/__init__.py

Whitespace-only changes.

nbresuse/tests/test_basic.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from mock import MagicMock, patch
2+
3+
4+
class TestBasic:
5+
'''Some basic tests, checking import, making sure APIs remain consistent, etc'''
6+
7+
def test_import_serverextension(self):
8+
'''Check that serverextension hooks are available'''
9+
from nbresuse import _jupyter_server_extension_paths, \
10+
_jupyter_nbextension_paths, \
11+
load_jupyter_server_extension
12+
13+
assert _jupyter_server_extension_paths() == [{'module': 'nbresuse'}]
14+
assert _jupyter_nbextension_paths() == [{
15+
"section": "notebook",
16+
"dest": "nbresuse",
17+
"src": "static",
18+
"require": "nbresuse/main"
19+
}]
20+
21+
# mock a notebook app
22+
nbapp_mock = MagicMock()
23+
nbapp_mock.web_app.settings = {}
24+
25+
# mock these out for unit test
26+
with patch('tornado.ioloop.PeriodicCallback') as periodic_callback_mock, \
27+
patch('nbresuse.ResourceUseDisplay') as resource_use_display_mock, \
28+
patch('nbresuse.PrometheusHandler') as prometheus_handler_mock:
29+
30+
# load up with mock
31+
load_jupyter_server_extension(nbapp_mock)
32+
33+
# assert that we installed the application in settings
34+
print(nbapp_mock.web_app.settings)
35+
assert 'nbresuse_display_config' in nbapp_mock.web_app.settings
36+
37+
# assert that we instantiated a periodic callback with the fake
38+
# prometheus
39+
assert periodic_callback_mock.return_value.start.call_count == 1
40+
assert prometheus_handler_mock.call_count == 1
41+
prometheus_handler_mock.assert_called_with(nbapp_mock)

nbresuse/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import six
33

44
# copy-pasted from the master of Traitlets source
5+
6+
57
class Callable(TraitType):
68
"""A trait which is callable.
79
Notes
@@ -15,4 +17,4 @@ def validate(self, obj, value):
1517
if six.callable(value):
1618
return value
1719
else:
18-
self.error(obj, value)
20+
self.error(obj, value)

setup.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[bdist_wheel]
2+
universal=1
3+
4+
[metadata]
5+
description_file = README.md
6+
7+
[flake8]
8+
max-line-length=200
9+
exclude=nbresuse/tests

setup.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
description="Simple Jupyter extension to show how much resources (RAM) your notebook is using",
1010
packages=setuptools.find_packages(),
1111
install_requires=[
12-
'psutil',
13-
'notebook',
12+
'psutil>=5.6.0',
13+
'notebook>=5.6.0',
1414
],
15+
extras_require={
16+
'dev': ['autopep8',
17+
'pytest',
18+
'flake8',
19+
'pytest-cov>=2.6.1',
20+
'mock']
21+
},
1522
data_files=[
1623
('share/jupyter/nbextensions/nbresuse', glob('nbresuse/static/*')),
1724
('etc/jupyter/jupyter_notebook_config.d', ['nbresuse/etc/serverextension.json']),

0 commit comments

Comments
 (0)