-
Notifications
You must be signed in to change notification settings - Fork 24
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: * Fix lint issues in README.md. Fixes #155
- Loading branch information
Showing
14 changed files
with
11,013 additions
and
2,575 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,194 @@ | ||
// Copyright 2020-2022 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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 writeSpy: jest.SpyInstance; | ||
let failedSpy: jest.SpyInstance; | ||
|
||
beforeAll(async () => { | ||
// Stub out Environment file functionality so we can verify it writes to | ||
// standard out (toolkit is backwards compatible). | ||
process.env["GITHUB_ENV"] = ""; | ||
}, 100000); | ||
|
||
beforeEach(() => { | ||
// Stub out ENV file functionality so we can verify it writes to standard out. | ||
process.env["GITHUB_PATH"] = ""; | ||
|
||
// @actions/core | ||
inputs = {}; | ||
// Defaults as per action.yml | ||
inputs["version"] = "1.26.0"; | ||
inputs["buf_domain"] = "buf.build"; | ||
logSpy = jest.spyOn(core, "info"); | ||
dbgSpy = jest.spyOn(core, "debug"); | ||
warnSpy = jest.spyOn(core, "warning"); | ||
inSpy = jest.spyOn(core, "getInput"); | ||
inSpy.mockImplementation((name) => inputs[name] ?? ""); | ||
failedSpy = jest.spyOn(core, "setFailed"); | ||
|
||
// buf methods. | ||
getSpy = jest.spyOn(buf, "getBuf"); | ||
getSpy.mockImplementation( | ||
async (): Promise<string | Error> => "/usr/local" | ||
); | ||
|
||
// os methods. | ||
os = {}; | ||
platSpy = jest.spyOn(osm, "platform"); | ||
platSpy.mockImplementation(() => os["platform"]); | ||
archSpy = jest.spyOn(osm, "arch"); | ||
archSpy.mockImplementation(() => os["arch"]); | ||
|
||
// cp methods. | ||
execSpy = jest.spyOn(cp, "execSync"); | ||
|
||
// path methods. | ||
|
||
joinSpy = jest.spyOn(path, "join"); | ||
joinSpy.mockImplementation((...paths: string[]): string => { | ||
// Switch path join behaviour based on set os.platform. | ||
if (os["platform"] == "win32") { | ||
return win32Join(...paths); | ||
} | ||
|
||
return posixJoin(...paths); | ||
}); | ||
|
||
// fs methods. | ||
existsSpy = jest.spyOn(fs, "existsSync"); | ||
readFileSpy = jest.spyOn(fs, "readFileSync"); | ||
|
||
// io methods. | ||
whichSpy = jest.spyOn(io, "which"); | ||
|
||
// process methods. | ||
writeSpy = jest.spyOn(process.stdout, "write"); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
// Prevent non-zero exit code as set by core.setFailed from failing tests. | ||
process.exitCode = 0; | ||
}); | ||
|
||
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 | ||
`; | ||
const versionContents = `1.26.2`; | ||
|
||
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("parses go.mod format if go_version_file contains go.mod basename", 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("returns contents if go_version_file is set to non go.mod filename", async () => { | ||
inputs["go_version_file"] = "buf.version"; | ||
existsSpy.mockImplementation(() => true); | ||
readFileSpy.mockImplementation(() => Buffer.from(versionContents)); | ||
|
||
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.2"'); | ||
expect(logSpy).toHaveBeenCalledWith( | ||
"Successfully setup buf version 1.26.2" | ||
); | ||
}); | ||
|
||
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(writeSpy).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.