Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust behaviour of context manager analysis #146

Closed
7 tasks done
jamescooke opened this issue May 3, 2020 · 0 comments · Fixed by #152
Closed
7 tasks done

Adjust behaviour of context manager analysis #146

jamescooke opened this issue May 3, 2020 · 0 comments · Fixed by #152
Assignees

Comments

@jamescooke
Copy link
Owner

jamescooke commented May 3, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant