From 2b430380b92c642b44445d8772a36da6c5bfc191 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 21 Feb 2022 12:02:06 +0100 Subject: [PATCH 1/4] test(scripts): add jest for scripts package --- scripts/jest.config.js | 6 +++ scripts/package.json | 5 +- scripts/release/__tests__/common.test.ts | 63 ++++++++++++++++++++++++ scripts/release/common.ts | 16 ++++++ scripts/release/process-release.ts | 15 +----- scripts/tsconfig.json | 1 + yarn.lock | 2 + 7 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 scripts/jest.config.js create mode 100644 scripts/release/__tests__/common.test.ts diff --git a/scripts/jest.config.js b/scripts/jest.config.js new file mode 100644 index 0000000000..67d9b434c4 --- /dev/null +++ b/scripts/jest.config.js @@ -0,0 +1,6 @@ +/* eslint-disable import/no-commonjs */ +/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', +}; diff --git a/scripts/package.json b/scripts/package.json index cf73e43f4c..de616795ba 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -5,7 +5,8 @@ "build": "tsc", "createReleaseIssue": "yarn build && node dist/scripts/release/create-release-issue.js", "processRelease": "yarn build && node dist/scripts/release/process-release.js", - "setHostsOptions": "yarn build && node dist/scripts/pre-gen/setHostsOptions.js" + "setHostsOptions": "yarn build && node dist/scripts/pre-gen/setHostsOptions.js", + "test": "jest" }, "devDependencies": { "@octokit/rest": "18.12.0", @@ -14,8 +15,10 @@ "@types/semver": "7.3.9", "dotenv": "16.0.0", "execa": "5.1.1", + "jest": "27.4.7", "js-yaml": "4.1.0", "semver": "7.3.5", + "ts-jest": "27.1.3", "typescript": "4.5.4" } } diff --git a/scripts/release/__tests__/common.test.ts b/scripts/release/__tests__/common.test.ts new file mode 100644 index 0000000000..fa261277f4 --- /dev/null +++ b/scripts/release/__tests__/common.test.ts @@ -0,0 +1,63 @@ +import { getMarkdownSection } from '../common'; + +describe('getMarkdownSection', () => { + it('gets the section correctly', () => { + const text = ` + # hello + + hi + + # world + + hey + `; + expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(` + "# hello + + hi + " + `); + }); + + it('gets the sub headings', () => { + const text = ` + # hi + + # hello + + ## sub-heading + + hello + + # this shouldn't be included + + right? + `; + + expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(` + "# hello + + ## sub-heading + + hello + " + `); + }); + + it('gets the whole text till the end', () => { + const text = ` + # hi + + # hello + + this is a test + `; + + expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(` + "# hello + + this is a test + " + `); + }); +}); diff --git a/scripts/release/common.ts b/scripts/release/common.ts index 248b94e422..4d333a1635 100644 --- a/scripts/release/common.ts +++ b/scripts/release/common.ts @@ -36,3 +36,19 @@ export const LANGS = [ ) ), ]; + +export function getMarkdownSection(markdown: string, title: string): string { + const levelIndicator = title.split(' ')[0]; // e.g. `##` + const lines = markdown + .slice(markdown.indexOf(title)) + .split('\n') + .map((line) => line.trim()); + let endIndex = lines.length; + for (let i = 1; i < lines.length; i++) { + if (lines[i].startsWith(`${levelIndicator} `)) { + endIndex = i; + break; + } + } + return lines.slice(0, endIndex).join('\n'); +} diff --git a/scripts/release/process-release.ts b/scripts/release/process-release.ts index 669c115c56..b0e9cfa8d7 100755 --- a/scripts/release/process-release.ts +++ b/scripts/release/process-release.ts @@ -6,24 +6,11 @@ import execa from 'execa'; import openapitools from '../../openapitools.json'; -import { MAIN_BRANCH, OWNER, REPO, run } from './common'; +import { MAIN_BRANCH, OWNER, REPO, run, getMarkdownSection } from './common'; import TEXT from './text'; dotenv.config(); -function getMarkdownSection(markdown: string, title: string): string { - const levelIndicator = title.split(' ')[0]; // e.g. `##` - const lines = markdown.slice(markdown.indexOf(title)).split('\n'); - let endIndex = lines.length; - for (let i = 1; i < lines.length; i++) { - if (lines[i].startsWith(`${levelIndicator} `)) { - endIndex = i; - break; - } - } - return lines.slice(0, endIndex).join('\n'); -} - if (!process.env.GITHUB_TOKEN) { throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); } diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index 20c3f4903a..34cc31a9fd 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -2,6 +2,7 @@ "extends": "../base.tsconfig.json", "compilerOptions": { "typeRoots": ["../node_modules/@types"], + "types": ["node", "jest"], "outDir": "dist" }, "include": ["pre-gen/setHostsOptions.ts", "release/*"], diff --git a/yarn.lock b/yarn.lock index 30b6c76563..e600c3d388 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5895,8 +5895,10 @@ __metadata: "@types/semver": 7.3.9 dotenv: 16.0.0 execa: 5.1.1 + jest: 27.4.7 js-yaml: 4.1.0 semver: 7.3.5 + ts-jest: 27.1.3 typescript: 4.5.4 languageName: unknown linkType: soft From c88a6b148f76d49d6c8215954d058a5269c45202 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 21 Feb 2022 15:46:59 +0100 Subject: [PATCH 2/4] chore: update jest config --- scripts/jest.config.js | 6 ------ scripts/jest.config.ts | 8 ++++++++ 2 files changed, 8 insertions(+), 6 deletions(-) delete mode 100644 scripts/jest.config.js create mode 100644 scripts/jest.config.ts diff --git a/scripts/jest.config.js b/scripts/jest.config.js deleted file mode 100644 index 67d9b434c4..0000000000 --- a/scripts/jest.config.js +++ /dev/null @@ -1,6 +0,0 @@ -/* eslint-disable import/no-commonjs */ -/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ -module.exports = { - preset: 'ts-jest', - testEnvironment: 'node', -}; diff --git a/scripts/jest.config.ts b/scripts/jest.config.ts new file mode 100644 index 0000000000..d2cc09a672 --- /dev/null +++ b/scripts/jest.config.ts @@ -0,0 +1,8 @@ +import type { Config } from '@jest/types'; + +const config: Config.InitialOptions = { + preset: 'ts-jest', + testEnvironment: 'node', +}; + +export default config; From e9f4d3fc86533fc0fa72843b839b491a948db63e Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 21 Feb 2022 15:51:16 +0100 Subject: [PATCH 3/4] chore(ci): run test on CI --- .github/workflows/check.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index f61eec366a..bbf2a021dd 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -230,3 +230,16 @@ jobs: - name: Run CTS run: yarn cts:test + + scripts: + runs-on: ubuntu-20.04 + timeout-minutes: 20 + steps: + - uses: actions/checkout@v2 + + - name: Restore cache + id: restore + uses: ./.github/actions/cache + + - name: Test scripts + run: yarn workspace scripts test From 1c7aaf7dfa311e927674da41db610ed28573ab40 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 21 Feb 2022 15:54:10 +0100 Subject: [PATCH 4/4] chore(ci): run test after setup job --- .github/workflows/check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index bbf2a021dd..8bfdfc0479 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -233,6 +233,7 @@ jobs: scripts: runs-on: ubuntu-20.04 + needs: setup timeout-minutes: 20 steps: - uses: actions/checkout@v2