Skip to content

Commit a2c424a

Browse files
authored
feat: add ruff linter with ci check (#1178)
* feat: add ruff linter with ci check Liniting the python files in tests directory would reduce whitespace etc change and enforce basic coding standard for those. Kind of opportunistic PR as there might be other opinions about how to deal with this. * fix: format with ruff
1 parent d291b27 commit a2c424a

File tree

9 files changed

+241
-74
lines changed

9 files changed

+241
-74
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
2828
readarray -t changed_files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.added_modified }}')"
2929
~/go/bin/editorconfig-checker ${changed_files[@]}
30+
- name: Lint and format Python with Ruff
31+
uses: astral-sh/ruff-action@v1
3032

3133
typo:
3234
runs-on: ubuntu-latest

tests/conftest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@
44
import pytest
55
from helper import TempRepository
66

7-
def create_repo(dirname = None):
7+
8+
def create_repo(dirname=None):
89
repo = TempRepository(dirname)
910
tmp_file_a = repo.create_tmp_file()
1011
tmp_file_b = repo.create_tmp_file()
1112
repo.switch_cwd_under_repo()
1213
return repo
1314

15+
1416
def init_repo_git_status(repo):
1517
git = repo.get_repo_git()
1618
git.add(".")
1719
git.config("--local", "user.name", "test")
1820
git.config("--local", "user.email", "[email protected]")
1921
git.commit("-m", "chore: initial commit")
2022

23+
2124
@pytest.fixture(scope="module")
2225
def temp_repo():
2326
repo = create_repo()
2427
init_repo_git_status(repo)
2528
return repo
2629

30+
2731
@pytest.fixture(scope="module")
2832
def named_temp_repo(request):
2933
dirname = request.param

tests/helper.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
GITLAB_ORIGIN = "https://gitlab.com/tj/git-extras.git"
1111
BITBUCKET_ORIGIN = "https://bitbucket.org/tj/git-extras.git"
1212

13+
1314
class TempRepository:
14-
def __init__(self, repo_work_dir = None):
15+
def __init__(self, repo_work_dir=None):
1516
self._system_tmpdir = tempfile.gettempdir()
1617
if repo_work_dir == None:
1718
repo_work_dir = tempfile.mkdtemp()
1819
else:
1920
repo_work_dir = os.path.join(self._system_tmpdir, repo_work_dir)
2021
self._cwd = repo_work_dir
21-
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:]
22+
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1 :]
2223
self._git_repo = Repo.init(repo_work_dir, b="default")
2324
self._files = []
2425
self.change_origin_to_github()
@@ -50,7 +51,7 @@ def create_tmp_dir(self):
5051
tmp_dir = tempfile.mkdtemp()
5152
return tmp_dir
5253

53-
def create_tmp_file(self, temp_dir = None):
54+
def create_tmp_file(self, temp_dir=None):
5455
if temp_dir == None:
5556
temp_dir = self._cwd
5657

@@ -86,8 +87,9 @@ def invoke_installed_extras_command(self, name, *params):
8687
origin_extras_command = os.path.join(GIT_EXTRAS_BIN, command_name)
8788
temp_extras_command = os.path.join(self._cwd, command_name)
8889
helpers = [
89-
os.path.join(GIT_EXTRAS_HELPER, "git-extra-utility"),
90-
os.path.join(GIT_EXTRAS_HELPER, "is-git-repo")]
90+
os.path.join(GIT_EXTRAS_HELPER, "git-extra-utility"),
91+
os.path.join(GIT_EXTRAS_HELPER, "is-git-repo"),
92+
]
9193

9294
if not os.path.exists(temp_extras_command):
9395
whole = []
@@ -106,7 +108,7 @@ def invoke_installed_extras_command(self, name, *params):
106108
os.chmod(temp_extras_command, 0o775)
107109

108110
script = [temp_extras_command, *params]
109-
print(f"Run the script \"{script}\"")
111+
print(f'Run the script "{script}"')
110112
return subprocess.run(script, capture_output=True)
111113

112114
def change_origin(self, origin_url):

tests/pyproject.toml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,66 @@ testpaths = ["."]
2121
[build-system]
2222
requires = ["poetry-core"]
2323
build-backend = "poetry.core.masonry.api"
24+
25+
[tool.ruff]
26+
# Exclude a variety of commonly ignored directories.
27+
exclude = [
28+
".bzr",
29+
".direnv",
30+
".eggs",
31+
".git",
32+
".git-rewrite",
33+
".hg",
34+
".ipynb_checkpoints",
35+
".mypy_cache",
36+
".nox",
37+
".pants.d",
38+
".pyenv",
39+
".pytest_cache",
40+
".pytype",
41+
".ruff_cache",
42+
".svn",
43+
".tox",
44+
".venv",
45+
".vscode",
46+
"__pypackages__",
47+
"_build",
48+
"buck-out",
49+
"build",
50+
"dist",
51+
"node_modules",
52+
"site-packages",
53+
"venv",
54+
]
55+
56+
# Same as Black.
57+
line-length = 88
58+
indent-width = 4
59+
60+
# Assume Python 3.9
61+
target-version = "py39"
62+
63+
[tool.ruff.lint]
64+
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
65+
select = ["E4", "E7", "E9", "F"]
66+
ignore = []
67+
68+
# Allow fix for all enabled rules (when `--fix`) is provided.
69+
fixable = ["ALL"]
70+
unfixable = []
71+
72+
# Allow unused variables when underscore-prefixed.
73+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
74+
75+
[tool.ruff.format]
76+
# Like Black, use double quotes for strings.
77+
quote-style = "double"
78+
79+
# Like Black, indent with spaces, rather than tabs.
80+
indent-style = "space"
81+
82+
# Like Black, respect magic trailing commas.
83+
skip-magic-trailing-comma = false
84+
85+
# Like Black, automatically detect the appropriate line ending.
86+
line-ending = "auto"

