-
Notifications
You must be signed in to change notification settings - Fork 614
Resolves #3088: version
sub-command expected behaviour documentation and testing
#3447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
haydentherapper
merged 7 commits into
sigstore:main
from
AppleGamer22:version-tests-and-json-fmt
Dec 20, 2023
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ca65a05
expected behaviour docs of the command sub-version
AppleGamer22 f335fe0
initial version stdout testing
AppleGamer22 11434a4
more verbose version stdout test
AppleGamer22 d047aff
resolves comments of PR 3447
AppleGamer22 1ef17f5
moved version docs to root level md file
AppleGamer22 168ba36
added missing new line in CLI.md
AppleGamer22 e7175e6
golangci linting fix
AppleGamer22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,99 @@ | ||
// Copyright 2023 The Sigstore Authors. | ||
// | ||
// 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. | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/release-utils/version" | ||
) | ||
|
||
// const cosignDescription = "cosign: A tool for Container Signing, Verification and Storage in an OCI registry." | ||
|
||
var ( | ||
expectedVersionInfo = version.GetVersionInfo() | ||
reGitVersion = regexp.MustCompile(fmt.Sprintf("\nGitVersion:\\s+%s\n", expectedVersionInfo.GitVersion)) | ||
reGitCommmit = regexp.MustCompile(fmt.Sprintf("GitCommit:\\s+%s\n", expectedVersionInfo.GitCommit)) | ||
reBuildDate = regexp.MustCompile(fmt.Sprintf("BuildDate:\\s+%s\n", expectedVersionInfo.BuildDate)) | ||
reGoVersion = regexp.MustCompile(fmt.Sprintf("GoVersion:\\s+%s\n", expectedVersionInfo.GoVersion)) | ||
reCompiler = regexp.MustCompile(fmt.Sprintf("Compiler:\\s+%s\n", expectedVersionInfo.Compiler)) | ||
rePlatform = regexp.MustCompile(fmt.Sprintf("Platform:\\s+%s\n", expectedVersionInfo.Platform)) | ||
) | ||
|
||
func getVersionSTDOUT(json bool) (bytes.Buffer, error) { | ||
command := New() | ||
if json { | ||
command.SetArgs([]string{"version", "--json"}) | ||
} else { | ||
command.SetArgs([]string{"version"}) | ||
|
||
} | ||
// testing approach inspired by https://github.com/zenizh/go-capturer/blob/master/main.go | ||
reader, writer, err := os.Pipe() | ||
if err != nil { | ||
return bytes.Buffer{}, errors.New("failed to create a pipe for testing os.Stdout") | ||
} | ||
stdout := os.Stdout | ||
os.Stdout = writer | ||
err = command.Execute() | ||
os.Stdout = stdout | ||
if err != nil { | ||
return bytes.Buffer{}, errors.New("version is expected to run with a") | ||
} | ||
writer.Close() | ||
|
||
var buffer bytes.Buffer | ||
_, err = io.Copy(&buffer, reader) | ||
return buffer, err | ||
} | ||
|
||
func testVersionASCII(t *testing.T) { | ||
buffer, err := getVersionSTDOUT(false) | ||
output := buffer.String() | ||
assert.NoError(t, err) | ||
assert.Regexp(t, reGitVersion, output, "output doesn't contain the Git version tag") | ||
assert.Regexp(t, reGitCommmit, output, "output doesn't contain the Git commit hash") | ||
assert.Regexp(t, reBuildDate, output, "output doesn't contain the build date") | ||
assert.Regexp(t, reGoVersion, output, "output doesn't contain the Go version") | ||
assert.Regexp(t, reCompiler, output, "output doesn't contain the compiler name") | ||
assert.Regexp(t, rePlatform, output, "output doesn't contain the platform name") | ||
} | ||
|
||
func testVersionJSON(t *testing.T) { | ||
buffer, err := getVersionSTDOUT(true) | ||
assert.NoError(t, err) | ||
output := buffer.Bytes() | ||
var actualVersionInfo version.Info | ||
err = json.Unmarshal(output, &actualVersionInfo) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedVersionInfo.GitVersion, actualVersionInfo.GitVersion) | ||
assert.Equal(t, expectedVersionInfo.GitCommit, actualVersionInfo.GitCommit) | ||
assert.Equal(t, expectedVersionInfo.BuildDate, actualVersionInfo.BuildDate) | ||
assert.Equal(t, expectedVersionInfo.GoVersion, actualVersionInfo.GoVersion) | ||
assert.Equal(t, expectedVersionInfo.Compiler, actualVersionInfo.Compiler) | ||
assert.Equal(t, expectedVersionInfo.Platform, actualVersionInfo.Platform) | ||
} | ||
|
||
func TestVersionOutput(t *testing.T) { | ||
t.Run("ASCII", testVersionASCII) | ||
t.Run("JSON", testVersionJSON) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.