Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion version_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io"
"path/filepath"
"strings"

goversion "github.com/bcomnes/goversion/v2/pkg"
Expand Down Expand Up @@ -67,8 +68,12 @@ func runVersionCommand(arguments []string, output, errorOutput io.Writer) int {
return 1
}

displayWorkDir, err := filepath.Abs(*workDir)
if err != nil {
fmt.Fprintln(errorOutput, "Error: resolve work directory:", err)
return 1
}
var meta goversion.VersionMeta
var err error
options := goversion.VersionOptions{
WorkDir: *workDir,
VersionFile: *versionFile,
Expand Down Expand Up @@ -103,6 +108,9 @@ func runVersionCommand(arguments []string, output, errorOutput io.Writer) int {
fmt.Fprintln(output, "Files updated:")
}
for _, file := range meta.UpdatedFiles {
if relative, err := filepath.Rel(displayWorkDir, file); err == nil {
file = relative
}
fmt.Fprintf(output, " %s\n", file)
}
}
Expand Down
2 changes: 1 addition & 1 deletion version_skip_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestVersionSkipDocsWorkdir(t *testing.T) {
}
for _, name := range []string{"version.go", "go.mod", "consumer.go", "README.md", "doc.go"} {
if !tc.skipDocs || (name != "README.md" && name != "doc.go") {
expected = append(expected, filepath.Join(dir, name))
expected = append(expected, name)
}
}
slices.Sort(listed)
Expand Down
90 changes: 90 additions & 0 deletions version_workdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,100 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)

func TestVersionCommandRelativeFileLists(t *testing.T) {
for _, workdir := range []string{"default", "relative", "absolute"} {
for _, dry := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/dry=%t", workdir, dry), func(t *testing.T) {
root := t.TempDir()
moduleDir := filepath.Join(root, "tools", "widget")
before := map[string]string{
"tools/widget/go.mod": "module example.com/acme/repo/tools/widget\n\ngo 1.23\n",
"tools/widget/version.go": "package widget\n\nvar Version = \"1.2.3\"\n",
"tools/widget/docs/version.md": "Version 1.2.3\n",
"docs/widget-install.md": "Install version 1.2.3\n",
"docs/extra.md": "Release 1.2.3\n",
}
for name, content := range before {
path := filepath.Join(root, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
for _, args := range [][]string{
{"init"}, {"config", "user.email", "test@example.com"},
{"config", "user.name", "Test User"}, {"config", "commit.gpgsign", "false"},
{"config", "tag.gpgsign", "false"}, {"add", "."}, {"commit", "-m", "initial"},
} {
versionSkipDocsGit(t, root, args...)
}
head := versionSkipDocsGit(t, root, "rev-parse", "HEAD")
var args []string
switch workdir {
case "default":
t.Chdir(moduleDir)
case "relative":
t.Chdir(root)
args = append(args, "-workdir", filepath.Join("tools", "widget"))
case "absolute":
t.Chdir(root)
args = append(args, "-workdir", moduleDir)
}
args = append(args, "-bump-file", "docs/version.md", "-bump-file", "../../docs/widget-install.md",
"-bump-file", filepath.Join(root, "docs", "extra.md"))
heading := "Files updated:\n"
if dry {
args = append(args, "-dry")
heading = "Files that would be updated:\n"
}
args = append(args, "major")
var output, errorOutput bytes.Buffer
if code := runVersionCommand(args, &output, &errorOutput); code != 0 {
t.Fatalf("runVersionCommand returned %d: %s", code, errorOutput.String())
}
_, files, found := strings.Cut(output.String(), heading)
if !found {
t.Fatalf("output missing %q:\n%s", heading, output.String())
}
listed := strings.Split(strings.TrimSuffix(files, "\n"), "\n")
want := []string{"version.go", "go.mod", "docs/version.md", "../../docs/widget-install.md", "../../docs/extra.md"}
for i, path := range want {
want[i] = " " + filepath.FromSlash(path)
}
slices.Sort(listed)
slices.Sort(want)
if !slices.Equal(listed, want) {
t.Errorf("file list = %q; want %q", listed, want)
}
if dry {
for name, content := range before {
got, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(name)))
if err != nil {
t.Fatal(err)
}
if string(got) != content {
t.Errorf("dry run changed %s", name)
}
}
if got := versionSkipDocsGit(t, root, "rev-parse", "HEAD"); got != head {
t.Errorf("dry run changed HEAD: %s -> %s", head, got)
}
}
if status := versionSkipDocsGit(t, root, "status", "--porcelain"); status != "" {
t.Errorf("working tree is dirty: %s", status)
}
})
}
}
}

func TestVersionCommandWorkdirDryRun(t *testing.T) {
root := t.TempDir()
moduleDir := filepath.Join(root, "go")
Expand Down
Loading