From fa4128d12f0328a7fd39137c41f1951c464a8090 Mon Sep 17 00:00:00 2001 From: Andrii Kirmas Date: Thu, 18 Mar 2021 19:00:25 +0300 Subject: [PATCH] Change launch function to output run --- __typing__/index.spec.ts | 10 +++---- func.spec.ts | 17 ++++++------ test-runner.ts | 11 +++++--- unit.spec.ts | 58 ++++++++++++++++++---------------------- 4 files changed, 47 insertions(+), 49 deletions(-) diff --git a/__typing__/index.spec.ts b/__typing__/index.spec.ts index a6afa11..44e28f2 100644 --- a/__typing__/index.spec.ts +++ b/__typing__/index.spec.ts @@ -1,10 +1,10 @@ -import {launch, run, rfs } from '../test-runner' +import launch, { rfs } from '../test-runner' const {NODE_ENV} = process.env //@ts-expect-error process.env.NODE_ENV = "production" -const launchers = launch() +const launchProd = launch() //@ts-expect-error afterAll(() => process.env.NODE_ENV = NODE_ENV) @@ -14,7 +14,7 @@ it('bootstrap3', async () => { , localFrom = `${__dirname}/bootstrap3.css` , input = rfs(from) - await run(launchers, { + await launchProd({ from: localFrom, input, outputPath: false @@ -26,7 +26,7 @@ it('bootstrap4', async () => { , localFrom = `${__dirname}/bootstrap4.css` , input = rfs(from) - await run(launchers, { + await launchProd({ from: localFrom, input, outputPath: false @@ -38,7 +38,7 @@ it('material10', async () => { , localFrom = `${__dirname}/material10.css` , input = rfs(from) - await run(launchers, { + await launchProd({ from: localFrom, input, outputPath: false diff --git a/func.spec.ts b/func.spec.ts index 15c2e16..60305bd 100644 --- a/func.spec.ts +++ b/func.spec.ts @@ -1,6 +1,6 @@ import {dirname, resolve} from 'path' import {sync} from 'globby' -import {launch, run, readOpts, rfs, suiteName } from './test-runner' +import launch, {readOpts, rfs, suiteName } from './test-runner' const $cwd = process.cwd() , suitesDir = "__func__" @@ -33,14 +33,13 @@ globbing(configPattern) , from = resolve(exp).replace(".d.ts", "") , input = sources[name] - it(name, async () => await run( - launch({...opts, destination: {}}), - { - from, - input, - outputPath: exp.replace(`${suiteDir}/`, "") - } - )) + it(name, () => launch({ + ...opts, destination: {} + })({ + from, + input, + outputPath: exp.replace(`${suiteDir}/`, "") + })) }) }) }) diff --git a/test-runner.ts b/test-runner.ts index 44f503d..337f8a1 100644 --- a/test-runner.ts +++ b/test-runner.ts @@ -4,6 +4,7 @@ import postcss7 = require("postcss") import creator7 = require("./src/7") import postcss8 from 'postcss8' import creator8 = require("./src") +import type Processor from 'postcss8/lib/processor' export type RunOpts = Partial<{ from: string @@ -13,14 +14,18 @@ export type RunOpts = Partial<{ }> const {parse: $parse} = JSON -, launch = (opts?: Options) => [postcss7([creator7(opts)]), postcss8([creator8(opts)])] +, launch = (opts?: Options) => { + const launchers = [postcss7([creator7(opts)]), postcss8([creator8(opts)])] + return (runOpts: RunOpts) => run(launchers, runOpts) +} + +export default launch export { - launch, run, rfs, rfsl, readOpts, suiteName } -async function run(launchers: ReturnType, runOpts: RunOpts) { +async function run(launchers: (postcss7.Processor | Processor)[], runOpts: RunOpts) { const { errorsCount = 0, from, diff --git a/unit.spec.ts b/unit.spec.ts index b4eb2ab..fcd7938 100644 --- a/unit.spec.ts +++ b/unit.spec.ts @@ -2,11 +2,11 @@ import {statSync, appendFileSync, writeFileSync} from 'fs' import {resolve} from "path" import { platform } from "os" -import {launch, run, rfsl, rfs } from './test-runner' +import launch, { rfsl, rfs } from './test-runner' import { $exists, $unlink } from './src/utils' const osBasedAssertion = platform() === "darwin" ? "toBeGreaterThan" : "toBeGreaterThanOrEqual" -, defaultLaunchers = launch() +, launchDefault = launch() , FALSY = ["", undefined, null, false, 0] , suitesDir = "__unit__" , from = `${suitesDir}/index.css` @@ -20,13 +20,13 @@ let dtsContent: Readonly beforeAll(async () => { const dtsPath = `${from}.d.ts` await $unlink(dtsPath) - await run(defaultLaunchers, {from}) + await launchDefault({from}) dtsContent = rfsl(dtsPath) }) describe('features', () => { it('falsy file', async () => await Promise.all( - FALSY.map(from => run(defaultLaunchers, { + FALSY.map(from => launchDefault({ //@ts-expect-error from, input: ".class{}" @@ -35,7 +35,7 @@ describe('features', () => { it("delete on empty", async () => { writeFileSync(dtsPath, dtsContent.join("\n")) - await run(defaultLaunchers, {from, input: "input {}", outputPath: false}) + await launchDefault({from, input: "input {}", outputPath: false}) expect(await $exists(dtsPath)).toBe(false) writeFileSync(dtsPath, dtsContent.join("\n")) }) @@ -43,27 +43,27 @@ describe('features', () => { describe('content overwrite', () => { beforeAll(async () => { await $unlink(dtsPath) - await run(defaultLaunchers, {from, outputPath}) + await launchDefault({from, outputPath}) dtsContent = rfsl(dtsPath) }) it('no overwrite on same content', async () => { const modified = modifiedTime() - await run(defaultLaunchers, {from, outputPath}) + await launchDefault({from, outputPath}) expect(modifiedTime()).toBe(modified) }) it('overwrite after file append', async () => { appendFileSync(dtsPath, "/**/") const modified = modifiedTime() - await run(defaultLaunchers, {from, outputPath}) + await launchDefault({from, outputPath}) expect(modifiedTime())[osBasedAssertion](modified) }) }) }) describe("scenario", () => { - const running = (file: string, outputPath: string|false = `${suitesDir}/${file}.css.d.ts`) => run(defaultLaunchers, {from, input: rfs(`${suitesDir}/${file}.css`), outputPath }) + const running = (file: string, outputPath: string|false = `${suitesDir}/${file}.css.d.ts`) => launchDefault({from, input: rfs(`${suitesDir}/${file}.css`), outputPath }) it("start", () => running("index")) it("strip to classname", () => running("only-classname")) @@ -79,24 +79,20 @@ describe("scenario", () => { describe('options', () => { describe('identifierPattern', () => { - it('not global pattern', async () => await run( - launch({identifierPattern: /\.([\w-]+)/}), - { - from, - input: fromContent, - errorsCount: 1, - outputPath - } - )) + it('not global pattern', () => launch({ + identifierPattern: /\.([\w-]+)/ + })({ + from, + input: fromContent, + errorsCount: 1, + outputPath + })) }) describe("destination", () => { it('here', async () => { const destination = {} - await run( - launch({destination}), - {from} - ) + await launch({destination})({from}) expect( destination @@ -106,29 +102,27 @@ describe('options', () => { }) it('falsy', async () => await Promise.all( - FALSY.map(destination => destination === false ? void 0 : - run( - //@ts-expect-error - launch({destination}), - {from, errorsCount: 1} - ) + FALSY.map(destination => destination === false + ? void 0 + //@ts-expect-error + : launch({destination})({from, errorsCount: 1}) ) )) }) describe("checkMode: true", () => { - const launchers = launch({checkMode: true}) + const launchCheckMode = launch({checkMode: true}) it("modified", async () => { const created = modifiedTime() - expect(await run(launchers, {from, input: ".input {}", outputPath: false}).catch(err => err)).toBeInstanceOf(Error) + expect(await launchCheckMode({from, input: ".input {}", outputPath: false}).catch(err => err)).toBeInstanceOf(Error) expect(modifiedTime()).toBe(created) }) it("exists, but shouldn't", async () => { const created = modifiedTime() expect( - await run(launchers, {from, input: "input {}", outputPath: false}) + await launchCheckMode({from, input: "input {}", outputPath: false}) .catch(err => err) ).toBeInstanceOf(Error) expect(modifiedTime()).toBe(created) @@ -137,7 +131,7 @@ describe('options', () => { it("not exists, but should", async () => { await $unlink(dtsPath) expect( - await run(launchers, {from, input: fromContent, outputPath: false}) + await launchCheckMode({from, input: fromContent, outputPath: false}) .catch(err => err) ).toBeInstanceOf(Error) expect(await $exists(dtsPath)).toBe(false)