From 8c686e64c6f29ba2a9d2a088c7b3238dcc8b78f8 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 31 May 2023 16:24:35 +0100 Subject: [PATCH] Allow use as a reusable action - add self testing - fixes current problems preventing use as an action - make use of pip caching for linter requirements --- .github/workflows/tox.yml | 15 ++++++++++++-- action.yml | 32 ++++++++++------------------- requirements.yml | 9 ++++++++ src/ansiblelint/testing/fixtures.py | 2 +- test/test_profiles.py | 12 ++++++++++- tools/install-reqs.sh | 22 +------------------- 6 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 requirements.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index bcfaa1f0ef..b0600f511e 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -37,11 +37,21 @@ jobs: eco py-devel platforms: linux,macos - + test-action: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Self test for ansible-lint@${{ github.action_ref || 'main' }} + uses: ./ + with: + # basically we only lint linter own configuration, which should be passing. + args: .ansible-lint build: name: ${{ matrix.name }} runs-on: ${{ matrix.os || 'ubuntu-22.04' }} - needs: pre + needs: + - pre + - test-action defaults: run: shell: ${{ matrix.shell || 'bash'}} @@ -210,6 +220,7 @@ jobs: needs: - build + - test-action runs-on: ubuntu-latest diff --git a/action.yml b/action.yml index 03ddde69e1..4c541bf53b 100644 --- a/action.yml +++ b/action.yml @@ -1,25 +1,13 @@ --- -name: ansible-lint +name: run-ansible-lint description: Run Ansible Lint author: Ansible by Red Hat branding: icon: shield color: red inputs: - path: - description: > - Specific path to lint instead of the default `.`. Multiple arguments are - not supported and you will need to alter linter configuration to - accommodate other use cases. - required: false - # That default is just a placeholder workaround for no arguments use-case - # Feeding "." or empty string to ansible-lint makes it believe it is a role - default: --show-relpath args: - description: deprecated - deprecationMessage: > - Arbitrary args are no longer accepted, please use path instead and - linter own configuration file to change its behavior. + description: Arguments to be passed to ansible-lint command required: false default: "" runs: @@ -30,15 +18,16 @@ runs: fetch-depth: 0 # needed by setuptools-scm submodules: true + - name: Generate ansible-lint-requirements.txt + shell: bash + run: | + wget --output-file=$HOME/requirements.txt https://raw.githubusercontent.com/ansible/ansible-lint/${{ github.action_ref || 'main' }}/.config/requirements-lock.txt + - name: Set up Python uses: actions/setup-python@v4 with: cache: pip - # As setting cache to pip fails if there is no requirements.txt, we - # trick it to also load /dev/null to avoid failure for no requirements. - cache-dependency-path: | - **/requirements*.txt - /dev/null + cache-dependency-path: ~/requirements.txt python-version: "3.11" - name: Install ansible-lint @@ -46,11 +35,12 @@ runs: # We need to set the version manually because $GITHUB_ACTION_PATH is not # a git clone and setuptools-scm would not be able to determine the version. # git+https://github.com/ansible/ansible-lint@${{ github.action_ref || 'main' }} + # SETUPTOOLS_SCM_PRETEND_VERSION=${{ github.action_ref || 'main' }} run: | cd $GITHUB_ACTION_PATH - SETUPTOOLS_SCM_PRETEND_VERSION=${{ github.action_ref || 'main' }} pip install ".[lock]" + pip install "ansible-lint[lock] @ git+https://github.com/ansible/ansible-lint@${{ github.action_ref || 'main' }}" ansible-lint --version - name: Run ansible-lint shell: bash - run: ansible-lint ${{ inputs.path }} + run: ansible-lint ${{ inputs.args }} diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000000..40b9b57142 --- /dev/null +++ b/requirements.yml @@ -0,0 +1,9 @@ +--- +# This is used during the ansible-lint own testing. +collections: + - ansible.posix + - ansible.windows + - community.crypto + - community.docker + - community.general + - community.molecule diff --git a/src/ansiblelint/testing/fixtures.py b/src/ansiblelint/testing/fixtures.py index 8fd3fe1102..d05e24b05d 100644 --- a/src/ansiblelint/testing/fixtures.py +++ b/src/ansiblelint/testing/fixtures.py @@ -23,7 +23,7 @@ from _pytest.fixtures import SubRequest -@pytest.fixture(name="default_rules_collection") +@pytest.fixture(name="default_rules_collection", scope="session") def fixture_default_rules_collection() -> RulesCollection: """Return default rule collection.""" assert DEFAULT_RULESDIR.is_dir() diff --git a/test/test_profiles.py b/test/test_profiles.py index e149d65067..a40382c830 100644 --- a/test/test_profiles.py +++ b/test/test_profiles.py @@ -7,6 +7,7 @@ from ansiblelint.rules import RulesCollection, filter_rules_with_profile from ansiblelint.rules.risky_shell_pipe import ShellWithoutPipefail +from ansiblelint.text import strip_ansi_escape def test_profile_min() -> None: @@ -43,7 +44,16 @@ def test_profile_listing(capfd: CaptureFixture[str]) -> None: # [WARNING]: Ansible is being run in a world writable directory # WSL2 has "WSL2" in platform name but WSL1 has "microsoft": platform_name = platform.platform().lower() - err_lines = [line for line in err.splitlines() if "SyntaxWarning:" not in line] + err_lines = [] + for line in strip_ansi_escape(err).splitlines(): + if "SyntaxWarning:" in line: + continue + if ( + "Skipped installing collection dependencies due to running in offline mode." + in line + ): + continue + err_lines.append(line) if all(word not in platform_name for word in ["wsl", "microsoft"]) and err_lines: assert ( not err_lines diff --git a/tools/install-reqs.sh b/tools/install-reqs.sh index a423922e35..dce02d2538 100755 --- a/tools/install-reqs.sh +++ b/tools/install-reqs.sh @@ -1,24 +1,4 @@ #!/bin/bash set -euo pipefail -pushd examples/playbooks/collections >/dev/null -MISSING=() -export ANSIBLE_COLLECTIONS_PATH=. -for COLLECTION in ansible.posix community.docker community.general community.molecule ansible.windows community.crypto; -do - COL_NAME=${COLLECTION//\./-} - FILENAME=$(find . -maxdepth 1 -name "$COL_NAME*" -print -quit) - if test -n "$FILENAME" - then - echo "Already cached $COL_NAME as $FILENAME" - else - MISSING+=("${COLLECTION}") - fi - if [ ${#MISSING[@]} -ne 0 ]; then - ansible-galaxy collection download -p . -v "${MISSING[@]}" - fi -done - echo "Install requirements.yml ..." -ansible-galaxy collection install *.tar.gz -p . -ansible-galaxy collection list -popd >/dev/null +ansible-galaxy collection install -r requirements.yml