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

[pylint] Updating PylintCheckers testing #23707

Merged
merged 11 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/pylint_custom_plugin/tests/test_files/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Test for CheckNamingMismatchGeneratedCode

from something import Something
from something2 import something2 as somethingTwo

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------


class Something():
def __init__(self):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Something():
def __init__(self):
pass
152 changes: 135 additions & 17 deletions scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,25 @@ def test_guidelines_link_active(self):
class TestFileHasCopyrightHeader(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.FileHasCopyrightHeader

# Unable to use the astroid for this testcase.
def test_copyright_header_acceptable(self):
file = open("./test_files/copyright_header_acceptable.py")
node = astroid.parse(file.read())
file.close()

with self.assertNoMessages():
self.checker.visit_module(node)

def test_copyright_header_violation(self):
file = open("./test_files/copyright_header_violation.py")
node = astroid.parse(file.read())
file.close()

with self.assertAddsMessages(
pylint.testutils.Message(
msg_id="file-needs-copyright-header", node=node
)
):
self.checker.visit_module(node)

def test_guidelines_link_active(self):
url = "https://azure.github.io/azure-sdk/policies_opensource.html"
Expand Down Expand Up @@ -2157,6 +2175,89 @@ def test_guidelines_link_active(self):
response = client._pipeline.run(request)
assert response.http_response.status_code == 200

class TestPackageNameDoesNotUseUnderscoreOrPeriod(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.PackageNameDoesNotUseUnderscoreOrPeriod

def test_package_name_acceptable(self):

package_name = astroid.extract_node(
"""
PACKAGE_NAME = "correct-package-name"
"""
)
module_node = astroid.Module(name = "node", file="setup.py", doc = """ """)
module_node.body = [package_name]

with self.assertNoMessages():
self.checker.visit_module(module_node)

def test_package_name_violation(self):

package_name = astroid.extract_node(
"""
PACKAGE_NAME = "incorrect.package-name"
"""
)
module_node = astroid.Module(name = "node", file="setup.py", doc = """ """)
module_node.body = [package_name]

with self.assertAddsMessages(
pylint.testutils.Message(
msg_id="package-name-incorrect", node=module_node
)
):
self.checker.visit_module(module_node)

def test_guidelines_link_active(self):
url = "https://azure.github.io/azure-sdk/python_design.html#packaging"
config = Configuration()
client = PipelineClient(url, config=config)
request = client.get(url)
response = client._pipeline.run(request)
assert response.http_response.status_code == 200

class TestServiceClientUsesNameWithClientSuffix(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.ServiceClientUsesNameWithClientSuffix

def test_client_suffix_acceptable(self):
client_node = astroid.extract_node(
"""
class MyClient():
def __init__(self):
pass
"""
)
module_node = astroid.Module(name = "node", file="_my_client.py", doc = """ """)
module_node.body = [client_node]

with self.assertNoMessages():
self.checker.visit_module(module_node)

def test_client_suffix_violation(self):
client_node = astroid.extract_node(
"""
class Violation():
def __init__(self):
pass
"""
)
module_node = astroid.Module(name = "node", file="_my_client.py", doc = """ """)
module_node.body = [client_node]
with self.assertAddsMessages(
pylint.testutils.Message(
msg_id="client-suffix-needed", node=module_node
)
):
self.checker.visit_module(module_node)

def test_guidelines_link_active(self):
url = "https://azure.github.io/azure-sdk/python_design.html#service-client"
config = Configuration()
client = PipelineClient(url, config=config)
request = client.get(url)
response = client._pipeline.run(request)
assert response.http_response.status_code == 200


class TestClientMethodNamesDoNotUseDoubleUnderscorePrefix(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.ClientMethodNamesDoNotUseDoubleUnderscorePrefix
Expand Down Expand Up @@ -2341,7 +2442,6 @@ def test_guidelines_link_active(self):
response = client._pipeline.run(request)
assert response.http_response.status_code == 200


class TestCheckDocstringAdmonitionNewline(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.CheckDocstringAdmonitionNewline

Expand Down Expand Up @@ -2573,17 +2673,7 @@ def __init__(self):
class TestCheckNamingMismatchGeneratedCode(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.CheckNamingMismatchGeneratedCode

def test_ignores_correct_alias_code(self):
module_node = astroid.extract_node(
"""
import something as somethingElse
"""
)

with self.assertNoMessages():
self.checker.visit_module(module_node)

def test_catches_incorrect_import_alias_code(self):
def test_import_naming_mismatch_violation(self):
import_one = astroid.extract_node(
'import Something'

Expand Down Expand Up @@ -2614,7 +2704,7 @@ def test_catches_incorrect_import_alias_code(self):
):
self.checker.visit_module(module_node)

def test_catches_incorrect_from_import_alias_code(self):
def test_import_from_naming_mismatch_violation(self):
import_one = astroid.extract_node(
'import Something'

Expand Down Expand Up @@ -2645,7 +2735,7 @@ def test_catches_incorrect_from_import_alias_code(self):
):
self.checker.visit_module(module_node)

def test_ignores_unaliased_import_init(self):
def test_naming_mismatch_acceptable(self):
import_one = astroid.extract_node(
'import Something'

Expand All @@ -2669,7 +2759,7 @@ def test_ignores_unaliased_import_init(self):
with self.assertNoMessages():
self.checker.visit_module(module_node)

def test_disable_pylint_alias(self):
def test_naming_mismatch_pylint_disable(self):

file = open("./test_files/__init__.py")
node = astroid.parse(file.read())
Expand All @@ -2678,6 +2768,14 @@ def test_disable_pylint_alias(self):
with self.assertNoMessages():
self.checker.visit_module(node)

def test_guidelines_link_active(self):
url = "https://github.com/Azure/autorest/blob/main/docs/generate/built-in-directives.md"
l0lawrence marked this conversation as resolved.
Show resolved Hide resolved
config = Configuration()
client = PipelineClient(url, config=config)
request = client.get(url)
response = client._pipeline.run(request)
assert response.http_response.status_code == 200

class TestCheckEnum(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.CheckEnum

Expand Down Expand Up @@ -2784,6 +2882,19 @@ def test_enum_file_both_violation(self):

self.checker.visit_classdef(node.body[1])

def test_guidelines_link_active(self):
response_one = self._create_url_pipeline("https://azure.github.io/azure-sdk/python_design.html#enumerations")
assert response_one.http_response.status_code == 200
response_two = self._create_url_pipeline("https://azure.github.io/azure-sdk/python_implementation.html#extensible-enumerations")
assert response_two.http_response.status_code == 200

def _create_url_pipeline(self,url):
config = Configuration()
client = PipelineClient(url, config=config)
request = client.get(url)
response = client._pipeline.run(request)
return response
l0lawrence marked this conversation as resolved.
Show resolved Hide resolved


class TestCheckAPIVersion(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = checker.CheckAPIVersion
Expand Down Expand Up @@ -2836,7 +2947,6 @@ def test_api_version_file_init_acceptable(self):
node = astroid.parse(file.read())
file.close()


with self.assertNoMessages():
self.checker.visit_classdef(node.body[0])

Expand All @@ -2852,3 +2962,11 @@ def test_api_version_file_violation(self):
)
):
self.checker.visit_classdef(node.body[0])

def test_guidelines_link_active(self):
url = "https://azure.github.io/azure-sdk/python_design.html#specifying-the-service-version"
config = Configuration()
client = PipelineClient(url, config=config)
request = client.get(url)
response = client._pipeline.run(request)
assert response.http_response.status_code == 200