Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions packages/radius-toolkit/src/lib/tokens/layer-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,57 @@ import { describe, it, expect } from "vitest";
import { semVerBump } from "./layer-diff.utils.js";

describe("semVerBump function", () => {
it("should bump minor version when there are additions", () => {
expect(semVerBump("1.0.0", [true, false, false])).toBe("1.1.0");
});

it("should bump build version when there are modifications", () => {
expect(semVerBump("1.1.0", [false, true, false])).toBe("1.1.1");
});
describe("when the version is less than 1.0.0", () => {

it("should bump major version when there are breaking changes", () => {
expect(semVerBump("0.0.0", [false, false, true])).toBe("0.1.0");
expect(semVerBump("0.1.0", [false, false, true])).toBe("0.2.0");
expect(semVerBump("1.1.0", [false, false, true])).toBe("2.0.0");
expect(semVerBump("2.1.0", [false, false, true])).toBe("3.0.0");
});
it("should bump patch version when there are additions", () => {
expect(semVerBump("0.1.0", [true, false, false])).toBe("0.1.1");
});

it("should bump patch version when there are modifications", () => {
expect(semVerBump("0.0.0", [false, true, false])).toBe("0.0.1");
});

it("should bump minor version when there are breaking changes", () => {
expect(semVerBump("0.0.0", [false, false, true])).toBe("0.1.0");
expect(semVerBump("0.1.0", [false, false, true])).toBe("0.2.0");
});

it("should not bump version when there are no changes", () => {
expect(semVerBump("0.0.0", [false, false, false])).toBe("0.0.0");
expect(semVerBump("0.1.0", [false, false, false])).toBe("0.1.0");
});

it("should handle multiple changes correctly", () => {
expect(semVerBump("0.1.8", [true, true, false])).toBe("0.1.9");
expect(semVerBump("0.0.3", [true, true, true])).toBe("0.1.0");
});

it("should not bump version when there are no changes", () => {
expect(semVerBump("0.0.0", [false, false, false])).toBe("0.0.0");
expect(semVerBump("0.1.0", [false, false, false])).toBe("0.1.0");
expect(semVerBump("1.1.0", [false, false, false])).toBe("1.1.0");
expect(semVerBump("2.1.0", [false, false, false])).toBe("2.1.0");
});

it("should handle multiple changes correctly", () => {
expect(semVerBump("2.1.0", [true, true, false])).toBe("2.2.0");
expect(semVerBump("2.1.0", [true, true, true])).toBe("3.0.0");
describe("when the version is equal to or greater than 1.0.0", () => {

it("should bump minor version when there are additions", () => {
expect(semVerBump("1.0.0", [true, false, false])).toBe("1.1.0");
});

it("should bump patch version when there are modifications", () => {
expect(semVerBump("1.1.0", [false, true, false])).toBe("1.1.1");
});

it("should bump major version when there are breaking changes", () => {
expect(semVerBump("1.1.0", [false, false, true])).toBe("2.0.0");
expect(semVerBump("2.1.0", [false, false, true])).toBe("3.0.0");
});

it("should not bump version when there are no changes", () => {
expect(semVerBump("1.1.0", [false, false, false])).toBe("1.1.0");
expect(semVerBump("2.1.0", [false, false, false])).toBe("2.1.0");
});

it("should handle multiple changes correctly", () => {
expect(semVerBump("2.1.0", [true, true, false])).toBe("2.2.0");
expect(semVerBump("2.1.0", [true, true, true])).toBe("3.0.0");
});
});
});
21 changes: 16 additions & 5 deletions packages/radius-toolkit/src/lib/tokens/layer-diff.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export const indexAllVariables = <T extends Pick<TokenLayers, "layers">>({
(acc, variable) => {
return acc[variable.name]
? {
...acc,
[variable.name]: `${acc[variable.name]}--${variable.value}`,
}
...acc,
[variable.name]: `${acc[variable.name]}--${variable.value}`,
}
: { ...acc, [variable.name]: variable.value };
},
{} as Record<string, string>
Expand All @@ -36,11 +36,15 @@ export const diffTokenLayers = (
export type BumpType = "minor" | "patch" | "major";
export type BumpStrategy = (version: string) => (idx: 0 | 1 | 2) => BumpType;

// the strategy is different based on whether the version number is currently under 1.0.0 or not
// under 1.0.0, the major version number is never bumped - only the minor and patch numbers
const bumpStrategies: BumpStrategy = (version) =>
semver.lt(version, "1.0.0")
? (idx) => (["minor", "patch", "patch"] as const)[idx]
: (idx) => (["major", "minor", "patch"] as const)[idx];

// bump the version number based on the changes to the tokens (see bumpStrategies above)
// when you want to do a stable release and bump the major number, you must do this manually in the package.json of your tokens package
export const semVerBump = (
version: string,
changes: [additions: boolean, modifications: boolean, breaking: boolean]
Expand All @@ -57,14 +61,21 @@ export const semVerBump = (
);
};

// semVerBump("0.0.0", [false, false, false]) /* 0.1.0 */; //?
// Expected results - semVerBump(version, [minor changes, patch changes, major changes])

// Version numbers under 1.0.0

// semVerBump("0.0.0", [false, false, false]) /* 0.0.0 */; //?
// semVerBump("0.0.0", [true, false, false]) /* 0.1.0 */; //?
// semVerBump("0.0.0", [false, true, false]) /* 0.1.0 */; //?
// semVerBump("0.0.0", [false, false, true]) /* 0.1.0 */; //?
// semVerBump("0.0.0", [false, false, true]) /* 0.0.1 */; //?
// semVerBump("0.1.0", [false, false, false]) /* 0.1.0 */; //?
// semVerBump("0.1.0", [false, false, true]) /* 0.2.0 */; //?
// semVerBump("0.1.0", [false, true, false]) /* 0.1.1 */; //?
// semVerBump("0.1.0", [true, false, false]) /* 0.1.1 */; //?

// Version numbers equal to or greater than 1.0.0

// semVerBump("1.1.0", [false, false, true]) /* 1.0.0 */; //?
// semVerBump("1.1.0", [false, true, false]) /* 1.1.1 */; //?
// semVerBump("2.1.0", [true, false, false]) /* 2.2.0 */; //?
Expand Down