From 1982162ba68670a039f4bb4983a89b79c5ca9468 Mon Sep 17 00:00:00 2001 From: lowlighter <22963968+lowlighter@users.noreply.github.com> Date: Wed, 26 Jan 2022 20:39:29 -0500 Subject: [PATCH] ci: add workflow for presets ci --- .github/workflows/ci.presets.yml | 22 +++++++++ package.json | 6 +-- tests/presets.test.js | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci.presets.yml create mode 100644 tests/presets.test.js diff --git a/.github/workflows/ci.presets.yml b/.github/workflows/ci.presets.yml new file mode 100644 index 00000000..fe225103 --- /dev/null +++ b/.github/workflows/ci.presets.yml @@ -0,0 +1,22 @@ +name: Continuous integration (presets) +on: + push: + branches: [ presets ] + +jobs: + test: + name: Build, test and analyze + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Setup NodeJS + uses: actions/setup-node@v2 + with: + node-version: 17 + - name: Setup metrics + run: npm ci + - name: Run tests + run: npm run test-presets diff --git a/package.json b/package.json index af7ff55e..2f072599 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.mjs", "scripts": { "start": "node source/app/web/index.mjs", - "test": "jest --runInBand", + "test": "jest --runInBand --testPathIgnorePatterns presets.test.js", + "test-presets": "jest --runInBand presets.test.js", "build": "node .github/scripts/build.mjs", "presets": "node .github/scripts/presets_examples.mjs", "quickstart": "node .github/scripts/quickstart/index.mjs", @@ -14,8 +15,7 @@ "format": "eslint source/**/*.mjs --fix", "dev": "nodemon source/app/web/index.mjs -e mjs,css,ejs,json", "postinstall": "node node_modules/puppeteer/install.js", - "indepth": "node source/plugins/languages/analyzers.mjs", - "autogen": "node .github/examples.mjs" + "indepth": "node source/plugins/languages/analyzers.mjs" }, "repository": { "type": "git", diff --git a/tests/presets.test.js b/tests/presets.test.js new file mode 100644 index 00000000..893c9baf --- /dev/null +++ b/tests/presets.test.js @@ -0,0 +1,78 @@ +//Imports +const processes = require("child_process") +const yaml = require("js-yaml") +const fss = require("fs") +const paths = require("path") + +//Git setup +const __metrics = paths.join(paths.dirname(__dirname, "..")) +const __presets = paths.join(__metrics, ".presets") + +//Clone presets branch +try { + fss.accessSync(__presets) +} +catch { + processes.execSync(`git clone https://github.com/lowlighter/metrics.git ${__presets} --branch presets --single-branch`) +} + +//Generate presets examples +for (const path of fss.readdirSync(__presets)) { + if (/^[.@]/.test(path)) + continue + if (!(fss.lstatSync(paths.join(__presets, path))).isDirectory()) + continue + + describe(`Preset: ${path}`, () => { + try { + //Load preset + const preset = yaml.load(fss.readFileSync(paths.join(__presets, path, "preset.yml"))) + test("syntax is valid", () => expect(true).toBe(true)) + try { + //Load schema + const {properties} = yaml.load(fss.readFileSync(paths.join(__presets, "@schema", `${preset.schema}.yml`))) + test("schema is valid", () => expect(preset.schema).toBeDefined()) + //Test schema + for (const [key, {type, required}] of Object.entries(properties)) { + if (required) + test(`preset.${key} is defined`, () => expect(preset[key]).toBeDefined()) + test(`preset.${key} type is ${type}`, () => { + switch (true) { + case /^'.*'$/.test(type):{ + const value = type.substring(1, type.length-1) + expect(preset[key]).toBe(value) + return + } + case /\{\}$/.test(type):{ + const typed = type.substring(0, type.length-2) + expect(typeof preset[key]).toBe("object") + if (typed !== "unknown") { + for (const value of Object.values(preset[key])) + expect(typeof value).toBe(typed) + } + return + } + case /\[\]$/.test(type):{ + const typed = type.substring(0, type.length-2) + expect(Array.isArray(preset[key])).toBe(true) + if (typed !== "unknown") { + for (const value of Object.values(preset[key])) + expect(typeof value).toBe(typed) + } + return + } + default: + expect(typeof preset[key]).toBe(type) + } + }) + } + } + catch (error) { + test(`schema is valid (got ${preset.schema})`, () => expect(false).toBe(true)) + } + } + catch { + test("syntax is valid", () => expect(false).toBe(true)) + } + }) +} \ No newline at end of file