diff --git a/client/src/components/ExtensionContext.ts b/client/src/components/ExtensionContext.ts index 1fd52e836..2e209008f 100644 --- a/client/src/components/ExtensionContext.ts +++ b/client/src/components/ExtensionContext.ts @@ -1,6 +1,6 @@ // Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ExtensionContext } from "vscode"; +import { ExtensionContext, Uri } from "vscode"; let context: ExtensionContext; @@ -26,3 +26,15 @@ export async function getContextValue( ): Promise { return context.workspaceState.get(key); } + +export async function setSecret(key: string, value: string): Promise { + await context.secrets.store(key, value); +} + +export async function getSecret(key: string): Promise { + return await context.secrets.get(key); +} + +export function getGlobalStorageUri(): Uri { + return context.globalStorageUri; +} diff --git a/client/src/connection/index.ts b/client/src/connection/index.ts index 3586e1be5..c7dcc4372 100644 --- a/client/src/connection/index.ts +++ b/client/src/connection/index.ts @@ -9,7 +9,6 @@ import { ViyaProfile, toAutoExecLines, } from "../components/profile"; -import { extensionContext } from "../node/extension"; import { ITCProtocol, getSession as getITCSession } from "./itc"; import { Config as RestConfig, getSession as getRestSession } from "./rest"; import { @@ -55,17 +54,9 @@ export function getSession(): Session { case ConnectionType.SSH: return getSSHSession(validProfile.profile); case ConnectionType.COM: - return getITCSession( - validProfile.profile, - ITCProtocol.COM, - extensionContext, - ); + return getITCSession(validProfile.profile, ITCProtocol.COM); case ConnectionType.IOM: - return getITCSession( - validProfile.profile, - ITCProtocol.IOMBridge, - extensionContext, - ); + return getITCSession(validProfile.profile, ITCProtocol.IOMBridge); default: throw new Error( l10n.t("Invalid connectionType. Check Profile settings."), diff --git a/client/src/connection/itc/index.ts b/client/src/connection/itc/index.ts index 7eb2ca87a..12029a6a9 100644 --- a/client/src/connection/itc/index.ts +++ b/client/src/connection/itc/index.ts @@ -1,11 +1,16 @@ // Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { ExtensionContext, Uri, l10n, window, workspace } from "vscode"; +import { Uri, l10n, window, workspace } from "vscode"; import { ChildProcessWithoutNullStreams, spawn } from "child_process"; import { resolve } from "path"; import { BaseConfig, RunResult } from ".."; +import { + getGlobalStorageUri, + getSecret, + setSecret, +} from "../../components/ExtensionContext"; import { Session } from "../session"; import { scriptContent } from "./script"; @@ -37,12 +42,10 @@ export class ITCSession extends Session { private _runReject: ((reason?) => void) | undefined; private _workDirectory: string; private _password: string; - private _context: ExtensionContext; - constructor(extensionContext: ExtensionContext) { + constructor() { super(); this._password = ""; - this._context = extensionContext; } public set config(value: Config) { @@ -123,12 +126,11 @@ export class ITCSession extends Session { return setupPromise; }; - private storePassword = async () => { - await this._context.secrets.store(PASSWORD_KEY, this._password); - }; + private storePassword = async () => + await setSecret(PASSWORD_KEY, this._password); private clearPassword = async () => { - await this._context.secrets.delete(PASSWORD_KEY); + await setSecret(PASSWORD_KEY, ""); this._password = ""; }; @@ -137,7 +139,7 @@ export class ITCSession extends Session { return ""; } - const storedPassword = await this._context.secrets.get(PASSWORD_KEY); + const storedPassword = await getSecret(PASSWORD_KEY); if (storedPassword) { return storedPassword; } @@ -308,14 +310,15 @@ do { * Fetches the ODS output results for the latest html results file. */ private fetchResults = async () => { + const globalStorageUri = getGlobalStorageUri(); try { - await workspace.fs.readDirectory(this._context.globalStorageUri); + await workspace.fs.readDirectory(globalStorageUri); } catch (e) { - await workspace.fs.createDirectory(this._context.globalStorageUri); + await workspace.fs.createDirectory(globalStorageUri); } const outputFileUri = Uri.joinPath( - this._context.globalStorageUri, + globalStorageUri, `${this._html5FileName}.htm`, ); this._shellProcess.stdin.write( @@ -368,7 +371,6 @@ $runner.FetchResultsFile($filePath, $outputFile)\n`, export const getSession = ( c: Partial, protocol: ITCProtocol, - extensionContext: ExtensionContext, ): Session => { const defaults = { host: "localhost", @@ -378,7 +380,7 @@ export const getSession = ( }; if (!sessionInstance) { - sessionInstance = new ITCSession(extensionContext); + sessionInstance = new ITCSession(); } sessionInstance.config = { ...defaults, ...c }; return sessionInstance; diff --git a/client/test/connection/itc/index.test.ts b/client/test/connection/itc/index.test.ts index d4933dc74..7161340be 100644 --- a/client/test/connection/itc/index.test.ts +++ b/client/test/connection/itc/index.test.ts @@ -7,6 +7,7 @@ import { join } from "path"; import { SinonSandbox, SinonStub, createSandbox } from "sinon"; import { stubInterface } from "ts-sinon"; +import { setContext } from "../../../src/components/ExtensionContext"; import { ITCProtocol, getSession } from "../../../src/connection/itc"; import { scriptContent } from "../../../src/connection/itc/script"; import { Session } from "../../../src/connection/session"; @@ -62,7 +63,9 @@ describe("ITC connection", () => { secrets: secretStore, }; - session = getSession(config, ITCProtocol.COM, stubbedExtensionContext); + setContext(stubbedExtensionContext); + + session = getSession(config, ITCProtocol.COM); session.onLogFn = () => { return; };