changelog generator & diff tool stormlightlabs.github.io/git-storm/
changelog changeset markdown golang git
at main 1.9 kB view raw
1package main 2 3import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9) 10 11const sampleChangelog = `# Changelog 12 13## [Unreleased] 14 15## [1.2.3] - 2024-01-01 16### Added 17- Initial release 18` 19 20func TestBumpCommandPrintsVersion(t *testing.T) { 21 dir := t.TempDir() 22 writeFile(t, filepath.Join(dir, "CHANGELOG.md"), sampleChangelog) 23 24 oldRepo := repoPath 25 oldOutput := output 26 repoPath = dir 27 output = "CHANGELOG.md" 28 t.Cleanup(func() { 29 repoPath = oldRepo 30 output = oldOutput 31 }) 32 33 cmd := bumpCmd() 34 cmd.SetArgs([]string{"--bump", "minor"}) 35 var out bytes.Buffer 36 cmd.SetOut(&out) 37 cmd.SetErr(&out) 38 39 if err := cmd.Execute(); err != nil { 40 t.Fatalf("bump command failed: %v", err) 41 } 42 43 if !strings.Contains(out.String(), "1.3.0") { 44 t.Fatalf("expected output to contain 1.3.0, got %q", out.String()) 45 } 46} 47 48func TestBumpCommandUpdatesPackageJSON(t *testing.T) { 49 dir := t.TempDir() 50 writeFile(t, filepath.Join(dir, "CHANGELOG.md"), sampleChangelog) 51 writeFile(t, filepath.Join(dir, "package.json"), `{"name":"demo","version":"1.2.3"}`) 52 53 oldRepo := repoPath 54 oldOutput := output 55 repoPath = dir 56 output = "CHANGELOG.md" 57 t.Cleanup(func() { 58 repoPath = oldRepo 59 output = oldOutput 60 }) 61 62 cmd := bumpCmd() 63 cmd.SetArgs([]string{"--bump", "patch", "--toolchain", "package.json"}) 64 var out bytes.Buffer 65 cmd.SetOut(&out) 66 cmd.SetErr(&out) 67 68 if err := cmd.Execute(); err != nil { 69 t.Fatalf("bump command failed: %v", err) 70 } 71 72 contents, err := os.ReadFile(filepath.Join(dir, "package.json")) 73 if err != nil { 74 t.Fatalf("failed to read package.json: %v", err) 75 } 76 77 if !strings.Contains(string(contents), "1.2.4") { 78 t.Fatalf("expected package.json to contain bumped version, got %s", contents) 79 } 80} 81 82func writeFile(t *testing.T, path, contents string) { 83 if err := os.WriteFile(path, []byte(contents), 0o644); err != nil { 84 t.Fatalf("failed to write %s: %v", path, err) 85 } 86}