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

test: fix sigkill timeout for execa monitor #581

Merged
merged 15 commits into from
Jul 26, 2023
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
11 changes: 11 additions & 0 deletions tests/__helpers__/packageManager.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export const getPackageManager = () => process.env.PACKAGE_MANAGER ?? 'yarn'

export const getAdditionalPackagerCommandArgs = (packageManager: string, command: string) => {
const packagerCommandArgsMap: Record<string, Record<string, string>> = {
yarn: {
install: ' --prefer-offline --verbose',
},
}

const commandArgsMap = packagerCommandArgsMap[packageManager] ?? {}
return commandArgsMap[command] ?? ''
}
6 changes: 0 additions & 6 deletions tests/__providers__/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ export const project = () =>
include: ['src'],
})

// d(`starting project setup`)
// Execa.commandSync(`yalc publish --no-scripts`)
// ctx.runOrThrow(`yalc add ${thisPackageName}`)
// ctx.runOrThrow(`yarn install --legacy-peer-deps --prefer-offline`)
// d(`done project setup`)

return api
})
.done()
37 changes: 22 additions & 15 deletions tests/__providers__/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Execa from 'execa'
import { provider, Provider } from 'konn'
import { Providers } from 'konn/providers'
import { getPackageManager } from '../__helpers__/packageManager'
import { getAdditionalPackagerCommandArgs, getPackageManager } from '../__helpers__/packageManager'

// import { Dir } from '../Dir'

Expand Down Expand Up @@ -35,8 +35,12 @@ export type Contributes = {
// runPackagerCommandGracefully(command: string, options?: RunOptions): Execa.ExecaSyncReturnValue
runAsyncOrThrow(command: string, options?: RunAsyncOptions): RunAsyncReturnType
runAsyncGracefully(command: string, options?: RunAsyncOptions): RunAsyncReturnType
runPackagerCommandAsyncOrThrow(command: string, options?: RunAsyncOptions): RunAsyncReturnType
runPackagerCommandAsyncGracefully(command: string, options?: RunAsyncOptions): RunAsyncReturnType
runPackagerCommandAsyncOrThrow(command: string, args: string, options?: RunAsyncOptions): RunAsyncReturnType
runPackagerCommandAsyncGracefully(
command: string,
args: string,
options?: RunAsyncOptions,
): RunAsyncReturnType
}

