Skip to content

Commit

Permalink
feat: test-badge
Browse files Browse the repository at this point in the history
  • Loading branch information
aelf-lxy committed Aug 13, 2024
1 parent ca686b5 commit bf2b61e
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 4 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Unit Test Coverage

on:
push:
branches:
- feature/test-chain-util
pull_request:
branches:
- feature/test-chain-util

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: yarn install

- name: Run tests with coverage
run: yarn run test:coverage

- name: Generate coverage badge
uses: jaywcjlove/coverage-badges-cli@main
with:
source: coverage/coverage-summary.json
output: coverage/badges.svg

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./coverage
33 changes: 33 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Publish
env:
CI: true
on:
push:
branches:
- feature/test-chain-util
tags:
- '!*'
jobs:
release:
name: Setup
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
registry-url: 'https://npm.pkg.github.com'

- name: Install dependencies
run: yarn install

- name: Publish to npm
run: |
npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
npm publish || (echo "pnpm publish failed" && exit 1)
env:
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
108 changes: 108 additions & 0 deletions .github/workflows/test-badge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Test Badge

permissions:
contents: write

on:
push:
branches:
- dev
- master
- feature/test-chain-util

env:
BRANCH_NAME: 'feature/badge-json'

jobs:
test:
name: Generate Test Badge
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 20
cache: yarn
- run: yarn install
- run: yarn run test:browser
- name: Install xmlstarlet
run: |
sudo apt-get update
sudo apt-get install -y xmlstarlet
- name: Extract test counts
run: |
echo "TESTS=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")" >> $GITHUB_ENV
echo "FAILURES=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")" >> $GITHUB_ENV
echo "ERRORS=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")" >> $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
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const tests = "${{ env.TESTS }}";
const failures = "${{ env.FAILURES }}";
const errors = "${{ env.ERRORS }}";
const success = tests - failures;
const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen");
const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${success} success","color":"${color}"}`;
fs.writeFileSync("${{ env.FILENAME }}", content);
- name: Check if file exists
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_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);
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# aelf-sdk.js - AELF JavaScript API

| Statements | Branches | Functions | Lines |
<p>
<a href="https://nodejs.org/download/">
<img alt="Node version" src="https://img.shields.io/node/v/aelf-sdk.svg">
</a>
<img alt="NPM" src="https://img.shields.io/npm/l/aelf-sdk">
<a href="http://commitizen.github.io/cz-cli/"><img alt="Commitizen friendly" src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg"></a>
<a href="https://github.com/AElfProject/aelf-web3.js/actions/workflows/publish.yml">
<img alt="coverage" src="https://github.com/AElfProject/aelf-web3.js/actions/workflows/publish.yml/badge.svg">
</a>
</p>

| Branch | Tests | Coverage |
|--------------|-----------------|----------------|
| `feature/test-chain-util` | ![Tests](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/aelf-web3.js/feature/badge-json/feature-test-chain-util-test-results.json) | ![Coverage](https://AElfProject.github.io/aelf-web3.js/badges.svg) |

<!-- | Statements | Branches | Functions | Lines |
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
| ![Statements](https://img.shields.io/badge/statements-96.16%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-92.62%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-98.61%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.1%25-brightgreen.svg?style=flat) |
| ![Statements](https://img.shields.io/badge/statements-96.16%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-92.62%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-98.61%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.1%25-brightgreen.svg?style=flat) | -->


## 1. Introduction
Expand Down
11 changes: 10 additions & 1 deletion jest.browser.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ module.exports = {
// projects: null,

// Use this configuration option to add custom reporters to Jest
// reporters: [],
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: '.',
outputName: 'jest-report.xml'
}
]
],

// Automatically reset mock state between every test
// resetMocks: false,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-sdk",
"version": "3.4.13-beta.1",
"version": "3.4.13-beta.2",
"description": "aelf-sdk js library",
"main": "dist/aelf.cjs.js",
"browser": "dist/aelf.umd.js",
Expand Down Expand Up @@ -122,6 +122,7 @@
"jest-environment-jsdom": "^29.5.0",
"jest-environment-jsdom-fifteen": "^1.0.2",
"jest-github-reporter": "^1.1.1",
"jest-junit": "^16.0.0",
"lint-staged": "^13.2.1",
"rimraf": "^5.0.0",
"size-limit": "^8.1.2",
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6293,6 +6293,16 @@ jest-haste-map@^29.7.0:
optionalDependencies:
fsevents "^2.3.2"

jest-junit@^16.0.0:
version "16.0.0"
resolved "https://registry.yarnpkg.com/jest-junit/-/jest-junit-16.0.0.tgz#d838e8c561cf9fdd7eb54f63020777eee4136785"
integrity sha512-A94mmw6NfJab4Fg/BlvVOUXzXgF0XIH6EmTgJ5NDPp4xoKq0Kr7sErb+4Xs9nZvu58pJojz5RFGpqnZYJTrRfQ==
dependencies:
mkdirp "^1.0.4"
strip-ansi "^6.0.1"
uuid "^8.3.2"
xml "^1.0.1"

jest-leak-detector@^29.7.0:
version "29.7.0"
resolved "https://registry.npmjs.org/jest-leak-detector/-/jest-leak-detector-29.7.0.tgz#5b7ec0dadfdfec0ca383dc9aa016d36b5ea4c728"
Expand Down Expand Up @@ -7340,6 +7350,11 @@ mkdirp@^0.5.1:
dependencies:
minimist "^1.2.6"

mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down Expand Up @@ -9684,6 +9699,11 @@ uuid@^3.3.2:
resolved "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

v8-to-istanbul@^9.0.1:
version "9.3.0"
resolved "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz#b9572abfa62bd556c16d75fdebc1a411d5ff3175"
Expand Down Expand Up @@ -10057,6 +10077,11 @@ xml-name-validator@^4.0.0:
resolved "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-4.0.0.tgz#79a006e2e63149a8600f15430f0a4725d1524835"
integrity sha512-ICP2e+jsHvAj2E2lIHxa5tjXRlKDJo4IdvPvCXbXQGdzSfmSpNVyIKMvoZHjDY9DP0zV17iI85o90vRFXNccRw==

xml@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
integrity sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==

xmlchars@^2.1.1, xmlchars@^2.2.0:
version "2.2.0"
resolved "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"
Expand Down

0 comments on commit bf2b61e

Please sign in to comment.