From 71d8d4cf6d5a301d001093d2e904cf8a94760735 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Sat, 4 May 2024 04:44:57 +0000 Subject: [PATCH] add ava tests --- .github/workflows/{eslint.yml => check.yml} | 22 +++++++++++++++++++-- package.json | 13 +++++++++++- sources/semver.ts | 4 ++-- tests/semver.test.ts | 14 +++++++++++++ tsconfig.json | 3 ++- 5 files changed, 50 insertions(+), 6 deletions(-) rename .github/workflows/{eslint.yml => check.yml} (52%) create mode 100644 tests/semver.test.ts diff --git a/.github/workflows/eslint.yml b/.github/workflows/check.yml similarity index 52% rename from .github/workflows/eslint.yml rename to .github/workflows/check.yml index 75be7a7..7e5f4b8 100644 --- a/.github/workflows/eslint.yml +++ b/.github/workflows/check.yml @@ -1,4 +1,4 @@ -name: Run ESLint +name: Run ESLint and ava tests on: push: @@ -24,4 +24,22 @@ jobs: - name: Install dependencies run: npm i - name: Run ESLint - run: npm run lint \ No newline at end of file + run: npm run lint + ava: + name: Run ava tests + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + steps: + - name: Set up NodeJS LTS + uses: actions/setup-node@v4 + with: + node-version: 'lts/*' + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install dependencies + run: npm i + - name: Run ava + run: npm run test + needs: [eslint] \ No newline at end of file diff --git a/package.json b/package.json index 8a28620..5e6bc18 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,17 @@ "type": "module", "scripts": { "ci": "tsx index.ts", - "lint": "tsc && eslint . --ext .ts" + "lint": "tsc && eslint . --ext .ts", + "test": "NODE_OPTIONS='--import=tsx --no-warnings' ava" + }, + "ava": { + "files": [ + "tests/**/*.test.ts" + ], + "extensions": { + "ts": "module" + }, + "workerThreads": false }, "dependencies": { "@actions/core": "^1.10.1", @@ -26,6 +36,7 @@ "@types/luxon": "^3.4.2", "@types/node": "^20.12.8", "@types/semver": "^7.5.8", + "ava": "^6.1.2", "commander": "^12.0.0", "luxon": "^3.4.4", "semver": "^7.6.0", diff --git a/sources/semver.ts b/sources/semver.ts index 5eb1d32..6536465 100644 --- a/sources/semver.ts +++ b/sources/semver.ts @@ -1,7 +1,7 @@ import * as Semver from 'semver' import * as Luxon from 'luxon' -function GetDaysAfterNewYear(Now: Luxon.DateTime) { +export function GetDaysAfterNewYear(Now: Luxon.DateTime) { const NewYear = Luxon.DateTime.utc(Now.year, 1, 1) const Days = Now.diff(NewYear, 'days').days @@ -14,7 +14,7 @@ export function UpdateDateVersion(Version: string) { var Now = Luxon.DateTime.utc() // Check if the date is updated and change. - if ((CurrentVersion.major !== Now.year) || (CurrentVersion.minor.toString() !== GetDaysAfterNewYear(Now).toString())) { + if ((CurrentVersion.major !== Number(`1${Now.year}`)) || (CurrentVersion.minor.toString() !== GetDaysAfterNewYear(Now).toString())) { NewVersion += `1${Now.year}.` NewVersion += `${GetDaysAfterNewYear(Now)}.` NewVersion += '0' diff --git a/tests/semver.test.ts b/tests/semver.test.ts new file mode 100644 index 0000000..6f9ecd4 --- /dev/null +++ b/tests/semver.test.ts @@ -0,0 +1,14 @@ +import test from 'ava' +import * as Luxon from 'luxon' +import {UpdateDateVersion, GetDaysAfterNewYear} from '../sources/semver.js' + +test('UpdateDateVersion with current date', T => { + const Now = Luxon.DateTime.utc() + if (UpdateDateVersion(`1${Now.year}.${GetDaysAfterNewYear(Now)}.0`) !== '1' + Now.year + '.' + GetDaysAfterNewYear(Now) + '.1') { + T.fail() + } + if (UpdateDateVersion('2020.1.0') !== `1${Now.year}.${GetDaysAfterNewYear(Now)}.0`) { + T.fail() + } + T.pass() +}) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 7bfa7a9..c5196c2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ }, "include": [ "index.ts", - "sources/**/*.ts" + "sources/**/*.ts", + "tests/**/*.ts" ] } \ No newline at end of file