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