Skip to content

Commit

Permalink
Test(Language Server): Add monorepo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed May 20, 2024
1 parent e1249c1 commit 5ecde0d
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 3,305 deletions.
34 changes: 17 additions & 17 deletions packages/language-server/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export declare type Workspace = {
path: string
uri: string
openedTextDocuments: TextDocument[]
cssLanguageService?: CSSLanguageService
languageService?: CSSLanguageService
languageServiceSettings: CSSLanguageServiceSettings
}

Expand Down Expand Up @@ -91,36 +91,36 @@ export default class CSSLanguageServer {
async onHover(params: HoverParams) {
await this.init()
const workspace = this.findClosestWorkspace(params.textDocument.uri)
if (workspace?.cssLanguageService) {
if (workspace.languageService) {
const document = this.documents.get(params.textDocument.uri)
if (document) return workspace.cssLanguageService.inspectSyntax(document, params.position)
if (document) return workspace.languageService.inspectSyntax(document, params.position)
}
}

async onCompletion(params: CompletionParams) {
await this.init()
const workspace = this.findClosestWorkspace(params.textDocument.uri)
if (workspace?.cssLanguageService) {
if (workspace.languageService) {
const document = this.documents.get(params.textDocument.uri)
if (document) return workspace.cssLanguageService.suggestSyntax(document, params.position, params.context)
if (document) return workspace.languageService.suggestSyntax(document, params.position, params.context)
}
}

async onDocumentColor(params: DocumentColorParams) {
await this.init()
const workspace = this.findClosestWorkspace(params.textDocument.uri)
if (workspace?.cssLanguageService) {
if (workspace.languageService) {
const document = this.documents.get(params.textDocument.uri)
if (document) return workspace.cssLanguageService.renderSyntaxColors(document)
if (document) return workspace.languageService.renderSyntaxColors(document)
}
}

async onColorPresentation(params: ColorPresentationParams) {
await this.init()
const workspace = this.findClosestWorkspace(params.textDocument.uri)
if (workspace?.cssLanguageService) {
if (workspace.languageService) {
const document = this.documents.get(params.textDocument.uri)
if (document) return workspace.cssLanguageService.editSyntaxColors(document, params.color, params.range)
if (document) return workspace.languageService.editSyntaxColors(document, params.color, params.range)
}
}

Expand All @@ -129,7 +129,7 @@ export default class CSSLanguageServer {
const workspace = this.findClosestWorkspace(params.document.uri)
if (!workspace || workspace.openedTextDocuments.includes(params.document)) return
if (!workspace.openedTextDocuments.length) {
this.initCSSLanguageService(workspace)
this.initLanguageService(workspace)
}
workspace.openedTextDocuments.push(params.document)
}
Expand All @@ -140,7 +140,7 @@ export default class CSSLanguageServer {
if (!workspace) return
workspace.openedTextDocuments.splice(workspace.openedTextDocuments.indexOf(params.document), 1)
if (!workspace.openedTextDocuments.length) {
this.destroyCSSLanguageService(workspace)
this.destroyLanguageService(workspace)
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ export default class CSSLanguageServer {
})
}

initCSSLanguageService(workspace: Workspace) {
initLanguageService(workspace: Workspace) {
let workspaceConfig: Config | undefined
try {
workspaceConfig = exploreConfig({
Expand All @@ -220,12 +220,12 @@ export default class CSSLanguageServer {
this.console.error(e)
}
this.console.info(`Initialized workspace ${workspaceConfig ? '(with config file)' : ''} ${workspace.uri}`)
workspace.cssLanguageService = new CSSLanguageService({ ...workspace.languageServiceSettings, config: workspaceConfig })
workspace.languageService = new CSSLanguageService({ ...workspace.languageServiceSettings, config: workspaceConfig })
}

destroyCSSLanguageService(workspace: Workspace) {
destroyLanguageService(workspace: Workspace) {
this.console.info(`Destroyed workspace ${workspace.uri}`)
delete workspace.cssLanguageService
delete workspace.languageService
}

findClosestWorkspace(textDocumentURI: string) {
Expand All @@ -236,9 +236,9 @@ export default class CSSLanguageServer {
}
}
if (process.env.DEBUG && !foundWorkspace) {
this.console.info(`No workspace found for ${textDocumentURI}`)
this.console.info(`This is an external document ${textDocumentURI} assigned to the first workspace`)
}
return foundWorkspace
return foundWorkspace || this.workspaces[0]
}

getWorkspace(uri: string) {
Expand Down
6 changes: 3 additions & 3 deletions packages/language-server/tests/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Writable, Readable, Duplex, DuplexOptions } from 'stream'
import { MessageReader, StreamMessageReader, StreamMessageWriter, createConnection, createProtocolConnection } from 'vscode-languageserver/node'
import { Duplex, DuplexOptions } from 'stream'
import { createConnection, createProtocolConnection } from 'vscode-languageserver/node'
import CSSLanguageServer, { Settings } from '../src'

export function connect(settings?: Settings) {
Expand All @@ -14,7 +14,7 @@ export function connect(settings?: Settings) {
const input = new Duplex(duplexOptions)
const output = new Duplex(duplexOptions)
const serverConnection = createConnection(input, output)
const clientConnection = createConnection(output, input)
const clientConnection = createProtocolConnection(output, input)
const server = new CSSLanguageServer(serverConnection, settings)
server.start()
clientConnection.listen()
Expand Down
20 changes: 20 additions & 0 deletions packages/language-server/tests/custom-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { resolve } from 'path'
import { withFixture } from './setup'
import { test } from 'vitest'
import { Settings } from '../src'

const settings: Settings = {
workspaces: [
'./c'
]
}

withFixture('monorepo', async (context) => {
test.concurrent('workspaces', async ({ expect }) => {
expect(context.server.workspaces.length).toBe(2)
expect(context.server.workspaces).toMatchObject([
{ path: resolve('tests/fixtures/monorepo') },
{ path: resolve('tests/fixtures/monorepo/c') },
])
})
}, settings)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="fg:custom"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Config } from '@master/css'

export default {
variables: {
custom: '#ff0000'
}
} as Config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="fg:custom"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Config } from '@master/css'

export default {
variables: {
custom: '#ffff00'
}
} as Config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="fg:global"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "vscode-playground-package-c",
"private": true
}
24 changes: 24 additions & 0 deletions packages/language-server/tests/monorepo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { resolve } from 'path'
import { withFixture } from './setup'
import { test } from 'vitest'

withFixture('monorepo', async (context) => {
test.concurrent('workspaces', async ({ expect }) => {
expect(context.server.workspaces.length).toBe(3)
expect(context.server.workspaces).toMatchObject([
{ path: resolve('tests/fixtures/monorepo') },
{ path: resolve('tests/fixtures/monorepo/a') },
{ path: resolve('tests/fixtures/monorepo/b') },
])
})

test('open a document and link it to the nearest workspace', async ({ expect }) => {
const dir = resolve(__dirname, './fixtures/monorepo/a')
const textDocument = context.createDocument('', { dir })
await context.server.onDidOpen({ document: textDocument })
const targetWorkspace = context.server.workspaces.find((workspace) => workspace.path === resolve(dir))
expect(targetWorkspace?.openedTextDocuments.length).toBe(1)
await context.server.onDidClose({ document: textDocument })
expect(context.rootWorkspace?.openedTextDocuments?.length).toBe(0)
})
})
8 changes: 4 additions & 4 deletions packages/language-server/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientCapabilities, Connection, InitializeParams, InitializeRequest, InitializedNotification } from 'vscode-languageserver/node'
import { ClientCapabilities, InitializeParams, InitializeRequest, InitializedNotification, ProtocolConnection } from 'vscode-languageserver/node'
import CSSLanguageServer, { Settings, Workspace } from '../src'
import { beforeAll, describe } from 'vitest'
import { resolve } from 'node:path'
Expand All @@ -13,15 +13,15 @@ declare type FixtureContext = {
rootUri: string,
rootWorkspace: Workspace | undefined,
workspaceFolders: { uri: string, name: string }[],
clientConnection: Connection,
clientConnection: ProtocolConnection,
createDocument: (text?: string, options?: { lang?: string, dir?: string }) => TextDocument
}

export function withFixture(fixture: string, cb: (context: FixtureContext) => void, settings?: Settings) {
describe(fixture, async () => {
const context = {}
beforeAll(async () => {
const fixtureDir = resolve(__dirname, `../fixtures/${fixture}`)
const fixtureDir = resolve(__dirname, `./fixtures/${fixture}`)
const workspaceFolders = [{
uri: URI.file(fixtureDir).toString(),
name: 'test'
Expand Down Expand Up @@ -97,7 +97,7 @@ export function withFixture(fixture: string, cb: (context: FixtureContext) => vo
workspaceFolders,
clientConnection,
rootWorkspace: server.getWorkspace(rootUri),
createDocument: (text = '<div></div>', options?: { lang?: string, dir?: string }) => {
createDocument: (text = '', options?: { lang?: string, dir?: string }) => {
return createDocument(text, { lang: options?.lang, dir: options?.dir || fixtureDir })
}
} as FixtureContext)
Expand Down
19 changes: 12 additions & 7 deletions packages/language-server/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'path'
import { withFixture } from './setup'
import { test } from 'vitest'

Expand All @@ -7,19 +8,23 @@ withFixture('basic', async (context) => {
expect(context.server.workspaces[0].uri).toBe(context.rootUri)
})

test.concurrent('open and close a document', async ({ expect }) => {
test('open and close a document', async ({ expect }) => {
const textDocument = context.createDocument()
await context.server.onDidOpen({ document: textDocument })
expect(context.rootWorkspace?.openedTextDocuments?.length).toBe(1)
expect(context.rootWorkspace?.openedTextDocuments).toEqual([textDocument])
await context.server.onDidClose({ document: textDocument })
expect(context.rootWorkspace?.openedTextDocuments?.length).toBe(0)
})
})

test('open an external document', async ({ expect }) => {
const dir = resolve(__dirname, './external')
const textDocument = context.createDocument('', { dir })
await context.server.onDidOpen({ document: textDocument })
expect(context.rootWorkspace?.openedTextDocuments?.length).toBe(1)
expect(context.rootWorkspace?.openedTextDocuments).toEqual([textDocument])
await context.server.onDidClose({ document: textDocument })
expect(context.rootWorkspace?.openedTextDocuments?.length).toBe(0)
})
})

// test.todo('modify and refresh config')
// test.todo('detect monorepo workspaces')
// test.todo('specify workspaces')
// test.todo('workspace without any config file')
// test.todo('multi-workspace config files')
5 changes: 2 additions & 3 deletions packages/language-server/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
test: {
testTimeout: 15000,
include: [
'**/*.{test,spec}.?(c|m)[jt]s?(x)',
'**/test.?(c|m)[jt]s?(x)'
'tests/**/*.{test,spec}.?(c|m)[jt]s?(x)',
'tests/**/test.?(c|m)[jt]s?(x)'
]
},
plugins: [tsconfigPaths({
Expand Down
Loading

0 comments on commit 5ecde0d

Please sign in to comment.