Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for determining the version of buf to use from go.mod including full test for the functionality. This allows go projects to have a single source for go module versions. Also: * Fixes from lint issues in README.md. Fixes #155
- Loading branch information
Showing
13 changed files
with
10,936 additions
and
2,577 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.tmp/ | ||
.vscode/ | ||
node_modules/ | ||
coverage/ |
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 |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import * as core from "@actions/core"; | ||
import * as io from "@actions/io"; | ||
import fs from "fs"; | ||
import cp from "child_process"; | ||
import osm, { type } from "os"; | ||
import path from "path"; | ||
import * as run from "../src/run"; | ||
import * as buf from "../src/buf"; | ||
|
||
const win32Join = path.win32.join; | ||
const posixJoin = path.posix.join; | ||
|
||
//jest.setTimeout(10000); | ||
|
||
describe("buf-setup", () => { | ||
let inputs = {} as any; | ||
let os = {} as any; | ||
|
||
let inSpy: jest.SpyInstance; | ||
let platSpy: jest.SpyInstance; | ||
let archSpy: jest.SpyInstance; | ||
let joinSpy: jest.SpyInstance; | ||
let execSpy: jest.SpyInstance; | ||
let logSpy: jest.SpyInstance; | ||
let dbgSpy: jest.SpyInstance; | ||
let warnSpy: jest.SpyInstance; | ||
let existsSpy: jest.SpyInstance; | ||
let readFileSpy: jest.SpyInstance; | ||
let getSpy: jest.SpyInstance; | ||
let whichSpy: jest.SpyInstance; | ||
let cnSpy: jest.SpyInstance; | ||
let failedSpy: jest.SpyInstance; | ||
|
||
beforeAll(async () => { | ||
process.env["GITHUB_ENV"] = ""; // Stub out Environment file functionality so we can verify it writes to standard out (toolkit is backwards compatible) | ||
}, 100000); | ||
|
||
beforeEach(() => { | ||
process.env["GITHUB_PATH"] = ""; // Stub out ENV file functionality so we can verify it writes to standard out | ||
|
||
// @actions/core | ||
inputs = {}; | ||
// Defaults as per action.yml | ||
inputs["version"] = "1.26.0"; | ||
inputs["buf_domain"] = "buf.build"; | ||
inSpy = jest.spyOn(core, "getInput"); | ||
inSpy.mockImplementation((name) => inputs[name] ?? ""); | ||
failedSpy = jest.spyOn(core, "setFailed"); | ||
|
||
// buf | ||
getSpy = jest.spyOn(buf, "getBuf"); | ||
getSpy.mockImplementation( | ||
async (): Promise<string | Error> => "/usr/local" | ||
); | ||
|
||
// os | ||
os = {}; | ||
platSpy = jest.spyOn(osm, "platform"); | ||
platSpy.mockImplementation(() => os["platform"]); | ||
archSpy = jest.spyOn(osm, "arch"); | ||
|
||
// cp | ||
archSpy.mockImplementation(() => os["arch"]); | ||
execSpy = jest.spyOn(cp, "execSync"); | ||
|
||
// switch path join behaviour based on set os.platform | ||
joinSpy = jest.spyOn(path, "join"); | ||
joinSpy.mockImplementation((...paths: string[]): string => { | ||
if (os["platform"] == "win32") { | ||
return win32Join(...paths); | ||
} | ||
|
||
return posixJoin(...paths); | ||
}); | ||
|
||
// fs | ||
existsSpy = jest.spyOn(fs, "existsSync"); | ||
readFileSpy = jest.spyOn(fs, "readFileSync"); | ||
|
||
// io | ||
whichSpy = jest.spyOn(io, "which"); | ||
|
||
// writes | ||
cnSpy = jest.spyOn(process.stdout, "write"); | ||
logSpy = jest.spyOn(core, "info"); | ||
dbgSpy = jest.spyOn(core, "debug"); | ||
warnSpy = jest.spyOn(core, "warning"); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
process.exitCode = 0; // Prevent non-zero exit code from failing test. | ||
}); | ||
|
||
afterAll(async () => { | ||
jest.restoreAllMocks(); | ||
}, 100000); | ||
|
||
describe("go-version-file", () => { | ||
const goModContents = `module example.com/mymodule | ||
go 1.14 | ||
require ( | ||
example.com/othermodule v1.2.3 | ||
example.com/thismodule v1.2.3 | ||
github.com/bufbuild/buf v1.26.1 | ||
) | ||
replace example.com/thatmodule => ../thatmodule | ||
exclude example.com/thismodule v1.3.0 | ||
`; | ||
|
||
it("return version if go_version_file isn't set", async () => { | ||
existsSpy.mockImplementation(() => false); | ||
|
||
await run.run(); | ||
|
||
expect(logSpy).toHaveBeenCalledWith( | ||
`Setting up buf version "${inputs["version"]}"` | ||
); | ||
expect(logSpy).toHaveBeenCalledWith( | ||
`Successfully setup buf version ${inputs["version"]}` | ||
); | ||
}); | ||
|
||
it("reads version from go_version_file if set", async () => { | ||
inputs["go_version_file"] = "go.mod"; | ||
existsSpy.mockImplementation(() => true); | ||
readFileSpy.mockImplementation(() => Buffer.from(goModContents)); | ||
|
||
await run.run(); | ||
|
||
expect(warnSpy).toHaveBeenCalledWith( | ||
"Both version and go_version_file inputs are specified, go_version_file will be preferred" | ||
); | ||
expect(logSpy).toHaveBeenCalledWith('Setting up buf version "1.26.1"'); | ||
expect(logSpy).toHaveBeenCalledWith( | ||
"Successfully setup buf version 1.26.1" | ||
); | ||
}); | ||
|
||
it("reports failure if go_version_file doesn't exist", async () => { | ||
inputs["go_version_file"] = "go.mod"; | ||
existsSpy.mockImplementation(() => false); | ||
|
||
await run.run(); | ||
|
||
expect(warnSpy).toHaveBeenCalledWith( | ||
"Both version and go_version_file inputs are specified, go_version_file will be preferred" | ||
); | ||
expect(cnSpy).toHaveBeenCalledWith( | ||
`::error::The specified go version file: "go.mod" does not exist${osm.EOL}` | ||
); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.