-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for directUrl usage during testing (#7898)
* When testing an application with directUrl functionality in Prisma populate DIRECT_URL env variable with TEST_DIRECT_URL or TEST_DATABASE_URL * Add regex parsing for directUrl env variable in schema.prisma and set it to test database * refactor to add a test * change back to project config * more fixes from local testing * remove stray `afterAll` * set same env var in execa --------- Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
- Loading branch information
Showing
4 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/testing/src/api/__tests__/directUrlHelpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import { checkAndReplaceDirectUrl, getDefaultDb } from '../directUrlHelpers' | ||
|
||
const FIXTURE_DIR_PATH = path.resolve('..', '..', '__fixtures__') | ||
|
||
const NO_DIRECT_URL_FIXTURE_PATH = path.join(FIXTURE_DIR_PATH, 'test-project') | ||
const DIRECT_URL_FIXTURE_PATH = path.join(FIXTURE_DIR_PATH, 'empty-project') | ||
|
||
it("does nothing if directUrl isn't set", () => { | ||
process.env.RWJS_CWD = NO_DIRECT_URL_FIXTURE_PATH | ||
|
||
checkAndReplaceDirectUrl( | ||
fs.readFileSync( | ||
path.join(NO_DIRECT_URL_FIXTURE_PATH, 'api', 'db', 'schema.prisma'), | ||
'utf-8' | ||
), | ||
getDefaultDb(NO_DIRECT_URL_FIXTURE_PATH) | ||
) | ||
|
||
expect(process.env.DIRECT_URL).toBeUndefined() | ||
|
||
delete process.env.RWJS_CWD | ||
}) | ||
|
||
it("overwrites directUrl if it's set", () => { | ||
process.env.RWJS_CWD = DIRECT_URL_FIXTURE_PATH | ||
|
||
const defaultDb = getDefaultDb(DIRECT_URL_FIXTURE_PATH) | ||
|
||
const directUrlEnvVar = checkAndReplaceDirectUrl( | ||
fs.readFileSync( | ||
path.join(DIRECT_URL_FIXTURE_PATH, 'api', 'db', 'schema.prisma'), | ||
'utf-8' | ||
), | ||
defaultDb | ||
) | ||
|
||
expect(process.env[directUrlEnvVar as string]).toBe(defaultDb) | ||
|
||
delete process.env.RWJS_CWD | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import path from 'path' | ||
|
||
export function getDefaultDb(rwjsCwd: string) { | ||
return `file:${path.join(rwjsCwd, '.redwood', 'test.db')}` | ||
} | ||
|
||
export function checkAndReplaceDirectUrl( | ||
prismaSchema: string, | ||
defaultDb: string | ||
) { | ||
// Check the schema.prisma for a directUrl. | ||
const directUrl = prismaSchema.match(PRISMA_DIRECT_URL_REGEXP) | ||
|
||
// If it's not there, make this a no-op. | ||
if (!directUrl) { | ||
return | ||
} | ||
|
||
// If it is, set its env var to the test equivalent. | ||
const directUrlEnvMatch = directUrl[0].match(BETWEEN_PARENTHESES_REGEXP) //[2] | ||
|
||
// This is mostly to please TS. But it's good to be safe because in this case we want to be 100% correct. | ||
if (!directUrlEnvMatch) { | ||
throw new Error( | ||
'Error parsing `directUrl` from schema.prisma. Proceeding with this env var could be dangerous. Please check your schema.prisma file; if everything looks ok, file an issue.' | ||
) | ||
} | ||
|
||
// `directUrlEnvMatch` look something like `["(DIRECT_URL)", "", "DIRECT_URL"]`. We want the third element. | ||
const directUrlEnv = directUrlEnvMatch[2] | ||
|
||
process.env[directUrlEnv] = | ||
process.env.TEST_DIRECT_URL || process.env.TEST_DATABASE_URL || defaultDb | ||
|
||
return directUrlEnv | ||
} | ||
|
||
const PRISMA_DIRECT_URL_REGEXP = /directUrl(\s?)=(\s?)env\(('|")(.*)('|")\)/g | ||
const BETWEEN_PARENTHESES_REGEXP = /\(('|")([^)]+)('|")\)/ |