Skip to content

Commit 1c9cae0

Browse files
committed
Update tests
1 parent 33e9231 commit 1c9cae0

File tree

3 files changed

+136
-117
lines changed

3 files changed

+136
-117
lines changed

source/.eslintrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ rules:
126126
func-call-spacing: error
127127
arrow-spacing: error
128128
generator-star-spacing: error
129+
object-curly-spacing: [error, never]
129130
rest-spread-spacing: error
131+
key-spacing: [error, {afterColon: false}]
130132
computed-property-spacing: error
131133
array-bracket-spacing: [error, never]
132134
no-whitespace-before-property: error

tests/ci.test.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
//Imports
2-
const path = require("path")
3-
const git = require("simple-git")(path.join(__dirname, ".."))
2+
const path = require("path")
3+
const git = require("simple-git")(path.join(__dirname, ".."))
44

55
//Edited files list
6-
const diff = async () => (await git.diff("origin/master...", ["--name-status"])).split("\n").map(x => x.trim()).filter(x => /^M\s+/.test(x)).map(x => x.replace(/^M\s+/, ""))
6+
const diff = async () => (await git.diff("origin/master...", ["--name-status"])).split("\n").map(x => x.trim()).filter(x => /^M\s+/.test(x)).map(x => x.replace(/^M\s+/, ""))
77

