Skip to content

Commit

Permalink
[ci] Enable pylint for tests/python/ci
Browse files Browse the repository at this point in the history
This fixes up the pylint issues as part of apache#11414 for the CI tests
  • Loading branch information
driazati committed Jun 10, 2022
1 parent e7f793d commit 4b8b843
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 74 deletions.
1 change: 1 addition & 0 deletions tests/lint/pylint.sh
Expand Up @@ -21,4 +21,5 @@ python3 -m pylint python/tvm --rcfile="$(dirname "$0")"/pylintrc
python3 -m pylint vta/python/vta --rcfile="$(dirname "$0")"/pylintrc
python3 -m pylint tests/python/unittest/test_tvmscript_type.py --rcfile="$(dirname "$0")"/pylintrc
python3 -m pylint tests/python/contrib/test_cmsisnn --rcfile="$(dirname "$0")"/pylintrc
python3 -m pylint tests/python/ci --rcfile="$(dirname "$0")"/pylintrc

17 changes: 17 additions & 0 deletions tests/python/ci/__init__.py
@@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Infrastructure and tests for CI scripts"""
114 changes: 66 additions & 48 deletions tests/python/ci/test_ci.py
Expand Up @@ -14,32 +14,21 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Test various CI scripts and GitHub Actions workflows"""
import subprocess
import sys
import json
from tempfile import tempdir
import textwrap
import pytest
import tvm.testing
from pathlib import Path

from test_utils import REPO_ROOT


class TempGit:
def __init__(self, cwd):
self.cwd = cwd

def run(self, *args, **kwargs):
proc = subprocess.run(["git"] + list(args), encoding="utf-8", cwd=self.cwd, **kwargs)
if proc.returncode != 0:
raise RuntimeError(f"git command failed: '{args}'")

return proc
import pytest
import tvm.testing
from .test_utils import REPO_ROOT, TempGit


def test_cc_reviewers(tmpdir_factory):
"""
Test that reviewers are added from 'cc @someone' messages in PRs
"""
reviewers_script = REPO_ROOT / "tests" / "scripts" / "github_cc_reviewers.py"

def run(pr_body, requested_reviewers, existing_review_users, expected_reviewers):
Expand All @@ -49,7 +38,7 @@ def run(pr_body, requested_reviewers, existing_review_users, expected_reviewers)
git.run("remote", "add", "origin", "https://github.com/apache/tvm.git")
reviews = [{"user": {"login": r}} for r in existing_review_users]
requested_reviewers = [{"login": r} for r in requested_reviewers]
proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[str(reviewers_script), "--dry-run", "--testing-reviews-json", json.dumps(reviews)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand Down Expand Up @@ -113,6 +102,9 @@ def run(pr_body, requested_reviewers, existing_review_users, expected_reviewers)


def test_update_branch(tmpdir_factory):
"""
Test that the last-successful branch script updates successfully
"""
update_script = REPO_ROOT / "tests" / "scripts" / "update_branch.py"

def run(statuses, expected_rc, expected_output):
Expand All @@ -132,7 +124,7 @@ def run(statuses, expected_rc, expected_output):
}
}
}
proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[str(update_script), "--dry-run", "--testonly-json", json.dumps(data)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand Down Expand Up @@ -214,6 +206,9 @@ def run(statuses, expected_rc, expected_output):


def test_skip_ci(tmpdir_factory):
"""
Test that CI is skipped when it should be
"""
skip_ci_script = REPO_ROOT / "tests" / "scripts" / "git_skip_ci.py"

def test(commands, should_skip, pr_title, why):
Expand All @@ -228,7 +223,7 @@ def test(commands, should_skip, pr_title, why):
for command in commands:
git.run(*command)
pr_number = "1234"
proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[str(skip_ci_script), "--pr", pr_number, "--pr-title", pr_title], cwd=git.cwd
)
expected = 0 if should_skip else 1
Expand Down Expand Up @@ -267,7 +262,8 @@ def test(commands, should_skip, pr_title, why):
],
should_skip=False,
pr_title="[no skip ci] test",
why="ci should not be skipped on a branch with [skip ci] in the last commit but not the PR title",
why="ci should not be skipped on a branch with "
"[skip ci] in the last commit but not the PR title",
)

