-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ExecutableMismatchViolation #9 * Add logic for ExecutableMismatch * Add WPS452 to test_noqa.py #5 * Remove dependency and related test * Implement the full checking logic (#13) * Fix linting problem (#14) * Clean up code (#15) Fix checking logic so that we don't thoroughly check every comment * Fix permissions (#16) * Make a new visitor to handle files with 0 comments (#17) * Add tests for ExecutableMismatchViolation (#18) * Add ExecutableMismatchViolation #9 * Add WPS452 to test_noqa.py #5 * Remove dependency and related test * Add test resource files from flake8-executable * Add tests for check_valid_shebang #7 * Implement the full checking logic (#13) * Fix linting problem (#14) * Clean up code (#15) Fix checking logic so that we don't thoroughly check every comment * Fix permissions (#16) * Ignore failing test files * Update test file to be compatible with the new visitor * Fix linter issues * Make a new visitor to handle files with 0 comments (#17) * Exclude test files from linting Co-authored-by: Hanzhang <48179160+hanzhsun@users.noreply.github.com> Co-authored-by: jrutqvist <joar.rutqvist@gmail.com> Co-authored-by: Gabriel Chang <Gc.chang95@gmail.com> * Update CHANGELOG.md * Moved test resources to fixtures to be ignored by autopep8 * Add integration test * Refactor tests * Make the requested changes Use temporary files for testing * Update noqa.py * Make additional requested changes * Fix small formatting and typing issues * Update comments.py * Update comments.py Co-authored-by: hanzhsu <emily_1999@link.cuhk.edu.hk> Co-authored-by: Gabriel Chang <Gc.chang95@gmail.com> Co-authored-by: Hanzhang <48179160+hanzhsun@users.noreply.github.com> Co-authored-by: jrutqvist <joar.rutqvist@gmail.com> Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
- Loading branch information
1 parent
6cecf90
commit cf3aef9
Showing
14 changed files
with
378 additions
and
21 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#!/usr/bin/env perl | ||
from sys import * | ||
import sys | ||
from typing import List, Union, Dict | ||
|
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
30 changes: 30 additions & 0 deletions
30
tests/test_visitors/test_tokenize/test_comments/conftest.py
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 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from os import chmod | ||
|
||
import pytest | ||
|
||
TEMP_FOLDER = 'tmp' | ||
MODE_EXECUTABLE = 0o755 | ||
MODE_NON_EXECUTABLE = 0o644 | ||
|
||
|
||
@pytest.fixture() | ||
def make_file(tmp_path): | ||
"""Fixture to make a temporary executable or non executable file.""" | ||
def factory( | ||
filename: str, | ||
file_content: str, | ||
is_executable: bool, | ||
) -> str: | ||
temp_folder = tmp_path / TEMP_FOLDER | ||
temp_folder.mkdir() | ||
test_file = temp_folder / filename | ||
file_mode = MODE_EXECUTABLE if is_executable else MODE_NON_EXECUTABLE | ||
|
||
test_file.write_text(file_content) | ||
chmod(test_file.as_posix(), file_mode) | ||
|
||
return test_file.as_posix() | ||
|
||
return factory |
197 changes: 197 additions & 0 deletions
197
tests/test_visitors/test_tokenize/test_comments/test_valid_shebang.py
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,197 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.violations.best_practices import ShebangViolation | ||
from wemake_python_styleguide.visitors.tokenize.comments import ShebangVisitor | ||
|
||
# Correct | ||
|
||
exe001_neg_shouldnt_be_executable = """ | ||
if __name__ == '__main__': | ||
print('I am not executable.') | ||
""" | ||
|
||
exe001_neg_executable = False | ||
exe001_neg_filename = 'exe001_neg.py' | ||
|
||
exe002_neg_shouldnt_be_executable = """ | ||
def a_lib_function(): | ||
print('I am not executable.') | ||
""" | ||
|
||
exe002_neg_executable = False | ||
exe002_neg_filename = 'exe002_neg.py' | ||
|
||
exe003_neg_good_shebang = """#!/usr/bin/python3 | ||
if __name__ == '__main__': | ||
print('I have a good shebang.') | ||
""" | ||
|
||
exe003_neg_executable = True | ||
exe003_neg_filename = 'exe003_neg.py' | ||
|
||
exe004_neg_no_space_before_shebang = """#!/usr/bin/python3 | ||
if __name__ == '__main__': | ||
print('I do not have whitespace before shebang.') | ||
""" | ||
|
||
exe004_neg_executable = True | ||
exe004_neg_filename = 'exe004_neg.py' | ||
|
||
exe005_neg_nothing_before_shebang = """#!/usr/bin/python3 | ||
if __name__ == '__main__': | ||
print('I do not have any blank or comment lines before shebang.') | ||
""" | ||
|
||
exe005_neg_executable = True | ||
exe005_neg_filename = 'exe005_neg.py' | ||
|
||
# Wrong | ||
|
||
exe001_pos_should_be_executable = """#!/usr/bin/python | ||
if __name__ == '__main__': | ||
print('I should be executable.') | ||
""" | ||
|
||
exe001_pos_executable = False | ||
exe001_pos_filename = 'exe001_pos.py' | ||
|
||
exe002_pos_shouldnt_be_executable = """ | ||
def a_lib_function(): | ||
print("I shouldn't be executable.") | ||
""" | ||
|
||
exe002_pos_executable = True | ||
exe002_pos_filename = 'exe002_pos.py' | ||
|
||
exe003_pos_good_shebang = """#!/bin/bash | ||
if __name__ == '__main__': | ||
print('I have a wrong shebang.') | ||
""" | ||
|
||
exe003_pos_executable = True | ||
exe003_pos_filename = 'exe003_pos.py' | ||
|
||
exe004_pos_no_space_before_shebang = """ #!/usr/bin/python3 | ||
if __name__ == '__main__': | ||
print('I have whitespace before shebang.') | ||
""" | ||
|
||
exe004_pos_executable = True | ||
exe004_pos_filename = 'exe004_pos.py' | ||
|
||
exe005_pos_nothing_before_shebang = """ | ||
# | ||
#!/usr/bin/python3 | ||
if __name__ == '__main__': | ||
print('I have blank and comment lines before shebang.') | ||
""" | ||
|
||
exe005_pos_executable = True | ||
exe005_pos_filename = 'exe005_pos.py' | ||
|
||
|
||
@pytest.mark.parametrize(('filename', 'file_content', 'executable'), [ | ||
( | ||
exe001_neg_filename, | ||
exe001_neg_shouldnt_be_executable, | ||
exe001_neg_executable, | ||
), | ||
( | ||
exe002_neg_filename, | ||
exe002_neg_shouldnt_be_executable, | ||
exe002_neg_executable, | ||
), | ||
( | ||
exe003_neg_filename, | ||
exe003_neg_good_shebang, | ||
exe003_neg_executable, | ||
), | ||
( | ||
exe004_neg_filename, | ||
exe004_neg_no_space_before_shebang, | ||
exe004_neg_executable, | ||
), | ||
( | ||
exe005_neg_filename, | ||
exe005_neg_nothing_before_shebang, | ||
exe005_neg_executable, | ||
), | ||
]) | ||
def test_exe_negative( | ||
make_file, | ||
assert_errors, | ||
parse_file_tokens, | ||
default_options, | ||
filename, | ||
file_content, | ||
executable, | ||
): | ||
"""Testing cases when no errors should be reported.""" | ||
path_to_file = make_file(filename, file_content, executable) | ||
file_tokens = parse_file_tokens(path_to_file) | ||
|
||
visitor = ShebangVisitor( | ||
default_options, | ||
filename=path_to_file, | ||
file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
assert_errors(visitor, []) | ||
|
||
|
||
@pytest.mark.parametrize(('filename', 'file_content', 'executable'), [ | ||
( | ||
exe001_pos_filename, | ||
exe001_pos_should_be_executable, | ||
exe001_pos_executable, | ||
), | ||
( | ||
exe002_pos_filename, | ||
exe002_pos_shouldnt_be_executable, | ||
exe002_pos_executable, | ||
), | ||
( | ||
exe003_pos_filename, | ||
exe003_pos_good_shebang, | ||
exe003_pos_executable, | ||
), | ||
( | ||
exe004_pos_filename, | ||
exe004_pos_no_space_before_shebang, | ||
exe004_pos_executable, | ||
), | ||
( | ||
exe005_pos_filename, | ||
exe005_pos_nothing_before_shebang, | ||
exe005_pos_executable, | ||
), | ||
]) | ||
def test_exe_posititve( | ||
make_file, | ||
assert_errors, | ||
parse_file_tokens, | ||
default_options, | ||
filename, | ||
file_content, | ||
executable, | ||
): | ||
"""Testing cases when no errors should be reported.""" | ||
path_to_file = make_file(filename, file_content, executable) | ||
file_tokens = parse_file_tokens(path_to_file) | ||
|
||
visitor = ShebangVisitor( | ||
default_options, | ||
filename=path_to_file, | ||
file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
assert_errors(visitor, [ShebangViolation]) |
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,8 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
|
||
|
||
def is_executable_file(filename: str) -> bool: | ||
"""Checks if a file is executable.""" | ||
return os.access(filename, os.X_OK) |
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
Oops, something went wrong.