88
//Files editions
9-
describe("Check files editions (checkout your files if needed)", () => {
10-
describe("Auto-generated files were not modified", () => void test.each([
9+
describe("Check files editions (checkout your files if needed)", () => {
10+
describe("Auto-generated files were not modified", () =>
11+
void test.each([
1112
"README.md",
1213
"source/plugins/README.md",
1314
"source/templates/README.md",
1415
"action.yml",
15-
"settings.example.json"
16+
"settings.example.json",
1617
])("%s", async file => expect((await diff()).includes(file)).toBe(false)))
17-
describe("Repository level files were not modified", () => void test.each([
18+
describe("Repository level files were not modified", () =>
19+
void test.each([
1820
".github/config/*",
1921
".github/ISSUE_TEMPLATE/*",
2022
".github/PULL_REQUEST_TEMPLATE/*",
@@ -35,11 +37,12 @@
3537
"tests/ci.test.js",
3638
"source/.eslintrc.yml",
3739
"source/app/mocks/.eslintrc.yml",
38-
"vercel.json"
40+
"vercel.json",
3941
])("%s", async file => expect((await diff()).filter(edited => new RegExp(`^${file.replace(/[.]/g, "[.]").replace(/[*]/g, "[\\s\\S]*")}$`).test(edited)).length).toBe(0)))
40-
})
42+
})
4143

4244
//Templates editions
43-
describe("Check templates editions", () => {
44-
test("Use community templates instead (see https://github.com/lowlighter/metrics/tree/master/source/templates/community)", async () => void expect((await diff()).filter(edited => /^sources[/]templates[/]/.test(edited) && /^source[/]templates[/](?:classic|terminal|markdown|repository|community)[/][\s\S]*$/.test(edited)).length).toBe(0))
45-
})
45+
describe("Check templates editions", () => {
46+
test("Use community templates instead (see https://github.com/lowlighter/metrics/tree/master/source/templates/community)", async () =>
47+
void expect((await diff()).filter(edited => /^sources[/]templates[/]/.test(edited) && /^source[/]templates[/](?:classic|terminal|markdown|repository|community)[/][\s\S]*$/.test(edited)).length).toBe(0))
48+
})

tests/metrics.test.js

Lines changed: 119 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
//Imports
2-
const processes = require("child_process")
3-
const yaml = require("js-yaml")
4-
const fs = require("fs")
5-
const path = require("path")
6-
const url = require("url")
7-
const axios = require("axios")
8-
const faker = require("faker")
9-
const ejs = require("ejs")
2+
const processes = require("child_process")
3+
const yaml = require("js-yaml")
4+
const fs = require("fs")
5+
const path = require("path")
6+
const url = require("url")
7+
const axios = require("axios")
8+
const faker = require("faker")
9+
const ejs = require("ejs")
1010

1111
//Github action
12-
const action = yaml.load(fs.readFileSync(path.join(__dirname, "../action.yml"), "utf8"))
13-
action.defaults = Object.fromEntries(Object.entries(action.inputs).map(([key, {default:value}]) => [key, value]))
14-
action.input = vars => Object.fromEntries([...Object.entries(action.defaults), ...Object.entries(vars)].map(([key, value]) => [`INPUT_${key.toLocaleUpperCase()}`, value]))
15-
action.run = async (vars) => await new Promise((solve, reject) => {
12+
const action = yaml.load(fs.readFileSync(path.join(__dirname, "../action.yml"), "utf8"))
13+
action.defaults = Object.fromEntries(Object.entries(action.inputs).map(([key, { default: value }]) => [key, value]))
14+
action.input = vars => Object.fromEntries([...Object.entries(action.defaults), ...Object.entries(vars)].map(([key, value]) => [`INPUT_${key.toLocaleUpperCase()}`, value]))
15+
action.run = async vars =>
16+
await new Promise((solve, reject) => {
1617
let [stdout, stderr] = ["", ""]
17-
const env = {...process.env, ...action.input(vars), GITHUB_REPOSITORY:"lowlighter/metrics"}
18-
const child = processes.spawn("node", ["source/app/action/index.mjs"], {env})
18+
const env = { ...process.env, ...action.input(vars), GITHUB_REPOSITORY: "lowlighter/metrics" }
19+
const child = processes.spawn("node", ["source/app/action/index.mjs"], { env })
1920
child.stdout.on("data", data => stdout += data)
2021
child.stderr.on("data", data => stderr += data)
2122
child.on("close", code => {
@@ -27,111 +28,124 @@
2728
})
2829

2930
//Web instance
30-
const web = {}
31-
web.run = async (vars) => (await axios(`http://localhost:3000/lowlighter?${new url.URLSearchParams(Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value])))}`)).status === 200
32-
web.start = async () => new Promise(solve => {
31+
const web = {}
32+
web.run = async vars => (await axios(`http://localhost:3000/lowlighter?${new url.URLSearchParams(Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value])))}`)).status === 200
33+
web.start = async () =>
34+
new Promise(solve => {
3335
let stdout = ""
34-
web.instance = processes.spawn("node", ["source/app/web/index.mjs"], {env:{...process.env, USE_MOCKED_DATA:true, NO_SETTINGS:true}})
36+
web.instance = processes.spawn("node", ["source/app/web/index.mjs"], { env: { ...process.env, USE_MOCKED_DATA: true, NO_SETTINGS: true } })
3537
web.instance.stdout.on("data", data => (stdout += data, /Server ready !/.test(stdout) ? solve() : null))
3638
web.instance.stderr.on("data", data => console.error(`${data}`))
3739
})
38-
web.stop = async () => await web.instance.kill("SIGKILL")
40+
web.stop = async () => await web.instance.kill("SIGKILL")
3941

4042
//Web instance placeholder
41-
require("./../source/app/web/statics/app.placeholder.js")
42-
const placeholder = globalThis.placeholder
43-
delete globalThis.placeholder
44-
placeholder.init({faker, ejs, axios:{async get(url) { return axios(`http://localhost:3000${url}`) }}})
45-
placeholder.run = async (vars) => {
46-
const options = Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value]))
47-
const enabled = Object.fromEntries(Object.entries(vars).filter(([key]) => /^plugin_[a-z]+$/.test(key)))
48-
const config = Object.fromEntries(Object.entries(options).filter(([key]) => /^config[.]/.test(key)))
49-
const base = Object.fromEntries(Object.entries(options).filter(([key]) => /^base[.]/.test(key)))
50-
return typeof await placeholder({
51-
templates:{selected:vars.template},
52-
plugins:{enabled:{...enabled, base}, options},
53-
config,
54-
version:"TEST",
55-
user:"lowlighter",
56-
avatar:"https://github.com/lowlighter.png",
57-
}) === "string"
58-
}
43+
require("./../source/app/web/statics/app.placeholder.js")
44+
const placeholder = globalThis.placeholder
45+
delete globalThis.placeholder
46+
placeholder.init({
47+
faker,
48+
ejs,
49+
axios: {
50+
async get(url) {
51+
return axios(`http://localhost:3000${url}`)
52+
},
53+
},
54+
})
55+
placeholder.run = async vars => {
56+
const options = Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value]))
57+
const enabled = Object.fromEntries(Object.entries(vars).filter(([key]) => /^plugin_[a-z]+$/.test(key)))
58+
const config = Object.fromEntries(Object.entries(options).filter(([key]) => /^config[.]/.test(key)))
59+
const base = Object.fromEntries(Object.entries(options).filter(([key]) => /^base[.]/.test(key)))
60+
return typeof await placeholder({
61+
templates: { selected: vars.template },
62+
plugins: { enabled: { ...enabled, base }, options },
63+
config,
64+
version: "TEST",
65+
user: "lowlighter",
66+
avatar: "https://github.com/lowlighter.png",
67+
}) === "string"
68+
}
5969

6070
//Setup
61-
beforeAll(async done => {
62-
//Clean community template
63-
await fs.promises.rmdir(path.join(__dirname, "../source/templates/@classic"), {recursive:true})
64-
//Start web instance
65-
await web.start()
66-
done()
67-
})
71+
beforeAll(async done => {
72+
//Clean community template
73+
await fs.promises.rmdir(path.join(__dirname, "../source/templates/@classic"), { recursive: true })
74+
//Start web instance
75+
await web.start()
76+
done()
77+
})
6878
//Teardown
69-
afterAll(async done => {
70-
//Stop web instance
71-
await web.stop()
72-
//Clean community template
73-
await fs.promises.rmdir(path.join(__dirname, "../source/templates/@classic"), {recursive:true})
74-
done()
75-
})
79+
afterAll(async done => {
80+
//Stop web instance
81+
await web.stop()
82+
//Clean community template
83+
await fs.promises.rmdir(path.join(__dirname, "../source/templates/@classic"), { recursive: true })
84+
done()
85+
})
7686

7787
//Load metadata (as jest doesn't support ESM modules, we use this dirty hack)
78-
const metadata = JSON.parse(`${processes.spawnSync("node", [
79-
"--input-type", "module",
80-
"--eval", 'import metadata from "./source/app/metrics/metadata.mjs";console.log(JSON.stringify(await metadata({log:false})))'
81-
]).stdout}`)
88+
const metadata = JSON.parse(`${
89+
processes.spawnSync("node", [
90+
"--input-type",
91+
"module",
92+
"--eval",
93+
'import metadata from "./source/app/metrics/metadata.mjs";console.log(JSON.stringify(await metadata({log:false})))',
94+
]).stdout
95+
}`)
8296

8397
//Build tests index
84-
const tests = []
85-
for (const name in metadata.plugins) {
86-
const cases = yaml
87-
.load(fs.readFileSync(path.join(__dirname, "../source/plugins", name, "tests.yml"), "utf8"))
88-
.map(({name:test, with:inputs, modes = [], timeout}) => {
89-
const skip = new Set(Object.entries(metadata.templates).filter(([_, {readme:{compatibility}}]) => !compatibility[name]).map(([template]) => template))
90-
if (!(metadata.plugins[name].supports.includes("repository")))
91-
skip.add("repository")
92-
return [test, inputs, {skip:[...skip], modes, timeout}]
93-
})
94-
tests.push(...cases)
95-
}
98+
const tests = []
99+
for (const name in metadata.plugins) {
100+
const cases = yaml
101+
.load(fs.readFileSync(path.join(__dirname, "../source/plugins", name, "tests.yml"), "utf8"))
102+
.map(({ name: test, with: inputs, modes = [], timeout }) => {
103+
const skip = new Set(Object.entries(metadata.templates).filter(([_, { readme: { compatibility } }]) => !compatibility[name]).map(([template]) => template))
104+
if (!(metadata.plugins[name].supports.includes("repository")))
105+
skip.add("repository")
106+
return [test, inputs, { skip: [...skip], modes, timeout }]
107+
})
108+
tests.push(...cases)
109+
}
96110

97111
//Tests run
98-
describe("GitHub Action", () =>
99-
describe.each([
100-
["classic", {}],
101-
["terminal", {}],
102-
["repository", {repo:"metrics"}],
103-
])("Template : %s", (template, query) => {
104-
for (const [name, input, {skip = [], modes = [], timeout} = {}] of tests)
105-
if ((skip.includes(template))||((modes.length)&&(!modes.includes("action"))))
106-
test.skip(name, () => null)
107-
else
108-
test(name, async () => expect(await action.run({template, base:"", query:JSON.stringify(query), plugins_errors_fatal:true, dryrun:true, use_mocked_data:true, verify:true, ...input})).toBe(true), timeout)
109-
})
110-
)
112+
describe("GitHub Action", () =>
113+
describe.each([
114+
["classic", {}],
115+
["terminal", {}],
116+
["repository", { repo: "metrics" }],
117+
])("Template : %s", (template, query) => {
118+
for (const [name, input, { skip = [], modes = [], timeout } = {}] of tests) {
119+
if ((skip.includes(template)) || ((modes.length) && (!modes.includes("action"))))
120+
test.skip(name, () => null)
121+
else
122+
test(name, async () => expect(await action.run({ template, base: "", query: JSON.stringify(query), plugins_errors_fatal: true, dryrun: true, use_mocked_data: true, verify: true, ...input })).toBe(true), timeout)
123+
}
124+
}))
111125

112-
describe("Web instance", () =>
113-
describe.each([
114-
["classic", {}],
115-
["terminal", {}],
116-
["repository", {repo:"metrics"}],
117-
])("Template : %s", (template, query) => {
118-
for (const [name, input, {skip = [], modes = [], timeout} = {}] of tests)
119-
if ((skip.includes(template))||((modes.length)&&(!modes.includes("web"))))
120-
test.skip(name, () => null)
121-
else
122-
test(name, async () => expect(await web.run({template, base:0, ...query, plugins_errors_fatal:true, verify:true, ...input})).toBe(true), timeout)
123-
})
124-
)
126+
describe("Web instance", () =>
127+
describe.each([
128+
["classic", {}],
129+
["terminal", {}],
130+
["repository", { repo: "metrics" }],
131+
])("Template : %s", (template, query) => {
132+
for (const [name, input, { skip = [], modes = [], timeout } = {}] of tests) {
133+
if ((skip.includes(template)) || ((modes.length) && (!modes.includes("web"))))
134+
test.skip(name, () => null)
135+
else
136+
test(name, async () => expect(await web.run({ template, base: 0, ...query, plugins_errors_fatal: true, verify: true, ...input })).toBe(true), timeout)
137+
}
138+
}))
125139

126-
describe("Web instance (placeholder)", () =>
127-
describe.each([
128-
["classic", {}],
129-
["terminal", {}],
130-
])("Template : %s", (template, query) => {
131-
for (const [name, input, {skip = [], modes = [], timeout} = {}] of tests)
132-
if ((skip.includes(template))||((modes.length)&&(!modes.includes("placeholder"))))
133-
test.skip(name, () => null)
134-
else
135-
test(name, async () => expect(await placeholder.run({template, base:0, ...query, ...input})).toBe(true), timeout)
136-
})
137-
)
140+
describe("Web instance (placeholder)", () =>
141+
describe.each([
142+
["classic", {}],
143+
["terminal", {}],
144+
])("Template : %s", (template, query) => {
145+
for (const [name, input, { skip = [], modes = [], timeout } = {}] of tests) {
146+
if ((skip.includes(template)) || ((modes.length) && (!modes.includes("placeholder"))))
147+
test.skip(name, () => null)
148+
else
149+
test(name, async () => expect(await placeholder.run({ template, base: 0, ...query, ...input })).toBe(true), timeout)
150+
}
151+
}))

0 commit comments

Comments
 (0)