Skip to content

Commit

Permalink
[INTERNAL] Sapui5Resolver: add tests for tag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Sep 4, 2023
1 parent f1b8185 commit ce4243e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
19 changes: 9 additions & 10 deletions lib/ui5Framework/Sapui5Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}

Expand Down
60 changes: 43 additions & 17 deletions test/lib/ui5framework/Sapui5Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -125,54 +127,78 @@ 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");

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(
Expand Down

0 comments on commit ce4243e

Please sign in to comment.