-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from jamescooke/context-manager-no-vacuum
Make context manager analysis non-greedy
- Loading branch information
Showing
45 changed files
with
1,064 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
def test(): | ||
def test() -> None: | ||
x = 1 | ||
result = x**2 | ||
|
||
assert result == 4 | ||
assert result == 1 | ||
|
||
|
||
def test_b(hello_world_path) -> None: | ||
with open(hello_world_path) as f: | ||
result = f.read() | ||
|
||
assert result == 'Hello World!\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
def test(): | ||
x = 1 | ||
result = x**2 | ||
assert result == 4 | ||
def test() -> None: | ||
x = 3 | ||
result = x**5 | ||
assert result == 243 | ||
|
||
|
||
def test_b(hello_world_path) -> None: | ||
with open(hello_world_path) as f: | ||
result = f.read() | ||
assert result == 'Hello World!\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
def test(): | ||
x = 1 | ||
def test() -> None: | ||
x = 3 | ||
|
||
result = x**2 | ||
assert result == 4 | ||
result = x**5 | ||
assert result == 243 | ||
|
||
|
||
def test_b(hello_world_path) -> None: | ||
with open(hello_world_path) as f: | ||
|
||
result = f.read() | ||
assert result == 'Hello World!\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pathlib | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def hello_world_path() -> pathlib.Path: | ||
""" | ||
Location of hello_world.txt | ||
""" | ||
return pathlib.Path(__file__).parent / 'data' / 'hello_world.txt' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
from project.auth import user_perms_changed | ||
# You know it"s black because it does the double quotes :D | ||
|
||
from .helpers import catch_signal | ||
|
||
def test_simple(hello_world_path) -> None: | ||
""" | ||
`with` statement is part of arrange. Blank lines are maintained around Act. | ||
""" | ||
with open(hello_world_path) as f: | ||
|
||
def test(api_client, url): | ||
data = {"user_id": 0, "project_permission": "admin"} | ||
result = f.read() | ||
|
||
with catch_signal(user_perms_changed) as callback: | ||
result = api_client.put(url, data=data) | ||
|
||
assert result.status_code == 400 | ||
assert callback.call_count == 0 | ||
assert result == "Hello World!\n" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import io | ||
|
||
import pytest | ||
|
||
|
||
def test_simple(hello_world_path) -> None: | ||
""" | ||
`with` statement is part of arrange. Blank lines are maintained around Act. | ||
""" | ||
with open(hello_world_path) as f: | ||
|
||
result = f.read() | ||
|
||
assert result == 'Hello World!\n' | ||
|
||
|
||
def test_whole(hello_world_path) -> None: | ||
""" | ||
`with` statement wraps whole of test | ||
""" | ||
with open(hello_world_path) as f: | ||
|
||
result = f.read() | ||
|
||
assert result == 'Hello World!\n' | ||
|
||
|
||
def test_extra_arrange(hello_world_path) -> None: | ||
""" | ||
Any extra arrangement goes in the `with` block. | ||
""" | ||
with open(hello_world_path) as f: | ||
f.read() | ||
|
||
result = f.read() | ||
|
||
assert result == '' | ||
|
||
|
||
def test_assert_in_block(hello_world_path) -> None: | ||
""" | ||
Any assertion that needs the `with` block open, goes after Act and a BL. | ||
""" | ||
with open(hello_world_path) as f: | ||
f.read() | ||
|
||
result = f.read() | ||
|
||
assert not f.closed | ||
assert f.closed | ||
assert result == '' | ||
|
||
|
||
def test_pytest_assert_raises_in_block(hello_world_path) -> None: | ||
""" | ||
Checking on a raise in a with block works with Pytest. | ||
""" | ||
with open(hello_world_path) as f: | ||
|
||
with pytest.raises(io.UnsupportedOperation): | ||
f.write('hello back') | ||
|
||
assert f.read() == 'Hello World!\n' | ||
|
||
|
||
def test_pytest_assert_raises_on_with(hello_world_path) -> None: | ||
""" | ||
Checking on the raise from a with statement works with Pytest. | ||
""" | ||
with pytest.raises(ValueError) as excinfo: | ||
with open(hello_world_path, 'zz'): | ||
pass | ||
|
||
assert 'invalid mode' in str(excinfo.value) | ||
|
||
|
||
def test_with_in_assert(hello_world_path) -> None: | ||
""" | ||
Using with statement in Assert block is valid | ||
""" | ||
words = ['Hello', 'World!\n'] | ||
|
||
result = ' '.join(words) | ||
|
||
with open(hello_world_path) as f: | ||
assert result == f.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import io | ||
import pathlib | ||
import unittest | ||
|
||
|
||
class Test(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.hello_world_path = pathlib.Path(__file__).parent.parent / 'data' / 'hello_world.txt' | ||
|
||
def test_assert_raises_in_block(self): | ||
""" | ||
Checking on a raise in a with block works with unittest. | ||
""" | ||
with open(self.hello_world_path) as f: | ||
|
||
with self.assertRaises(io.UnsupportedOperation): | ||
f.write('hello back') | ||
|
||
self.assertEqual(f.read(), 'Hello World!\n') | ||
|
||
def test_assert_raises_on_with(self): | ||
""" | ||
Checking on the raise from a with statement works with Pytest. | ||
""" | ||
with self.assertRaises(ValueError) as cm: | ||
with open(self.hello_world_path, 'zz'): | ||
pass | ||
|
||
self.assertIn('invalid mode', str(cm.exception)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
black | ||
flake8 | ||
mypy | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# | ||
# This file is autogenerated by pip-compile | ||
# To update, run: | ||
# | ||
# pip-compile --output-file=examples.txt examples.in | ||
# | ||
appdirs==1.4.3 # via black | ||
attrs==19.3.0 # via black, pytest | ||
black==19.10b0 # via -r examples.in | ||
click==7.1.1 # via black | ||
entrypoints==0.3 # via flake8 | ||
flake8==3.7.9 # via -r examples.in | ||
mccabe==0.6.1 # via flake8 | ||
more-itertools==8.2.0 # via pytest | ||
mypy-extensions==0.4.3 # via mypy | ||
mypy==0.770 # via -r examples.in | ||
packaging==20.3 # via pytest | ||
pathspec==0.7.0 # via black | ||
pluggy==0.13.1 # via pytest | ||
py==1.8.1 # via pytest | ||
pycodestyle==2.5.0 # via flake8 | ||
pyflakes==2.1.1 # via flake8 | ||
pyparsing==2.4.7 # via packaging | ||
pytest==5.4.1 # via -r examples.in | ||
regex==2020.2.20 # via black | ||
six==1.14.0 # via packaging | ||
toml==0.10.0 # via black | ||
typed-ast==1.4.1 # via black, mypy | ||
typing-extensions==3.7.4.2 # via mypy | ||
wcwidth==0.1.9 # via pytest |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ column_limit = 120 | |
|
||
[tool:pytest] | ||
addopts=--tb=short --color=yes | ||
log_print=False | ||
show_capture = false |
Oops, something went wrong.