sed を使用した % 分数の解析

sed を使用した % 分数の解析

私のgithub ciタスクのコードカバレッジを解析しようとしていますが、すべてが機能しますが、カバレッジ%の結果を解析することはできません。コードカバレッジのパーセントスコアを解析するのに役立ちます。正規表現は機能しません。

注文する

name: Pytest Coverage
on:
  pull_request:
    branches: [ main, dev ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.10
      uses: actions/setup-python@v2
      with:
        python-version: "3.10"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest pytest-cov
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Build coverage file
      run: |
        pytest --cache-clear --cov=src tests/ > pytest-coverage.txt
    - name: Comment coverage
      uses: coroo/[email protected]
    - name: Get Coverage %
      run: |
        LAST_LINE=$(tail -4 pytest-coverage.txt)
        LAST_LINE=$(head -n 1 <<< "$LAST_LINE")
        echo "target line is $LAST_LINE"
        COVERAGE=$(sed -n '$s/.*?\([0-9]+\)%.*/\1/p' <<< "$LAST_LINE")
        echo "overall coverage is $COVERAGE"

$LAST_LINEは

TOTAL                                               2401   1538    36%

$COVERAGEは現在空です。予想される出力は次のとおりです。

36%

ベストアンサー1

使用sed

COVERAGE=$(sed 's/.*[[:space:]]\([0-9]\+%\)/\1/' <<< "$LAST_LINE")

おすすめ記事