From 64480c987966d387340119bc953a40b11f6e7bb1 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Wed, 18 Mar 2020 18:17:07 +1300 Subject: [PATCH] refactor: Rewrite tests Signed-off-by: Richie Bendall --- package.json | 3 +++ test.js | 66 +++++++++++++++++++++++++++++---------------------- yarn.lock | Bin 183012 -> 184394 bytes 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 9ffbd509..d317b635 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,12 @@ }, "devDependencies": { "css": "^2.2.4", + "file-ext": "^1.0.0", + "get-ext": "^1.0.2", "has-flag": "^4.0.0", "husky": "^4.2.1", "nodemon": "^2.0.2", + "path-extra": "^4.3.0", "standard": "^14.3.1" }, "resolutions": { diff --git a/test.js b/test.js index 9593a3c2..ceb01390 100644 --- a/test.js +++ b/test.js @@ -1,45 +1,54 @@ -const path = require('path') const fs = require('fs-extra') const CSS = require('css') const { validDomainId } = require('./routes/utils') const hasFlag = require('has-flag') +const getExtension = require('file-ext') +const path = require('path-extra') +const is = require('@sindresorhus/is') -function report (content, fix) { +async function report (content, fix) { console.error(content) - if (fix && hasFlag('--fix')) fix() + if (fix && hasFlag('--fix')) await fix() process.exitCode = 1 } (async () => { const users = await fs.readdir('users') - users.forEach(async user => { - if (!user.endsWith('json')) { - report(`${user} is not a json file`, () => - fs.unlink(path.join('users', user), () => { }) - ) + + for (const user of users) { + if (getExtension(user) !== 'json') { + await report(`${user} is not a json file`, async () => { + await fs.remove(user) + }) } - if (!validDomainId(user.replace('.json', ''))) { - report(`${user} is not a valid domain id.`) + + if (!validDomainId(path.base(user))) { + await report(`${user} is not a valid domain id.`) } + try { const data = await fs.readFile(path.join('users', user), 'utf8') + try { - const u = JSON.parse(data) - if (!u.locked && !u.copyright) { + const parsedData = JSON.parse(data) + + if (!parsedData.locked && !parsedData.copyright) { report(`Copyright not specified in ${user}`) } - if (u.version) { - report(`Version tag found in ${user}`, () => { - delete u.version - const stringified = `${JSON.stringify(u, 0, 2)}\n` - fs.writeFile(path.join('users', user), stringified, () => { }) + + if (parsedData.version) { + await report(`Version tag found in ${user}`, async () => { + delete parsedData.version + const stringified = `${JSON.stringify(parsedData, 0, 2)}\n` + await fs.writeFile(path.join('users', user), stringified) }) } - if (typeof u.gravatar === 'string') { - report(`Gravatar boolean encoded as string found in ${user}`, () => { - u.gravatar = u.gravatar === 'true' - const stringified = `${JSON.stringify(u, 0, 2)}\n` - fs.writeFile(path.join('users', user), stringified, () => { }) + + if (is.string(parsedData.gravatar)) { + await report(`Gravatar boolean encoded as string found in ${user}`, async () => { + parsedData.gravatar = parsedData.gravatar === 'true' + const stringified = `${JSON.stringify(parsedData, 0, 2)}\n` + await fs.writeFile(path.join('users', user), stringified) }) } } catch ({ message }) { @@ -48,15 +57,16 @@ function report (content, fix) { } catch ({ message }) { report(`Unable to read ${user} (${message})`) } - }) + } const themes = await fs.readdir('themes') - await themes.forEach(async theme => { - if (theme.endsWith('css')) { + + for (const theme of themes) { + if (getExtension(theme) === 'css') { try { - const data = await fs.readFile(path.join('themes', theme), 'utf8') + const cssData = await fs.readFile(path.join('themes', theme), 'utf8') try { - CSS.parse(data) + CSS.parse(cssData) } catch ({ message }) { report(`Invalid CSS in ${theme} (${message})`) } @@ -64,5 +74,5 @@ function report (content, fix) { report(`Unable to read ${theme} (${message})`) } } - }) + } })() diff --git a/yarn.lock b/yarn.lock index c5371bd77d6a35b1b8285911c29f349f8edb139a..9841028ba197644b4aeecccf0439d17557b426bf 100644 GIT binary patch delta 965 zcmY+DJ#X7a7{?*QK>?>rkk)mY42C+YlJ3ZRAp;b#6pMOcNxejoEe!_vawk%vMTvT$ zsLfxYY@n$~va~}x6z$-l=-8!0;Px|QX}WglR#NfOyXATAxdZ;c=lS2;7k7Vub@#`g z-tFhvda;FW=Jcz-_seNg%on-qu{6xX0!a2^xcN+g0_Fx`0_C8$(kOJywHGcNn=SRG$A(?K zsB~zxH-dvfTPbxzzgOCT2{M4~U51-peD&?o@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