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 a27015abd73c5a14523a46d0fe4e093ed206c9ca 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 | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 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..faae0068 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,57 @@ +name: Format check + +on: + pull_request: + branches: + - main + paths: + - '**.py' + +jobs: + generate: + runs-on: ubuntu-latest + outputs: + files: ${{ steps.git-diff-filter.outputs.files }} + name: Detect added and changed files + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: GrantBirki/git-diff-action@v2 + id: git-diff-action + with: + base_branch: origin/main + search_path: '**.py' + # Ignore deleted files + git_options: '--no-color --diff-filter=d' + + - name: Filter json diff + id: git-diff-filter + env: + JSON_DIFF: ${{ steps.git-diff-action.outputs.json-diff }} + run: | + echo $JSON_DIFF | jq + files=$(echo $JSON_DIFF | jq -c -r '[.files[] | {path: .path}]') + echo "files=$files" >> $GITHUB_OUTPUT + + check-files: + needs: generate + if: ${{ needs.generate.outputs.files != '[]' }} + runs-on: ubuntu-latest + # Allow the workflow run to pass when this job fails + continue-on-error: true + strategy: + matrix: + files: ${{ fromJSON(needs.generate.outputs.files) }} + name: Check file ${{ matrix.files.path }} + steps: + - uses: actions/checkout@v4 + - uses: astral-sh/ruff-action@v1 + with: + args: "format --check --diff" + src: "${{ matrix.files.path }}" + - if: ${{ failure() }} + run: | + echo "::warning file=${{ matrix.files.path }},title=File format check failed::Run 'ruff format ${{ matrix.files.path }}'" From dac63fce7bdf06c7fd9867f6c806f68b514c3e41 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Mon, 4 Nov 2024 20:56:47 +0100 Subject: [PATCH 3/4] TEST --- src/west/manifest.py | 2 ++ tests/test_commands.py | 1 + 2 files changed, 3 insertions(+) diff --git a/src/west/manifest.py b/src/west/manifest.py index da557d35..573715b5 100644 --- a/src/west/manifest.py +++ b/src/west/manifest.py @@ -498,6 +498,7 @@ def _assert(cond): # # Public functions +# Add a random comment # def manifest_path() -> str: @@ -627,6 +628,7 @@ def is_group(raw_group: RawGroupType) -> bool: # "--group-filter=path-prefix:foo" to create additional logical # groups based on the workspace layout or other metadata + #this will give a format error return ((raw_group >= 0) if isinstance(raw_group, (float, int)) else bool(raw_group and not _RESERVED_GROUP_RE.search(raw_group))) diff --git a/tests/test_commands.py b/tests/test_commands.py index 8838bb3b..ed42fd1d 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -10,6 +10,7 @@ def test_parse_git_version(): # White box test for git parsing behavior. + # add this assert gv(b'git version 2.25.1\n') == (2, 25, 1) assert gv(b'git version 2.28.0.windows.1\n') == (2, 28, 0) assert gv(b'git version 2.24.3 (Apple Git-128)\n') == (2, 24, 3) From eb0596b675120d7ebd47d7fb35b9782c08c965c6 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Tue, 5 Nov 2024 19:20:40 +0100 Subject: [PATCH 4/4] TEST ADD FILE --- tests/test_alias_copy.py | 90 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/test_alias_copy.py diff --git a/tests/test_alias_copy.py b/tests/test_alias_copy.py new file mode 100644 index 00000000..6dba3984 --- /dev/null +++ b/tests/test_alias_copy.py @@ -0,0 +1,90 @@ +# Copyright (c) 2024, Basalte bv +# +# SPDX-License-Identifier: Apache-2.0 + +import os +import subprocess + +import pytest + +from conftest import cmd + +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') + cmd('config --system alias.test3 topdir') + + topdir_out = cmd('topdir') + + assert cmd('test1') == topdir_out + assert cmd('test2') == topdir_out + assert cmd('test3') == topdir_out + +def test_alias_help(): + cmd('config alias.test topdir') + + help_out = cmd('help test') + + 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') + cmd('config alias.test3 test1') + + with pytest.raises(subprocess.CalledProcessError) as excinfo: + cmd('test1', stderr=subprocess.STDOUT) + + assert 'unknown command "test1";' in str(excinfo.value.stdout) + +def test_alias_empty(): + cmd(['config', 'alias.empty', '']) + + # help command shouldn't fail + cmd('help') + + with pytest.raises(subprocess.CalledProcessError) as excinfo: + cmd('empty', stderr=subprocess.STDOUT) + + 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}' + formatted = cmd(['list', '-f', list_format]) + + cmd(['config', 'alias.list', f'list -f "{list_format}"']) + + after = cmd('list') + + assert before != after + assert formatted == after