Skip to content

Commit

Permalink
fix keystone prisma error message, and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Apr 15, 2024
1 parent 1e150d3 commit e90447e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/scripts/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function prisma (cwd: string, args: string[], frozen: boolean) {

// this is the compiled version of the configuration which was generated during the build step
if (!(await fs.stat(builtConfigPath).catch(() => null))) {
console.error('🚨 keystone build must be run before running keystone start')
console.error('🚨 keystone build must be run before running keystone prisma')
throw new ExitError(1)
}

Expand Down
8 changes: 5 additions & 3 deletions tests/cli-tests/__snapshots__/prisma.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`keystone prisma exits with the same code as the prisma child process exits with 1`] = `
"? Generated GraphQL and Prisma schemas
"? GraphQL and Prisma schemas are up to date
! Unknown command "bad-thing"
Expand Down Expand Up @@ -59,12 +59,14 @@ Examples
Display Prisma debug info
$ prisma debug
"
`;

exports[`keystone prisma uses the db url in the keystone config 1`] = `
"? Generated GraphQL and Prisma schemas
"? GraphQL and Prisma schemas are up to date
Prisma schema loaded from schema.prisma
Datasource "sqlite": SQLite database "app.db" at "file:./app.db"
Error: P1003: Database app.db does not exist at ./app.db"
Error: P1003: Database app.db does not exist at ./app.db
"
`;
40 changes: 22 additions & 18 deletions tests/cli-tests/prisma.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import execa from 'execa'
import { basicKeystoneConfig, cliBinPath, schemas, symlinkKeystoneDeps, testdir } from './utils'
import {
basicKeystoneConfig,
schemas,
spawnCommand,
spawnCommand2,
symlinkKeystoneDeps,
testdir
} from './utils'

// testing erroring when the schemas are not up to date is in artifacts.test.ts

test('keystone prisma exits with the same code as the prisma child process exits with', async () => {
const tmp = await testdir({
const cwd = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.js': basicKeystoneConfig,
})
const result = await execa('node', [cliBinPath, 'prisma', 'bad-thing'], {
reject: false,
all: true,
cwd: tmp,
})
expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot()
expect(result.exitCode).toBe(1)

await spawnCommand(cwd, ['build', '--no-ui'])
const { exitCode, output } = await spawnCommand2(cwd, ['prisma', 'bad-thing'])

expect(exitCode).toEqual(1)
expect(output.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot()
})

test('keystone prisma uses the db url in the keystone config', async () => {
const tmp = await testdir({
const cwd = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.js': basicKeystoneConfig,
})
const result = await execa('node', [cliBinPath, 'prisma', 'migrate', 'status'], {
reject: false,
all: true,
cwd: tmp,
})
expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot()
expect(result.exitCode).toBe(1)

await spawnCommand(cwd, ['build', '--no-ui'])
const { exitCode, output } = await spawnCommand2(cwd, ['prisma', 'migrate', 'status'])

expect(exitCode).toEqual(1)
expect(output.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot()
})
37 changes: 37 additions & 0 deletions tests/cli-tests/prisma.test.ts.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
basicKeystoneConfig,
cliBinPath,
schemas,
spawnCommand,
symlinkKeystoneDeps,
testdir
} from './utils'

// testing erroring when the schemas are not up to date is in artifacts.test.ts

test('keystone prisma exits with the same code as the prisma child process exits with', async () => {
const cwd = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.js': basicKeystoneConfig,
})

const output = await spawnCommand(cwd, ['prisma', 'bad-thing'])
expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot()
expect(result.exitCode).toBe(1)
})

test('keystone prisma uses the db url in the keystone config', async () => {
const tmp = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.js': basicKeystoneConfig,
})
const result = await execa('node', [cliBinPath, 'prisma', 'migrate', 'status'], {
reject: false,
all: true,
cwd: tmp,
})
expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot()
expect(result.exitCode).toBe(1)
})
18 changes: 16 additions & 2 deletions tests/cli-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,27 @@ export async function spawnCommand (cwd: string, commands: string[]) {
p.stdout.on('data', (data) => (output += data.toString('utf-8')))
p.stderr.on('data', (data) => (output += data.toString('utf-8')))
p.on('error', err => reject(err))
p.on('exit', code => {
if (code) return reject(new ExitError(code))
p.on('exit', exitCode => {
if (typeof exitCode === 'number' && exitCode !== 0) return reject(new ExitError(exitCode))
resolve(output)
})
})
}

export async function spawnCommand2 (cwd: string, commands: string[]) {
let output = ''
return new Promise<{
exitCode: number | null,
output: string
}>((resolve, reject) => {
const p = spawn('node', [cliBinPath, ...commands], { cwd })
p.stdout.on('data', (data) => (output += data.toString('utf-8')))
p.stderr.on('data', (data) => (output += data.toString('utf-8')))
p.on('error', err => reject(err))
p.on('exit', exitCode => (resolve({ exitCode, output })))
})
}

let dirsToRemove: string[] = []

afterAll(async () => {
Expand Down

0 comments on commit e90447e

Please sign in to comment.