-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build-infrastructure): Add setVersion API
- Loading branch information
1 parent
199b9d0
commit 5c83787
Showing
4 changed files
with
107 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
build-tools/packages/build-infrastructure/src/test/versions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { strict as assert } from "node:assert"; | ||
import path from "node:path"; | ||
|
||
import { expect } from "chai"; | ||
import { afterEach, describe, it } from "mocha"; | ||
import * as semver from "semver"; | ||
import { simpleGit } from "simple-git"; | ||
|
||
import { loadFluidRepo } from "../fluidRepo.js"; | ||
import type { ReleaseGroupName, WorkspaceName } from "../types.js"; | ||
import { setVersion } from "../versions.js"; | ||
|
||
import { testDataPath, testRepoRoot } from "./init.js"; | ||
|
||
const repo = loadFluidRepo(path.join(testDataPath, "./testRepo")); | ||
const main = repo.releaseGroups.get("main" as ReleaseGroupName); | ||
assert(main !== undefined); | ||
|
||
const group2 = repo.releaseGroups.get("group2" as ReleaseGroupName); | ||
assert(group2 !== undefined); | ||
|
||
const group3 = repo.releaseGroups.get("group3" as ReleaseGroupName); | ||
assert(group3 !== undefined); | ||
|
||
const secondWorkspace = repo.workspaces.get("second" as WorkspaceName); | ||
assert(secondWorkspace !== undefined); | ||
|
||
/** | ||
* A git client rooted in the test repo. Used for resetting tests. | ||
*/ | ||
const git = simpleGit(testRepoRoot); | ||
|
||
describe("setVersion", () => { | ||
afterEach(async () => { | ||
await git.checkout(["HEAD", "--", testRepoRoot]); | ||
repo.reload(); | ||
}); | ||
|
||
it("release group", async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
await setVersion(main.packages, semver.parse("1.2.1")!); | ||
repo.reload(); | ||
|
||
const allCorrect = main.packages.every((pkg) => pkg.version === "1.2.1"); | ||
expect(main.version).to.equal("1.2.1"); | ||
expect(allCorrect).to.be.true; | ||
}); | ||
|
||
it("workspace", async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
await setVersion(secondWorkspace.packages, semver.parse("2.2.1")!); | ||
repo.reload(); | ||
|
||
const allCorrect = secondWorkspace.packages.every((pkg) => pkg.version === "2.2.1"); | ||
expect(allCorrect).to.be.true; | ||
}); | ||
|
||
it("repo", async () => { | ||
const packages = [...repo.packages.values()]; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
await setVersion(packages, semver.parse("1.2.1")!); | ||
repo.reload(); | ||
|
||
const allCorrect = packages.every((pkg) => pkg.version === "1.2.1"); | ||
expect(allCorrect).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import * as semver from "semver"; | ||
|
||
import { updatePackageJsonFile } from "./packageJsonUtils.js"; | ||
import type { IPackage, PackageJson } from "./types.js"; | ||
|
||
/** | ||
* Sets the version of a group of packages. | ||
* | ||
* Note that any loaded objects such as an IFluidRepo instance may need to be reloaded after calling this function. | ||
* | ||
* @param fluidRepo - The {@link IFluidRepo}. | ||
* @param packages - An array of objects whose version should be updated. | ||
* @param version - The version to set. | ||
*/ | ||
export async function setVersion<J extends PackageJson>( | ||
packages: IPackage[], | ||
version: semver.SemVer, | ||
): Promise<void> { | ||
const translatedVersion = version; | ||
for (const pkg of packages) { | ||
updatePackageJsonFile<J>(pkg.directory, (json) => { | ||
json.version = translatedVersion.version; | ||
}); | ||
} | ||
} |