From 8d5d96dce713509b7281425420c8eee8c5880161 Mon Sep 17 00:00:00 2001 From: Bruno Iecker Date: Mon, 16 Oct 2023 13:59:36 -0300 Subject: [PATCH] Get rid of unnamed packages. Closes issue #157 --- src/lib/package/package_manager.test.ts | 20 ++++++++++++++++ src/lib/package/package_manager.ts | 31 +++++++++++++++++-------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/lib/package/package_manager.test.ts b/src/lib/package/package_manager.test.ts index d195719d..64d520cf 100644 --- a/src/lib/package/package_manager.test.ts +++ b/src/lib/package/package_manager.test.ts @@ -50,6 +50,26 @@ Deno.test('Managers should resolve packages with a transitive dependency', async assertEquals(packageNames, ['transitive dependency', 'dependency with transitive', 'package with a transitive dependency',]) }) +Deno.test('Managers should not crash with empty package', async () => { + // Given + const config = await getConfigWithMockRepo() + const manager = new PackageManager(config) + // When + const resolvedPackages = manager.resolvePackages(['package without dependencies', '', ' ', 'package with a transitive dependency']) + // Then + let packageNames = resolvedPackages?.map(it => it.name) + assertEquals(packageNames, ['package without dependencies', 'transitive dependency', 'dependency with transitive', 'package with a transitive dependency']) +}) + +Deno.test('Managers should ignore empty package', async () => { + // Given + let packageNames = ['package without dependencies', '', ' ', 'package with a transitive dependency'] + // When + PackageManager.removeExtension(packageNames) + // Then + assertEquals(packageNames, ['package without dependencies', 'package with a transitive dependency']) +}) + async function getConfigWithMockRepo(): Promise { const config = TestHelper.getConfig(); const repoMock = new MockRepository('mockRepo1', [ diff --git a/src/lib/package/package_manager.ts b/src/lib/package/package_manager.ts index 61b0544e..f4b2551c 100644 --- a/src/lib/package/package_manager.ts +++ b/src/lib/package/package_manager.ts @@ -31,6 +31,9 @@ export default class PackageManager { let names: Set = new Set(); // Solving circular references - Issue #11 let error: boolean = false; for (const pkgName of pkgNames) { + if (!pkgName || !pkgName.trim()) { + continue + } let repo = (installedOnly ? this.config.repositoryManager.repositoryInstalled : this.config.repositoryManager.repository); let myError: boolean = this.resolveInRepo(repo, pkgs, names, pkgName, showLog); error = error || myError; @@ -62,16 +65,24 @@ export default class PackageManager { } static removeExtension(pkgNames: string[]) { - log.debug(`removeExtension <- ${pkgNames}`) - - for (let idx in pkgNames) { - pkgNames[idx] = pkgNames[idx] - .replace(/\.levain\.yaml$/, "") - .replace(/\.levain\.yml$/, "") - .replace(/\.levain$/, "") - } - - log.debug(`removeExtension -> ${pkgNames}`) + log.debug(`removeExtension <- ${pkgNames}`); + + let filteredPkgNames = pkgNames + .map(pkgName => { + if (!pkgName || !pkgName.trim()) { + log.warning(`Ignoring package with null name, check your configuration file.`) + return ""; + } else { + return pkgName + .replace(/\.levain\.yaml$/, "") + .replace(/\.levain\.yml$/, "") + .replace(/\.levain$/, ""); + }}) + .filter(pkg => pkg !== ""); + + pkgNames.splice(0, pkgNames.length, ...filteredPkgNames); + + log.debug(`removeExtension -> ${pkgNames}`); } package(pkgName: string): Package | undefined {