Skip to content

Commit

Permalink
Fix config overwrite on replace bug (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess committed May 30, 2023
1 parent e6aa5ea commit 6422b1f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func (a App) Init(cwd string, force bool) error {
return nil
}

// TODO: When it rewrites the config back, it does the rendered config with all
// the .Current and .Next set to the actual values
// Read the config in from scratch so it's not rendered (or make a new one)
// and then only update the version before saving back

// Major handles the major subcommand.
func (a App) Major(push, force, dryRun bool) error {
return a.bump(major, push, force, dryRun)
Expand All @@ -183,6 +188,7 @@ func (a App) Patch(push, force, dryRun bool) error {
// replaceAll is a helper that performs and reports on file replacement
// as part of bumping.
func (a App) replaceAll(current, next semver.Version, dryRun bool) error {
originalConfig := a.Cfg
if err := a.Cfg.Render(current.String(), next.String()); err != nil {
return err
}
Expand All @@ -192,9 +198,9 @@ func (a App) replaceAll(current, next semver.Version, dryRun bool) error {
}

// Also replace the Version in the config file
a.Cfg.Version = next.String()
originalConfig.Version = next.String()
if !dryRun {
if err := a.Cfg.Save(config.Filename); err != nil {
if err := originalConfig.Save(config.Filename); err != nil {
return err
}
}
Expand Down
54 changes: 54 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ func TestAppMajor(t *testing.T) {
t.Errorf("README replaced incorrectly: got %q, wanted %q", string(readme), want)
}

// Check it's replaced the config version but not the message templates etc.
cfg, err := config.Load(filepath.Join(tmp, ".tag.toml"))
if err != nil {
t.Fatalf("Could not read replaced config file: %v", err)
}

if cfg.Version != "1.0.0" {
t.Errorf("Wrong version in replaced config file. Got %s, wanted %s", cfg.Version, "1.0.0")
}

if cfg.Git.MessageTemplate != "Bump version {{.Current}} -> {{.Next}}" {
t.Errorf("Wrong message template in replaced config file. Got %s, wanted %s", cfg.Git.MessageTemplate, "Bump version {{.Current}} -> {{.Next}}")
}

if cfg.Git.TagTemplate != "v{{.Next}}" {
t.Errorf("Wrong tag template in replaced config file. Got %s, wanted %s", cfg.Git.TagTemplate, "v{{.Next}}")
}

// Check it's made the appropriate commit
gitLog := exec.Command("git", "log", "--oneline")
stdout, err := gitLog.CombinedOutput()
Expand Down Expand Up @@ -322,6 +340,24 @@ func TestAppMinor(t *testing.T) {
t.Errorf("README replaced incorrectly: got %q, wanted %q", string(readme), want)
}

// Check it's replaced the config version but not the message templates etc.
cfg, err := config.Load(filepath.Join(tmp, ".tag.toml"))
if err != nil {
t.Fatalf("Could not read replaced config file: %v", err)
}

if cfg.Version != "0.2.0" {
t.Errorf("Wrong version in replaced config file. Got %s, wanted %s", cfg.Version, "0.2.0")
}

if cfg.Git.MessageTemplate != "Bump version {{.Current}} -> {{.Next}}" {
t.Errorf("Wrong message template in replaced config file. Got %s, wanted %s", cfg.Git.MessageTemplate, "Bump version {{.Current}} -> {{.Next}}")
}

if cfg.Git.TagTemplate != "v{{.Next}}" {
t.Errorf("Wrong tag template in replaced config file. Got %s, wanted %s", cfg.Git.TagTemplate, "v{{.Next}}")
}

// Check it's made the appropriate commit
gitLog := exec.Command("git", "log", "--oneline")
stdout, err := gitLog.CombinedOutput()
Expand Down Expand Up @@ -444,6 +480,24 @@ func TestAppPatch(t *testing.T) {
t.Errorf("README replaced incorrectly: got %q, wanted %q", string(readme), want)
}

// Check it's replaced the config version but not the message templates etc.
cfg, err := config.Load(filepath.Join(tmp, ".tag.toml"))
if err != nil {
t.Fatalf("Could not read replaced config file: %v", err)
}

if cfg.Version != "0.1.1" {
t.Errorf("Wrong version in replaced config file. Got %s, wanted %s", cfg.Version, "0.1.1")
}

if cfg.Git.MessageTemplate != "Bump version {{.Current}} -> {{.Next}}" {
t.Errorf("Wrong message template in replaced config file. Got %s, wanted %s", cfg.Git.MessageTemplate, "Bump version {{.Current}} -> {{.Next}}")
}

if cfg.Git.TagTemplate != "v{{.Next}}" {
t.Errorf("Wrong tag template in replaced config file. Got %s, wanted %s", cfg.Git.TagTemplate, "v{{.Next}}")
}

// Check it's made the appropriate commit
gitLog := exec.Command("git", "log", "--oneline")
stdout, err := gitLog.CombinedOutput()
Expand Down

0 comments on commit 6422b1f

Please sign in to comment.