Skip to content

Commit

Permalink
test(introspection): cleanup test using tempy folder (#3170)
Browse files Browse the repository at this point in the history
Co-authored-by: prisma-bot <prismabots@gmail.com>
  • Loading branch information
Jolg42 and prisma-bot committed Jul 30, 2020
1 parent 613f1e8 commit 058dc05
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/packages/introspection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"prettier": "2.0.5",
"rimraf": "3.0.2",
"strip-ansi": "6.0.0",
"tempy": "^0.6.0",
"ts-jest": "26.1.4",
"ts-node": "8.10.2",
"typescript": "3.9.7"
Expand Down
79 changes: 43 additions & 36 deletions src/packages/introspection/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
import { join } from 'path'
import { promisify } from 'util'
import fs from 'fs'
import rimraf from 'rimraf'
import { join } from 'path'
import tempy from 'tempy'
import { Init, defaultSchema, defaultEnv } from '../commands/Init'
import stripAnsi from 'strip-ansi'

const del = promisify(rimraf)
const tmp = join(__dirname, '../../prisma')

beforeEach(async () => {
await del(tmp)
})

afterAll(async () => {
await del(tmp)
})

describe('init', () => {
test('is schema and env written on disk replace', async () => {
const tmpDir = tempy.directory()
const cwd = process.cwd()
process.chdir(tmpDir)

const init = Init.new()
const result = stripAnsi(await init.parse([]))

const schema = fs.readFileSync(join(tmp, 'schema.prisma'), 'utf-8')
const env = fs.readFileSync(join(tmp, '.env'), 'utf-8')
expect(result).toMatchInlineSnapshot(`
"
✔ Your Prisma schema was created at prisma/schema.prisma.
You can now open it in your favorite editor.
Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started.
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
3. Run prisma introspect to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.
More information in our documentation:
https://pris.ly/d/getting-started
"
`)

const schema = fs.readFileSync(
join(tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
expect(schema).toMatch(defaultSchema())
expect(env).toMatch(defaultEnv())
expect(result).toMatchInlineSnapshot(`
"
✔ Your Prisma schema was created at prisma/schema.prisma.
You can now open it in your favorite editor.

Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started.
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
3. Run prisma introspect to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.
const env = fs.readFileSync(join(tmpDir, 'prisma', '.env'), 'utf-8')
expect(env).toMatch(defaultEnv())

More information in our documentation:
https://pris.ly/d/getting-started
"
`)
process.chdir(cwd)
})

test('works with url param', async () => {
const tmpDir = tempy.directory()
const cwd = process.cwd()
process.chdir(tmpDir)

const init = Init.new()
const result = stripAnsi(
await init.parse(['--url', process.env.TEST_POSTGRES_URI!]),
)

const schema = fs.readFileSync(join(tmp, 'schema.prisma'), 'utf-8')
const env = fs.readFileSync(join(tmp, '.env'), 'utf-8')

expect(schema).toMatch(defaultSchema())
expect(env).toMatch(defaultEnv(process.env.TEST_POSTGRES_URI!))
expect(result).toMatchInlineSnapshot(`
"
✔ Your Prisma schema was created at prisma/schema.prisma.
Expand All @@ -67,5 +63,16 @@ describe('init', () => {
https://pris.ly/d/getting-started
"
`)

const schema = fs.readFileSync(
join(tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
expect(schema).toMatch(defaultSchema())

const env = fs.readFileSync(join(tmpDir, 'prisma', '.env'), 'utf-8')
expect(env).toMatch(defaultEnv(process.env.TEST_POSTGRES_URI!))

process.chdir(cwd)
})
})
6 changes: 2 additions & 4 deletions src/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 058dc05

Please sign in to comment.