Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not start a shell to run child process #29

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dtc-aws-plugin/src/dynamo-db-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {DynamoDB, AttributeValue} from '@aws-sdk/client-dynamodb'
import {DynamoDBDocument} from '@aws-sdk/lib-dynamodb'
import extraAssert from '@cgauge/assert'
import nodeAssert from 'node:assert'
import {is, optional, unknown, record, diff, TypeFromSchema, union} from 'type-assurance'
import {is, optional, unknown, record, diff, TypeFromSchema, union} from '@cgauge/type-guard'

const DynamoArrange = {
table: String,
Expand Down
2 changes: 1 addition & 1 deletion dtc-aws-plugin/src/event-bridge-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EventBridge} from '@aws-sdk/client-eventbridge'
import {info} from '@cgauge/dtc'
import {is, unknown, record, diff} from 'type-assurance'
import {is, unknown, record, diff} from '@cgauge/type-guard'

const EventBridgeAct = {
eventBus: String,
Expand Down
2 changes: 1 addition & 1 deletion dtc-aws-plugin/src/lambda-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Lambda} from '@aws-sdk/client-lambda'
import extraAssert from '@cgauge/assert'
import {info} from '@cgauge/dtc'
import {is, unknown, record, diff} from 'type-assurance'
import {is, unknown, record, diff} from '@cgauge/type-guard'

