Skip to content

Commit

Permalink
Change launch function to output run
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kirmas committed Mar 18, 2021
1 parent 75a95ef commit fa4128d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
10 changes: 5 additions & 5 deletions __typing__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -14,7 +14,7 @@ it('bootstrap3', async () => {
, localFrom = `${__dirname}/bootstrap3.css`
, input = rfs(from)

await run(launchers, {
await launchProd({
from: localFrom,
input,
outputPath: false
Expand All @@ -26,7 +26,7 @@ it('bootstrap4', async () => {
, localFrom = `${__dirname}/bootstrap4.css`
, input = rfs(from)

await run(launchers, {
await launchProd({
from: localFrom,
input,
outputPath: false
Expand All @@ -38,7 +38,7 @@ it('material10', async () => {
, localFrom = `${__dirname}/material10.css`
, input = rfs(from)

await run(launchers, {
await launchProd({
from: localFrom,
input,
outputPath: false
Expand Down
17 changes: 8 additions & 9 deletions func.spec.ts
Original file line number Diff line number Diff line change
@@ -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__"
Expand Down Expand Up @@ -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}/`, "")
}))
})
})
})
11 changes: 8 additions & 3 deletions test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<typeof launch>, runOpts: RunOpts) {
async function run(launchers: (postcss7.Processor | Processor)[], runOpts: RunOpts) {
const {
errorsCount = 0,
from,
Expand Down
58 changes: 26 additions & 32 deletions unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -20,13 +20,13 @@ let dtsContent: Readonly<string[]>
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{}"
Expand All @@ -35,35 +35,35 @@ 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"))
})

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"))
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit fa4128d

Please sign in to comment.