export const monitorAsyncMethod = async (
Expand All @@ -49,7 +53,7 @@ export const monitorAsyncMethod = async (
console.log(`EXECA ${execaChildProcess.command} START`)
const timeoutId = setTimeout(() => {
execaChildProcess.kill('SIGTERM', {
forceKillAfterTimeout: options.timeout + 5000,
forceKillAfterTimeout: 5000,
})
}, options.timeout)
try {
Expand Down Expand Up @@ -121,16 +125,16 @@ export const run = (params?: Params): Provider<Needs, Contributes> =>
// reject: false,
// })
// },
// runPackagerCommandOrThrow(command, options) {
// runPackagerCommandOrThrow(command, args, options) {
// log.trace(`will_run`, { command })
// return Execa.commandSync(`${packageManager} ${command}`, {
// return Execa.commandSync(`${packageManager} ${command} ${args}`, {
// cwd,
// stdio,
// ...options,
// })
// },
// runPackagerCommandGracefully(command, options) {
// return this.runPackagerCommandOrThrow(command, {
// runPackagerCommandGracefully(command, args, options) {
// return this.runPackagerCommandOrThrow(command, args, {
// ...options,
// reject: false,
// })
Expand All @@ -153,20 +157,23 @@ export const run = (params?: Params): Provider<Needs, Contributes> =>
reject: false,
})
},
runPackagerCommandAsyncOrThrow(partialCommand, options) {
const command = `${packageManager} ${partialCommand}`
log.trace(`will_run`, { command })
return injectCommand(
runPackagerCommandAsyncOrThrow(command, args, options) {
const fullCommand = `${packageManager} ${command} ${args}${getAdditionalPackagerCommandArgs(
packageManager,
command,
Execa.command(command, {
)}`
log.trace(`will_run`, { fullCommand })
return injectCommand(
fullCommand,
Execa.command(fullCommand, {
cwd,
stdio,
...options,
}),
)
},
runPackagerCommandAsyncGracefully(command, options) {
return this.runPackagerCommandAsyncOrThrow(command, {
runPackagerCommandAsyncGracefully(command, args, options) {
return this.runPackagerCommandAsyncOrThrow(command, args, {
...options,
reject: false,
})
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/__global__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('ts-node/register')
import * as Execa from 'execa'
import type { Config } from '@jest/types'
import { getPackageManager } from '../../__helpers__/packageManager'
Expand Down
52 changes: 28 additions & 24 deletions tests/e2e/gentime-setting-output-psl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { monitorAsyncMethod, run } from '../__providers__/run'

const ctx = konn().useBeforeEach(providers.dir()).useBeforeEach(run()).useBeforeEach(project()).done()

it('gentime setting output: custom directory', async () => {
expect.assertions(1)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/basic'))
await ctx.fs.removeAsync('prisma/nexus-prisma.ts')
await ctx.fs.writeAsync(
'prisma/schema.prisma',
`
it(
'gentime setting output: custom directory',
async () => {
expect.assertions(1)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/basic'))
await ctx.fs.removeAsync('prisma/nexus-prisma.ts')
await ctx.fs.writeAsync(
'prisma/schema.prisma',
`
datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
Expand All @@ -32,20 +34,22 @@ it('gentime setting output: custom directory', async () => {
id String @id @default(cuid())
}
`,
)
await ctx.runAsyncOrThrow(
`${Path.join(process.cwd(), 'node_modules/.bin/yalc')} add ${ctx.thisPackageName}`,
)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install --legacy-peer-deps --prefer-offline', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runPackagerCommandAsyncOrThrow('prisma generate')
const result = await ctx.runPackagerCommandAsyncOrThrow('run --silent dev', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
})
expect(stripEndingLines(result.stdout)).toMatchSnapshot()
})
)
await ctx.runAsyncOrThrow(
`${Path.join(process.cwd(), 'node_modules/.bin/yalc')} add ${ctx.thisPackageName}`,
)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install', '--legacy-peer-deps', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runPackagerCommandAsyncOrThrow('run', 'prisma generate')
const result = await ctx.runPackagerCommandAsyncOrThrow('run', '--silent dev', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
})
expect(stripEndingLines(result.stdout)).toMatchSnapshot()
},
320 * 1000,
)
42 changes: 23 additions & 19 deletions tests/e2e/gentime-setting-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ import { monitorAsyncMethod, run } from '../__providers__/run'

const ctx = konn().useBeforeEach(providers.dir()).useBeforeEach(run()).useBeforeEach(project()).done()

it('gentime setting output: custom directory', async () => {
expect.assertions(1)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/basic'))
await ctx.runAsyncOrThrow(
`${Path.join(process.cwd(), 'node_modules/.bin/yalc')} add ${ctx.thisPackageName}`,
)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install --legacy-peer-deps --prefer-offline', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runPackagerCommandAsyncOrThrow('run prisma generate')
const result = await ctx.runPackagerCommandAsyncOrThrow('run --silent dev', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
})
expect(stripEndingLines(result.stdout)).toMatchSnapshot()
})
it(
'gentime setting output: custom directory',
async () => {
expect.assertions(1)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/basic'))
await ctx.runAsyncOrThrow(
`${Path.join(process.cwd(), 'node_modules/.bin/yalc')} add ${ctx.thisPackageName}`,
)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install', '--legacy-peer-deps', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runPackagerCommandAsyncOrThrow('run', 'prisma generate')
const result = await ctx.runPackagerCommandAsyncOrThrow('run', '--silent dev', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
})
expect(stripEndingLines(result.stdout)).toMatchSnapshot()
},
320 * 1000,
)
12 changes: 6 additions & 6 deletions tests/e2e/kitchen-sink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const GRAPHQL_SCHEMA_FILE_PATH = `schema.graphql`
const SERVER_READY_MESSAGE = `GraphQL API ready at http://localhost:4000/`

async function runTestProjectBuild(): Promise<ProjectResult> {
const runFirstBuild = await ctx.runPackagerCommandAsyncGracefully('run build')
const runReflectPrisma = await ctx.runPackagerCommandAsyncGracefully('run reflect:prisma')
const runReflectNexus = await ctx.runPackagerCommandAsyncGracefully('run reflect:nexus')
const runSecondBuild = await ctx.runPackagerCommandAsyncGracefully('run build')
const runFirstBuild = await ctx.runPackagerCommandAsyncGracefully('run', 'build')
const runReflectPrisma = await ctx.runPackagerCommandAsyncGracefully('run', 'reflect:prisma')
const runReflectNexus = await ctx.runPackagerCommandAsyncGracefully('run', 'reflect:nexus')
const runSecondBuild = await ctx.runPackagerCommandAsyncGracefully('run', 'build')
const fileGraphqlSchema = await ctx.fs.readAsync(GRAPHQL_SCHEMA_FILE_PATH)
const fileTypegen = await ctx.fs.readAsync(TYPEGEN_FILE_PATH)

Expand Down Expand Up @@ -75,7 +75,7 @@ beforeEach(async () => {
),
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install --legacy-peer-deps --prefer-offline', {
ctx.runPackagerCommandAsyncOrThrow('install', '--legacy-peer-deps', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
Expand Down Expand Up @@ -358,7 +358,7 @@ it('A full-featured project type checks, generates expected GraphQL schema, and

d(`migrating database`)

await ctx.runPackagerCommandAsyncOrThrow('run db:migrate')
await ctx.runPackagerCommandAsyncOrThrow('run', 'db:migrate')

d(`starting server`)

Expand Down
51 changes: 26 additions & 25 deletions tests/e2e/ncc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ import { monitorAsyncMethod, run } from '../__providers__/run'

const ctx = konn().useBeforeEach(providers.dir()).useBeforeEach(run()).useBeforeEach(project()).done()

it('works with ncc', async () => {
expect.assertions(1)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/ncc'))
await ctx.runAsyncOrThrow(
`${Path.join(process.cwd(), 'node_modules/.bin/yalc')} add ${ctx.thisPackageName}`,
)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install --legacy-peer-deps --prefer-offline', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runPackagerCommandAsyncOrThrow('build')

// Remove this to ensure that when the ncc build is run in the next step
// it is truly running independent of any node_modules.
await ctx.fs.removeAsync('node_modules')

const result = await ctx.runPackagerCommandAsyncOrThrow('run --silent start:dist', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
})

expect(stripEndingLines(result.stdout)).toMatchSnapshot()
})
it(
'works with ncc',
async () => {
expect.assertions(1)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/ncc'))
await ctx.runAsyncOrThrow(
`${Path.join(process.cwd(), 'node_modules/.bin/yalc')} add ${ctx.thisPackageName}`,
)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install', '--legacy-peer-deps', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runPackagerCommandAsyncOrThrow('build', '')
// Remove this to ensure that when the ncc build is run in the next step
// it is truly running independent of any node_modules.
await ctx.fs.removeAsync('node_modules')
const result = await ctx.runPackagerCommandAsyncOrThrow('run', '--silent start:dist', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
})
expect(stripEndingLines(result.stdout)).toMatchSnapshot()
},
320 * 1000,
)
54 changes: 29 additions & 25 deletions tests/e2e/ts-node-import-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,45 @@ import { monitorAsyncMethod, run } from '../__providers__/run'

const ctx = konn().useBeforeEach(providers.dir()).useBeforeEach(run()).useBeforeEach(project()).done()

it('when project does not have ts-node installed nexus-prisma generator still generates if there are no TS generator config files present', async () => {
expect.assertions(2)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/ts-node-import-error'))
await ctx.fs.writeAsync(
`prisma/schema.prisma`,
createPrismaSchema({
content: dindist`
it(
'when project does not have ts-node installed nexus-prisma generator still generates if there are no TS generator config files present',
async () => {
expect.assertions(2)
await ctx.fixture.useAsync(Path.join(__dirname, 'fixtures/ts-node-import-error'))
await ctx.fs.writeAsync(
`prisma/schema.prisma`,
createPrismaSchema({
content: dindist`
model Foo {
id String @id
}
`,
}),
)
}),
)

await ctx.fs.writeAsync(
'prisma/nexus-prisma.ts',
dindist`
await ctx.fs.writeAsync(
'prisma/nexus-prisma.ts',
dindist`
throw new Error('Oops, something unexpected happened.')
`,
)
)

await ctx.runAsyncOrThrow(`yalc add ${ctx.thisPackageName}`)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install --legacy-peer-deps --prefer-offline', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)
await ctx.runAsyncOrThrow(`yalc add ${ctx.thisPackageName}`)
await monitorAsyncMethod(
() =>
ctx.runPackagerCommandAsyncOrThrow('install', '--legacy-peer-deps', {
env: { PEER_DEPENDENCY_CHECK: 'false' },
}),
{ retry: 3, timeout: 90 * 1000 },
)

const result = await ctx.runPackagerCommandAsyncGracefully('run --silent build')
const result = await ctx.runPackagerCommandAsyncGracefully('run', '--silent build')

expect(normalizeGeneratorOutput(result.stderr)).toMatchSnapshot('stderr')
expect(normalizeGeneratorOutput(result.stdout)).toMatchSnapshot('stdout')
})
expect(normalizeGeneratorOutput(result.stderr)).toMatchSnapshot('stderr')
expect(normalizeGeneratorOutput(result.stdout)).toMatchSnapshot('stdout')
},
320 * 1000,
)

const normalizeGeneratorOutput = (output: string) =>
output
Expand Down
Loading