const LambdaCall = {
functionName: String,
Expand Down
14 changes: 8 additions & 6 deletions dtc-aws-plugin/src/sns-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {SNS} from '@aws-sdk/client-sns'
import { info } from '@cgauge/dtc'
import {is, unknown, record, optional, union, diff} from 'type-assurance'
import {info} from '@cgauge/dtc'
import {is, unknown, record, optional, union, diff} from '@cgauge/type-guard'

const SnsCall = {
topic: String,
message: record(String, unknown),
messageAttributes: optional(record(String, {
DataType: union(String, undefined),
StringValue: optional(String),
}))
messageAttributes: optional(
record(String, {
DataType: union(String, undefined),
StringValue: optional(String),
}),
),
}

const sns = new SNS({})
Expand Down
6 changes: 3 additions & 3 deletions dtc-graphql-plugin/src/graphql-call-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import extraAssert from '@cgauge/assert'
import { info } from '@cgauge/dtc'
import {info} from '@cgauge/dtc'
import {GraphQLClient} from 'graphql-request'
import {is, record, diff, optional, unknown} from 'type-assurance'
import {is, record, diff, optional, unknown} from '@cgauge/type-guard'

const GraphQlCall = {
url: String,
Expand All @@ -14,7 +14,7 @@ let response: any

export const act = async (args: unknown) => {
response = undefined

if (!is(args, GraphQlCall)) {
const mismatch = diff(args, GraphQlCall)
info(`GraphQL plugin declared but test declaration didn't match the act. Invalid ${mismatch[0]}\n`)
Expand Down
2 changes: 1 addition & 1 deletion dtc-mysql-plugin/src/mysql-mock-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {debug} from '@cgauge/dtc'
import {Mock, mock} from 'node:test'
import * as mysql from './mock.js'
import nodeSqlParser from 'node-sql-parser'
import {is, unknown, record, optional, TypeFromSchema} from 'type-assurance'
import {is, unknown, record, optional, TypeFromSchema} from '@cgauge/type-guard'

const MockMysql = {
input: String,
Expand Down
2 changes: 1 addition & 1 deletion dtc-playwright-plugin/src/playwright-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Page, expect, Locator} from '@playwright/test'
import {is, unknown, record, union, optional, TypeFromSchema} from 'type-assurance'
import {is, unknown, record, union, optional, TypeFromSchema} from '@cgauge/type-guard'

const PlaywrightActionTarget = {
name: union('getByTestId', 'getByPlaceholder', 'getByText', 'getByTitle', 'getByLabel'),
Expand Down
5 changes: 2 additions & 3 deletions dtc-playwright-plugin/src/playwright-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

export default (constructorArgs?: string[]) =>
async (testCaseExecutions: TestCaseExecution[], _plugins: string[], runnerArgs: string[], config?: string) => {
const args = [...runnerArgs, ...constructorArgs ?? []]
const childProcess = spawnSync(`npx playwright test ${args?.join(' ')} ${__dirname}`, {
const args = ['playwright', 'test', ...runnerArgs, ...constructorArgs ?? [], __dirname]
const childProcess = spawnSync('npx', args, {
stdio: 'inherit',
shell: true,
env: {
...process.env,
...(testCaseExecutions.length === 1 && {DTC_PLAYWRIGHT_PATH: testCaseExecutions[0].filePath}),
Expand Down
3 changes: 1 addition & 2 deletions dtc/src/domain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {intersection} from '@cgauge/type-guard'
import {optional, record, TypeFromSchema, union, unknown} from 'type-assurance'
import {intersection, optional, record, TypeFromSchema, union, unknown} from '@cgauge/type-guard'

export type Loader = (filePath: string) => Promise<TestCase>

Expand Down
2 changes: 1 addition & 1 deletion dtc/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TestCaseExecution, Loader, TestCase} from './domain'
import {readdir, stat} from 'node:fs/promises'
import {join} from 'path'
import {assert} from 'type-assurance'
import {assert} from '@cgauge/type-guard'

const generateTestCaseExecution = async (filePath: string, loader: Loader): Promise<TestCaseExecution> => {
const testCase = await loader(filePath)
Expand Down
3 changes: 1 addition & 2 deletions dtc/src/plugins/function-call-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import extraAssert from '@cgauge/assert'
import {info} from '../utils.js'
import nodeAssert from 'node:assert'
import {is, optional, unknown, record, diff} from 'type-assurance'
import {intersection} from '@cgauge/type-guard'
import {is, optional, unknown, record, diff, intersection} from '@cgauge/type-guard'

const FunctionCallAct = {
import: String,
Expand Down
2 changes: 1 addition & 1 deletion dtc/src/plugins/http-call-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nodeAssert from 'node:assert/strict'
import extraAssert from '@cgauge/assert'
import {is, union, optional, unknown, record, assert as typeAssert, diff} from 'type-assurance'
import {is, union, optional, unknown, record, assert as typeAssert, diff} from '@cgauge/type-guard'
import {info} from '../utils'

let response: Response | undefined
Expand Down
2 changes: 1 addition & 1 deletion dtc/src/plugins/http-mock-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nodeAssert from 'node:assert/strict'
import extraAssert from '@cgauge/assert'
import nock from 'nock'
import {is, optional, record, union} from 'type-assurance'
import {is, optional, record, union} from '@cgauge/type-guard'

const MockHttp = {
url: String,
Expand Down
12 changes: 3 additions & 9 deletions dtc/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,21 @@ import {fileURLToPath} from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))

test('It executes test using cli', async () => {
const childProcess = spawnSync(`npx tsx ${__dirname}/../src/cli.ts ./test/fixtures/unit.js`, {
const childProcess = spawnSync('npx', ['tsx', `${__dirname}/../src/cli.ts`, './test/fixtures/unit.js'], {
stdio: 'inherit',
shell: true,
})

nodeAssert.equal(childProcess.status, 0)
})

test('It fails test using cli', async () => {
const childProcess = spawnSync(`npx tsx ${__dirname}/../src/cli.ts ./test/fixtures/unit-fail.js`, {
shell: true,
})
const childProcess = spawnSync('npx', ['tsx', `${__dirname}/../src/cli.ts`, './test/fixtures/unit-fail.js'])

nodeAssert.equal(childProcess.status, 1)
})

test('It executes all files', async () => {
const childProcess = spawnSync(`npx tsx ${__dirname}/../src/cli.ts`, {
stdio: 'inherit',
shell: true,
})
const childProcess = spawnSync('npx', ['tsx', `${__dirname}/../src/cli.ts`], {stdio: 'inherit'})

nodeAssert.equal(childProcess.status, 0)
})
77 changes: 74 additions & 3 deletions type-guard/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
import {is, Schema, TypeFromSchema} from 'type-assurance'
import {diff, optional, unknown, Schema, assert} from 'type-assurance'

/**
* Marker for optional properties.
*/
const OPTIONAL = 'type-assurance:optional' as const

/**
* Type to get the actual type from a schema.
*/
export type TypeFromSchema<T> = T extends StringConstructor
? string
: T extends NumberConstructor
? number
: T extends BooleanConstructor
? boolean
: T extends ReadonlyArray<Schema>
? {[P in keyof T]: TypeFromSchema<T[P]>}
: T extends {[key: string]: Schema}
? Expand<OptionalProps<T> & RequiredProps<T>>
: T extends new (...args: any) => infer R
? R
: T extends (v: unknown) => v is infer R
? R
: T

type Expand<T> = T extends unknown ? {[K in keyof T]: T[K]} : never

/**
* Extract only the optional props of a schema.
*/
type OptionalProps<T> = {
-readonly [K in keyof T as T[K] extends {[OPTIONAL]: true} ? K : never]?: TypeFromSchema<T[K]>
}

/**
* Extract only the required props of a schema.
*/
type RequiredProps<T> = {
-readonly [K in keyof T as T[K] extends {[OPTIONAL]: true} ? never : K]: TypeFromSchema<T[K]>
}

/**
* Type guard to check if a value is compatible with a given schema.
*/
export function is<const T extends Schema>(value: unknown, schema: T): value is TypeFromSchema<T> {
return diff(value, schema).length === 0
}

/**
* Creates a type guard that checks if a value matches any of the given schemas.
*/
export function union<T extends Schema[]>(...schemas: T) {
return (v: unknown): v is TypeFromSchema<T[number]> => schemas.some((schema) => is(v, schema))
}

/**
* Valid Record keys
*/
export type RecordKeys = StringConstructor | NumberConstructor | string | number

/**
* Creates a type guard that checks if a value matches a given Record<K, V>.
*/
export function record<const K extends RecordKeys, const V extends Schema>(key: K, value: V) {
return (v: unknown): v is Record<TypeFromSchema<K>, TypeFromSchema<V>> =>
typeof v === 'object' &&
v !== null &&
Object.values(v).every((y) => is(y, value)) &&
Object.keys(v).every((y) => is(y, key))
}

/**
* Converts an array of types to a intersection of types
Expand All @@ -10,6 +80,7 @@ export type ArrayToIntersection<T> = T extends []
: never

export function intersection<T extends Schema[]>(...schemas: T) {
return (v: unknown): v is ArrayToIntersection<TypeFromSchema<T>> =>
schemas.every((schema) => is(v, schema as Schema))
return (v: unknown): v is ArrayToIntersection<TypeFromSchema<T>> => schemas.every((schema) => is(v, schema as Schema))
}

export {diff, optional, unknown, assert}
4 changes: 2 additions & 2 deletions type-guard/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {test} from 'node:test'
import nodeAssert from 'node:assert'
import {ArrayToIntersection, intersection} from '../src/index'
import {is, record, TypeFromSchema, union} from 'type-assurance'
import {intersection} from '../src/index'
import {is, TypeFromSchema} from '@cgauge/type-guard'

test('intersection', () => {
const a: unknown = {foo: 'bar', baz: 42}
Expand Down