diff --git a/src/commands/publish/handler.ts b/src/commands/publish/handler.ts index 4961a7e0..5a29c751 100644 --- a/src/commands/publish/handler.ts +++ b/src/commands/publish/handler.ts @@ -37,6 +37,7 @@ export async function publishHandler({ }: PublishCommandOptions): Promise { let ethProvider = provider || eth_provider; let contentProvider = provider || content_provider; + const isMultiVariant = Boolean(allVariants) || Boolean(variants); const isCi = process.env.CI; @@ -80,7 +81,8 @@ export async function publishHandler({ rootDir: dir, variantsDirName, composeFileName - }) + }), + isMultiVariant }), verbosityOptions ); diff --git a/src/params.ts b/src/params.ts index 1da09560..0712fbb7 100644 --- a/src/params.ts +++ b/src/params.ts @@ -3,8 +3,8 @@ import { ManifestFormat } from "./files/manifest/types.js"; export * from "./files/compose/params.js"; -export class CliError extends Error {} -export class YargsError extends Error {} +export class CliError extends Error { } +export class YargsError extends Error { } // Github Actions params @@ -14,6 +14,7 @@ export const branchNameRoot = "dappnodebot/bump-upstream/"; export const defaultDir = "./"; export const defaultVariantsDirName = "package_variants"; +export const singleVariantName = "single-variant"; // This is the default name of the environment variable that will be used to select each of the variants export const defaultVariantsEnvName = "NETWORK"; export const defaultVariantsEnvValues = ["mainnet", "testnet"]; diff --git a/src/tasks/buildAndUpload/buildVariantMap.ts b/src/tasks/buildAndUpload/buildVariantMap.ts index 34b2fefe..b962b594 100644 --- a/src/tasks/buildAndUpload/buildVariantMap.ts +++ b/src/tasks/buildAndUpload/buildVariantMap.ts @@ -1,14 +1,13 @@ import path from "path"; import { parseArchitectures } from "../../utils/parseArchitectures.js"; import { - getComposePath, readCompose, getComposePackageImages, parseComposeUpstreamVersion, readManifest } from "../../files/index.js"; import { Compose, Manifest } from "@dappnode/types"; -import { defaultComposeFileName } from "../../params.js"; +import { defaultComposeFileName, singleVariantName } from "../../params.js"; import { BuildVariantsMap, BuildVariantsMapEntry } from "../../types.js"; export function buildVariantMap({ @@ -23,7 +22,7 @@ export function buildVariantMap({ composeFileName?: string; }): BuildVariantsMap { if (!variants || variants.length === 0) - return { default: createVariantMapEntry({ rootDir, composeFileName }) }; + return { [singleVariantName]: createVariantMapEntry({ rootDir, composeFileName }) }; const map: BuildVariantsMap = {}; @@ -48,23 +47,18 @@ export function createVariantMapEntry({ composeFileName: string; variantPath?: string; }): BuildVariantsMapEntry { - const { manifest, format } = variantPath - ? readManifest([{ dir: rootDir }, { dir: variantPath }]) - : readManifest([{ dir: rootDir }]); + const manifestPaths = [{ dir: rootDir }]; + const composePaths = [{ dir: rootDir, composeFileName }]; - const { name: dnpName, version } = manifest; - - const composePaths = [getComposePath({ dir: rootDir, composeFileName })]; + if (variantPath) { + manifestPaths.push({ dir: variantPath }); + composePaths.push({ dir: variantPath, composeFileName }); + } - if (variantPath) - composePaths.push(getComposePath({ dir: variantPath, composeFileName })); + const { manifest, format: manifestFormat } = readManifest(manifestPaths); + const compose = readCompose(composePaths); - const compose = variantPath - ? readCompose([ - { dir: rootDir, composeFileName }, - { dir: variantPath, composeFileName } - ]) - : readCompose([{ dir: rootDir, composeFileName }]); + const { name: dnpName, version } = manifest; // TODO: Handle upstream object defined case if (!manifest.upstream) @@ -72,11 +66,12 @@ export function createVariantMapEntry({ return { manifest, - manifestFormat: format, + manifestFormat, compose, releaseDir: getReleaseDirPath({ rootDir, dnpName, version }), + manifestPaths, composePaths, images: getComposePackageImages(compose, manifest), diff --git a/src/tasks/buildAndUpload/getFileValidationTask.ts b/src/tasks/buildAndUpload/getFileValidationTask.ts index 0d5520d8..3144f4c3 100644 --- a/src/tasks/buildAndUpload/getFileValidationTask.ts +++ b/src/tasks/buildAndUpload/getFileValidationTask.ts @@ -6,7 +6,7 @@ import { validateSetupWizardSchema, validateDappnodeCompose } from "@dappnode/schemas"; -import { readSetupWizardIfExists } from "../../files/index.js"; +import { getComposePath, readSetupWizardIfExists } from "../../files/index.js"; import { CliError } from "../../params.js"; export function getFileValidationTask({ @@ -53,7 +53,7 @@ async function validateVariantFiles( validatePackageVersion(manifest.version); // Validate compose file using docker compose - await validateComposeSchema(composePaths); + await validateComposeSchema(composePaths.map(pathObj => getComposePath(pathObj))); // Validate compose file specifically for Dappnode requirements validateDappnodeCompose(compose, manifest); diff --git a/src/tasks/buildAndUpload/getSaveUploadResultsTask.ts b/src/tasks/buildAndUpload/getSaveUploadResultsTask.ts index eb68fbb4..c0edadbc 100644 --- a/src/tasks/buildAndUpload/getSaveUploadResultsTask.ts +++ b/src/tasks/buildAndUpload/getSaveUploadResultsTask.ts @@ -9,51 +9,32 @@ export function getSaveUploadResultsTask({ rootDir, variantsDirPath, contentProvider, - skipUpload + skipUpload, + isMultiVariant }: { variantsMap: BuildVariantsMap; rootDir: string; variantsDirPath: string; contentProvider: string; skipUpload?: boolean; + isMultiVariant?: boolean; }): ListrTask { return { title: "Save upload results", skip: () => skipUpload, task: async ctx => { - // Single package - if (variantsMap.default) { - const { name, version } = variantsMap.default.manifest; - const { releaseMultiHash: hash } = ctx[name]; - - if (hash) - addReleaseRecord({ - dir: rootDir, - version, - hash, - to: contentProvider - }); - // Multi-variant package - } else { - for (const [ - variant, - { - manifest: { name, version } - } - ] of Object.entries(variantsMap)) { - const variantDir = path.join(variantsDirPath, variant); - const { releaseMultiHash: hash } = ctx[name]; + for (const [variant, { manifest: { name, version } }] of Object.entries(variantsMap)) { + const { releaseMultiHash: hash } = ctx[name]; - if (!hash) continue; + if (!hash) continue; - addReleaseRecord({ - dir: variantDir, - version, - hash, - to: contentProvider - }); - } + addReleaseRecord({ + dir: isMultiVariant ? path.join(variantsDirPath, variant) : rootDir, + version, + hash, + to: contentProvider + }); } try { diff --git a/src/tasks/publish/index.ts b/src/tasks/publish/index.ts index 067aa342..2eb39314 100644 --- a/src/tasks/publish/index.ts +++ b/src/tasks/publish/index.ts @@ -1,11 +1,12 @@ import { ListrTask } from "listr"; import { PublishOptions } from "./types.js"; import { ListrContextPublish } from "../../types.js"; -import { getFetchApmVersionsTask } from "./subtasks/getFetchApmVersionsTask.js"; +import { getFetchNextVersionsFromApmTask } from "./subtasks/getFetchApmVersionsTask.js"; import { getBuildAndUploadTask } from "./subtasks/getBuildAndUploadTask.js"; import { getGenerateTxTask } from "./subtasks/getGenerateTxsTask.js"; import { getCreateGithubReleaseTask } from "./subtasks/getCreateGithubReleaseTask.js"; import { getVerifyEthConnectionTask } from "./subtasks/getVerifyEthConnectionTask.js"; +import { getUpdateFilesTask } from "./subtasks/getUpdateFilesTask.js"; export function publish({ releaseType, @@ -21,17 +22,22 @@ export function publish({ githubRelease, verbosityOptions, variantsDirPath, - variantsMap + variantsMap, + isMultiVariant }: PublishOptions): ListrTask[] { return [ getVerifyEthConnectionTask({ ethProvider }), - getFetchApmVersionsTask({ + getFetchNextVersionsFromApmTask({ releaseType, ethProvider, + variantsMap + }), + getUpdateFilesTask({ rootDir: dir, variantsDirPath, composeFileName, - variantsMap + variantsMap, + isMultiVariant }), getBuildAndUploadTask({ buildOptions: { diff --git a/src/tasks/publish/subtasks/getFetchApmVersionTask.ts b/src/tasks/publish/subtasks/getFetchApmVersionTask.ts deleted file mode 100644 index 2e16000e..00000000 --- a/src/tasks/publish/subtasks/getFetchApmVersionTask.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { ListrTask } from "listr"; -import { ListrContextPublish, ReleaseType } from "../../../types.js"; -import { - readManifest, - writeManifest, - readCompose, - updateComposeImageTags, - writeCompose -} from "../../../files/index.js"; -import { getNextVersionFromApm } from "../../../utils/versions/getNextVersionFromApm.js"; -import { Manifest } from "@dappnode/types"; -import { ManifestFormat } from "../../../files/manifest/types.js"; - -export function getFetchApmVersionTask({ - releaseType, - ethProvider, - dir, - composeFileName -}: { - releaseType: ReleaseType; - ethProvider: string; - dir: string; - composeFileName: string; -}): ListrTask { - return { - title: "Fetch current version from APM", - task: async ctx => { - const { manifest, format: manifestFormat } = readManifest([{ dir }]); - const { name, version } = manifest; - - ctx[name] = ctx[name] || {}; - - try { - ctx[name].nextVersion = await increaseFromApmVersion({ - type: releaseType, - ethProvider, - dir, - composeFileName, - manifest, - manifestFormat - }); - } catch (e) { - if (e.message.includes("NOREPO")) ctx[name].nextVersion = version; - else throw e; - } - } - }; -} - -// TODO: Try to test this without exporting the function (not used anywhere else) -export async function increaseFromApmVersion({ - type, - ethProvider, - dir, - composeFileName, - manifest, - manifestFormat -}: { - type: ReleaseType; - ethProvider: string; - dir: string; - composeFileName: string; - manifest: Manifest; - manifestFormat: ManifestFormat; -}): Promise { - // Check variables - const nextVersion = await getNextVersionFromApm({ - type, - ethProvider, - ensName: manifest.name - }); - - // Increase the version - manifest.version = nextVersion; - - // Modify and write the manifest and docker-compose - writeManifest(manifest, manifestFormat, { dir }); - const { name, version } = manifest; - const compose = readCompose([{ dir, composeFileName }]); - const newCompose = updateComposeImageTags(compose, { name, version }); - writeCompose(newCompose, { dir, composeFileName }); - - return nextVersion; -} diff --git a/src/tasks/publish/subtasks/getFetchApmVersionsTask.ts b/src/tasks/publish/subtasks/getFetchApmVersionsTask.ts index febc59d1..ff312281 100644 --- a/src/tasks/publish/subtasks/getFetchApmVersionsTask.ts +++ b/src/tasks/publish/subtasks/getFetchApmVersionsTask.ts @@ -1,134 +1,55 @@ import { ListrTask } from "listr"; import { BuildVariantsMap, ListrContextPublish, ReleaseType } from "../../../types.js"; -import { - writeManifest, - readCompose, - updateComposeImageTags, - writeCompose, - readManifest -} from "../../../files/index.js"; import { getNextVersionFromApm } from "../../../utils/versions/getNextVersionFromApm.js"; -import path from "path"; import { Manifest } from "@dappnode/types"; -export function getFetchApmVersionsTask({ +export function getFetchNextVersionsFromApmTask({ releaseType, ethProvider, - rootDir, - variantsDirPath, - composeFileName, variantsMap }: { releaseType: ReleaseType; ethProvider: string; - rootDir: string; - variantsDirPath: string; - composeFileName: string; variantsMap: BuildVariantsMap; }): ListrTask { return { title: "Fetch current versions from APM", task: async ctx => { - for (const [variant, { manifest }] of Object.entries(variantsMap)) - await setNextVersionToContext({ - ctx, + + for (const [, { manifest }] of Object.entries(variantsMap)) { + const dnpName = manifest.name; + + ctx[dnpName] = ctx[dnpName] || {}; + ctx[dnpName].nextVersion = await getNextPackageVersion({ + manifest, releaseType, - ethProvider, - dir: rootDir, - variantsDirPath, - composeFileName, - variant: variant === "default" ? null : variant, - manifest + ethProvider }); + + } } }; } -async function setNextVersionToContext({ - ctx, +export async function getNextPackageVersion({ + manifest, releaseType, ethProvider, - dir, - variantsDirPath, - composeFileName, - variant, - manifest }: { - ctx: ListrContextPublish; + manifest: Manifest; releaseType: ReleaseType; ethProvider: string; - dir: string; - variantsDirPath: string; - composeFileName: string; - variant: string | null; - manifest: Manifest; -}): Promise { - const { name, version } = manifest - ctx[name] = ctx[name] || {}; +}) { + const { name, version } = manifest; try { - const nextVersion = await increaseFromApmVersion({ + return await getNextVersionFromApm({ type: releaseType, ethProvider, - dir, - composeFileName, - variant, - variantsDirPath, ensName: name }); - - ctx[name].nextVersion = nextVersion; - manifest.version = nextVersion; } catch (e) { - if (e.message.includes("NOREPO")) ctx[name].nextVersion = version; + if (e.message.includes("NOREPO")) return version; else throw e; } -} - -// TODO: Try to test this without exporting the function (not used anywhere else) -export async function increaseFromApmVersion({ - type, - ethProvider, - dir, - composeFileName, - variant, - variantsDirPath, - ensName -}: { - type: ReleaseType; - ethProvider: string; - dir: string; - composeFileName: string; - variant: string | null; - variantsDirPath: string; - ensName: string; -}): Promise { - const variantDir = variant ? path.join(variantsDirPath, variant) : dir; - - // Check variables - const nextVersion = await getNextVersionFromApm({ - type, - ethProvider, - ensName - }); - - const { manifest, format } = readManifest([{ dir: variantDir }]); - - // Increase the version - manifest.version = nextVersion; - - // Modify and write the manifest and docker-compose - writeManifest(manifest, format, { dir: variantDir }); - - const compose = readCompose([{ dir: variantDir, composeFileName }]); - const newCompose = updateComposeImageTags(compose, { - name: ensName, - version: nextVersion - }); - writeCompose(newCompose, { - dir: variantDir, - composeFileName - }); - - return nextVersion; -} +} \ No newline at end of file diff --git a/src/tasks/publish/subtasks/getUpdateFilesTask.ts b/src/tasks/publish/subtasks/getUpdateFilesTask.ts new file mode 100644 index 00000000..2f1bf1bd --- /dev/null +++ b/src/tasks/publish/subtasks/getUpdateFilesTask.ts @@ -0,0 +1,128 @@ +import path from "path"; +import { ListrTask } from "listr"; +import { BuildVariantsMap, ListrContextPublish } from "../../../types.js"; +import { + writeManifest, + readCompose, + updateComposeImageTags, + writeCompose, + readManifest +} from "../../../files/index.js"; + +export function getUpdateFilesTask({ + rootDir, + variantsDirPath, + composeFileName, + variantsMap, + isMultiVariant +}: { + rootDir: string; + variantsDirPath: string; + composeFileName: string; + variantsMap: BuildVariantsMap; + isMultiVariant: boolean; +}): ListrTask { + return { + title: "Update compose and manifest files", + task: async ctx => { + for (const [variant, { manifest: { name }, composePaths, manifestPaths }] of Object.entries(variantsMap)) { + const nextVersion = ctx[name].nextVersion; + + if (!nextVersion) { + throw new Error( + `No next version found for ${name}. It should have been fetched from APM.` + ); + } + + updateVariantFiles({ + rootDir, + composeFileName, + variant, + variantsDirPath, + dnpName: name, + nextVersion, + isMultiVariant + }); + + // Update variantsMap entry + variantsMap[variant].compose = readCompose(composePaths); + variantsMap[variant].manifest = readManifest(manifestPaths).manifest; + } + } + }; +} + +// TODO: Test without exporting +export function updateVariantFiles({ + rootDir, + composeFileName, + variant, + variantsDirPath, + dnpName, + nextVersion, + isMultiVariant +}: { + rootDir: string; + composeFileName: string; + variant: string; + variantsDirPath: string; + dnpName: string; + nextVersion: string; + isMultiVariant: boolean; +}): void { + // For multi-variant packages, manifest and compose files to be modified are located in the variant folder + // Single variant packages have single-variant as the variant name + const filesDir = isMultiVariant ? path.join(variantsDirPath, variant) : rootDir; + + updateManifestFileVersion({ + manifestDir: filesDir, + nextVersion + }); + + updateComposeFileImages({ + composeDir: filesDir, + composeFileName, + nextVersion, + dnpName + }); +} + +function updateManifestFileVersion({ + manifestDir, + nextVersion +}: { + manifestDir: string; + nextVersion: string; +}) { + const { manifest, format } = readManifest([{ dir: manifestDir }]); + + // Increase the version + manifest.version = nextVersion; + + // Modify and write the manifest and docker-compose + writeManifest(manifest, format, { dir: manifestDir }); +} + +function updateComposeFileImages({ + composeDir, + composeFileName, + nextVersion, + dnpName +}: { + composeDir: string; + composeFileName: string; + nextVersion: string; + dnpName: string; +}) { + const compose = readCompose([{ dir: composeDir, composeFileName }]); + + const newCompose = updateComposeImageTags(compose, { + name: dnpName, + version: nextVersion + }, { editExternalImages: true }); + + writeCompose(newCompose, { + dir: composeDir, + composeFileName + }); +} \ No newline at end of file diff --git a/src/tasks/publish/types.ts b/src/tasks/publish/types.ts index 21bfa357..37b84672 100644 --- a/src/tasks/publish/types.ts +++ b/src/tasks/publish/types.ts @@ -17,4 +17,5 @@ export interface PublishOptions { verbosityOptions: VerbosityOptions; variantsDirPath: string; variantsMap: BuildVariantsMap; + isMultiVariant: boolean; } diff --git a/src/types.ts b/src/types.ts index 21409dfe..7e9778eb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ -import { Architecture, Compose, Manifest } from "@dappnode/types"; -import { ManifestFormat } from "./files/manifest/types.js"; +import { Architecture, Compose, ComposePaths, Manifest } from "@dappnode/types"; +import { ManifestFormat, ManifestPaths } from "./files/manifest/types.js"; export interface CliGlobalOptions { dir?: string; @@ -39,7 +39,8 @@ export interface BuildVariantsMapEntry extends PublishVariantsMapEntry { // File paths releaseDir: string; - composePaths: string[]; + composePaths: ComposePaths[]; + manifestPaths: ManifestPaths[]; // Package information images: PackageImage[]; diff --git a/test/tasks/buildAndUpload/buildVariantMap.test.ts b/test/tasks/buildAndUpload/buildVariantMap.test.ts index 1b57c31c..d89edf6e 100644 --- a/test/tasks/buildAndUpload/buildVariantMap.test.ts +++ b/test/tasks/buildAndUpload/buildVariantMap.test.ts @@ -5,7 +5,8 @@ import { initHandler } from "../../../src/commands/init/handler.js"; import { defaultComposeFileName, defaultVariantsDirName, - defaultVariantsEnvValues + defaultVariantsEnvValues, + singleVariantName } from "../../../src/params.js"; import { defaultArch } from "@dappnode/types"; import path from "path"; @@ -24,21 +25,22 @@ describe("buildVariantMap", function () { }); }); - it("should return a map with only default variant", function () { + it("should return a map with a single variant", function () { const result = buildVariantMap({ rootDir: testDir, variantsDirPath: defaultVariantsDirName, variants: null }); - expect(result).to.have.all.keys("default"); - const defaultVariant = result.default; + expect(result).to.have.all.keys(singleVariantName); + const defaultVariant = result[singleVariantName]; expect(defaultVariant).to.have.all.keys( "manifest", "manifestFormat", "compose", "releaseDir", + "manifestPaths", "composePaths", "images", "architectures" @@ -52,8 +54,11 @@ describe("buildVariantMap", function () { expect(defaultVariant.images).to.be.an("array"); expect(defaultVariant.architectures).to.be.an("array"); - expect(defaultVariant.composePaths).to.include.members([ - `${testDir}/${defaultComposeFileName}` + expect(defaultVariant.composePaths).to.deep.include.members([ + { + composeFileName: defaultComposeFileName, + dir: testDir + } ]); expect(defaultVariant.architectures).to.include(defaultArch); @@ -102,9 +107,15 @@ describe("buildVariantMap", function () { expect(result[variant].architectures).to.be.an("array"); // Example: Validate specific variant paths - expect(result[variant].composePaths).to.include.members([ - `${testDir}/${defaultComposeFileName}`, - `${testDir}/${defaultVariantsDirName}/${variant}/${defaultComposeFileName}` + expect(result[variant].composePaths).to.deep.include.members([ + { + composeFileName: defaultComposeFileName, + dir: testDir + }, + { + composeFileName: defaultComposeFileName, + dir: `${testDir}/${defaultVariantsDirName}/${variant}` + } ]); // Assuming we can check details about manifest, compose object structure if known diff --git a/test/utils/versions/getNextPackageVersion.test.ts b/test/utils/versions/getNextPackageVersion.test.ts new file mode 100644 index 00000000..0d1993d1 --- /dev/null +++ b/test/utils/versions/getNextPackageVersion.test.ts @@ -0,0 +1,46 @@ +import { expect } from "chai"; +import semver from "semver"; +import { cleanTestDir, generateCompose, testDir } from "../../testUtils.js"; +import { + defaultComposeFileName, + defaultManifestFormat +} from "../../../src/params.js"; +import { + writeCompose, + readCompose, + writeManifest, + readManifest +} from "../../../src/files/index.js"; +import { Manifest } from "@dappnode/types"; +import { ManifestFormat } from "../../../src/files/manifest/types.js"; +import { getNextPackageVersion } from "../../../src/tasks/publish/subtasks/getFetchApmVersionsTask.js"; + +describe("getNextPackageVersion", function () { + this.timeout(60 * 1000); + + const dnpName = "admin.dnp.dappnode.eth"; + const manifest: Manifest = { + name: dnpName, + version: "0.1.0", + type: "dncore" + }; + + before("Clean testDir", () => cleanTestDir()); + after("Clean testDir", () => cleanTestDir()); + + it("Should get the next version from APM and update manifest and compose files", async () => { + // Write initial manifest and compose files + writeManifest(manifest, ManifestFormat.json, { dir: testDir }); + writeCompose({ version: "3.8", services: { [dnpName]: { image: `${dnpName}:${manifest.version}` } } }, { dir: testDir }); + + // Fetch the next version from APM + const nextVersion = await getNextPackageVersion({ + manifest, + releaseType: "patch", + ethProvider: "infura" + }); + + // Ensure the next version is valid + expect(semver.valid(nextVersion)).to.be.ok; + }); +}); \ No newline at end of file diff --git a/test/utils/versions/getNextVersionFromApm.test.ts b/test/utils/versions/getNextVersionFromApm.test.ts deleted file mode 100644 index aadac0a6..00000000 --- a/test/utils/versions/getNextVersionFromApm.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { expect } from "chai"; -import semver from "semver"; -import { getNextVersionFromApm } from "../../../src/utils/versions/getNextVersionFromApm.js"; -import { readManifest, writeManifest } from "../../../src/files/index.js"; -import { cleanTestDir, testDir } from "../../testUtils.js"; -import { defaultManifestFormat } from "../../../src/params.js"; - -// This test will create the following fake files -// ./dappnode_package.json => fake manifest -// -// Then it will expect the function to fetch the latest version from APM and log it - -describe("getNextVersionFromApm", function () { - this.timeout(60 * 1000); - - const manifest = { - name: "admin.dnp.dappnode.eth", - version: "0.1.0" - }; - - before("Clean testDir", () => cleanTestDir()); - after("Clean testDir", () => cleanTestDir()); - - it("Should get the last version from APM", async () => { - writeManifest(manifest, defaultManifestFormat, { dir: testDir }); - - const { - manifest: { name } - } = readManifest([{ dir: testDir }]); - - const nextVersion = await getNextVersionFromApm({ - type: "patch", - ethProvider: "infura", - ensName: name - }); - // Check that the console output contains a valid semver version - expect(semver.valid(nextVersion)).to.be.ok; - }); -}); diff --git a/test/utils/versions/increaseFromApmVersion.test.ts b/test/utils/versions/increaseFromApmVersion.test.ts deleted file mode 100644 index 64acf1e3..00000000 --- a/test/utils/versions/increaseFromApmVersion.test.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { expect } from "chai"; -import semver from "semver"; -import { cleanTestDir, generateCompose, testDir } from "../../testUtils.js"; -import { increaseFromApmVersion } from "../../../src/tasks/publish/subtasks/getFetchApmVersionTask.js"; -import { - defaultComposeFileName, - defaultManifestFormat -} from "../../../src/params.js"; -import { - writeCompose, - readCompose, - writeManifest, - readManifest -} from "../../../src/files/index.js"; -import { Manifest } from "@dappnode/types"; -import { ManifestFormat } from "../../../src/files/manifest/types.js"; - -// This test will create the following fake files -// ./dappnode_package.json => fake manifest -// -// Then it will expect the function to -// - log the next version -// - modify the existing manifest and increase its version -// - generate a docker compose with the next version - -describe("increaseFromApmVersion", function () { - this.timeout(60 * 1000); - - const dnpName = "admin.dnp.dappnode.eth"; - const manifest: Manifest = { - name: dnpName, - version: "0.1.0", - type: "dncore" - }; - - before("Clean testDir", () => cleanTestDir()); - after("Clean testDir", () => cleanTestDir()); - - it("Should get the last version from APM", async () => { - writeManifest(manifest, defaultManifestFormat, { dir: testDir }); - writeCompose(generateCompose(manifest), { dir: testDir }); - - const nextVersion = await increaseFromApmVersion({ - type: "patch", - ethProvider: "infura", - composeFileName: defaultComposeFileName, - dir: testDir, - manifest, - manifestFormat: ManifestFormat.json - }); - - // Check that the console output contains a valid semver version - expect(semver.valid(nextVersion)).to.be.ok; - - // Check that the compose was edited correctly to the next version - const compose = readCompose([{ dir: testDir }]); - expect(compose.services[dnpName].image).to.equal( - `admin.dnp.dappnode.eth:${nextVersion}`, - "compose should be edited to the next version" - ); - // Check that the manifest was edited correctly to the next version - const { manifest: newManifest } = readManifest([{ dir: testDir }]); - expect(newManifest.version).to.equal( - nextVersion, - "manifest should be edited to the next version" - ); - }); -}); diff --git a/test/utils/versions/updateVariantFiles.test.ts b/test/utils/versions/updateVariantFiles.test.ts new file mode 100644 index 00000000..2e8cef93 --- /dev/null +++ b/test/utils/versions/updateVariantFiles.test.ts @@ -0,0 +1,64 @@ +import { expect } from "chai"; +import path from "path"; +import { cleanTestDir, testDir } from "../../testUtils.js"; +import { defaultComposeFileName, singleVariantName } from "../../../src/params.js"; +import { + writeManifest, + readCompose, + writeCompose, + readManifest +} from "../../../src/files/index.js"; +import { Compose, Manifest } from "@dappnode/types"; +import { ManifestFormat } from "../../../src/files/manifest/types.js"; +import { updateVariantFiles, } from "../../../src/tasks/publish/subtasks/getUpdateFilesTask.js"; + + +describe("Update Variant Files and Entries", function () { + this.timeout(60 * 1000); + + const dnpName = "admin.dnp.dappnode.eth"; + + const manifest: Manifest = { + name: dnpName, + version: "0.1.0", + type: "dncore" + }; + + const compose: Compose = { + version: "3.8", + services: { [dnpName]: { image: "override-me" } } + }; + + const nextVersion = "0.1.1"; + + before("Clean testDir", () => cleanTestDir()); + after("Clean testDir", () => cleanTestDir()); + + it("Should update manifest and compose files correctly", async () => { + // Write initial manifest and compose files + writeManifest(manifest, ManifestFormat.json, { dir: testDir }); + writeCompose(compose, { dir: testDir }); + + // Update variant files + updateVariantFiles({ + rootDir: testDir, + composeFileName: defaultComposeFileName, + variant: singleVariantName, + variantsDirPath: testDir, + dnpName, + nextVersion, + isMultiVariant: false + }); + + // Check that the manifest was updated correctly + const { manifest: newManifest } = readManifest([{ dir: testDir }]); + expect(newManifest.version).to.equal(nextVersion, "Manifest version should be updated to the next version"); + + // Check that the compose was updated correctly + const updatedCompose = readCompose([{ dir: testDir, composeFileName: defaultComposeFileName }]); + expect(updatedCompose.services[dnpName].image).to.equal( + `${dnpName}:${nextVersion}`, + "Compose image should be updated to the next version" + ); + }); +});