-
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.
- Loading branch information
1 parent
cb13faa
commit dee086c
Showing
5 changed files
with
54 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,98 +14,83 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { execSync } from "node:child_process"; | ||
import { exec } from "node:child_process"; | ||
import { promisify } from "node:util"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { autoVersion } from "./autoVersion.js"; | ||
|
||
vi.mock("node:child_process"); | ||
const execAsync = promisify(exec); | ||
|
||
describe("autoVersion", () => { | ||
const execSyncMock = vi.mocked(execSync); | ||
const execMock = vi.mocked(execAsync); | ||
const execReturnValue = (out: string) => ({ stdout: out, stderr: "" }); | ||
|
||
it("should return a valid SemVer version from git describe", () => { | ||
it("should return a valid SemVer version from git describe", async () => { | ||
const validGitVersion = "1.2.3"; | ||
execSyncMock.mockReturnValue(validGitVersion); | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = autoVersion(); | ||
const version = await autoVersion(); | ||
expect(version).toBe("1.2.3"); | ||
|
||
expect(execSyncMock).toHaveBeenCalledWith( | ||
"git describe --tags --first-parent --dirty", | ||
{ encoding: "utf8" }, | ||
); | ||
}); | ||
|
||
it("should replace default prefix v from git describe output", () => { | ||
it("should replace default prefix v from git describe output", async () => { | ||
const validGitVersion = "v1.2.3"; | ||
execSyncMock.mockReturnValue(validGitVersion); | ||
const version = autoVersion(); | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion(); | ||
expect(version).toBe("1.2.3"); | ||
expect(execSyncMock).toHaveBeenCalledWith( | ||
"git describe --tags --first-parent --dirty", | ||
{ encoding: "utf8" }, | ||
); | ||
}); | ||
|
||
it("should replace the prefix from the found git tag", () => { | ||
it("should replace the prefix from the found git tag", async () => { | ||
const validGitVersion = "@[email protected]"; | ||
execSyncMock.mockReturnValue(validGitVersion); | ||
|
||
const version = autoVersion("@package@"); | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion("@package@"); | ||
expect(version).toBe("1.2.3"); | ||
expect(execSyncMock).toHaveBeenCalledWith( | ||
"git describe --tags --first-parent --dirty --match=\"@package@*\"", | ||
{ encoding: "utf8" }, | ||
); | ||
}); | ||
|
||
it("should only replace the prefix if found at the start of the tag only", () => { | ||
it("should only replace the prefix if found at the start of the tag only", async () => { | ||
const validGitVersion = "1.2.3-package"; | ||
execSyncMock.mockReturnValue(validGitVersion); | ||
|
||
const version = autoVersion("-package"); | ||
execMock.mockResolvedValue(execReturnValue(validGitVersion)); | ||
|
||
const version = await autoVersion("-package"); | ||
expect(version).toBe("1.2.3-package"); | ||
expect(execSyncMock).toHaveBeenCalledWith( | ||
"git describe --tags --first-parent --dirty --match=\"-package*\"", | ||
{ encoding: "utf8" }, | ||
); | ||
}); | ||
|
||
it("should throw an error if git describe returns a non-SemVer string", () => { | ||
it("should throw an error if git describe returns a non-SemVer string", async () => { | ||
const nonSemVerGitVersion = "not-semver"; | ||
execSyncMock.mockReturnValue(nonSemVerGitVersion); | ||
expect(() => autoVersion()).toThrowError(); | ||
execMock.mockResolvedValue(execReturnValue(nonSemVerGitVersion)); | ||
|
||
await expect(autoVersion()).rejects.toThrowError(); | ||
}); | ||
|
||
it("should throw an error if git isn't found", () => { | ||
execSyncMock.mockImplementation(() => { | ||
it("should throw an error if git isn't found", async () => { | ||
execMock.mockImplementation(() => { | ||
throw new Error("Command not found"); | ||
}); | ||
|
||
expect(() => autoVersion()).toThrowError( | ||
await expect(autoVersion()).rejects.toThrowError( | ||
"Unable to determine the version using git-describe as git is not installed", | ||
); | ||
}); | ||
|
||
it("should throw an error if git isn't found", () => { | ||
execSyncMock.mockImplementation(() => { | ||
it("should throw an error if the current directory is not a git repository", async () => { | ||
execMock.mockImplementation(() => { | ||
throw new Error("fatal: not a git repository"); | ||
}); | ||
|
||
expect(() => autoVersion()).toThrowError( | ||
await expect(autoVersion()).rejects.toThrowError( | ||
"Unable to determine the version using git-describe as the current directory is not a git repository", | ||
); | ||
}); | ||
|
||
it("should throw an error if git isn't found", () => { | ||
execSyncMock.mockImplementation(() => { | ||
it("should throw an error if no git tags are found", async () => { | ||
execMock.mockImplementation(() => { | ||
throw new Error("fatal: no names found, cannot describe anything."); | ||
}); | ||
|
||
expect(() => autoVersion()).toThrowError( | ||
await expect(autoVersion()).rejects.toThrowError( | ||
"Unable to determine the version using git-describe as no 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