solved(python): baekjoon 2150 #79
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Run Tests for Python Problems | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| detect-changes: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.detect-changes.outputs.matrix }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 2 | |
| - name: Detect changed files | |
| id: detect-changes | |
| run: | | |
| git config core.quotepath false | |
| if git rev-parse HEAD~1 >/dev/null 2>&1; then | |
| base_commit="HEAD~1" | |
| else | |
| base_commit="HEAD" | |
| fi | |
| # 파이썬 파일이 수정된 디렉토리만 추출 | |
| changed_dirs=$(git diff --name-only $base_commit HEAD | grep -E '\.py$' | awk -F'/' '{OFS="/"; $NF=""; print $0}' | sed 's/\/$//' | sort -u || true) | |
| json="[" | |
| for dir in $changed_dirs; do | |
| if [ -d "$dir" ]; then | |
| root=$(echo "$dir" | awk -F'/' '{print $1"/"$2}') | |
| version_file="$root/.python-version" | |
| json+="{\"dir\":\"$dir\",\"python_version\":\"$(cat "$version_file" | tr -d '\r\n')\"}," | |
| fi | |
| done | |
| json="${json%,}]" | |
| echo "matrix=$json" >> $GITHUB_OUTPUT | |
| shell: bash | |
| run-tests: | |
| needs: detect-changes | |
| runs-on: ubuntu-latest | |
| if: needs.detect-changes.outputs.matrix != '[]' | |
| strategy: | |
| matrix: | |
| include: ${{ fromJson(needs.detect-changes.outputs.matrix) }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python ${{ matrix.python_version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python_version }} | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install pytest parameterized | |
| - name: Run Tests in ${{ matrix.dir }} | |
| run: | | |
| pytest ${{ matrix.dir }}/test_main.py |