forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artificial-panic.test.ts
294 lines (258 loc) · 8.06 KB
/
artificial-panic.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { jestContext } from '@prisma/get-platform'
import { serialize } from '@prisma/get-platform/src/test-utils/jestSnapshotSerializer'
import { getDMMF, isRustPanic, MultipleSchemas, relativizePathInPSLError } from '@prisma/internals'
import { DbPull } from '@prisma/migrate'
import { Format } from '../Format'
import { Validate } from '../Validate'
const ctx = jestContext.new().assemble()
/// Normally, we use absolute paths for schema files throughout the codebase.
/// However, for tests we can't just store aboslute snapshot within the snapshot - we
/// want it to be transferable between different machines and operating systems. So, that's
/// why we are converting paths to relative before snapshoting them
function sanitizeSchemas(schemas: MultipleSchemas | undefined): MultipleSchemas | undefined {
return schemas?.map(([path, content]) => [relativizePathInPSLError(path), content])
}
/**
* Note: under the hood, these artificial-panic tests uses the Wasm'd `getConfig` and `getDMMF` definitions
*/
describe('artificial-panic introspection', () => {
// backup env vars
const OLD_ENV = { ...process.env }
afterEach(() => {
// reset env vars to backup state
process.env = { ...OLD_ENV }
})
it('schema-engine', async () => {
ctx.fixture('artificial-panic')
expect.assertions(6)
process.env.FORCE_PANIC_SCHEMA_ENGINE = '1'
const command = new DbPull()
try {
await command.parse(['--print'])
} catch (e) {
expect(e).toMatchInlineSnapshot(`
"Error in Schema engine.
Reason: [/some/rust/path:0:0] This is the debugPanic artificial panic
"
`)
expect(isRustPanic(e)).toBe(true)
expect(e.rustStack).toBeTruthy()
expect(e.schemaPath).toBeTruthy()
expect(sanitizeSchemas(e.schema)).toMatchInlineSnapshot(`
[
[
"prisma/schema.prisma",
"// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgres://user:password@randomhost:5432"
}
",
],
]
`),
expect(e).toMatchObject({
area: 'LIFT_CLI',
})
}
})
})
describe('artificial-panic formatter', () => {
const OLD_ENV = { ...process.env }
afterEach(() => {
process.env = { ...OLD_ENV }
})
it('formatter', async () => {
ctx.fixture('artificial-panic')
expect.assertions(5)
process.env.FORCE_PANIC_PRISMA_SCHEMA = '1'
const command = new Format()
try {
await command.parse([])
} catch (e) {
expect(serialize(e.message)).toMatchInlineSnapshot(`
""RuntimeError: panicked at prisma-schema-wasm/src/lib.rs:0:0:
This is the panic triggered by \`prisma_fmt::debug_panic()\`""
`)
expect(isRustPanic(e)).toBe(true)
expect(e.rustStack).toBeTruthy()
expect(e.schemaPath.replace(/\\/g, '/')) // replace due to Windows CI
.toContain('prisma/schema.prisma')
expect(sanitizeSchemas(e.schema)).toMatchInlineSnapshot(`
[
[
"prisma/schema.prisma",
"// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgres://user:password@randomhost:5432"
}
",
],
]
`)
}
})
})
describe('artificial-panic get-config', () => {
const OLD_ENV = { ...process.env }
afterEach(() => {
process.env = { ...OLD_ENV }
})
it('get-config', async () => {
ctx.fixture('artificial-panic')
expect.assertions(5)
process.env.FORCE_PANIC_QUERY_ENGINE_GET_CONFIG = '1'
const command = new Validate()
try {
await command.parse([])
} catch (e) {
expect(serialize(e.message)).toMatchInlineSnapshot(`
""RuntimeError: panicked at prisma-schema-wasm/src/lib.rs:0:0:
This is the panic triggered by \`prisma_fmt::debug_panic()\`""
`)
expect(isRustPanic(e)).toBe(true)
expect(e.rustStack).toBeTruthy()
expect(sanitizeSchemas(e.schema)).toMatchInlineSnapshot(`
[
[
"prisma/schema.prisma",
"// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgres://user:password@randomhost:5432"
}
",
],
]
`)
expect(e).toMatchObject({
schemaPath: undefined,
})
}
})
})
describe('artificial-panic validate', () => {
const OLD_ENV = { ...process.env }
afterEach(() => {
process.env = { ...OLD_ENV }
})
it('validate', async () => {
ctx.fixture('artificial-panic')
expect.assertions(5)
process.env.FORCE_PANIC_QUERY_ENGINE_GET_DMMF = '1'
const command = new Validate()
try {
await command.parse([])
} catch (e) {
expect(serialize(e.message)).toMatchInlineSnapshot(`
""RuntimeError: panicked at prisma-schema-wasm/src/lib.rs:0:0:
This is the panic triggered by \`prisma_fmt::debug_panic()\`""
`)
expect(isRustPanic(e)).toBe(true)
expect(e.rustStack).toBeTruthy()
expect(sanitizeSchemas(e.schema)).toMatchInlineSnapshot(`
[
[
"prisma/schema.prisma",
"// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgres://user:password@randomhost:5432"
}
",
],
]
`)
expect(e.schemaPath).toBeTruthy()
}
})
it('format', async () => {
ctx.fixture('artificial-panic')
expect.assertions(5)
process.env.FORCE_PANIC_QUERY_ENGINE_GET_DMMF = '1'
const command = new Format()
try {
await command.parse([])
} catch (e) {
expect(serialize(e.message)).toMatchInlineSnapshot(`
""RuntimeError: panicked at prisma-schema-wasm/src/lib.rs:0:0:
This is the panic triggered by \`prisma_fmt::debug_panic()\`""
`)
expect(isRustPanic(e)).toBe(true)
expect(e.rustStack).toBeTruthy()
expect(sanitizeSchemas(e.schema)).toMatchInlineSnapshot(`
[
[
"prisma/schema.prisma",
"// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgres://user:password@randomhost:5432"
}
",
],
]
`)
expect(e.schemaPath).toBeTruthy()
}
})
})
describe('artificial-panic getDMMF', () => {
const OLD_ENV = { ...process.env }
afterEach(() => {
process.env = { ...OLD_ENV }
})
it('getDMMF', async () => {
ctx.fixture('artificial-panic')
expect.assertions(5)
process.env.FORCE_PANIC_QUERY_ENGINE_GET_DMMF = '1'
try {
await getDMMF({
datamodel: /* prisma */ `generator client {
provider = "prisma-client-js"
}`,
})
} catch (e) {
expect(serialize(e.message)).toMatchInlineSnapshot(`
""RuntimeError: panicked at prisma-schema-wasm/src/lib.rs:0:0:
This is the panic triggered by \`prisma_fmt::debug_panic()\`""
`)
expect(isRustPanic(e)).toBe(true)
expect(e.rustStack).toBeTruthy()
expect(e.schema).toMatchInlineSnapshot(`
[
[
"schema.prisma",
"generator client {
provider = "prisma-client-js"
}",
],
]
`)
expect(e).toMatchObject({
schemaPath: undefined,
})
}
})
})