Skip to content
Merged
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
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,22 @@ jobs:
- run: pnpm install --frozen-lockfile
- run: pnpm build:sqlint
- run: pnpm --filter sql-language-server test:db
vscode-e2e:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# minimum supported version (engines.vscode) and latest
vscode-version: [1.101.0, stable]
steps:
- uses: actions/checkout@v7
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
node-version: 22.x
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: xvfb-run -a pnpm test:vscode
env:
VSCODE_VERSION: ${{ matrix.vscode-version }}
- run: pnpm vscode:package
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ packages/server/dist/*
.pnp.*
.node-version
yarn-error.log
*.vsix
.vscode-test
20 changes: 7 additions & 13 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
**/test/
.**
**/*.ts
**/*.map
**/tsconfig.json
**/tslint.json
.cache
.vscode
**/*/rollup.config.js
**/*/jest.config.js
**/*/yarn-error.log
**/*/.npmignore
**/*/node_modules
# Both the client and the server are bundled by esbuild, so the extension only
# ships these files (plus package.json, README.md and LICENSE, which vsce adds).
**
!packages/client/out/extension.js
!packages/server/dist/vscodeExtensionServer.js
!README.md
!LICENSE
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,30 @@ $ pnpm migrate:postgres # postgres
$ pnpm migrate:mysql # mysql
$ pnpm migrate:sqlite # sqlite3
```

#### Running tests

Node.js 22.13+ and pnpm are required (`corepack enable` sets up the pnpm version pinned in `package.json`).

```sh
$ pnpm install
$ pnpm npm:prepublish # build sqlint and the server
$ pnpm lint && pnpm typecheck
$ pnpm test:parser && pnpm test:sqlint && pnpm test:server
```

Database integration tests run only when connection URLs are given:

```sh
$ SQLLS_TEST_POSTGRES=postgres://user:pass@127.0.0.1:5432/db \
SQLLS_TEST_MYSQL=mysql://user:pass@127.0.0.1:3306/db \
pnpm --filter sql-language-server test:db
```

The VS Code extension is tested end to end by launching VS Code (downloaded into `packages/client/.vscode-test`).
Set `VSCODE_VERSION` to test a specific version (default: `stable`).

```sh
$ pnpm test:vscode
$ pnpm vscode:package # build the .vsix
```
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default tseslint.config(
'**/node_modules/',
'**/dist/',
'**/out/',
'**/.vscode-test/',
'**/bin/',
'**/npm_bin/',
'packages/sql-parser/',
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:server": "pnpm --filter sql-language-server test",
"test:parser": "pnpm --filter @joe-re/sql-parser test",
"test:sqlint": "pnpm --filter sqlint test",
"test:vscode": "pnpm vsc-compile && pnpm --filter sql-language-server_vsc test:e2e",
"build:sqlint": "pnpm --filter sqlint build",
"build:server": "pnpm --filter sql-language-server compile:index",
"watch": "run-p watch:client watch:server",
Expand All @@ -41,7 +42,8 @@
"dev": "pnpm build:sqlint && run-p watch:sqlint watch:server watch:dev-server:client watch:dev-server:server",
"vscode:prepublish": "pnpm run vsc-compile",
"npm:prepublish": "pnpm build:sqlint && pnpm build:server",
"vscode:publish": "pnpm vscode:prepublish && vsce publish",
"vscode:package": "vsce package --no-dependencies",
"vscode:publish": "vsce publish --no-dependencies",
"changeset": "changeset",
"release:version": "changeset version",
"release:publish": "pnpm npm:prepublish && changeset publish"
Expand Down Expand Up @@ -92,6 +94,7 @@
"devDependencies": {
"@changesets/cli": "^3.0.3",
"@eslint/js": "^9.39.5",
"@vscode/vsce": "^4.0.0",
"eslint": "^9.39.5",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-import": "^2.32.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
"scripts": {
"compile": "esbuild ./extension.ts --bundle --platform=node --external:vscode --outfile=out/extension.js",
"watch": "pnpm compile --watch",
"typecheck": "tsc -p ."
"typecheck": "tsc -p .",
"compile:e2e": "esbuild test/e2e/runTest.ts test/e2e/suite.ts --bundle --platform=node --external:vscode --external:@vscode/test-electron --outdir=out/test",
"test:e2e": "pnpm compile:e2e && node out/test/runTest.js"
},
"engines": {
"node": "^22.13.0 || >=24",
"vscode": "^1.101.0"
},
"activationEvents": [
Expand All @@ -25,6 +28,7 @@
"devDependencies": {
"@types/node": "^22.18.0",
"@types/vscode": "~1.101.0",
"@vscode/test-electron": "^3.1.0",
"esbuild": "^0.28.2",
"tslib": "^2.0.0",
"typescript": "~6.0.3"
Expand Down
48 changes: 48 additions & 0 deletions packages/client/test/e2e/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import path from 'path'
import { DatabaseSync } from 'node:sqlite'
import { runTests } from '@vscode/test-electron'

// Launches VS Code with the extension from the repository root against a
// temporary workspace whose sqlite3 connection is configured in settings.json.
async function main() {
const repoRoot = path.resolve(__dirname, '../../../..')
const workspace = mkdtempSync(path.join(tmpdir(), 'sqlls-e2e-'))
try {
const dbFile = path.join(workspace, 'e2e.sqlite3')
const db = new DatabaseSync(dbFile)
db.exec(
'CREATE TABLE e2e_users (id INTEGER PRIMARY KEY, e2e_name TEXT NOT NULL)'
)
db.close()
mkdirSync(path.join(workspace, '.vscode'))
writeFileSync(
path.join(workspace, '.vscode', 'settings.json'),
JSON.stringify({
'sqlLanguageServer.connections': [
{ name: 'e2e-sqlite', adapter: 'sqlite3', filename: dbFile },
],
})
)

await runTests({
version: process.env.VSCODE_VERSION || 'stable',
extensionDevelopmentPath: repoRoot,
extensionTestsPath: path.resolve(__dirname, 'suite'),
launchArgs: [
workspace,
'--disable-extensions',
'--disable-workspace-trust',
],
extensionTestsEnv: { SQLLS_E2E_WORKSPACE: workspace },
})
} finally {
rmSync(workspace, { recursive: true, force: true })
}
}

main().catch((e) => {
console.error('Failed to run VS Code tests', e)
process.exitCode = 1
})
109 changes: 109 additions & 0 deletions packages/client/test/e2e/suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import assert from 'assert'
import path from 'path'
import * as vscode from 'vscode'

// Runs inside the VS Code extension host. Exported as `run` for
// @vscode/test-electron (extensionTestsPath).

type TestCase = { name: string; fn: () => Promise<void> }
const tests: TestCase[] = []
const test = (name: string, fn: () => Promise<void>) => tests.push({ name, fn })

async function waitFor<T>(
description: string,
fn: () => T | undefined | Promise<T | undefined>,
timeoutMs = 30000
): Promise<T> {
const start = Date.now()
for (;;) {
const value = await fn()
if (value) return value
if (Date.now() - start > timeoutMs) {
throw new Error(`Timed out waiting for ${description}`)
}
await new Promise((r) => setTimeout(r, 200))
}
}

async function openSqlDocument(name: string, text: string) {
const workspace = process.env.SQLLS_E2E_WORKSPACE!
const uri = vscode.Uri.file(path.join(workspace, name))
await vscode.workspace.fs.writeFile(uri, Buffer.from(text))
const document = await vscode.workspace.openTextDocument(uri)
await vscode.window.showTextDocument(document)
assert.strictEqual(document.languageId, 'sql')
return document
}

async function completionLabels(
document: vscode.TextDocument,
position: vscode.Position
) {
const list = await vscode.commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
document.uri,
position
)
return list.items.map((v) =>
typeof v.label === 'string' ? v.label : v.label.label
)
}

test('reports sqlint diagnostics', async () => {
const document = await openSqlDocument('lint.sql', 'select id from users\n')
const diagnostics = await waitFor('diagnostics', () => {
const v = vscode.languages.getDiagnostics(document.uri)
return v.length > 0 ? v : undefined
})
const messages = diagnostics.map((v) => v.message)
assert.ok(
messages.includes('reserved word must be uppercase'),
`unexpected diagnostics: ${JSON.stringify(messages)}`
)
})

test('completes keywords and tables read through node:sqlite', async () => {
const text = 'SELECT * FROM '
const document = await openSqlDocument('complete.sql', text)
const position = new vscode.Position(0, text.length)
// The schema is loaded asynchronously after the server has initialized
const labels = await waitFor('table completion', async () => {
const v = await completionLabels(document, position)
return v.includes('e2e_users') ? v : undefined
})
assert.ok(labels.includes('SELECT'), JSON.stringify(labels))
})

test('fixes all fixable problems', async () => {
const document = await openSqlDocument(
'fix.sql',
'SELECT id FROM users WHERE id=1\n'
)
await waitFor('diagnostics', () =>
vscode.languages.getDiagnostics(document.uri).length > 0 ? true : undefined
)
await vscode.commands.executeCommand('extension.fixAllFixableProblems')
const fixed = await waitFor('document to be fixed', () =>
document.getText() !== 'SELECT id FROM users WHERE id=1\n'
? document.getText()
: undefined
)
assert.notStrictEqual(fixed, 'SELECT id FROM users WHERE id=1\n')
})

export async function run(): Promise<void> {
const failures: string[] = []
for (const { name, fn } of tests) {
try {
await fn()
console.log(` ✓ ${name}`)
} catch (e) {
console.error(` ✗ ${name}\n`, e)
failures.push(name)
}
}
console.log(`${tests.length - failures.length}/${tests.length} passed`)
if (failures.length > 0) {
throw new Error(`${failures.length} test(s) failed: ${failures.join(', ')}`)
}
}
4 changes: 3 additions & 1 deletion packages/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"noEmit": true
},
"exclude": [
"node_modules"
"node_modules",
"out",
".vscode-test"
]
}
Loading
Loading