generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check the installed valgrind version
- Loading branch information
Showing
5 changed files
with
152 additions
and
14 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,51 @@ | ||
import {exec} from "@actions/exec"; | ||
|
||
const MIN_VALGRIND_VERSION = [3, 16, 0]; // Below 3.16.0, --collect-systime=nsec is not supported | ||
|
||
export const checkValgrindVersion = async (): Promise<void> => { | ||
let versionOutput = ""; | ||
try { | ||
await exec("valgrind", ["--version"], { | ||
silent: true, | ||
listeners: { | ||
stdout: (data: Buffer) => { | ||
versionOutput += data.toString(); | ||
}, | ||
}, | ||
}); | ||
} catch (error) { | ||
throw new Error("valgrind is not installed"); | ||
} | ||
versionOutput = versionOutput.trim(); | ||
const version = parseValgrindVersion(versionOutput); | ||
if (!isVersionValid(version)) { | ||
throw new Error( | ||
`valgrind version ${version.join( | ||
"." | ||
)} is not supported, please upgrade to at least ${MIN_VALGRIND_VERSION.join( | ||
"." | ||
)}. Upgrading to Ubuntu 22.04+ will allow you to have a valid version.` | ||
); | ||
} | ||
}; | ||
|
||
export const parseValgrindVersion = (versionOutput: string): number[] => { | ||
// versionOutput is something like "valgrind-3.16.0" | ||
const versionString = versionOutput.match(/valgrind-(.*)/)?.[1]; | ||
if (!versionString) { | ||
throw new Error("Failed to get valgrind version"); | ||
} | ||
const version = versionString.split(".").map(Number); | ||
if (version.length !== 3) { | ||
throw new Error(`valgrind version ${versionString} is not valid`); | ||
} | ||
return version; | ||
}; | ||
|
||
export const isVersionValid = (version: number[]): boolean => | ||
version[0] >= MIN_VALGRIND_VERSION[0] && | ||
(version[0] > MIN_VALGRIND_VERSION[0] || | ||
version[1] >= MIN_VALGRIND_VERSION[1]) && | ||
(version[0] > MIN_VALGRIND_VERSION[0] || | ||
version[1] > MIN_VALGRIND_VERSION[1] || | ||
version[2] >= MIN_VALGRIND_VERSION[2]); |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
isVersionValid, | ||
parseValgrindVersion, | ||
checkValgrindVersion, | ||
} from "../src/helpers/valgrind"; | ||
import {exec} from "@actions/exec"; | ||
|
||
jest.mock("@actions/exec"); | ||
const execMock = exec as unknown as jest.Mock; | ||
|
||
describe("valgrind helpers", () => { | ||
describe("isVersionValid", () => { | ||
test("should return true when version is greater than or equal to minimum version", () => { | ||
expect(isVersionValid([3, 16, 0])).toBe(true); | ||
expect(isVersionValid([3, 16, 1])).toBe(true); | ||
expect(isVersionValid([3, 17, 0])).toBe(true); | ||
expect(isVersionValid([4, 0, 0])).toBe(true); | ||
}); | ||
|
||
test("should return false when version is less than minimum version", () => { | ||
expect(isVersionValid([3, 15, 0])).toBe(false); | ||
expect(isVersionValid([2, 0, 0])).toBe(false); | ||
expect(isVersionValid([3, 15, 5])).toBe(false); | ||
expect(isVersionValid([3, 14, 0])).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("parseValgrindVersion", () => { | ||
test("should parse version string correctly", () => { | ||
const versionString = "valgrind-3.16.0"; | ||
const version = parseValgrindVersion(versionString); | ||
expect(version).toEqual([3, 16, 0]); | ||
}); | ||
|
||
test("should throw an error if version string is not found", () => { | ||
const versionString = "some random string"; | ||
expect(() => parseValgrindVersion(versionString)).toThrow( | ||
"Failed to get valgrind version" | ||
); | ||
}); | ||
|
||
test("should throw an error if version string is invalid", () => { | ||
const versionString = "valgrind-3.16.0.1"; | ||
expect(() => parseValgrindVersion(versionString)).toThrow( | ||
"valgrind version 3.16.0.1 is not valid" | ||
); | ||
}); | ||
}); | ||
describe("checkValgrindVersion", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
test("should throw an error if valgrind is not installed", async () => { | ||
execMock.mockRejectedValueOnce( | ||
new Error("Error: Unable to locate executable file") | ||
); | ||
await expect(checkValgrindVersion()).rejects.toThrow( | ||
"valgrind is not installed" | ||
); | ||
}); | ||
test("should throw an error if parseValgrindVersion returns a non-version value", async () => { | ||
execMock.mockImplementationOnce(async (_, __, {listeners}) => { | ||
listeners.stdout?.(Buffer.from("valgrind-foo")); | ||
}); | ||
await expect(checkValgrindVersion()).rejects.toThrow( | ||
"valgrind version foo is not valid" | ||
); | ||
}); | ||
test("should throw an error if installed valgrind version is too old", async () => { | ||
execMock.mockImplementationOnce(async (_, __, {listeners}) => { | ||
listeners.stdout?.(Buffer.from("valgrind-3.15.0")); | ||
}); | ||
await expect(checkValgrindVersion()).rejects.toThrow( | ||
"valgrind version 3.15.0 is not supported, please upgrade to at least 3.16.0. Upgrading to Ubuntu 22.04+ will allow you to have a valid version." | ||
); | ||
}); | ||
test("should not throw an error when the Valgrind version is valid", async () => { | ||
execMock.mockImplementationOnce(async (_, __, {listeners}) => { | ||
listeners.stdout?.(Buffer.from("valgrind-3.16.0")); | ||
}); | ||
await expect(checkValgrindVersion()).resolves.not.toThrow(); | ||
}); | ||
}); | ||
}); |