|
| 1 | +import { test, expect } from './fixtures' |
| 2 | +import { |
| 3 | + createVirtualEnvironment, |
| 4 | + openServerPage, |
| 5 | + pipInstall, |
| 6 | + REPO_ROOT, |
| 7 | + SUSHI_SOURCE_PATH, |
| 8 | + waitForLoadedSQLMesh, |
| 9 | +} from './utils' |
| 10 | +import path from 'path' |
| 11 | +import fs from 'fs-extra' |
| 12 | + |
| 13 | +async function setupPythonEnvironment(tempDir: string): Promise<void> { |
| 14 | + // Create a temporary directory for the virtual environment |
| 15 | + const venvDir = path.join(tempDir, '.venv') |
| 16 | + fs.mkdirSync(venvDir, { recursive: true }) |
| 17 | + |
| 18 | + // Create virtual environment |
| 19 | + const pythonDetails = await createVirtualEnvironment(venvDir) |
| 20 | + |
| 21 | + // Install sqlmesh from the local repository with LSP support |
| 22 | + const customMaterializations = path.join( |
| 23 | + REPO_ROOT, |
| 24 | + 'examples', |
| 25 | + 'custom_materializations', |
| 26 | + ) |
| 27 | + const sqlmeshWithExtras = `${REPO_ROOT}[lsp,bigquery]` |
| 28 | + await pipInstall(pythonDetails, [sqlmeshWithExtras, customMaterializations]) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Creates an entrypoint file used to test the LSP configuration. |
| 33 | + * |
| 34 | + * The entrypoint file is a bash script that simply calls out to the |
| 35 | + */ |
| 36 | +const createEntrypointFile = ( |
| 37 | + tempDir: string, |
| 38 | + entrypointFileName: string, |
| 39 | + bitToStripFromArgs = '', |
| 40 | +): { |
| 41 | + entrypointFile: string |
| 42 | + fileWhereStoredInputs: string |
| 43 | +} => { |
| 44 | + const entrypointFile = path.join(tempDir, entrypointFileName) |
| 45 | + const fileWhereStoredInputs = path.join(tempDir, 'inputs.txt') |
| 46 | + const sqlmeshLSPFile = path.join(tempDir, '.venv/bin/sqlmesh_lsp') |
| 47 | + |
| 48 | + // Create the entrypoint file |
| 49 | + fs.writeFileSync( |
| 50 | + entrypointFile, |
| 51 | + `#!/bin/bash |
| 52 | +echo "$@" > ${fileWhereStoredInputs} |
| 53 | +# Strip bitToStripFromArgs from the beginning of the args if it matches |
| 54 | +if [[ "$1" == "${bitToStripFromArgs}" ]]; then |
| 55 | + shift |
| 56 | +fi |
| 57 | +# Call the sqlmesh_lsp with the remaining arguments |
| 58 | +${sqlmeshLSPFile} "$@"`, |
| 59 | + { mode: 0o755 }, // Make it executable |
| 60 | + ) |
| 61 | + |
| 62 | + return { |
| 63 | + entrypointFile, |
| 64 | + fileWhereStoredInputs, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +test.describe('Test LSP Entrypoint configuration', () => { |
| 69 | + test('specify single entrypoint relalative path', async ({ |
| 70 | + page, |
| 71 | + sharedCodeServer, |
| 72 | + tempDir, |
| 73 | + }) => { |
| 74 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 75 | + |
| 76 | + await setupPythonEnvironment(tempDir) |
| 77 | + |
| 78 | + const { fileWhereStoredInputs } = createEntrypointFile( |
| 79 | + tempDir, |
| 80 | + 'entrypoint.sh', |
| 81 | + ) |
| 82 | + |
| 83 | + const settings = { |
| 84 | + 'sqlmesh.lspEntrypoint': './entrypoint.sh', |
| 85 | + } |
| 86 | + // Write the settings to the settings.json file |
| 87 | + const settingsPath = path.join(tempDir, '.vscode', 'settings.json') |
| 88 | + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) |
| 89 | + fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2)) |
| 90 | + |
| 91 | + await openServerPage(page, tempDir, sharedCodeServer) |
| 92 | + |
| 93 | + // Wait for the models folder to be visible |
| 94 | + await page.waitForSelector('text=models') |
| 95 | + |
| 96 | + // Click on the models folder, excluding external_models |
| 97 | + await page |
| 98 | + .getByRole('treeitem', { name: 'models', exact: true }) |
| 99 | + .locator('a') |
| 100 | + .click() |
| 101 | + |
| 102 | + // Open the customer_revenue_lifetime model |
| 103 | + await page |
| 104 | + .getByRole('treeitem', { name: 'customers.sql', exact: true }) |
| 105 | + .locator('a') |
| 106 | + .click() |
| 107 | + |
| 108 | + await waitForLoadedSQLMesh(page) |
| 109 | + |
| 110 | + // Check that the output file exists and contains the entrypoint script arguments |
| 111 | + expect(fs.existsSync(fileWhereStoredInputs)).toBe(true) |
| 112 | + expect(fs.readFileSync(fileWhereStoredInputs, 'utf8')).toBe(`--stdio |
| 113 | +`) |
| 114 | + }) |
| 115 | + |
| 116 | + test('specify one entrypoint absolute path', async ({ |
| 117 | + page, |
| 118 | + sharedCodeServer, |
| 119 | + tempDir, |
| 120 | + }) => { |
| 121 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 122 | + |
| 123 | + await setupPythonEnvironment(tempDir) |
| 124 | + |
| 125 | + const { entrypointFile, fileWhereStoredInputs } = createEntrypointFile( |
| 126 | + tempDir, |
| 127 | + 'entrypoint.sh', |
| 128 | + ) |
| 129 | + // Assert that the entrypoint file is an absolute path |
| 130 | + expect(path.isAbsolute(entrypointFile)).toBe(true) |
| 131 | + |
| 132 | + const settings = { |
| 133 | + 'sqlmesh.lspEntrypoint': `${entrypointFile}`, |
| 134 | + } |
| 135 | + // Write the settings to the settings.json file |
| 136 | + const settingsPath = path.join(tempDir, '.vscode', 'settings.json') |
| 137 | + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) |
| 138 | + fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2)) |
| 139 | + |
| 140 | + await openServerPage(page, tempDir, sharedCodeServer) |
| 141 | + |
| 142 | + // Wait for the models folder to be visible |
| 143 | + await page.waitForSelector('text=models') |
| 144 | + |
| 145 | + // Click on the models folder, excluding external_models |
| 146 | + await page |
| 147 | + .getByRole('treeitem', { name: 'models', exact: true }) |
| 148 | + .locator('a') |
| 149 | + .click() |
| 150 | + |
| 151 | + // Open the customer_revenue_lifetime model |
| 152 | + await page |
| 153 | + .getByRole('treeitem', { name: 'customers.sql', exact: true }) |
| 154 | + .locator('a') |
| 155 | + .click() |
| 156 | + |
| 157 | + await waitForLoadedSQLMesh(page) |
| 158 | + |
| 159 | + // Check that the output file exists and contains the entrypoint script arguments |
| 160 | + expect(fs.existsSync(fileWhereStoredInputs)).toBe(true) |
| 161 | + expect(fs.readFileSync(fileWhereStoredInputs, 'utf8')).toBe(`--stdio |
| 162 | +`) |
| 163 | + }) |
| 164 | + |
| 165 | + test('specify entrypoint with arguments', async ({ |
| 166 | + page, |
| 167 | + sharedCodeServer, |
| 168 | + tempDir, |
| 169 | + }) => { |
| 170 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 171 | + |
| 172 | + await setupPythonEnvironment(tempDir) |
| 173 | + |
| 174 | + const { fileWhereStoredInputs } = createEntrypointFile( |
| 175 | + tempDir, |
| 176 | + 'entrypoint.sh', |
| 177 | + '--argToIgnore', |
| 178 | + ) |
| 179 | + |
| 180 | + const settings = { |
| 181 | + 'sqlmesh.lspEntrypoint': './entrypoint.sh --argToIgnore', |
| 182 | + } |
| 183 | + // Write the settings to the settings.json file |
| 184 | + const settingsPath = path.join(tempDir, '.vscode', 'settings.json') |
| 185 | + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) |
| 186 | + fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2)) |
| 187 | + |
| 188 | + await openServerPage(page, tempDir, sharedCodeServer) |
| 189 | + |
| 190 | + // Wait for the models folder to be visible |
| 191 | + await page.waitForSelector('text=models') |
| 192 | + |
| 193 | + // Click on the models folder, excluding external_models |
| 194 | + await page |
| 195 | + .getByRole('treeitem', { name: 'models', exact: true }) |
| 196 | + .locator('a') |
| 197 | + .click() |
| 198 | + |
| 199 | + // Open the customer_revenue_lifetime model |
| 200 | + await page |
| 201 | + .getByRole('treeitem', { name: 'customers.sql', exact: true }) |
| 202 | + .locator('a') |
| 203 | + .click() |
| 204 | + |
| 205 | + await waitForLoadedSQLMesh(page) |
| 206 | + |
| 207 | + // Check that the output file exists and contains the entrypoint script arguments |
| 208 | + expect(fs.existsSync(fileWhereStoredInputs)).toBe(true) |
| 209 | + expect(fs.readFileSync(fileWhereStoredInputs, 'utf8')) |
| 210 | + .toBe(`--argToIgnore --stdio |
| 211 | +`) |
| 212 | + }) |
| 213 | +}) |
0 commit comments