From ac2494eba264c6ba2564922192fd3bee40cd01f7 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Mon, 4 Oct 2021 00:58:27 +0300 Subject: [PATCH] support multiple files --- test-scss/helpers.js | 25 +++++++- test-scss/index.js | 58 ++++++++++--------- test-scss/index.spec.scss | 2 - test-scss/tests/{test.scss => test.spec.scss} | 0 test-scss/tests/test2.spec.scss | 9 +++ 5 files changed, 63 insertions(+), 31 deletions(-) delete mode 100644 test-scss/index.spec.scss rename test-scss/tests/{test.scss => test.spec.scss} (100%) create mode 100644 test-scss/tests/test2.spec.scss diff --git a/test-scss/helpers.js b/test-scss/helpers.js index 8ef78ac64..b618f213c 100644 --- a/test-scss/helpers.js +++ b/test-scss/helpers.js @@ -3,6 +3,8 @@ 'use strict' const process = require('node:process') +const { exec } = require('node:child_process') +const fs = require('fs') const BE_VERBOSE = process.argv.includes('-v') const colors = { // could use https://github.com/chalk/chalk @@ -46,11 +48,32 @@ const printTest = (title, force = false) => const printTestDetails = (title, force = false) => print('- ' + title, 8, null, force)// print test title (it) +const execSassPromise = inputFile => { + const outputFile = inputFile.replace('spec.scss', 'css') + const sassScript = `sass --style expanded --quiet --no-source-map --no-error-css ${inputFile}:${outputFile}` + + return new Promise((resolve, reject) => { + // eslint-disable-next-line no-unused-vars + exec(sassScript, async (error, stdout, stderr) => { + if (error) { + print(`error: ${error.message}`) + reject(error) + return + } + + const cssFile = await fs.promises.readFile(outputFile, 'utf8') + await fs.promises.unlink(outputFile) + resolve(cssFile) + }) + }) +} + module.exports = { colors, ErrorAssertion, print, printModule, printTest, - printTestDetails + printTestDetails, + execSassPromise } diff --git a/test-scss/index.js b/test-scss/index.js index 0b5d17f5e..18b017baa 100644 --- a/test-scss/index.js +++ b/test-scss/index.js @@ -18,19 +18,17 @@ 'use strict' -let exitStatus = 0 -const errors = [] +let hasFailedAssertions = false +let errors = [] const process = require('node:process') const BE_VERBOSE = process.argv.includes('-v') -const fs = require('fs') +const path = require('path') +const glob = require('glob') const helpers = require('./helpers') const sassTrue = require('sass-true') -const { exec } = require('child_process') const rootDir = 'test-scss' -const outputFile = rootDir + '/index.css' -const sassScript = `sass --style expanded --quiet --no-source-map --no-error-css ${rootDir}/index.spec.scss:${outputFile}` const describeModule = function (module) { helpers.printModule(module.module) @@ -43,7 +41,7 @@ const describeModule = function (module) { helpers.printTest(test.test) for (const assertion of test.assertions || []) { if (!assertion.passed) { - exitStatus = 1 + hasFailedAssertions = true countFailed++ const assertionDetails = sassTrue.formatFailureMessage(assertion) errors.push(new helpers.ErrorAssertion(module.module, test.test, assertionDetails)) @@ -56,32 +54,36 @@ const describeModule = function (module) { } } -helpers.print('Start scss tests... \n', 0, helpers.colors.Green, true) -// eslint-disable-next-line no-unused-vars -exec(sassScript, (error, stdout, stderr) => { +async function run() { + const files = glob.sync(path.resolve(__dirname, 'tests/**/*.scss')) + for (const file of files) { + errors = [] + const relative = file.slice(Math.max(0, file.indexOf(rootDir))) + helpers.print(`Processing ${relative}`, 0, helpers.colors.Blue, true) + // eslint-disable-next-line no-await-in-loop + const result = await helpers.execSassPromise(relative) - if (error) { - helpers.print(`error: ${error.message}`) - process.exit(1) - } + if (result.status === 'rejected') { + process.exit(1) + } - const cssFile = fs.readFileSync(outputFile).toString() - const modules = sassTrue.parse(cssFile) + const modules = sassTrue.parse(result) + for (const module of modules) { + describeModule(module) + } - for (const module of modules) { - describeModule(module) - } - - if (!BE_VERBOSE) { - for (const error of errors) { - helpers.printModule(error.module, true) - helpers.printTest(error.test, true) - helpers.printTestDetails(error.assertionDetails, true) + if (!BE_VERBOSE) { + for (const error of errors) { + helpers.printModule(error.module, true) + helpers.printTest(error.test, true) + helpers.printTestDetails(error.assertionDetails, true) + } } } - fs.unlinkSync(outputFile) helpers.print('End scss tests', 0, helpers.colors.Green, true) - process.exit(exitStatus) -}) + process.exit(hasFailedAssertions ? 1 : 0) +} +helpers.print('Start scss tests... \n', 0, helpers.colors.Green, true) +run() // begin execution diff --git a/test-scss/index.spec.scss b/test-scss/index.spec.scss deleted file mode 100644 index e4399fa03..000000000 --- a/test-scss/index.spec.scss +++ /dev/null @@ -1,2 +0,0 @@ -@import "tests/test"; - diff --git a/test-scss/tests/test.scss b/test-scss/tests/test.spec.scss similarity index 100% rename from test-scss/tests/test.scss rename to test-scss/tests/test.spec.scss diff --git a/test-scss/tests/test2.spec.scss b/test-scss/tests/test2.spec.scss new file mode 100644 index 000000000..56abb8f70 --- /dev/null +++ b/test-scss/tests/test2.spec.scss @@ -0,0 +1,9 @@ +@import "../../node_modules/sass-true/sass/true"; + +@include describe("A randowm module") { + + @include it("and a random test test") { + @include assert-equal(3, 3); + } +} +