tests/test_git_abort.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from git import GitCommandError
22

3+
34
class TestGitAbort:
45
def test_init(self, temp_repo):
56
git = temp_repo.get_repo_git()

tests/test_git_archive_file.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os, pytest
22

3+
34
class TestGitArchiveFile:
45
def test_init(self, temp_repo):
56
git = temp_repo.get_repo_git()
@@ -16,14 +17,17 @@ def test_archive_file_on_tags_branch(self, temp_repo):
1617
filename = "{0}.{1}.zip".format(temp_repo.get_repo_dirname(), git.describe())
1718
assert filename in os.listdir()
1819

19-
def test_archive_file_on_any_not_tags_branch_without_default_branch(self, temp_repo):
20+
def test_archive_file_on_any_not_tags_branch_without_default_branch(
21+
self, temp_repo
22+
):
2023
git = temp_repo.get_repo_git()
2124
git.checkout("-b", "not-tags-branch")
2225
temp_repo.invoke_installed_extras_command("archive-file")
2326
filename = "{0}.{1}.{2}.zip".format(
24-
temp_repo.get_repo_dirname(),
25-
git.describe("--always", "--long"),
26-
"not-tags-branch")
27+
temp_repo.get_repo_dirname(),
28+
git.describe("--always", "--long"),
29+
"not-tags-branch",
30+
)
2731
assert filename in os.listdir()
2832

2933
def test_archive_file_on_any_not_tags_branch_with_default_branch(self, temp_repo):
@@ -32,28 +36,28 @@ def test_archive_file_on_any_not_tags_branch_with_default_branch(self, temp_repo
3236
git.config("git-extras.default-branch", "default")
3337
temp_repo.invoke_installed_extras_command("archive-file")
3438
filename = "{0}.{1}.zip".format(
35-
temp_repo.get_repo_dirname(),
36-
git.describe("--always", "--long"))
39+
temp_repo.get_repo_dirname(), git.describe("--always", "--long")
40+
)
3741
assert filename in os.listdir()
3842

3943
def test_archive_file_on_branch_name_has_slash(self, temp_repo):
4044
git = temp_repo.get_repo_git()
4145
git.checkout("-b", "feature/slash")
4246
temp_repo.invoke_installed_extras_command("archive-file")
4347
filename = "{0}.{1}.{2}.zip".format(
44-
temp_repo.get_repo_dirname(),
45-
git.describe("--always", "--long"),
46-
"feature-slash")
48+
temp_repo.get_repo_dirname(),
49+
git.describe("--always", "--long"),
50+
"feature-slash",
51+
)
4752
assert filename in os.listdir()
4853

4954
@pytest.mark.parametrize("named_temp_repo", ["backslash\\dir"], indirect=True)
5055
def test_archive_file_on_dirname_has_backslash(self, named_temp_repo):
5156
named_temp_repo.invoke_installed_extras_command("archive-file")
5257
git = named_temp_repo.get_repo_git()
5358
filename = "{0}.{1}.{2}.zip".format(
54-
"backslash-dir",
55-
git.describe("--always", "--long"),
56-
"default")
59+
"backslash-dir", git.describe("--always", "--long"), "default"
60+
)
5761
assert filename in os.listdir()
5862

5963
def test_archive_file_on_tag_name_has_slash(self, temp_repo):
@@ -65,6 +69,6 @@ def test_archive_file_on_tag_name_has_slash(self, temp_repo):
6569
temp_repo.invoke_installed_extras_command("archive-file")
6670
description_include_version = git.describe("--always", "--long")
6771
filename = "{0}.{1}.zip".format(
68-
temp_repo.get_repo_dirname(),
69-
description_include_version.replace("/", "-"))
72+
temp_repo.get_repo_dirname(), description_include_version.replace("/", "-")
73+
)
7074
assert filename in os.listdir()

tests/test_git_authors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os, subprocess
22

3-
expected_authors_list = "test <[email protected]>\ntestagain <[email protected]>\n"
3+
expected_authors_list = (
4+
"test <[email protected]>\ntestagain <[email protected]>\n"
5+
)
46
expected_authors_list_without_email = "test\ntestagain\n"
57
authors_file = "AUTHORS"
68

9+
710
class TestGitAuthors:
811
def test_init(self, temp_repo):
912
git = temp_repo.get_repo_git()

0 commit comments

Comments
 (0)