| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/miniscruff/changie/core" |
| 10 |
"github.com/miniscruff/changie/then" |
| 11 |
) |
| 12 |
|
| 13 |
func mergeTestConfig() *core.Config { |
| 14 |
return &core.Config{ |
| 15 |
ChangesDir: "news", |
| 16 |
UnreleasedDir: "future", |
| 17 |
HeaderPath: "header.rst", |
| 18 |
ChangelogPath: "news.md", |
| 19 |
VersionExt: "md", |
| 20 |
VersionFormat: "## {{.Version}}", |
| 21 |
KindFormat: "### {{.Kind}}", |
| 22 |
ChangeFormat: "* {{.Body}}", |
| 23 |
Kinds: []core.KindConfig{ |
| 24 |
{Label: "Added"}, |
| 25 |
{Label: "Removed"}, |
| 26 |
{Label: "Other"}, |
| 27 |
}, |
| 28 |
Newlines: core.NewlinesConfig{ |
| 29 |
// BeforeVersion: 1, |
| 30 |
// AfterVersion: 1, |
| 31 |
AfterChange: 1, |
| 32 |
BeforeChangelogVersion: 0, |
| 33 |
// AfterChangelogVersion: 1, |
| 34 |
}, |
| 35 |
Replacements: []core.Replacement{ |
| 36 |
{ |
| 37 |
Path: "replace.json", |
| 38 |
Find: ` "version": ".*",`, |
| 39 |
Replace: ` "version": "{{.VersionNoPrefix}}",`, |
| 40 |
}, |
| 41 |
}, |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestMergeVersionsSuccessfully(t *testing.T) { |
| 46 |
cfg := mergeTestConfig() |
| 47 |
cfg.HeaderPath = "" |
| 48 |
cfg.Replacements = nil |
| 49 |
then.WithTempDirConfig(t, cfg) |
| 50 |
|
| 51 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 52 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "v0.2.0.md") |
| 53 |
|
| 54 |
cmd := NewMerge(core.NewTemplateCache()) |
| 55 |
err := cmd.Run(cmd.Command, nil) |
| 56 |
then.Nil(t, err) |
| 57 |
|
| 58 |
changeContents := `second version |
| 59 |
first version |
| 60 |
` |
| 61 |
then.FileContents(t, changeContents, "news.md") |
| 62 |
} |
| 63 |
|
| 64 |
func TestMergeVersionsSuccessfullyWithProject(t *testing.T) { |
| 65 |
cfg := mergeTestConfig() |
| 66 |
cfg.HeaderPath = "" |
| 67 |
cfg.Replacements = nil |
| 68 |
cfg.Projects = []core.ProjectConfig{ |
| 69 |
{ |
| 70 |
Label: "A thing", |
| 71 |
Key: "a", |
| 72 |
ChangelogPath: "a/thing/CHANGELOG.md", |
| 73 |
Replacements: []core.Replacement{ |
| 74 |
{ |
| 75 |
Path: "a/VERSION", |
| 76 |
Find: "version", |
| 77 |
Replace: "{{.Version}}", |
| 78 |
}, |
| 79 |
}, |
| 80 |
}, |
| 81 |
} |
| 82 |
then.WithTempDirConfig(t, cfg) |
| 83 |
|
| 84 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "a", "v0.1.0.md") |
| 85 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "a", "v0.2.0.md") |
| 86 |
then.WriteFile(t, []byte("version\n"), "a", "VERSION") |
| 87 |
|
| 88 |
cmd := NewMerge(core.NewTemplateCache()) |
| 89 |
|
| 90 |
err := cmd.Run(cmd.Command, nil) |
| 91 |
then.Nil(t, err) |
| 92 |
|
| 93 |
changeContents := `second version |
| 94 |
first version |
| 95 |
` |
| 96 |
then.FileContents(t, changeContents, "a", "thing", "CHANGELOG.md") |
| 97 |
then.FileContents(t, "v0.2.0\n", "a", "VERSION") |
| 98 |
} |
| 99 |
|
| 100 |
func TestMergeVersionsSuccessfullyWithProjectAndNoChanges(t *testing.T) { |
| 101 |
cfg := mergeTestConfig() |
| 102 |
cfg.HeaderPath = "" |
| 103 |
cfg.Replacements = nil |
| 104 |
cfg.Projects = []core.ProjectConfig{ |
| 105 |
{ |
| 106 |
Label: "A thing", |
| 107 |
Key: "a", |
| 108 |
ChangelogPath: "a/thing/CHANGELOG.md", |
| 109 |
}, |
| 110 |
} |
| 111 |
then.WithTempDirConfig(t, cfg) |
| 112 |
|
| 113 |
cmd := NewMerge(core.NewTemplateCache()) |
| 114 |
|
| 115 |
err := cmd.Run(cmd.Command, nil) |
| 116 |
then.Nil(t, err) |
| 117 |
|
| 118 |
changeContents := `` |
| 119 |
then.FileContents(t, changeContents, "a", "thing", "CHANGELOG.md") |
| 120 |
} |
| 121 |
|
| 122 |
func TestMergeVersionsWithUnreleasedChanges(t *testing.T) { |
| 123 |
cfg := mergeTestConfig() |
| 124 |
cfg.HeaderPath = "" |
| 125 |
cfg.Replacements = nil |
| 126 |
then.WithTempDirConfig(t, cfg) |
| 127 |
|
| 128 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 129 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "v0.2.0.md") |
| 130 |
|
| 131 |
unrel := core.Change{ |
| 132 |
Kind: "Added", |
| 133 |
Body: "new feature coming soon", |
| 134 |
} |
| 135 |
writeChangeFile(t, cfg, &unrel) |
| 136 |
|
| 137 |
cmd := NewMerge(core.NewTemplateCache()) |
| 138 |
cmd.UnreleasedHeader = "## Coming Soon" |
| 139 |
err := cmd.Run(cmd.Command, nil) |
| 140 |
then.Nil(t, err) |
| 141 |
|
| 142 |
changeContents := `## Coming Soon |
| 143 |
### Added |
| 144 |
* new feature coming soon |
| 145 |
second version |
| 146 |
first version |
| 147 |
` |
| 148 |
then.FileContents(t, changeContents, "news.md") |
| 149 |
} |
| 150 |
|
| 151 |
func TestMergeVersionsWithUnreleasedChangesErrorsOnBadChanges(t *testing.T) { |
| 152 |
cfg := mergeTestConfig() |
| 153 |
cfg.HeaderPath = "" |
| 154 |
cfg.Replacements = nil |
| 155 |
then.WithTempDirConfig(t, cfg) |
| 156 |
|
| 157 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 158 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "v0.2.0.md") |
| 159 |
|
| 160 |
aVer := []byte("not a valid change") |
| 161 |
then.WriteFile(t, aVer, cfg.ChangesDir, cfg.UnreleasedDir, "a.yaml") |
| 162 |
|
| 163 |
cmd := NewMerge(core.NewTemplateCache()) |
| 164 |
cmd.UnreleasedHeader = "## Coming Soon" |
| 165 |
err := cmd.Run(cmd.Command, nil) |
| 166 |
then.NotNil(t, err) |
| 167 |
} |
| 168 |
|
| 169 |
func TestMergeVersionsWithUnreleasedChangesErrorsOnBadChangeFormat(t *testing.T) { |
| 170 |
cfg := mergeTestConfig() |
| 171 |
cfg.HeaderPath = "" |
| 172 |
cfg.Replacements = nil |
| 173 |
cfg.ChangeFormat = "{{...invalid format{{{" |
| 174 |
then.WithTempDirConfig(t, cfg) |
| 175 |
|
| 176 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 177 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "v0.2.0.md") |
| 178 |
|
| 179 |
unrel := core.Change{ |
| 180 |
Kind: "Added", |
| 181 |
Body: "new feature coming soon", |
| 182 |
} |
| 183 |
writeChangeFile(t, cfg, &unrel) |
| 184 |
|
| 185 |
cmd := NewMerge(core.NewTemplateCache()) |
| 186 |
cmd.UnreleasedHeader = "## Coming Soon" |
| 187 |
err := cmd.Run(cmd.Command, nil) |
| 188 |
then.NotNil(t, err) |
| 189 |
} |
| 190 |
|
| 191 |
func TestMergeVersionsWithUnreleasedChangesInOneProject(t *testing.T) { |
| 192 |
cfg := mergeTestConfig() |
| 193 |
cfg.HeaderPath = "" |
| 194 |
cfg.Replacements = nil |
| 195 |
cfg.Projects = []core.ProjectConfig{ |
| 196 |
{ |
| 197 |
Label: "A thing", |
| 198 |
Key: "a", |
| 199 |
ChangelogPath: "a/thing/CHANGELOG.md", |
| 200 |
}, |
| 201 |
{ |
| 202 |
Label: "B thing", |
| 203 |
Key: "b", |
| 204 |
ChangelogPath: "b/thing/CHANGELOG.md", |
| 205 |
}, |
| 206 |
} |
| 207 |
then.WithTempDirConfig(t, cfg) |
| 208 |
|
| 209 |
then.WriteFile(t, []byte("first A version\n"), cfg.ChangesDir, "a", "v0.1.0.md") |
| 210 |
then.WriteFile(t, []byte("second A version\n"), cfg.ChangesDir, "a", "v0.2.0.md") |
| 211 |
then.Nil(t, os.MkdirAll(filepath.Join("a", "thing"), core.CreateDirMode)) |
| 212 |
then.WriteFile(t, []byte("first B version\n"), cfg.ChangesDir, "b", "v0.1.0.md") |
| 213 |
then.WriteFile(t, []byte("second B version\n"), cfg.ChangesDir, "b", "v0.2.0.md") |
| 214 |
then.Nil(t, os.MkdirAll(filepath.Join("b", "thing"), core.CreateDirMode)) |
| 215 |
|
| 216 |
unrel := core.Change{ |
| 217 |
Kind: "Added", |
| 218 |
Body: "new feature coming soon", |
| 219 |
Project: "a", |
| 220 |
} |
| 221 |
writeChangeFile(t, cfg, &unrel) |
| 222 |
|
| 223 |
cmd := NewMerge(core.NewTemplateCache()) |
| 224 |
cmd.UnreleasedHeader = "## Coming Soon" |
| 225 |
err := cmd.Run(cmd.Command, nil) |
| 226 |
then.Nil(t, err) |
| 227 |
|
| 228 |
changeContentsA := `## Coming Soon |
| 229 |
### Added |
| 230 |
* new feature coming soon |
| 231 |
second A version |
| 232 |
first A version |
| 233 |
` |
| 234 |
then.FileContents(t, changeContentsA, "a", "thing", "CHANGELOG.md") |
| 235 |
|
| 236 |
changeContentsB := `second B version |
| 237 |
first B version |
| 238 |
` |
| 239 |
then.FileContents(t, changeContentsB, "b", "thing", "CHANGELOG.md") |
| 240 |
} |
| 241 |
|
| 242 |
func TestMergeVersionsWithHeaderAndReplacements(t *testing.T) { |
| 243 |
cfg := mergeTestConfig() |
| 244 |
then.WithTempDirConfig(t, cfg) |
| 245 |
|
| 246 |
jsonContents := `{ |
| 247 |
"key": "value", |
| 248 |
"version": "old-version", |
| 249 |
}` |
| 250 |
|
| 251 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 252 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "v0.2.0.md") |
| 253 |
then.WriteFile(t, []byte("ignored\n"), cfg.ChangesDir, "ignored.txt") |
| 254 |
then.WriteFile(t, []byte("a simple header\n"), cfg.ChangesDir, cfg.HeaderPath) |
| 255 |
then.WriteFile(t, []byte(jsonContents), "replace.json") |
| 256 |
|
| 257 |
cmd := NewMerge(core.NewTemplateCache()) |
| 258 |
err := cmd.Run(cmd.Command, nil) |
| 259 |
then.Nil(t, err) |
| 260 |
|
| 261 |
changeContents := `a simple header |
| 262 |
second version |
| 263 |
first version |
| 264 |
` |
| 265 |
then.FileContents(t, changeContents, cfg.ChangelogPath) |
| 266 |
|
| 267 |
newContents := `{ |
| 268 |
"key": "value", |
| 269 |
"version": "0.2.0", |
| 270 |
}` |
| 271 |
then.FileContents(t, newContents, "replace.json") |
| 272 |
} |
| 273 |
|
| 274 |
func TestMergeDryRun(t *testing.T) { |
| 275 |
cfg := mergeTestConfig() |
| 276 |
then.WithTempDirConfig(t, cfg) |
| 277 |
|
| 278 |
changeContents := `a simple header |
| 279 |
second version |
| 280 |
first version |
| 281 |
` |
| 282 |
writer := strings.Builder{} |
| 283 |
|
| 284 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 285 |
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "v0.2.0.md") |
| 286 |
then.WriteFile(t, []byte("ignored\n"), cfg.ChangesDir, "ignored.txt") |
| 287 |
then.WriteFile(t, []byte("a simple header\n"), cfg.ChangesDir, cfg.HeaderPath) |
| 288 |
|
| 289 |
cmd := NewMerge(core.NewTemplateCache()) |
| 290 |
cmd.DryRun = true |
| 291 |
cmd.SetOut(&writer) |
| 292 |
err := cmd.Run(cmd.Command, nil) |
| 293 |
then.Nil(t, err) |
| 294 |
then.Equals(t, changeContents, writer.String()) |
| 295 |
} |
| 296 |
|
| 297 |
func TestMergeSkipsVersionsIfNoneFound(t *testing.T) { |
| 298 |
cfg := mergeTestConfig() |
| 299 |
then.WithTempDirConfig(t, cfg) |
| 300 |
|
| 301 |
changeContents := "a simple header\n" |
| 302 |
builder := strings.Builder{} |
| 303 |
|
| 304 |
then.WriteFile(t, []byte("a simple header\n"), cfg.ChangesDir, cfg.HeaderPath) |
| 305 |
|
| 306 |
cmd := NewMerge(core.NewTemplateCache()) |
| 307 |
cmd.DryRun = true |
| 308 |
cmd.SetOut(&builder) |
| 309 |
|
| 310 |
err := cmd.Run(cmd.Command, nil) |
| 311 |
then.Nil(t, err) |
| 312 |
then.Equals(t, changeContents, builder.String()) |
| 313 |
} |
| 314 |
|
| 315 |
func TestErrorMergeBadConfig(t *testing.T) { |
| 316 |
then.WithTempDir(t) |
| 317 |
|
| 318 |
cmd := NewMerge(core.NewTemplateCache()) |
| 319 |
err := cmd.Run(cmd.Command, nil) |
| 320 |
then.NotNil(t, err) |
| 321 |
} |
| 322 |
|
| 323 |
func TestErrorMergeBadReplacement(t *testing.T) { |
| 324 |
cfg := mergeTestConfig() |
| 325 |
cfg.Replacements[0].Replace = "{{bad....}}" |
| 326 |
then.WithTempDirConfig(t, cfg) |
| 327 |
|
| 328 |
then.WriteFile(t, []byte("a simple header\n"), cfg.ChangesDir, cfg.HeaderPath) |
| 329 |
then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "v0.1.0.md") |
| 330 |
|
| 331 |
cmd := NewMerge(core.NewTemplateCache()) |
| 332 |
err := cmd.Run(cmd.Command, nil) |
| 333 |
then.NotNil(t, err) |
| 334 |
} |