Skip to content

Commit 0b3cd0c

Browse files
Get a clean environment to run bricks (#240)
Bricks can pickup auth creds from environment variables. We need to get a clean environment to run bricks commands in.
1 parent a3811ba commit 0b3cd0c

File tree

2 files changed

+84
-38
lines changed

2 files changed

+84
-38
lines changed
Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,68 @@
11
import * as assert from "assert";
2-
import {instance, mock} from "ts-mockito";
2+
import {instance, mock, when} from "ts-mockito";
3+
import {Uri} from "vscode";
34
import type {ConnectionManager} from "../configuration/ConnectionManager";
4-
import {SyncTask} from "./BricksTasks";
5+
import {DatabricksWorkspace} from "../configuration/DatabricksWorkspace";
6+
import {SyncDestination} from "../configuration/SyncDestination";
7+
import {LazyCustomSyncTerminal, SyncTask} from "./BricksTasks";
58
import type {CliWrapper} from "./CliWrapper";
69

710
describe(__filename, () => {
811
let connection: ConnectionManager;
912
let cli: CliWrapper;
1013

1114
beforeEach(() => {
12-
connection = instance(mock<ConnectionManager>());
13-
cli = instance(mock<CliWrapper>());
15+
connection = mock<ConnectionManager>();
16+
cli = mock<CliWrapper>();
1417
});
1518

1619
it("should create a sync task", () => {
17-
const task = new SyncTask(connection, cli, "incremental", () => {});
20+
const task = new SyncTask(
21+
instance(connection),
22+
instance(cli),
23+
"incremental",
24+
() => {}
25+
);
1826

1927
assert.equal(task.definition.type, "databricks");
2028
assert.equal(task.definition.task, "sync");
2129
assert.equal(task.isBackground, true);
2230
assert.deepEqual(task.problemMatchers, ["$bricks-sync"]);
2331
});
32+
33+
it("should create pseudo terminal with correct environment variables", () => {
34+
const mockDbWorkspace = mock(DatabricksWorkspace);
35+
when(mockDbWorkspace.profile).thenReturn("profile");
36+
37+
const mockSyncDestination = mock(SyncDestination);
38+
when(mockSyncDestination.vscodeWorkspacePath).thenReturn(
39+
Uri.file("/path/to/local/workspace")
40+
);
41+
42+
when(connection.databricksWorkspace).thenReturn(
43+
instance(mockDbWorkspace)
44+
);
45+
when(connection.syncDestination).thenReturn(
46+
instance(mockSyncDestination)
47+
);
48+
49+
const terminal = new LazyCustomSyncTerminal(
50+
instance(connection),
51+
instance(cli),
52+
"full",
53+
() => {}
54+
);
55+
56+
assert.deepEqual(terminal.getProcessOptions(), {
57+
cwd: "/path/to/local/workspace",
58+
env: {
59+
/* eslint-disable @typescript-eslint/naming-convention */
60+
BRICKS_ROOT: "/path/to/local/workspace",
61+
DATABRICKS_CONFIG_PROFILE: "profile",
62+
HOME: process.env.HOME,
63+
PATH: process.env.PATH,
64+
/* eslint-enable @typescript-eslint/naming-convention */
65+
},
66+
});
67+
});
2468
});

packages/databricks-vscode/src/cli/BricksTasks.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
TaskRevealKind,
77
TaskScope,
88
window,
9-
workspace,
109
Event,
1110
EventEmitter,
1211
} from "vscode";
@@ -96,7 +95,7 @@ class CustomSyncTerminal implements Pseudoterminal {
9695

9796
private startSyncProcess() {
9897
this.syncProcess = spawn(this.cmd, this.args, {
99-
env: {...process.env, ...this.options.env},
98+
env: this.options?.env,
10099
cwd: this.options?.cwd,
101100
});
102101

@@ -158,7 +157,7 @@ class CustomSyncTerminal implements Pseudoterminal {
158157
* vscode task, which allows us to parse the stdout/stderr bricks sync logs and compute
159158
* sync completeness state based on the output logs
160159
*/
161-
class LazyCustomSyncTerminal extends CustomSyncTerminal {
160+
export class LazyCustomSyncTerminal extends CustomSyncTerminal {
162161
private command?: Command;
163162
private killThis = false;
164163

@@ -183,41 +182,44 @@ class LazyCustomSyncTerminal extends CustomSyncTerminal {
183182
},
184183
},
185184
options: {
186-
get(): SpawnOptions {
187-
const workspacePath = workspace.rootPath;
188-
if (!workspacePath) {
189-
window.showErrorMessage(
190-
"Can't start sync: No workspace opened!"
191-
);
192-
throw new Error(
193-
"Can't start sync: No workspace opened!"
194-
);
195-
}
196-
197-
const dbWorkspace = this.connection.databricksWorkspace;
198-
if (!dbWorkspace) {
199-
window.showErrorMessage(
200-
"Can't start sync: Databricks connection not configured!"
201-
);
202-
throw new Error(
203-
"Can't start sync: Databricks connection not configured!"
204-
);
205-
}
206-
207-
return {
208-
cwd: workspacePath,
209-
env: {
210-
/* eslint-disable @typescript-eslint/naming-convention */
211-
BRICKS_ROOT: workspacePath,
212-
DATABRICKS_CONFIG_PROFILE: dbWorkspace.profile,
213-
/* eslint-enable @typescript-eslint/naming-convention */
214-
},
215-
};
185+
get: () => {
186+
return this.getProcessOptions();
216187
},
217188
},
218189
});
219190
}
220191

192+
getProcessOptions(): SpawnOptions {
193+
const workspacePath =
194+
this.connection.syncDestination?.vscodeWorkspacePath.fsPath;
195+
if (!workspacePath) {
196+
window.showErrorMessage("Can't start sync: No workspace opened!");
197+
throw new Error("Can't start sync: No workspace opened!");
198+
}
199+
200+
const dbWorkspace = this.connection.databricksWorkspace;
201+
if (!dbWorkspace) {
202+
window.showErrorMessage(
203+
"Can't start sync: Databricks connection not configured!"
204+
);
205+
throw new Error(
206+
"Can't start sync: Databricks connection not configured!"
207+
);
208+
}
209+
210+
return {
211+
cwd: workspacePath,
212+
env: {
213+
/* eslint-disable @typescript-eslint/naming-convention */
214+
BRICKS_ROOT: workspacePath,
215+
DATABRICKS_CONFIG_PROFILE: dbWorkspace.profile,
216+
HOME: process.env.HOME,
217+
PATH: process.env.PATH,
218+
/* eslint-enable @typescript-eslint/naming-convention */
219+
},
220+
} as SpawnOptions;
221+
}
222+
221223
getSyncCommand(): Command {
222224
if (
223225
this.connection.state !== "CONNECTED" &&

0 commit comments

Comments
 (0)