mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 13:24:18 +00:00
Add more test cases
This commit is contained in:
parent
86dc39a572
commit
e49feed735
4 changed files with 172 additions and 4 deletions
159
testing/resources/jupyter_case_2.ipynb
Normal file
159
testing/resources/jupyter_case_2.ipynb
Normal file
|
|
@ -0,0 +1,159 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "1a4e6df0",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from typing import List, Dict"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "d72a909f",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def some_function(arg1: List[str], arg2: Dict[str, float]):\n",
|
||||||
|
" print(arg1)\n",
|
||||||
|
" return arg2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "0040a4f4",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Section 1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "3a1b985c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"['a', 'b']\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = some_function(['a', 'b'], {'c': 0.2})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "2dfd7d0b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"assert 3 == 3"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "de5a89c9",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Here is a math formulas:\n",
|
||||||
|
"\n",
|
||||||
|
"$$c = \\sqrt{a^2 + b^2}$$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "4952b6de",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def some_other_function() -> bool:\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Some docstring\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" return False"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "bf1cb851",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"There can be double quotes in non-code cells, like this: \"this\". These should not be changed to single quotes."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "01224536",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"False\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"print(some_other_function())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "b54d0660",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"There can also be double quotes in the cell output. Again, they should not be changed to single quotes."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "dc0edb11",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"abcd\"efg\".\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"print('abcd\"efg\".')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
|
|
@ -1,16 +1,25 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.string_fixer_for_jupyter_notebooks import main
|
from pre_commit_hooks.string_fixer_for_jupyter_notebooks import main
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
def test_rewrite(tmpdir):
|
TESTS = (
|
||||||
with open(get_resource_path('before.ipynb')) as f:
|
('jupyter_case_1_before.ipynb', 'jupyter_case_1_after.ipynb', 1),
|
||||||
|
# the file in Case 2 is not altered, so we reload the same file
|
||||||
|
('jupyter_case_2.ipynb', 'jupyter_case_2.ipynb', 0),
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(('input_file', 'output_file', 'expected_retv'), TESTS)
|
||||||
|
def test_rewrite(input_file, output_file, expected_retv, tmpdir):
|
||||||
|
with open(get_resource_path(input_file)) as f:
|
||||||
before_contents = f.read()
|
before_contents = f.read()
|
||||||
|
|
||||||
with open(get_resource_path('after.ipynb')) as f:
|
with open(get_resource_path(output_file)) as f:
|
||||||
after_contents = f.read()
|
after_contents = f.read()
|
||||||
|
|
||||||
path = tmpdir.join('file.ipynb')
|
path = tmpdir.join('file.ipynb')
|
||||||
path.write(before_contents)
|
path.write(before_contents)
|
||||||
retval = main([str(path)])
|
retval = main([str(path)])
|
||||||
assert path.read() == after_contents
|
assert path.read() == after_contents
|
||||||
assert retval == 1
|
assert retval == expected_retv
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue