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

Lint additional four files in the tests folder #1708

Merged
merged 4 commits into from
Dec 7, 2021

Conversation

MVrachev
Copy link
Collaborator

@MVrachev MVrachev commented Dec 3, 2021

Related to #1582

Description of the changes being introduced by the pull request:

When preparing the pr which was going to resolve #1582 I had to rename the test files
related to the old codebase. That way I was able to exclude the old test files when linting
the tests/ folder.
While I did that I realized I have to decide whether we want to lint and consequently rename
utils.py, test_utils.py, simple_server.py, and aggregate_tests.py after we remove the files testing the old code.
I decided that it's worth linting them for the following reasons:

  • utils.py provides utilities that are used by our new code such as the TestServerProcess class
    which handles server handling, logging, and the run_sub_tests_with_dataset decorator which
    we used in multiple files.
  • test_utils.py will be needed even after we remove the old test files as we want to have tests
    that verify that TestServerProcess from utils.py is working as expected
  • simple_server.py is the only helper for server startup used in the new code base and
    more precisely to start the server in test_updater_ng.py
  • aggregate_tests.py will be needed to aggregate the test files

That's why I decided to run the linters upon those files.
I tried to summarize the manual changes I had to make in the commit messages
as they weren't many.
Most of the changes are automatic and are result from black and isort.

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

Apply all 4 linters on tests/utils.py, so we can lint it in future and
not rename and exclude it.
It's expected that most if not all code in tests/utils.py will be
useful even when we remove the tests file on the old code.
Keep in mind when reviewing that type annotations where already added
in theupdateframework@e2deff3

Black and isort changes where automatically made.
The only manual changes are:
- pylint disables
- function docstrings
- initializations of attributes of "TestServerProcess" __init__()
- additional asserts of the attributes types

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
Apply all 4 linters on tests/test_utils.py, so we can lint it in the
future and not rename and exclude it.
As we are going to use part or most of tests/utils.py after we remove
the test files testing the old code, then we would need to keep the
test file testing utils.py - test_utils.py

Black and isort changes where automatically made.
The only manual changes are:
- pylint disable once in can_connect
- remove TestServerProcess.tearDown and instead clean it manually in the
tests, so we don't have to define server_process_handler as a class
variable and assert it's type in every test
- move can_connect outside TestServerProcess as it doesn't use self
anymore
- add type annotations
- function docstrings

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
@coveralls
Copy link

coveralls commented Dec 3, 2021

Pull Request Test Coverage Report for Build 1548823628

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 97.581%

Totals Coverage Status
Change from base Build 1548655082: 0.0%
Covered Lines: 4032
Relevant Lines: 4116

💛 - Coveralls

Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Martin! LGTM.

Comment on lines 52 to 57
handler: Type[
Union[SimpleHTTPRequestHandler, QuietHTTPRequestHandler]
] = QuietHTTPRequestHandler

if use_quiet_http_request_handler:
handler = QuietHTTPRequestHandler
else:
handler = SimpleHTTPRequestHandler
if len(sys.argv) > 2:
handler = SimpleHTTPRequestHandler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the purpose of this script the type annotation really seems like an overkill that impairs readability. Would it be problematic if we just did:

if len(sys.argv) > 2 and sys.argv[2]:
  handler = QuietHTTPRequestHandler
else:
  handler = SimpleHTTPRequestHandler

Copy link
Collaborator Author

@MVrachev MVrachev Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way I receive the following two errors by mypy:

tests/simple_server.py:61: error: Cannot assign multiple types to name "handler"  without an explicit "Type[...]" annotation  [misc]

tests/simple_server.py:61: error: Incompatible types in assignment (expression has type "Type[SimpleHTTPRequestHandler]", variable has type "Type[QuietHTTPRequestHandler]")  [assignment]

I wanted to simplify it somehow but didn't come up with an idea of how to do it without a mypy error...
That's how I end up with this ugly line...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could "declare" handler first. Looks pretty unpythonic to me, but a bit nicer then the long line above.

handler: Type[Union[SimpleHTTPRequestHandler, QuietHTTPRequestHandler]]

if len(sys.argv) > 2 and sys.argv[2]:
  handler = QuietHTTPRequestHandler
else:
  handler = SimpleHTTPRequestHandler

But I'm fine with leaving it as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right that's better even if it's not the most pythonic way it's still more understandable.
Updated the code with that suggestion.

+ "!"
)

if line.startswith(expected_msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, did black make this elif an if? Or did pylint flag it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was pylint who flag it with:
R1720: Unnecessary "elif" after "raise" (no-else-raise)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Oh right, I remember that one.

@MVrachev MVrachev mentioned this pull request Dec 6, 2021
3 tasks
@MVrachev
Copy link
Collaborator Author

MVrachev commented Dec 6, 2021

I added a new commit linting aggregate_tests.py as we expect to use it even after we drop the old test files.

@MVrachev MVrachev changed the title Lint additional three files in the tests folder Lint additional four files in the tests folder Dec 6, 2021
Apply all 4 linters on tests/simple_server.py, so we can lint it in the
future and not rename and exclude it.
As we are going to use part or most of tests/utils.py after we remove
the test files testing the old code, then we would need to keep the
simple_server.py file. It's currently used in tests/updater_ng.py
testing the new updater implementation.

Black and isort changes where automatically made.
The only manual changes are:
- pylint disable once in can_connect
- add type annotations
- simplifications around setting up the handler variable
- function docstrings

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
All changes are automatically generated by black.
The other linters didn't find any errors in the file.
It's expected that most if not all code in aggregate_tests.py will be
useful even when we remove the tests file on the old code.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
@lukpueh lukpueh merged commit aebd8b0 into theupdateframework:develop Dec 7, 2021
@MVrachev MVrachev deleted the prepare-for-lint branch December 7, 2021 10:09
@lukpueh lukpueh mentioned this pull request Dec 13, 2021
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 this pull request may close these issues.

tests: Start linting tests
3 participants