test(
Expand Down Expand Up @@ -307,6 +303,9 @@ def test(commands, should_skip, pr_title, why):


def test_skip_globs(tmpdir_factory):
"""
Test that CI is skipped if only certain files are edited
"""
script = REPO_ROOT / "tests" / "scripts" / "git_skip_ci_globs.py"

def run(files, should_skip):
Expand All @@ -316,7 +315,7 @@ def run(files, should_skip):
git.run("checkout", "-b", "main")
git.run("remote", "add", "origin", "https://github.com/apache/tvm.git")

proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[
str(script),
"--files",
Expand All @@ -342,9 +341,12 @@ def run(files, should_skip):


def test_ping_reviewers(tmpdir_factory):
"""
Test that reviewers are messaged after a time period of inactivity
"""
reviewers_script = REPO_ROOT / "tests" / "scripts" / "ping_reviewers.py"

def run(pr, check):
def run(pr, check): # pylint: disable=invalid-name
git = TempGit(tmpdir_factory.mktemp("tmp_git_dir"))
# Jenkins git is too old and doesn't have 'git init --initial-branch'
git.run("init")
Expand All @@ -361,7 +363,7 @@ def run(pr, check):
}
}
}
proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[
str(reviewers_script),
"--dry-run",
Expand Down Expand Up @@ -486,14 +488,20 @@ def all_time_keys(time):


def assert_in(needle: str, haystack: str):
"""
Check that 'needle' is in 'haystack'
"""
if needle not in haystack:
raise AssertionError(f"item not found:\n{needle}\nin:\n{haystack}")


def test_github_tag_teams(tmpdir_factory):
"""
Check that individuals are tagged from team headers
"""
tag_script = REPO_ROOT / "tests" / "scripts" / "github_tag_teams.py"

def run(type, data, check):
def run(source_type, data, check):
git = TempGit(tmpdir_factory.mktemp("tmp_git_dir"))
git.run("init")
git.run("checkout", "-b", "main")
Expand Down Expand Up @@ -528,9 +536,9 @@ def run(type, data, check):
}
}
env = {
type: json.dumps(data),
source_type: json.dumps(data),
}
proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[
str(tag_script),
"--dry-run",
Expand All @@ -549,8 +557,8 @@ def run(type, data, check):
assert_in(check, proc.stdout)

run(
"ISSUE",
{
source_type="ISSUE",
data={
"title": "A title",
"number": 1234,
"user": {
Expand All @@ -563,12 +571,12 @@ def run(type, data, check):
""".strip()
),
},
"No one to cc, exiting",
check="No one to cc, exiting",
)

run(
"ISSUE",
{
source_type="ISSUE",
data={
"title": "A title",
"number": 1234,
"user": {
Expand All @@ -583,11 +591,11 @@ def run(type, data, check):
""".strip()
),
},
"No one to cc, exiting",
check="No one to cc, exiting",
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "A title",
"number": 1234,
Expand All @@ -602,11 +610,12 @@ def run(type, data, check):
something"""
),
},
check="would have updated issues/1234 with {'body': '\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}",
check="would have updated issues/1234 with {'body': "
"'\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}",
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "A title",
"number": 1234,
Expand All @@ -625,7 +634,7 @@ def run(type, data, check):
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "[something] A title",
"number": 1234,
Expand All @@ -640,11 +649,12 @@ def run(type, data, check):
something"""
),
},
check="would have updated issues/1234 with {'body': '\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}",
check="would have updated issues/1234 with {'body': "
"'\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}",
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "[something] A title",
"number": 1234,
Expand All @@ -663,7 +673,7 @@ def run(type, data, check):
)

run(
type="PR",
source_type="PR",
data={
"title": "[something] A title",
"number": 1234,
Expand All @@ -683,7 +693,7 @@ def run(type, data, check):
)

run(
type="PR",
source_type="PR",
data={
"title": "[something] A title",
"number": 1234,
Expand All @@ -703,27 +713,31 @@ def run(type, data, check):
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "[something] A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "something2"}],
# pylint: disable=line-too-long
"body": textwrap.dedent(
"""
`mold` and `lld` can be a much faster alternative to `ld` from gcc. We should modify our CMakeLists.txt to detect and use these when possible. cc @person1
cc @person4
"""
),
# pylint: enable=line-too-long
},
check="would have updated issues/1234 with {'body': '\\n`mold` and `lld` can be a much faster alternative to `ld` from gcc. We should modify our CMakeLists.txt to detect and use these when possible. cc @person1\\n\\ncc @person2 @person4\\n'}",
check="would have updated issues/1234 with {'body': '\\n`mold` and `lld` can be a much"
" faster alternative to `ld` from gcc. We should modify our CMakeLists.txt to "
"detect and use these when possible. cc @person1\\n\\ncc @person2 @person4\\n'}",
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "[something3] A title",
"number": 1234,
Expand All @@ -733,11 +747,12 @@ def run(type, data, check):
"labels": [{"name": "something2"}],
"body": "@person2 @SOME1-ONE-",
},
check="Dry run, would have updated issues/1234 with {'body': '@person2 @SOME1-ONE-\\n\\ncc @person1'}",
check="Dry run, would have updated issues/1234 with"
" {'body': '@person2 @SOME1-ONE-\\n\\ncc @person1'}",
)

run(
type="ISSUE",
source_type="ISSUE",
data={
"title": "[] A title",
"number": 1234,
Expand Down Expand Up @@ -781,6 +796,9 @@ def run(type, data, check):
],
)
def test_should_rebuild_docker(tmpdir_factory, changed_files, name, check, expected_code):
"""
Check that the Docker images are built when necessary
"""
tag_script = REPO_ROOT / "tests" / "scripts" / "should_rebuild_docker.py"

git = TempGit(tmpdir_factory.mktemp("tmp_git_dir"))
Expand Down Expand Up @@ -824,7 +842,7 @@ def test_should_rebuild_docker(tmpdir_factory, changed_files, name, check, expec
},
}

proc = subprocess.run(
proc = subprocess.run( # pylint: disable=subprocess-run-check
[
str(tag_script),
"--testing-docker-data",
Expand Down

0 comments on commit 4b8b843

Please sign in to comment.