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

ci: Add format check workflow #766

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 66 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Format check

on:
pull_request:
branches:
- main
paths:
- '**.py'

jobs:
find-changed-files:
runs-on: ubuntu-latest
outputs:
files: ${{ steps.git-diff-files.outputs.files }}
name: Detect added and changed files

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Create json diff
uses: GrantBirki/git-diff-action@v2
id: git-diff
with:
base_branch: origin/main
search_path: '**.py'
json_diff_file_output: diff.json
file_output_only: 'true'
# Ignore deleted files
git_options: '--no-color --diff-filter=d'
pdgendt marked this conversation as resolved.
Show resolved Hide resolved

- name: Convert json diff to matrix array
id: git-diff-files
env:
JSON_DIFF: ${{ steps.git-diff.outputs.json-diff-path }}
run: |
# Github output expects oneliners, use compact mode
files=$(cat $JSON_DIFF | jq -c -r '[.files[] | {path: .path}]')
echo "files=$files" >> $GITHUB_OUTPUT

ruff-format:
needs: find-changed-files
if: ${{ needs.find-changed-files.outputs.files != '[]' }}
runs-on: ubuntu-latest
# Allow the workflow run to pass when this job fails
continue-on-error: true
pdgendt marked this conversation as resolved.
Show resolved Hide resolved
strategy:
fail-fast: false
matrix:
files: ${{ fromJSON(needs.find-changed-files.outputs.files) }}

name: Check file ${{ matrix.files.path }}
steps:
- uses: actions/checkout@v4

- name: Run ruff format check for ${{ matrix.files.path }}
uses: astral-sh/ruff-action@v1
with:
args: "format --check --diff"
src: "${{ matrix.files.path }}"

- name: Annotate unformatted file
if: ${{ failure() }}
run: |
echo "::warning file=${{ matrix.files.path }},title=File format check failed::Run 'ruff format ${{ matrix.files.path }}'"
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ extend-select = [
ignore = [
"UP027", # deprecated pyupgrade rule
]

[tool.ruff.format]
quote-style = "preserve"
line-ending = "lf"
9 changes: 9 additions & 0 deletions tests/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

assert 'TOXTEMPDIR' in os.environ, "you must run these tests using tox"


@pytest.fixture(autouse=True)
def autouse_tmpdir(config_tmpdir, west_init_tmpdir):
# Since this module tests west's configuration file features,
# adding autouse=True to the config_tmpdir and west_init_tmpdir fixtures
# saves typing and is less error-prone than using it below in every test case.
pass


def test_alias_commands():
cmd('config alias.test1 topdir')
cmd('config --global alias.test2 topdir')
Expand All @@ -28,6 +30,7 @@ def test_alias_commands():
assert cmd('test2') == topdir_out
assert cmd('test3') == topdir_out


def test_alias_help():
cmd('config alias.test topdir')

Expand All @@ -36,13 +39,15 @@ def test_alias_help():
assert "An alias that expands to: topdir" in help_out
assert cmd('-h test') == help_out


def test_alias_recursive_commands():
list_format = '{revision} TESTALIAS {name}'
cmd(['config', 'alias.test1', f'list -f "{list_format}"'])
cmd('config alias.test2 test1')

assert cmd('test2') == cmd(['list', '-f', list_format])


def test_alias_infinite_recursion():
cmd('config alias.test1 test2')
cmd('config alias.test2 test3')
Expand All @@ -53,6 +58,7 @@ def test_alias_infinite_recursion():

assert 'unknown command "test1";' in str(excinfo.value.stdout)


def test_alias_empty():
cmd(['config', 'alias.empty', ''])

Expand All @@ -64,18 +70,21 @@ def test_alias_empty():

assert 'empty alias "empty"' in str(excinfo.value.stdout)


def test_alias_early_args():
cmd('config alias.test1 topdir')

# An alias with an early command argument shouldn't fail
assert "Replacing alias test1 with ['topdir']" in cmd('-v test1')


def test_alias_command_with_arguments():
list_format = '{revision} TESTALIAS {name}'
cmd(['config', 'alias.revs', f'list -f "{list_format}"'])

assert cmd('revs') == cmd(['list', '-f', list_format])


def test_alias_override():
before = cmd('list')
list_format = '{name} : {revision}'
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

gv = WestCommand._parse_git_version


def test_parse_git_version():
# White box test for git parsing behavior.
assert gv(b'git version 2.25.1\n') == (2, 25, 1)
Expand Down