From 3aee569f553f389ec48bf74b3dc3de05e4c03ff2 Mon Sep 17 00:00:00 2001 From: Johannes Lumpe Date: Wed, 19 Jun 2019 19:48:52 -0400 Subject: [PATCH] fix: properly handle non-existing dependencies (#8) --- src/cli/__tests__/index.ts | 17 ++++++++++++ src/cli/commands/versionCheck.ts | 2 +- src/core/versionCheck.ts | 26 +++++++++++-------- .../application_a/package.json | 8 ++++++ .../config_fixture.json | 16 ++++++++++++ 5 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/check/simple-config-non-existing-dep/application_a/package.json create mode 100644 test/fixtures/check/simple-config-non-existing-dep/config_fixture.json diff --git a/src/cli/__tests__/index.ts b/src/cli/__tests__/index.ts index 8cfbf0d..8100c3c 100644 --- a/src/cli/__tests__/index.ts +++ b/src/cli/__tests__/index.ts @@ -438,6 +438,23 @@ Dependency sets }); }); + describe('non existing dependencies in sets', () => { + it('should not fail', async () => { + await executeCliWithCopiedConfigFixture( + 'check/simple-config-non-existing-dep', + )('check', '--verbose'); + expect(getCleanedMockStdout()).toBe(trim` + ⚠ + ┌─────────────┬───────────┬─────────┬────────┬───────────────────────────────┐ + │Application │Dependency │Installed│Required│Valid │ + ├─────────────┼───────────┼─────────┼────────┼───────────────────────────────┤ + │application_a│nonexisting│- │1.x │⚠ Infinity days left to upgrade│ + └─────────────┴───────────┴─────────┴────────┴───────────────────────────────┘ + Check tentatively passed! + `); + }); + }); + describe('some invalid entries within grace period', () => { // needs to be in sync with value used in config fixtures const DEPENDENCY_ADDED_DATE = 1560081600000; diff --git a/src/cli/commands/versionCheck.ts b/src/cli/commands/versionCheck.ts index 2d05890..2642842 100644 --- a/src/cli/commands/versionCheck.ts +++ b/src/cli/commands/versionCheck.ts @@ -39,7 +39,7 @@ function colorTextForStatus( function getDependencyResultRow(result: DependencyResult): HorizontalTableRow { return [ colorTextForStatus(result.dependency, result.result), - colorTextForStatus(result.currentVersion, result.result), + colorTextForStatus(result.currentVersion || '-', result.result), colorTextForStatus(result.requiredVersion, result.result), result.result === 'TENTATIVE_PASS' ? `${logSymbols.warning} ${formatDuration( diff --git a/src/core/versionCheck.ts b/src/core/versionCheck.ts index 7f4cc65..4ff9281 100644 --- a/src/core/versionCheck.ts +++ b/src/core/versionCheck.ts @@ -172,18 +172,22 @@ export function checkDependencies({ for (const application of applicationsToCheck) { const dependencyVersion = dependenciesByApplication[application.path][dependency]; - // TODO fix directly value access - const { value } = getMinSemverVersion( - dependencyVersion, - dependency, - ); - if (value instanceof Error) { - throw value; + // instantly fail if dependency does not exist at all + let dependencySatisfied = !!dependencyVersion; + if (dependencyVersion) { + // TODO fix directly value access + const { value } = getMinSemverVersion( + dependencyVersion, + dependency, + ); + if (value instanceof Error) { + throw value; + } + dependencySatisfied = semver.satisfies( + value, + semver.validRange(requiredDependencyVersion), + ); } - const dependencySatisfied = semver.satisfies( - value, - semver.validRange(requiredDependencyVersion), - ); const appResult = applicationDependencyResults[application.path] || (applicationDependencyResults[application.path] = { diff --git a/test/fixtures/check/simple-config-non-existing-dep/application_a/package.json b/test/fixtures/check/simple-config-non-existing-dep/application_a/package.json new file mode 100644 index 0000000..8c18e0a --- /dev/null +++ b/test/fixtures/check/simple-config-non-existing-dep/application_a/package.json @@ -0,0 +1,8 @@ +{ + "name": "application-a", + "dependencies": { + "dep_a": "^1.0.0", + "dep_b": "1.2.3", + "dep_c": "~1.5.0" + } +} diff --git a/test/fixtures/check/simple-config-non-existing-dep/config_fixture.json b/test/fixtures/check/simple-config-non-existing-dep/config_fixture.json new file mode 100644 index 0000000..1d10329 --- /dev/null +++ b/test/fixtures/check/simple-config-non-existing-dep/config_fixture.json @@ -0,0 +1,16 @@ +{ + "all-valid-a": { + "applications": [{ "path": "application_a", "name": "application-a"}], + "dependencies": { + "public": { + "dependencySemvers": { + "nonexisting": { + "dateAdded": 1560081600000, + "semver": "nonexisting@1.x" + } + }, + "gracePeriod": Infinity + } + } + } +}