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: reorganize tests #1733

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 4 additions & 18 deletions packages/language-server/src/__test__/helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as fs from 'fs'
import { Position, TextDocument } from 'vscode-languageserver-textdocument'
import { loadSchemaFiles } from '@prisma/schema-files-loader'
import path from 'path'
import { URI } from 'vscode-uri'
import { PrismaSchema, SchemaDocument } from '../lib/Schema'

export const CURSOR_CHARACTER = '|'

const fixturesDir = path.resolve(__dirname, '../../test/fixtures')
const multifileFixturesDir = path.join(fixturesDir, 'multifile')
const fixturesDir = path.resolve(__dirname, 'fixtures')

export function getTextDocument(testFilePath: string): TextDocument {
const absPath = path.join(fixturesDir, testFilePath)
Expand All @@ -17,23 +14,12 @@ export function getTextDocument(testFilePath: string): TextDocument {
return TextDocument.create(fixturePathToUri(absPath), 'prisma', 1, content)
}

export async function getMultifileSchema(folderPath: string): Promise<PrismaSchema> {
const files = await loadSchemaFiles(path.join(multifileFixturesDir, folderPath))
const schemaDocs = files.map(([filePath, content]) => {
const uri = fixturePathToUri(filePath)
const doc = TextDocument.create(uri, 'prisma', 1, content)
return new SchemaDocument(doc)
})

return new PrismaSchema(schemaDocs)
}

export function fixturePathToUri(fixturePath: string) {
const absPath = path.isAbsolute(fixturePath) ? fixturePath : path.join(fixturesDir, fixturePath)
export function fixturePathToUri(fixturePath: string, rootDir = fixturesDir) {
const absPath = path.isAbsolute(fixturePath) ? fixturePath : path.join(rootDir, fixturePath)

// that would normalize testFilePath and resolve all of
// the . and ..
const relPath = path.relative(fixturesDir, absPath)
const relPath = path.relative(rootDir, absPath)
// this will normalize slashes on win/linux
return URI.file(`/${relPath}`).toString()
}
Expand Down
18 changes: 16 additions & 2 deletions packages/language-server/src/__test__/multifile/MultifileHelper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { URI } from 'vscode-uri'
import { PrismaSchema, SchemaDocument } from '../../lib/Schema'
import path from 'path'
import { getMultifileSchema } from '../helper'
import { fixturePathToUri } from '../helper'
import { Position, TextEdit } from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { loadSchemaFiles } from '@prisma/schema-files-loader'

const multifileFixturesDir = path.join(__dirname, 'fixtures')

export async function getMultifileHelper(fixturePath: string) {
const schema = await getMultifileSchema(fixturePath)
const helper = new MultfileHelper(`/multifile/${fixturePath}`, schema)
const helper = new MultfileHelper(`/${fixturePath}`, schema)
return helper
}

Expand Down Expand Up @@ -81,3 +84,14 @@ class Line {
}
}
}

async function getMultifileSchema(folderPath: string): Promise<PrismaSchema> {
const files = await loadSchemaFiles(path.join(multifileFixturesDir, folderPath))
const schemaDocs = files.map(([filePath, content]) => {
const uri = fixturePathToUri(filePath, multifileFixturesDir)
const doc = TextDocument.create(uri, 'prisma', 1, content)
return new SchemaDocument(doc)
})

return new PrismaSchema(schemaDocs)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test('basic doc', async () => {
"line": 1,
},
},
"targetUri": "file:///multifile/user-posts/User.prisma",
"targetUri": "file:///user-posts/User.prisma",
},
]
`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('invalid doc', async () => {
expect(response).toMatchInlineSnapshot(`
DiagnosticMap {
"_map": Map {
"file:///multifile/linting/Post.prisma" => [
"file:///linting/Post.prisma" => [
{
"message": "Type "Like" is neither a built-in type, nor refers to another model, composite type, or enum.",
"range": {
Expand All @@ -27,8 +27,8 @@ test('invalid doc', async () => {
"source": "Prisma",
},
],
"file:///multifile/linting/User.prisma" => [],
"file:///multifile/linting/config.prisma" => [],
"file:///linting/User.prisma" => [],
"file:///linting/config.prisma" => [],
},
}
`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('basic doc', async () => {
const updated = helper.applyChanges(changes)
expect(updated).toMatchInlineSnapshot(`
{
"file:///multifile/quick-fix/Profile.prisma": "model Profile {
"file:///quick-fix/Profile.prisma": "model Profile {
id String @id @default(uuid())
userId String @unique
user User @relation(fields: [userId], references: [id])
Expand Down
16 changes: 8 additions & 8 deletions packages/language-server/src/__test__/multifile/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('rename model', async () => {
expect(response).toMatchInlineSnapshot(`
{
"changes": {
"file:///multifile/user-posts/Post.prisma": [
"file:///user-posts/Post.prisma": [
{
"newText": "Account",
"range": {
Expand All @@ -32,7 +32,7 @@ test('rename model', async () => {
},
},
],
"file:///multifile/user-posts/User.prisma": [
"file:///user-posts/User.prisma": [
{
"newText": "Account",
"range": {
Expand Down Expand Up @@ -67,7 +67,7 @@ test('rename model', async () => {

expect(helper.applyChanges(response?.changes)).toMatchInlineSnapshot(`
{
"file:///multifile/user-posts/Post.prisma": "/// This is a blog post
"file:///user-posts/Post.prisma": "/// This is a blog post
model Post {
id String @id @default(uuid())
title String
Expand All @@ -76,7 +76,7 @@ test('rename model', async () => {
author Account @relation(fields: [authorId], references: [id])
}
",
"file:///multifile/user-posts/User.prisma": "/// This is the user of the platform
"file:///user-posts/User.prisma": "/// This is the user of the platform
model Account {
id String @id @default(uuid())
name String
Expand Down Expand Up @@ -104,7 +104,7 @@ test('rename field', async () => {
expect(response).toMatchInlineSnapshot(`
{
"changes": {
"file:///multifile/user-posts/Post.prisma": [
"file:///user-posts/Post.prisma": [
{
"newText": "primaryKey",
"range": {
Expand All @@ -119,7 +119,7 @@ test('rename field', async () => {
},
},
],
"file:///multifile/user-posts/User.prisma": [
"file:///user-posts/User.prisma": [
{
"newText": "primaryKey",
"range": {
Expand Down Expand Up @@ -153,7 +153,7 @@ test('rename field', async () => {

expect(helper.applyChanges(response?.changes)).toMatchInlineSnapshot(`
{
"file:///multifile/user-posts/Post.prisma": "/// This is a blog post
"file:///user-posts/Post.prisma": "/// This is a blog post
model Post {
id String @id @default(uuid())
title String
Expand All @@ -162,7 +162,7 @@ test('rename field', async () => {
author User @relation(fields: [authorId], references: [primaryKey])
}
",
"file:///multifile/user-posts/User.prisma": "/// This is the user of the platform
"file:///user-posts/User.prisma": "/// This is the user of the platform
model User {
primaryKey String @id @default(uuid() @map("id"))
name String
Expand Down
Loading