Convert runMany and runManySync to use injectManyWithMeta + move to seperate package (#7244)

* Convert runMany and runManySync to use injectManyWithMeta

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup type errors due to new Runnable requirements

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add documentation for verifyRunnablesAreDAG

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Simplify convertToWithIdWith

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move all utility functions to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move testing utilities to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move run-many and run-many-sync to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Replace all internal uses of utilities with new packages

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Use new @k8slens/run-many package in core

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add dep to open-lens

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup uses of @k8slens/test-utils

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup getGlobalOverride

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move tests to new package too

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup uses of AsyncResult and autoBind

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup remaining import issues

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Finial fixups to fix build

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix lint

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Revert moving "testUsingFakeTime" to separate package

- This fixes tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix integration tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix unit test failing due to spelling fix

Signed-off-by: Sebastian Malton <sebastian@malton.name>

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2023-03-10 10:07:28 +02:00
1 parent ad9bafe2a5
commit 2789bcebcb
754 files changed
+2715 -2752

No files matched your search

@@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../../../common/test-utils/get-global-override";
import { getGlobalOverride } from "@k8slens/test-utils";
import computeShellEnvironmentInjectable from "./compute-shell-environment.injectable";
export default getGlobalOverride(computeShellEnvironmentInjectable, () => async () => ({
@@ -3,13 +3,13 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { AsyncResult } from "../../../common/utils/async-result";
import type { AsyncResult } from "@k8slens/utilities";
import { getInjectable } from "@ogre-tools/injectable";
import isWindowsInjectable from "../../../common/vars/is-windows.injectable";
import computeUnixShellEnvironmentInjectable from "./compute-unix-shell-environment.injectable";
export type EnvironmentVariables = Partial<Record<string, string>>;
export type ComputeShellEnvironment = (shell: string) => Promise<AsyncResult<EnvironmentVariables | undefined, string>>;
export type ComputeShellEnvironment = (shell: string) => AsyncResult<EnvironmentVariables | undefined, string>;
const computeShellEnvironmentInjectable = getInjectable({
id: "compute-shell-environment",
@@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../../../common/test-utils/get-global-override";
import { getGlobalOverride } from "@k8slens/test-utils";
import computeUnixShellEnvironmentInjectable from "./compute-unix-shell-environment.injectable";
export default getGlobalOverride(computeUnixShellEnvironmentInjectable, () => async () => {
@@ -10,14 +10,14 @@ import randomUUIDInjectable from "../../../main/crypto/random-uuid.injectable";
import loggerInjectable from "../../../common/logger.injectable";
import processExecPathInjectable from "./execPath.injectable";
import processEnvInjectable from "./env.injectable";
import { object } from "../../../common/utils";
import type { AsyncResult } from "../../../common/utils/async-result";
import { object } from "@k8slens/utilities";
import type { AsyncResult } from "@k8slens/utilities";
export interface UnixShellEnvOptions {
signal: AbortSignal;
}
export type ComputeUnixShellEnvironment = (shell: string, opts: UnixShellEnvOptions) => Promise<AsyncResult<EnvironmentVariables, string>>;
export type ComputeUnixShellEnvironment = (shell: string, opts: UnixShellEnvOptions) => AsyncResult<EnvironmentVariables, string>;
/**
* @param src The object containing the current environment variables
@@ -36,9 +36,9 @@ const getResetProcessEnv = (src: Partial<Record<string, string>>, overrides: Par
...overrides,
},
resetEnvPairs: (target) => {
for (const [name, orginalValue] of originals) {
if (typeof orginalValue === "string") {
target[name] = orginalValue;
for (const [name, originalValue] of originals) {
if (typeof originalValue === "string") {
target[name] = originalValue;
} else {
delete target[name];
}
@@ -61,7 +61,7 @@ const computeUnixShellEnvironmentInjectable = getInjectable({
const processExecPath = di.inject(processExecPathInjectable);
const processEnv = di.inject(processEnvInjectable);
const getShellSpecifices = (shellName: string) => {
const getShellSpecifics = (shellName: string) => {
const mark = randomUUID().replace(/-/g, "");
const regex = new RegExp(`${mark}(\\{.*\\})${mark}`);
@@ -100,7 +100,7 @@ const computeUnixShellEnvironmentInjectable = getInjectable({
TERM: "screen-256color-bce", // required for fish
});
const shellName = getBasenameOfPath(shellPath);
const { command, shellArgs, regex } = getShellSpecifices(shellName);
const { command, shellArgs, regex } = getShellSpecifics(shellName);
logger.info(`[UNIX-SHELL-ENV]: running against ${shellPath}`, { command, shellArgs });
@@ -143,7 +143,7 @@ const computeUnixShellEnvironmentInjectable = getInjectable({
if (code || signal) {
return resolve({
callWasSuccessful: false,
error: `Shell did not exit sucessfully: ${getErrorContext({ code, signal })}`,
error: `Shell did not exit successfully: ${getErrorContext({ code, signal })}`,
});
}
@@ -157,7 +157,7 @@ const computeUnixShellEnvironmentInjectable = getInjectable({
if (!matchedOutput) {
return resolve({
callWasSuccessful: false,
error: "Something has blocked the shell from producing the environement variables",
error: "Something has blocked the shell from producing the environment variables",
});
}
@@ -6,7 +6,7 @@
import type { DiContainer } from "@ogre-tools/injectable";
import type { ChildProcessWithoutNullStreams } from "child_process";
import EventEmitter from "events";
import { flushPromises } from "../../../common/test-utils/flush-promises";
import { flushPromises } from "@k8slens/test-utils";
import type { Spawn } from "../../../main/child-process/spawn.injectable";
import spawnInjectable from "../../../main/child-process/spawn.injectable";
import randomUUIDInjectable from "../../../main/crypto/random-uuid.injectable";
@@ -142,7 +142,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -155,7 +155,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -248,7 +248,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -261,7 +261,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -353,7 +353,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -366,7 +366,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -457,7 +457,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 1,\n "signal": null,\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -470,7 +470,7 @@ describe("computeUnixShellEnvironment technical tests", () => {
it("should resolve with a failed call", async () => {
await expect(unixShellEnv).resolves.toEqual({
callWasSuccessful: false,
error: 'Shell did not exit sucessfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
error: 'Shell did not exit successfully: {\n "code": 0,\n "signal": "SIGKILL",\n "stdout": "",\n "stderr": ""\n}',
});
});
});
@@ -5,67 +5,66 @@
import { getInjectable } from "@ogre-tools/injectable";
import loggerInjectable from "../../../common/logger.injectable";
import { onLoadOfApplicationInjectionToken } from "@k8slens/application";
import { unionPATHs } from "../../../common/utils/union-env-path";
import isSnapPackageInjectable from "../../../common/vars/is-snap-package.injectable";
import electronAppInjectable from "../../../main/electron-app/electron-app.injectable";
import computeShellEnvironmentInjectable from "./compute-shell-environment.injectable";
import userShellSettingInjectable from "../../../common/user-store/shell-setting.injectable";
import emitShellSyncFailedInjectable from "./emit-failure.injectable";
import { unionPATHs } from "@k8slens/utilities";
const setupShellInjectable = getInjectable({
id: "setup-shell",
instantiate: (di) => {
const logger = di.inject(loggerInjectable);
const isSnapPackage = di.inject(isSnapPackageInjectable);
const electronApp = di.inject(electronAppInjectable);
const resolvedUserShellSetting = di.inject(userShellSettingInjectable);
const computeShellEnvironment = di.inject(computeShellEnvironmentInjectable);
const emitShellSyncFailed = di.inject(emitShellSyncFailedInjectable);
instantiate: (di) => ({
run: async () => {
const logger = di.inject(loggerInjectable);
const isSnapPackage = di.inject(isSnapPackageInjectable);
const electronApp = di.inject(electronAppInjectable);
const resolvedUserShellSetting = di.inject(userShellSettingInjectable);
const computeShellEnvironment = di.inject(computeShellEnvironmentInjectable);
const emitShellSyncFailed = di.inject(emitShellSyncFailedInjectable);
return {
id: "setup-shell",
run: async (): Promise<void> => {
logger.info("🐚 Syncing shell environment");
logger.info("🐚 Syncing shell environment");
const result = await computeShellEnvironment(resolvedUserShellSetting.get());
const result = await computeShellEnvironment(resolvedUserShellSetting.get());
if (!result.callWasSuccessful) {
logger.error(`[SHELL-SYNC]: ${result.error}`);
emitShellSyncFailed(result.error);
if (!result.callWasSuccessful) {
logger.error(`[SHELL-SYNC]: ${result.error}`);
emitShellSyncFailed(result.error);
return;
}
return;
}
const env = result.response;
const env = result.response;
if (!env) {
return void logger.debug("[SHELL-SYNC]: nothing to do, env not special in shells");
}
if (!env) {
logger.debug("[SHELL-SYNC]: nothing to do, env not special in shells");
if (!env.LANG) {
// the LANG env var expects an underscore instead of electron's dash
env.LANG = `${electronApp.getLocale().replace("-", "_")}.UTF-8`;
} else if (!env.LANG.endsWith(".UTF-8")) {
env.LANG += ".UTF-8";
}
return;
}
if (!isSnapPackage) {
// Prefer the synced PATH over the initial one
process.env.PATH = unionPATHs(env.PATH ?? "", process.env.PATH ?? "");
}
if (!env.LANG) {
// the LANG env var expects an underscore instead of electron's dash
env.LANG = `${electronApp.getLocale().replace("-", "_")}.UTF-8`;
} else if (!env.LANG.endsWith(".UTF-8")) {
env.LANG += ".UTF-8";
}
// The spread operator allows joining of objects. The precedence is last to first.
process.env = {
...env,
...process.env,
};
if (!isSnapPackage) {
// Prefer the synced PATH over the initial one
process.env.PATH = unionPATHs(env.PATH ?? "", process.env.PATH ?? "");
}
logger.info(`[SHELL-SYNC]: Synced shell env`);
logger.debug(`[SHELL-SYNC]: updated env`, process.env);
},
};
},
// The spread operator allows joining of objects. The precedence is last to first.
process.env = {
...env,
...process.env,
};
logger.info(`[SHELL-SYNC]: Synced shell env`);
logger.debug(`[SHELL-SYNC]: updated env`, process.env);
},
}),
injectionToken: onLoadOfApplicationInjectionToken,
});