-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add package.json auto version strategy
- Loading branch information
Showing
9 changed files
with
145 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@osdk/cli": patch | ||
--- | ||
|
||
Add package.json auto version strategy |
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
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
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 |
---|---|---|
|
@@ -14,63 +14,93 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { findUp } from "find-up"; | ||
import { exec } from "node:child_process"; | ||
import { promises as fsPromises } from "node:fs"; | ||
import { promisify } from "node:util"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { autoVersion } from "./autoVersion.js"; | ||
|
||
vi.mock("find-up"); | ||
vi.mock("node:child_process"); | ||
vi.mock("node:fs"); | ||
const execAsync = promisify(exec); | ||
|
||
describe("autoVersion", () => { | ||
const execMock = vi.mocked(execAsync); | ||
const execReturnValue = (out: string) => ({ stdout: out, stderr: "" }); | ||
|
||
it("should return a valid SemVer version from package.json", async () => { | ||
const validPackageJsonVersion = "1.2.3"; | ||
vi.mocked(findUp).mockResolvedValue("/path/package.json"); | ||
vi.mocked(fsPromises.readFile).mockResolvedValue( | ||
JSON.stringify({ version: validPackageJsonVersion }), | ||
); | ||
const version = await autoVersion({ | ||
type: "package-json", | ||
}); | ||
expect(version).toBe("1.2.3"); | ||
}); | ||
|
||
it("should return a valid SemVer version from git describe", async () => { | ||
const validGitVersion = "1.2.3"; | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion(); | ||
const version = await autoVersion({ | ||
type: "git-describe", | ||
}); | ||
expect(version).toBe("1.2.3"); | ||
}); | ||
|
||
it("should replace default prefix v from git describe output", async () => { | ||
const validGitVersion = "v1.2.3"; | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion(); | ||
const version = await autoVersion({ | ||
type: "git-describe", | ||
}); | ||
expect(version).toBe("1.2.3"); | ||
}); | ||
|
||
it("should replace the prefix from the found git tag", async () => { | ||
const validGitVersion = "@[email protected]"; | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion("@package@"); | ||
const version = await autoVersion({ | ||
type: "git-describe", | ||
tagPrefix: "@package@", | ||
}); | ||
expect(version).toBe("1.2.3"); | ||
}); | ||
|
||
it("should only replace the prefix if found at the start of the tag only", async () => { | ||
const validGitVersion = "1.2.3-package"; | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion("-package"); | ||
const version = await autoVersion({ | ||
type: "git-describe", | ||
tagPrefix: "@package@", | ||
}); | ||
expect(version).toBe("1.2.3-package"); | ||
}); | ||
|
||
it("should throw an error if git describe returns a non-SemVer string", async () => { | ||
const nonSemVerGitVersion = "not-semver"; | ||
execMock.mockResolvedValue(execReturnValue(nonSemVerGitVersion)); | ||
|
||
await expect(autoVersion()).rejects.toThrowError(); | ||
await expect(autoVersion({ | ||
type: "git-describe", | ||
})).rejects.toThrowError(); | ||
}); | ||
|
||
it("should throw an error if git isn't found", async () => { | ||
execMock.mockImplementation(() => { | ||
throw new Error("Command not found"); | ||
}); | ||
|
||
await expect(autoVersion()).rejects.toThrowError( | ||
await expect(autoVersion({ | ||
type: "git-describe", | ||
})).rejects.toThrowError( | ||
"git is not installed", | ||
); | ||
}); | ||
|
@@ -80,7 +110,9 @@ describe("autoVersion", () => { | |
throw new Error("fatal: not a git repository"); | ||
}); | ||
|
||
await expect(autoVersion()).rejects.toThrowError( | ||
await expect(autoVersion({ | ||
type: "git-describe", | ||
})).rejects.toThrowError( | ||
"the current directory is not a git repository", | ||
); | ||
}); | ||
|
@@ -90,7 +122,9 @@ describe("autoVersion", () => { | |
throw new Error("fatal: no names found, cannot describe anything."); | ||
}); | ||
|
||
await expect(autoVersion()).rejects.toThrowError( | ||
await expect(autoVersion({ | ||
type: "git-describe", | ||
})).rejects.toThrowError( | ||
"no matching tags were found.", | ||
); | ||
}); | ||
|
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
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