From 5af0ecb9f7182b76e5cf92461be00d9090d6247c Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Tue, 5 Nov 2024 19:30:34 +0100 Subject: [PATCH 1/4] project: Add ruff format settings Add setting for the ruff formatter to check during CI. Signed-off-by: Pieter De Gendt --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index f3153cc4..10f1dddf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,3 +60,7 @@ extend-select = [ ignore = [ "UP027", # deprecated pyupgrade rule ] + +[tool.ruff.format] +quote-style = "preserve" +line-ending = "lf" From d92a25dd60b947a558523af827ac3198b1006b5f Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Mon, 4 Nov 2024 20:21:18 +0100 Subject: [PATCH 2/4] ci: Add format check workflow Add a CI workflow to report formatting issues on changed files. This is to gradually update files in the repository to be conform with PEP8 formatting. Signed-off-by: Pieter De Gendt --- .github/workflows/format.yml | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .github/workflows/format.yml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 00000000..15974344 --- /dev/null +++ b/.github/workflows/format.yml @@ -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' + + - 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 + 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 }}'" From ec78fb5f3d3fb9705b9c62630b22fd2baa99b336 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Fri, 8 Nov 2024 14:56:28 +0100 Subject: [PATCH 3/4] tests: Format test_alias.py Formatting test_alias.py is trivial. Signed-off-by: Pieter De Gendt --- tests/test_alias.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_alias.py b/tests/test_alias.py index 88ad7e45..0f7bf39f 100644 --- a/tests/test_alias.py +++ b/tests/test_alias.py @@ -10,6 +10,7 @@ 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, @@ -17,6 +18,7 @@ def autouse_tmpdir(config_tmpdir, west_init_tmpdir): # 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') @@ -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') @@ -36,6 +39,7 @@ 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}"']) @@ -43,6 +47,7 @@ def test_alias_recursive_commands(): assert cmd('test2') == cmd(['list', '-f', list_format]) + def test_alias_infinite_recursion(): cmd('config alias.test1 test2') cmd('config alias.test2 test3') @@ -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', '']) @@ -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}' From 12c4f8d10daf8af8c6f8ef71f26ea092c5403c7a Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Fri, 8 Nov 2024 15:00:42 +0100 Subject: [PATCH 4/4] tests: Format test_commands.py Formatting test_commands.py is trivial. Signed-off-by: Pieter De Gendt --- tests/test_commands.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 8838bb3b..d0dee4ec 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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)