|
| 1 | +import { test } from '@playwright/test' |
| 2 | +import fs from 'fs-extra' |
| 3 | +import { |
| 4 | + createVirtualEnvironment, |
| 5 | + openLineageView, |
| 6 | + pipInstall, |
| 7 | + PythonEnvironment, |
| 8 | + REPO_ROOT, |
| 9 | + startVSCode, |
| 10 | + SUSHI_SOURCE_PATH, |
| 11 | +} from './utils' |
| 12 | +import os from 'os' |
| 13 | +import path from 'path' |
| 14 | +import { setTcloudVersion, setupAuthenticatedState } from './tcloud_utils' |
| 15 | + |
| 16 | +function writeEnvironmentConfig(sushiPath: string) { |
| 17 | + const configPath = path.join(sushiPath, 'config.py') |
| 18 | + const originalConfig = fs.readFileSync(configPath, 'utf8') |
| 19 | + |
| 20 | + const newConfig = |
| 21 | + ` |
| 22 | +import os |
| 23 | +
|
| 24 | +test_var = os.getenv("TEST_VAR") |
| 25 | +if test_var is None or test_var == "": |
| 26 | + raise Exception("TEST_VAR is not set") |
| 27 | +` + originalConfig |
| 28 | + |
| 29 | + fs.writeFileSync(configPath, newConfig) |
| 30 | +} |
| 31 | + |
| 32 | +async function setupEnvironment(): Promise<[string, PythonEnvironment]> { |
| 33 | + const tempDir = await fs.mkdtemp( |
| 34 | + path.join(os.tmpdir(), 'vscode-test-tcloud-'), |
| 35 | + ) |
| 36 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 37 | + const pythonEnvDir = path.join(tempDir, '.venv') |
| 38 | + const pythonDetails = await createVirtualEnvironment(pythonEnvDir) |
| 39 | + const custom_materializations = path.join( |
| 40 | + REPO_ROOT, |
| 41 | + 'examples', |
| 42 | + 'custom_materializations', |
| 43 | + ) |
| 44 | + const sqlmeshWithExtras = `${REPO_ROOT}[bigquery,lsp]` |
| 45 | + await pipInstall(pythonDetails, [sqlmeshWithExtras, custom_materializations]) |
| 46 | + |
| 47 | + const settings = { |
| 48 | + 'python.defaultInterpreterPath': pythonDetails.pythonPath, |
| 49 | + 'sqlmesh.environmentPath': pythonEnvDir, |
| 50 | + } |
| 51 | + await fs.ensureDir(path.join(tempDir, '.vscode')) |
| 52 | + await fs.writeJson(path.join(tempDir, '.vscode', 'settings.json'), settings, { |
| 53 | + spaces: 2, |
| 54 | + }) |
| 55 | + |
| 56 | + return [tempDir, pythonDetails] |
| 57 | +} |
| 58 | + |
| 59 | +test.describe('python environment variable injection on sqlmesh_lsp', () => { |
| 60 | + test('normal setup - error ', async () => { |
| 61 | + const [tempDir, _] = await setupEnvironment() |
| 62 | + writeEnvironmentConfig(tempDir) |
| 63 | + const { window, close } = await startVSCode(tempDir) |
| 64 | + try { |
| 65 | + await openLineageView(window) |
| 66 | + await window.waitForSelector('text=Error creating context') |
| 67 | + } finally { |
| 68 | + await close() |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + test('normal setup - set', async () => { |
| 73 | + const [tempDir, _] = await setupEnvironment() |
| 74 | + writeEnvironmentConfig(tempDir) |
| 75 | + const env_file = path.join(tempDir, '.env') |
| 76 | + fs.writeFileSync(env_file, 'TEST_VAR=test_value') |
| 77 | + const { window, close } = await startVSCode(tempDir) |
| 78 | + try { |
| 79 | + await openLineageView(window) |
| 80 | + await window.waitForSelector('text=Loaded SQLMesh context') |
| 81 | + } finally { |
| 82 | + await close() |
| 83 | + } |
| 84 | + }) |
| 85 | +}) |
| 86 | + |
| 87 | +async function setupTcloudProject( |
| 88 | + tempDir: string, |
| 89 | + pythonDetails: PythonEnvironment, |
| 90 | +) { |
| 91 | + // Install the mock tcloud package |
| 92 | + const mockTcloudPath = path.join(__dirname, 'tcloud') |
| 93 | + await pipInstall(pythonDetails, [mockTcloudPath]) |
| 94 | + |
| 95 | + // Create a tcloud.yaml to mark this as a tcloud project |
| 96 | + const tcloudConfig = { |
| 97 | + url: 'https://mock.tobikodata.com', |
| 98 | + org: 'test-org', |
| 99 | + project: 'test-project', |
| 100 | + } |
| 101 | + await fs.writeFile( |
| 102 | + path.join(tempDir, 'tcloud.yaml'), |
| 103 | + `url: ${tcloudConfig.url}\norg: ${tcloudConfig.org}\nproject: ${tcloudConfig.project}\n`, |
| 104 | + ) |
| 105 | + // Write mock ".tcloud_auth_state.json" file |
| 106 | + await setupAuthenticatedState(tempDir) |
| 107 | + // Set tcloud version to 2.10.1 |
| 108 | + await setTcloudVersion(tempDir, '2.10.1') |
| 109 | +} |
| 110 | + |
| 111 | +test.describe('tcloud version', () => { |
| 112 | + test('normal setup - error ', async () => { |
| 113 | + const [tempDir, pythonDetails] = await setupEnvironment() |
| 114 | + await setupTcloudProject(tempDir, pythonDetails) |
| 115 | + writeEnvironmentConfig(tempDir) |
| 116 | + const { window, close } = await startVSCode(tempDir) |
| 117 | + try { |
| 118 | + await openLineageView(window) |
| 119 | + await window.waitForSelector('text=Error creating context') |
| 120 | + } finally { |
| 121 | + await close() |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + test('normal setup - set', async () => { |
| 126 | + const [tempDir, pythonDetails] = await setupEnvironment() |
| 127 | + await setupTcloudProject(tempDir, pythonDetails) |
| 128 | + writeEnvironmentConfig(tempDir) |
| 129 | + const env_file = path.join(tempDir, '.env') |
| 130 | + fs.writeFileSync(env_file, 'TEST_VAR=test_value') |
| 131 | + const { window, close } = await startVSCode(tempDir) |
| 132 | + try { |
| 133 | + await openLineageView(window) |
| 134 | + await window.waitForSelector('text=Loaded SQLMesh context') |
| 135 | + } finally { |
| 136 | + await close() |
| 137 | + } |
| 138 | + }) |
| 139 | +}) |
0 commit comments