|
| 1 | +import { CodeActionParams, CompletionParams, DocumentFormattingParams } from 'vscode-languageserver' |
| 2 | +import { TextDocument } from 'vscode-languageserver-textdocument' |
| 3 | +import { |
| 4 | + handleCodeActions, |
| 5 | + handleCompletionRequest, |
| 6 | + handleDiagnosticsRequest, |
| 7 | + handleDocumentFormatting, |
| 8 | +} from '../MessageHandler' |
| 9 | +import { CURSOR_CHARACTER, findCursorPosition, getTextDocument } from './helper' |
| 10 | + |
| 11 | +import * as assert from 'assert' |
| 12 | + |
| 13 | +suite('Artificial Panics', () => { |
| 14 | + const OLD_ENV = { ...process.env } |
| 15 | + |
| 16 | + test('code actions', () => { |
| 17 | + const fixturePath = './artificial-panic/schema.prisma' |
| 18 | + const document = getTextDocument(fixturePath) |
| 19 | + |
| 20 | + const params: CodeActionParams = { |
| 21 | + textDocument: document, |
| 22 | + range: { |
| 23 | + start: { line: 0, character: 0 }, |
| 24 | + end: { line: 0, character: 0 }, |
| 25 | + }, |
| 26 | + context: { |
| 27 | + diagnostics: [ |
| 28 | + { |
| 29 | + range: { |
| 30 | + start: { line: 0, character: 0 }, |
| 31 | + end: { line: 0, character: 0 }, |
| 32 | + }, |
| 33 | + message: 'dx msg', |
| 34 | + }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + process.env.FORCE_PANIC_PRISMA_FMT = '1' |
| 40 | + |
| 41 | + let calledCount = 0 |
| 42 | + let calledArg: undefined | unknown = undefined |
| 43 | + |
| 44 | + // No official mock implementation in mocha |
| 45 | + // -> DIY mock for onError |
| 46 | + const onError = (arg: unknown) => { |
| 47 | + calledCount += 1 |
| 48 | + calledArg = arg |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + const _codeActions = handleCodeActions(params, document, onError) |
| 53 | + |
| 54 | + assert.fail("This shouldn't happen!") |
| 55 | + } catch (e) { |
| 56 | + assert.ok(calledArg) |
| 57 | + assert.strictEqual(calledCount, 1) |
| 58 | + } finally { |
| 59 | + process.env = { ...OLD_ENV } |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + test('formatter', () => { |
| 64 | + const fixturePath = './artificial-panic/schema.prisma' |
| 65 | + const document = getTextDocument(fixturePath) |
| 66 | + |
| 67 | + const params: DocumentFormattingParams = { |
| 68 | + textDocument: document, |
| 69 | + options: { |
| 70 | + tabSize: 2, |
| 71 | + insertSpaces: true, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + process.env.FORCE_PANIC_PRISMA_FMT = '1' |
| 76 | + |
| 77 | + let calledCount = 0 |
| 78 | + let calledArg: undefined | unknown = undefined |
| 79 | + |
| 80 | + const onError = (arg: unknown) => { |
| 81 | + calledCount += 1 |
| 82 | + calledArg = arg |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + const _formatResult = handleDocumentFormatting(params, document, onError) |
| 87 | + |
| 88 | + assert.fail("This shouldn't happen!") |
| 89 | + } catch (e) { |
| 90 | + assert.ok(calledArg) |
| 91 | + assert.strictEqual(calledCount, 1) |
| 92 | + } finally { |
| 93 | + process.env = { ...OLD_ENV } |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + test('linting', () => { |
| 98 | + const fixturePath = './artificial-panic/schema.prisma' |
| 99 | + const document = getTextDocument(fixturePath) |
| 100 | + |
| 101 | + process.env.FORCE_PANIC_PRISMA_FMT = '1' |
| 102 | + |
| 103 | + let calledCount = 0 |
| 104 | + let calledArg: undefined | unknown = undefined |
| 105 | + |
| 106 | + const onError = (arg: unknown) => { |
| 107 | + calledCount += 1 |
| 108 | + calledArg = arg |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + const _diagnostics = handleDiagnosticsRequest(document, onError) |
| 113 | + |
| 114 | + assert.fail("This shouldn't happen!") |
| 115 | + } catch (e) { |
| 116 | + assert.ok(calledArg) |
| 117 | + assert.strictEqual(calledCount, 1) |
| 118 | + } finally { |
| 119 | + process.env = { ...OLD_ENV } |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + test('preview features', () => { |
| 124 | + const fixturePath = './artificial-panic/schema.prisma' |
| 125 | + let document = getTextDocument(fixturePath) |
| 126 | + |
| 127 | + const schema = document.getText() |
| 128 | + |
| 129 | + const position = findCursorPosition(schema) |
| 130 | + |
| 131 | + document = TextDocument.create( |
| 132 | + './artificial-panic/schema.prisma', |
| 133 | + 'prisma', |
| 134 | + 1, |
| 135 | + schema.replace(CURSOR_CHARACTER, ''), |
| 136 | + ) |
| 137 | + |
| 138 | + const params: CompletionParams = { |
| 139 | + textDocument: document, |
| 140 | + position, |
| 141 | + } |
| 142 | + |
| 143 | + process.env.FORCE_PANIC_PRISMA_FMT_LOCAL = '1' |
| 144 | + |
| 145 | + let calledCount = 0 |
| 146 | + let calledArg: undefined | unknown = undefined |
| 147 | + |
| 148 | + const onError = (arg: unknown) => { |
| 149 | + calledCount += 1 |
| 150 | + calledArg = arg |
| 151 | + } |
| 152 | + |
| 153 | + try { |
| 154 | + const _completions = handleCompletionRequest(params, document, onError) |
| 155 | + |
| 156 | + assert.fail("This shouldn't happen!") |
| 157 | + } catch (e) { |
| 158 | + assert.ok(calledArg) |
| 159 | + assert.strictEqual(calledCount, 1) |
| 160 | + } finally { |
| 161 | + process.env = { ...OLD_ENV } |
| 162 | + } |
| 163 | + }) |
| 164 | + |
| 165 | + test('native types', () => { |
| 166 | + const fixturePath = './artificial-panic/native-types.prisma' |
| 167 | + let document = getTextDocument(fixturePath) |
| 168 | + |
| 169 | + const schema = document.getText() |
| 170 | + |
| 171 | + const position = findCursorPosition(schema) |
| 172 | + |
| 173 | + document = TextDocument.create( |
| 174 | + './artificial-panic/native-types.prisma', |
| 175 | + 'prisma', |
| 176 | + 1, |
| 177 | + schema.replace(CURSOR_CHARACTER, ''), |
| 178 | + ) |
| 179 | + |
| 180 | + const params: CompletionParams = { |
| 181 | + textDocument: document, |
| 182 | + position, |
| 183 | + } |
| 184 | + |
| 185 | + process.env.FORCE_PANIC_PRISMA_FMT_LOCAL = '1' |
| 186 | + |
| 187 | + let calledCount = 0 |
| 188 | + let calledArg: undefined | unknown = undefined |
| 189 | + |
| 190 | + const onError = (arg: unknown) => { |
| 191 | + calledCount += 1 |
| 192 | + calledArg = arg |
| 193 | + } |
| 194 | + |
| 195 | + try { |
| 196 | + const _completions = handleCompletionRequest(params, document, onError) |
| 197 | + |
| 198 | + assert.fail("This shouldn't happen!") |
| 199 | + } catch (e) { |
| 200 | + assert.ok(calledArg) |
| 201 | + assert.strictEqual(calledCount, 1) |
| 202 | + } finally { |
| 203 | + process.env = { ...OLD_ENV } |
| 204 | + } |
| 205 | + }) |
| 206 | + |
| 207 | + test('completions', () => { |
| 208 | + const fixturePath = './artificial-panic/schema.prisma' |
| 209 | + const document = getTextDocument(fixturePath) |
| 210 | + |
| 211 | + const params: CompletionParams = { |
| 212 | + textDocument: document, |
| 213 | + position: { character: 0, line: 0 }, |
| 214 | + } |
| 215 | + |
| 216 | + process.env.FORCE_PANIC_PRISMA_FMT = '1' |
| 217 | + |
| 218 | + let calledCount = 0 |
| 219 | + let calledArg: undefined | unknown = undefined |
| 220 | + |
| 221 | + const onError = (arg: unknown) => { |
| 222 | + calledCount += 1 |
| 223 | + calledArg = arg |
| 224 | + } |
| 225 | + |
| 226 | + try { |
| 227 | + const _completions = handleCompletionRequest(params, document, onError) |
| 228 | + |
| 229 | + assert.fail("This shouldn't happen!") |
| 230 | + } catch (e) { |
| 231 | + assert.ok(calledArg) |
| 232 | + assert.strictEqual(calledCount, 1) |
| 233 | + } finally { |
| 234 | + process.env = { ...OLD_ENV } |
| 235 | + } |
| 236 | + }) |
| 237 | +}) |
0 commit comments