Adjust behaviour of context manager analysis
jamescooke opened this issue · 0 comments
Current behaviour
As of v0.9.0, Flake8-AAA considers that all with
statements wrapping the Act block are part of that Act block. So for a simple test using open()
this is the current required layout:
def test():
with open('f.txt') as f:
result = f.read()
assert result == 'Hello World!\n'
Analysis shows that the with open()
statement has been absorbed into the Act block:
$ python -m flake8_aaa test.py
------+------------------------------------------------------------------------
1 DEF|def test():
2 ACT| with open('f.txt') as f:
3 ACT| result = f.read()
4 BL |
5 ASS| assert result == 'Hello World!\n'
------+------------------------------------------------------------------------
0 | ERRORS
======+========================================================================
PASSED!
Doing an open()
on a file is an Arrange operation - it's preparing for the test. Therefore it should be part of the Arrange block. That means that the Act block just consists of the result assignment line result = f.read()
.
Now for the layout - the README states:
each test is broken down into three distinct parts separated by blank lines.
Therefore there must be a blank line before the result assignment. It is inside the with
statement block and the test is laid out as:
def test():
with open('f.txt') as f:
result = f.read()
assert result == 'Hello World!\n'
However, using this layout with the current version of Flake8-AAA gives an error - analysis shows:
$ python -m flake8_aaa test.py
------+------------------------------------------------------------------------
1 DEF|def test():
2 ACT| with open('f.txt') as f:
3 BL |
^ AAA05 blank line in block
4 ACT| result = f.read()
5 BL |
6 ASS| assert result == 'Hello World!\n'
------+------------------------------------------------------------------------
1 | ERROR
======+========================================================================
FAILED with 1 ERROR
Expected behaviour
No error is raised from the example above, analysis gives line 2 as an Arrange block:
$ python -m flake8_aaa test.py
------+------------------------------------------------------------------------
1 DEF|def test():
2 ARR| with open('f.txt') as f:
3 BL |
4 ACT| result = f.read()
5 BL |
6 ASS| assert result == 'Hello World!\n'
------+------------------------------------------------------------------------
0 | ERRORS
======+========================================================================
PASSED!
Checklist
-
Remove Act block building code that vacuums up parent
with
statements. -
Add new examples to good files:
with
in Arrange,with
in Assert,with
spanning whole test -
Add bad examples for: non-assertive
with
wrapping Act (assignment and raises types). -
Run
pytest
against new / edited good and bad examples. All examples should be executable tests and passing. -
Check how black likes to format
with
statements - there may be incompatibility. -
Update documentation on "Build Act Block" https://flake8-aaa.readthedocs.io/en/stable/discovery.html#build-act-block
-
Update CHANGELOG