From c7b97e2c0da5a5a5b2d0f0c039325786e01532f8 Mon Sep 17 00:00:00 2001 From: hkobew Date: Fri, 24 Jan 2025 12:32:49 -0500 Subject: [PATCH] refactor: move tryFunctions to core and include tests --- packages/core/src/amazonq/lsp/util.ts | 17 ----------- packages/core/src/shared/lsp/lspResolver.ts | 3 +- .../core/src/shared/lsp/manifestResolver.ts | 3 +- packages/core/src/shared/utilities/tsUtils.ts | 17 +++++++++++ .../src/test/shared/utilities/tsUtils.test.ts | 28 +++++++++++++++++++ 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 packages/core/src/test/shared/utilities/tsUtils.test.ts diff --git a/packages/core/src/amazonq/lsp/util.ts b/packages/core/src/amazonq/lsp/util.ts index 3829f643e79..f0a384db115 100644 --- a/packages/core/src/amazonq/lsp/util.ts +++ b/packages/core/src/amazonq/lsp/util.ts @@ -26,20 +26,3 @@ export async function lspSetupStage( return result }) } - -/** - * Try Functions in the order presented and return the first returned result. If none return, throw the final error. - * @param functions non-empty list of functions to try. - * @returns - */ -export async function tryFunctions(functions: (() => Promise)[]): Promise { - let currentError: Error = new Error('No functions provided') - for (const func of functions) { - try { - return await func() - } catch (e) { - currentError = e as Error - } - } - throw currentError -} diff --git a/packages/core/src/shared/lsp/lspResolver.ts b/packages/core/src/shared/lsp/lspResolver.ts index 5903aa1bc6c..26043eb1d25 100644 --- a/packages/core/src/shared/lsp/lspResolver.ts +++ b/packages/core/src/shared/lsp/lspResolver.ts @@ -13,7 +13,8 @@ import { TargetContent, logger, LspResult, LspVersion, Manifest } from './types' import { getApplicationSupportFolder } from '../vscode/env' import { createHash } from '../crypto' import request from '../request' -import { lspSetupStage, tryFunctions } from '../../amazonq/lsp/util' +import { lspSetupStage } from '../../amazonq/lsp/util' +import { tryFunctions } from '../utilities/tsUtils' export class LanguageServerResolver { constructor( diff --git a/packages/core/src/shared/lsp/manifestResolver.ts b/packages/core/src/shared/lsp/manifestResolver.ts index 1cc8ed647c8..82e90596507 100644 --- a/packages/core/src/shared/lsp/manifestResolver.ts +++ b/packages/core/src/shared/lsp/manifestResolver.ts @@ -9,7 +9,8 @@ import { RetryableResourceFetcher } from '../resourcefetcher/httpResourceFetcher import { Timeout } from '../utilities/timeoutUtils' import globals from '../extensionGlobals' import { Manifest } from './types' -import { lspSetupStage, tryFunctions } from '../../amazonq/lsp/util' +import { lspSetupStage } from '../../amazonq/lsp/util' +import { tryFunctions } from '../utilities/tsUtils' const logger = getLogger('lsp') diff --git a/packages/core/src/shared/utilities/tsUtils.ts b/packages/core/src/shared/utilities/tsUtils.ts index e4fbf5a2b3f..80d7736f7e0 100644 --- a/packages/core/src/shared/utilities/tsUtils.ts +++ b/packages/core/src/shared/utilities/tsUtils.ts @@ -94,6 +94,23 @@ export function createFactoryFunction any>(cto return (...args) => new ctor(...args) } +/** + * Try Functions in the order presented and return the first returned result. If none return, throw the final error. + * @param functions non-empty list of functions to try. + * @returns + */ +export async function tryFunctions(functions: (() => Promise)[]): Promise { + let currentError: Error = new Error('No functions provided') + for (const func of functions) { + try { + return await func() + } catch (e) { + currentError = e as Error + } + } + throw currentError +} + type NoSymbols = { [Property in keyof T]: Property extends symbol ? never : Property }[keyof T] export type InterfaceNoSymbol = Pick> /** diff --git a/packages/core/src/test/shared/utilities/tsUtils.test.ts b/packages/core/src/test/shared/utilities/tsUtils.test.ts new file mode 100644 index 00000000000..bb75a8c436e --- /dev/null +++ b/packages/core/src/test/shared/utilities/tsUtils.test.ts @@ -0,0 +1,28 @@ +/*! + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +import { tryFunctions } from '../../../shared/utilities/tsUtils' +import assert from 'assert' + +describe('tryFunctions', function () { + it('should return the result of the first function that returns', async function () { + const f1 = () => Promise.reject('f1') + const f2 = () => Promise.resolve('f2') + const f3 = () => Promise.reject('f3') + + assert.strictEqual(await tryFunctions([f1, f2, f3]), 'f2') + }) + + it('if all reject, then should throw final error', async function () { + const f1 = () => Promise.reject('f1') + const f2 = () => Promise.reject('f2') + const f3 = () => Promise.reject('f3') + + await assert.rejects( + async () => await tryFunctions([f1, f2, f3]), + (e) => e === 'f3' + ) + }) +})