From 336eddcfddd4f11a7ad513d4f3c2feb4a9fdb2f9 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Mon, 4 Sep 2023 14:57:44 +0200 Subject: [PATCH] Sapui5Resolver: add tests for tag handling --- lib/ui5Framework/Sapui5Resolver.js | 19 ++++---- test/lib/ui5framework/Sapui5Resolver.js | 60 ++++++++++++++++++------- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/lib/ui5Framework/Sapui5Resolver.js b/lib/ui5Framework/Sapui5Resolver.js index bd5811bbf..6666e9c0e 100644 --- a/lib/ui5Framework/Sapui5Resolver.js +++ b/lib/ui5Framework/Sapui5Resolver.js @@ -105,24 +105,23 @@ class Sapui5Resolver extends AbstractResolver { }) }; } - static async fetchAllVersions({ui5HomeDir, cwd} = {}) { - const installer = new Installer({ - cwd: cwd ? path.resolve(cwd) : process.cwd(), - ui5HomeDir: - ui5HomeDir ? path.resolve(ui5HomeDir) : - path.join(os.homedir(), ".ui5") - }); + static async fetchAllVersions(options) { + const installer = this._getInstaller(options); return await installer.fetchPackageVersions({pkgName: DIST_PKG_NAME}); } - static async fetchAllTags({ui5HomeDir, cwd} = {}) { - const installer = new Installer({ + static async fetchAllTags(options) { + const installer = this._getInstaller(options); + return installer.fetchPackageDistTags({pkgName: DIST_PKG_NAME}); + } + + static _getInstaller({ui5HomeDir, cwd} = {}) { + return new Installer({ cwd: cwd ? path.resolve(cwd) : process.cwd(), ui5HomeDir: ui5HomeDir ? path.resolve(ui5HomeDir) : path.join(os.homedir(), ".ui5") }); - return installer.fetchPackageDistTags({pkgName: DIST_PKG_NAME}); } } diff --git a/test/lib/ui5framework/Sapui5Resolver.js b/test/lib/ui5framework/Sapui5Resolver.js index 822ce0fa3..8081c4abc 100644 --- a/test/lib/ui5framework/Sapui5Resolver.js +++ b/test/lib/ui5framework/Sapui5Resolver.js @@ -7,12 +7,14 @@ import Openui5Resolver from "../../../lib/ui5Framework/Openui5Resolver.js"; test.beforeEach(async (t) => { t.context.InstallerStub = sinon.stub(); + t.context.fetchPackageDistTags = sinon.stub(); t.context.fetchPackageVersionsStub = sinon.stub(); t.context.installPackageStub = sinon.stub(); t.context.getTargetDirForPackageStub = sinon.stub(); t.context.readJsonStub = sinon.stub(); t.context.InstallerStub.callsFake(() => { return { + fetchPackageDistTags: t.context.fetchPackageDistTags, fetchPackageVersions: t.context.fetchPackageVersionsStub, installPackage: t.context.installPackageStub, getTargetDirForPackage: t.context.getTargetDirForPackageStub, @@ -125,40 +127,48 @@ test.serial("Sapui5Resolver: handleLibrary", async (t) => { t.is(loadDistMetadataStub.callCount, 1, "loadDistMetadata should be called once"); }); -test.serial("Sapui5Resolver: Static fetchAllVersions", async (t) => { +test.serial("Sapui5Resolver: Static _getInstaller", (t) => { const {Sapui5Resolver} = t.context; - const expectedVersions = ["1.75.0", "1.75.1", "1.76.0"]; const options = { cwd: "/cwd", ui5HomeDir: "/ui5HomeDir" }; - t.context.fetchPackageVersionsStub.returns(expectedVersions); + const installer = Sapui5Resolver._getInstaller(options); - const versions = await Sapui5Resolver.fetchAllVersions(options); + t.is(t.context.InstallerStub.callCount, 1, "Installer should be called once"); + t.true(t.context.InstallerStub.calledWithNew(), "Installer should be called with new"); + t.is(installer, t.context.InstallerStub.getCall(0).returnValue, "Installer instance is returned"); + t.deepEqual(t.context.InstallerStub.getCall(0).args, [{ + cwd: path.resolve("/cwd"), + ui5HomeDir: path.resolve("/ui5HomeDir") + }], "Installer should be called with expected arguments"); +}); - t.deepEqual(versions, expectedVersions, "Fetched versions should be correct"); +test.serial("Sapui5Resolver: Static _getInstaller without options", (t) => { + const {Sapui5Resolver} = t.context; - t.is(t.context.fetchPackageVersionsStub.callCount, 1, "fetchPackageVersions should be called once"); - t.deepEqual(t.context.fetchPackageVersionsStub.getCall(0).args, [{pkgName: "@sapui5/distribution-metadata"}], - "fetchPackageVersions should be called with expected arguments"); + const installer = Sapui5Resolver._getInstaller(); t.is(t.context.InstallerStub.callCount, 1, "Installer should be called once"); t.true(t.context.InstallerStub.calledWithNew(), "Installer should be called with new"); + t.is(installer, t.context.InstallerStub.getCall(0).returnValue, "Installer instance is returned"); t.deepEqual(t.context.InstallerStub.getCall(0).args, [{ - cwd: path.resolve("/cwd"), - ui5HomeDir: path.resolve("/ui5HomeDir") + cwd: process.cwd(), + ui5HomeDir: path.join(os.homedir(), ".ui5") }], "Installer should be called with expected arguments"); }); -test.serial("Sapui5Resolver: Static fetchAllVersions without options", async (t) => { +test.serial("Sapui5Resolver: Static fetchAllVersions", async (t) => { const {Sapui5Resolver} = t.context; const expectedVersions = ["1.75.0", "1.75.1", "1.76.0"]; t.context.fetchPackageVersionsStub.returns(expectedVersions); + const getInstallerSpy = sinon.spy(Sapui5Resolver, "_getInstaller"); + const versions = await Sapui5Resolver.fetchAllVersions(); t.deepEqual(versions, expectedVersions, "Fetched versions should be correct"); @@ -166,13 +176,29 @@ test.serial("Sapui5Resolver: Static fetchAllVersions without options", async (t) t.is(t.context.fetchPackageVersionsStub.callCount, 1, "fetchPackageVersions should be called once"); t.deepEqual(t.context.fetchPackageVersionsStub.getCall(0).args, [{pkgName: "@sapui5/distribution-metadata"}], "fetchPackageVersions should be called with expected arguments"); + t.is(getInstallerSpy.callCount, 1, "_getInstaller should be called once"); + t.is(getInstallerSpy.getCall(0).args[0], undefined, "_getInstaller should be called without any options"); +}); - t.is(t.context.InstallerStub.callCount, 1, "Installer should be called once"); - t.true(t.context.InstallerStub.calledWithNew(), "Installer should be called with new"); - t.deepEqual(t.context.InstallerStub.getCall(0).args, [{ - cwd: process.cwd(), - ui5HomeDir: path.join(os.homedir(), ".ui5") - }], "Installer should be called with expected arguments"); +test.serial("Sapui5Resolver: Static fetchAllTags", async (t) => { + const {Sapui5Resolver} = t.context; + + const expectedTags = ["latest", "latest-1.71", "latest-1"]; + + t.context.fetchPackageDistTags.returns(expectedTags); + + const getInstallerSpy = sinon.spy(Sapui5Resolver, "_getInstaller"); + + const tags = await Sapui5Resolver.fetchAllTags(); + + t.deepEqual(tags, expectedTags, "Fetched tags should be correct"); + + t.is(t.context.fetchPackageDistTags.callCount, 1, "fetchPackageVersions should be called once"); + t.deepEqual(t.context.fetchPackageDistTags.getCall(0).args, [{pkgName: "@sapui5/distribution-metadata"}], + "fetchPackageVersions should be called with expected arguments"); + + t.is(getInstallerSpy.callCount, 1, "_getInstaller should be called once"); + t.is(getInstallerSpy.getCall(0).args[0], undefined, "_getInstaller should be called without any options"); }); test.serial(