Skip to content

Commit

Permalink
Tune impl of test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
eanzhao committed Aug 6, 2024
1 parent 88cdb14 commit 561603f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 145 deletions.
147 changes: 6 additions & 141 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,157 +1,22 @@
name: Test

permissions:
contents: write

on:
push:
branches:
- dev
- master
- feature/use-github-actions

env:
SOLUTION_NAME: "AElf.All.sln"
BRANCH_NAME: "feature/badge"

jobs:
build-and-test:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0'

- name: Download AElf build tools
run: bash scripts/download_binary.sh

- name: Install Protobuf
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler

- run: dotnet restore ${{ env.SOLUTION_NAME }}
- run: dotnet build --no-restore ${{ env.SOLUTION_NAME }}

- name: Run Unit Tests
run: |
for testproj in $(find . -name '*Tests')
do
dotnet test --no-build --no-restore --verbosity normal -l:"trx;LogFileName=TestResults.xml" --collect:"XPlat Code Coverage" $testproj || true
done
continue-on-error: true

- name: Install ReportGenerator tool
run: dotnet tool install --global dotnet-reportgenerator-globaltool

- name: Create Code Coverage Report
run: |
reportgenerator "-reports:**/coverage.cobertura.xml" "-targetdir:./coverage/reports" -reporttypes:"Cobertura"
echo "COVERAGE_FILE=./coverage/reports/Cobertura.xml" >> $GITHUB_ENV
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ${{ env.COVERAGE_FILE }}
fail_ci_if_error: true
continue-on-error: true

- name: Install xmlstarlet
run: |
sudo apt-get update
sudo apt-get install -y xmlstarlet
- name: Extract Test Counts
id: test_counts
run: |
passed=0
failed=0
skipped=0
for file in $(find . -name 'TestResults.xml')
do
passed=$((passed + $(xmlstarlet sel -N x="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" -t -v "/x:TestRun/x:ResultSummary/x:Counters/@passed" "$file")))
failed=$((failed + $(xmlstarlet sel -N x="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" -t -v "/x:TestRun/x:ResultSummary/x:Counters/@failed" "$file")))
total_value=$(xmlstarlet sel -N x="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" -t -v "/x:TestRun/x:ResultSummary/x:Counters/@total" "$file")
executed_value=$(xmlstarlet sel -N x="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" -t -v "/x:TestRun/x:ResultSummary/x:Counters/@executed" "$file")
skipped=$((skipped + total_value - executed_value))
done
echo "PASSED=$passed" >> $GITHUB_ENV
echo "FAILED=$failed" >> $GITHUB_ENV
echo "SKIPPED=$skipped" >> $GITHUB_ENV
- name: Set file name
run: echo "FILENAME=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-')-test-results.json" >> $GITHUB_ENV

- name: Prepare Content
uses: actions/github-script@v5
- name: Run dotnet ci action
uses: AElfProject/[email protected]
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const passed = "${{ env.PASSED }}";
const failed = "${{ env.FAILED }}";
const skipped = "${{ env.SKIPPED }}";
const gistId = "${{ env.GIST_ID }}";
const color = failed > 0 ? "red" : (skipped > 0 ? "green" : "brightgreen");
const content = `{"schemaVersion":1,"label":"tests","message":"${passed} passed, ${failed} failed, ${skipped} skipped","color":"${color}"}`;
fs.writeFileSync("${{ env.FILENAME }}", content);
- name: Check if file exists
uses: actions/github-script@v5
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
let fileExists = false;
try {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: "${{ env.FILENAME }}",
ref: "${{ env.BRANCH_NAME }}",
});
fileExists = !!data;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
core.exportVariable('FILE_EXISTS', fileExists);
- name: Create or update file
uses: actions/github-script@v5
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const filePath = path.join(process.env.GITHUB_WORKSPACE, "${{ env.FILENAME }}");
const fileContent = fs.readFileSync(filePath, 'utf8');
const params = {
owner: context.repo.owner,
repo: context.repo.repo,
path: "${{ env.FILENAME }}",
message: `Update ${{ env.FILENAME }}`,
content: Buffer.from(fileContent).toString('base64'),
branch: "${{ env.BRANCH_NAME }}"
};
if (${{ env.FILE_EXISTS }}) {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: "${{ env.FILENAME }}",
ref: "${{ env.BRANCH_NAME }}"
});
params.sha = data.sha;
}
await github.rest.repos.createOrUpdateFileContents(params);
commit-token: ${{ secrets.COMMIT_TOKEN }}
codecov-token: ${{ secrets.CODECOV_TOKEN }}
branch-name: "feature/badge-json"
solution-name: "AElf.All.sln"
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
[![Twitter Follow](https://img.shields.io/twitter/follow/aelfblockchain.svg?label=%40aelfblockchain&style=social)](https://twitter.com/aelfblockchain)
[![Telegram](https://badgen.net/badge/telegram/join/blue?icon=telegram)](https://t.me/aelfdeveloper)

BRANCH | GITHUB ACTIONS BUILD | TESTS | CODE COVERAGE
------ |--------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------
MASTER | ![GitHub Workflow Build Status](https://github.com/AElfProject/AElf/actions/workflows/build.yml/badge.svg?branch=master) | ![GitHub Workflow Test Status](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/AElf/feature/badge/master-test-results.json) | [![codecov](https://codecov.io/gh/AElfProject/AElf/branch/master/graph/badge.svg)](https://codecov.io/gh/AElfProject/AElf)
DEV | ![GitHub Workflow Build Status](https://github.com/AElfProject/AElf/actions/workflows/build.yml/badge.svg?branch=dev) | ![GitHub Workflow Test Status](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/AElf/feature/badge/dev-test-results.json) | [![codecov](https://codecov.io/gh/AElfProject/AElf/branch/dev/graph/badge.svg)](https://codecov.io/gh/AElfProject/AElf)
BRANCH | GITHUB ACTIONS BUILD | TESTS | CODE COVERAGE
------ |--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------
MASTER | ![GitHub Workflow Build Status](https://github.com/AElfProject/AElf/actions/workflows/build.yml/badge.svg?branch=master) | ![GitHub Workflow Test Status](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/AElf/feature/badge-json/master-test-results.json) | [![codecov](https://codecov.io/gh/AElfProject/AElf/branch/master/graph/badge.svg)](https://codecov.io/gh/AElfProject/AElf)
DEV | ![GitHub Workflow Build Status](https://github.com/AElfProject/AElf/actions/workflows/build.yml/badge.svg?branch=dev) | ![GitHub Workflow Test Status](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/AElf/feature/badge-json/dev-test-results.json) | [![codecov](https://codecov.io/gh/AElfProject/AElf/branch/dev/graph/badge.svg)](https://codecov.io/gh/AElfProject/AElf)


Welcome to AElf's official GitHub repo !
Expand Down

0 comments on commit 561603f

Please sign in to comment.