| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/miniscruff/changie/core" |
| 8 |
"github.com/miniscruff/changie/then" |
| 9 |
) |
| 10 |
|
| 11 |
func latestConfig() *core.Config { |
| 12 |
return &core.Config{ |
| 13 |
ChangesDir: "chgs", |
| 14 |
UnreleasedDir: "unrel", |
| 15 |
HeaderPath: "head.tpl.md", |
| 16 |
ChangelogPath: "changelog.md", |
| 17 |
VersionExt: "md", |
| 18 |
VersionFormat: "", |
| 19 |
KindFormat: "", |
| 20 |
ChangeFormat: "", |
| 21 |
Kinds: []core.KindConfig{}, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func TestLatestVersionEchosLatestVersion(t *testing.T) { |
| 26 |
cfg := latestConfig() |
| 27 |
then.WithTempDirConfig(t, cfg) |
| 28 |
|
| 29 |
then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md") |
| 30 |
then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md") |
| 31 |
then.CreateFile(t, cfg.ChangesDir, "v0.2.0-rc1.md") |
| 32 |
then.CreateFile(t, cfg.ChangesDir, "head.tpl.md") |
| 33 |
|
| 34 |
cmd := NewLatest() |
| 35 |
|
| 36 |
builder := strings.Builder{} |
| 37 |
cmd.SetOut(&builder) |
| 38 |
|
| 39 |
err := cmd.Run(cmd.Command, nil) |
| 40 |
then.Nil(t, err) |
| 41 |
then.Equals(t, "v0.2.0-rc1", builder.String()) |
| 42 |
} |
| 43 |
|
| 44 |
func TestLatestEchoLatestNonPrerelease(t *testing.T) { |
| 45 |
cfg := latestConfig() |
| 46 |
then.WithTempDirConfig(t, cfg) |
| 47 |
|
| 48 |
builder := strings.Builder{} |
| 49 |
|
| 50 |
then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md") |
| 51 |
then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md") |
| 52 |
then.CreateFile(t, cfg.ChangesDir, "v0.2.0-rc1.md") |
| 53 |
then.CreateFile(t, cfg.ChangesDir, "head.tpl.md") |
| 54 |
|
| 55 |
cmd := NewLatest() |
| 56 |
cmd.SkipPrereleases = true |
| 57 |
cmd.SetOut(&builder) |
| 58 |
|
| 59 |
err := cmd.Run(cmd.Command, nil) |
| 60 |
then.Nil(t, err) |
| 61 |
then.Equals(t, "v0.1.0", builder.String()) |
| 62 |
} |
| 63 |
|
| 64 |
func TestLatestWithoutPrefix(t *testing.T) { |
| 65 |
cfg := latestConfig() |
| 66 |
then.WithTempDirConfig(t, cfg) |
| 67 |
|
| 68 |
builder := strings.Builder{} |
| 69 |
|
| 70 |
then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md") |
| 71 |
then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md") |
| 72 |
then.CreateFile(t, cfg.ChangesDir, "head.tpl.md") |
| 73 |
|
| 74 |
cmd := NewLatest() |
| 75 |
cmd.RemovePrefix = true |
| 76 |
cmd.SetOut(&builder) |
| 77 |
|
| 78 |
err := cmd.Run(cmd.Command, nil) |
| 79 |
then.Nil(t, err) |
| 80 |
then.Equals(t, "0.1.0", builder.String()) |
| 81 |
} |
| 82 |
|
| 83 |
func TestLatestVersionWithProject(t *testing.T) { |
| 84 |
cfg := latestConfig() |
| 85 |
cfg.Projects = []core.ProjectConfig{ |
| 86 |
{ |
| 87 |
Label: "r project", |
| 88 |
Key: "r", |
| 89 |
}, |
| 90 |
} |
| 91 |
cfg.ProjectsVersionSeparator = "#" |
| 92 |
then.WithTempDirConfig(t, cfg) |
| 93 |
|
| 94 |
then.CreateFile(t, cfg.ChangesDir, "r", "v0.0.1.md") |
| 95 |
then.CreateFile(t, cfg.ChangesDir, "r", "v0.1.0.md") |
| 96 |
then.CreateFile(t, cfg.ChangesDir, "r", "v0.2.0-rc1.md") |
| 97 |
then.CreateFile(t, cfg.ChangesDir, "r", "head.tpl.md") |
| 98 |
|
| 99 |
builder := strings.Builder{} |
| 100 |
cmd := NewLatest() |
| 101 |
cmd.Project = "r" |
| 102 |
|
| 103 |
cmd.SetOut(&builder) |
| 104 |
|
| 105 |
err := cmd.Run(cmd.Command, nil) |
| 106 |
then.Nil(t, err) |
| 107 |
then.Equals(t, "r#v0.2.0-rc1", builder.String()) |
| 108 |
} |
| 109 |
|
| 110 |
func TestLatestVersionWithBadProject(t *testing.T) { |
| 111 |
cfg := latestConfig() |
| 112 |
cfg.Projects = []core.ProjectConfig{ |
| 113 |
{ |
| 114 |
Label: "r project", |
| 115 |
Key: "r", |
| 116 |
}, |
| 117 |
} |
| 118 |
cfg.ProjectsVersionSeparator = "#" |
| 119 |
then.WithTempDirConfig(t, cfg) |
| 120 |
|
| 121 |
then.CreateFile(t, cfg.ChangesDir, "r", "v0.0.1.md") |
| 122 |
then.CreateFile(t, cfg.ChangesDir, "r", "v0.1.0.md") |
| 123 |
then.CreateFile(t, cfg.ChangesDir, "r", "v0.2.0-rc1.md") |
| 124 |
then.CreateFile(t, cfg.ChangesDir, "r", "head.tpl.md") |
| 125 |
|
| 126 |
cmd := NewLatest() |
| 127 |
cmd.Project = "missing_project_again" |
| 128 |
|
| 129 |
builder := strings.Builder{} |
| 130 |
cmd.SetOut(&builder) |
| 131 |
|
| 132 |
err := cmd.Run(cmd.Command, nil) |
| 133 |
then.NotNil(t, err) |
| 134 |
} |
| 135 |
|
| 136 |
func TestErrorLatestBadConfig(t *testing.T) { |
| 137 |
then.WithTempDir(t) |
| 138 |
|
| 139 |
cmd := NewLatest() |
| 140 |
err := cmd.Run(cmd.Command, nil) |
| 141 |
then.NotNil(t, err) |
| 142 |
} |
| 143 |
|
| 144 |
func TestErrorLatestBadWrite(t *testing.T) { |
| 145 |
cfg := latestConfig() |
| 146 |
then.WithTempDirConfig(t, cfg) |
| 147 |
|
| 148 |
w := then.NewErrWriter() |
| 149 |
|
| 150 |
then.CreateFile(t, cfg.ChangesDir, "v0.0.1.md") |
| 151 |
then.CreateFile(t, cfg.ChangesDir, "v0.1.0.md") |
| 152 |
|
| 153 |
cmd := NewLatest() |
| 154 |
cmd.SetOut(w) |
| 155 |
w.Raised(t, cmd.Run(cmd.Command, nil)) |
| 156 |
} |