| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/spf13/cobra" |
| 11 |
|
| 12 |
"github.com/miniscruff/changie/then" |
| 13 |
) |
| 14 |
|
| 15 |
func swapInReaderOutWriter(t *testing.T, cmd *cobra.Command, inReader, outWriter *os.File) { |
| 16 |
cmd.SetOut(outWriter) |
| 17 |
cmd.SetIn(inReader) |
| 18 |
|
| 19 |
t.Cleanup(func() { |
| 20 |
cmd.SetOut(nil) |
| 21 |
cmd.SetIn(nil) |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
func testEcho(t *testing.T, cmd *cobra.Command, reader io.Reader, args []string, expect string) { |
| 26 |
cmd.SetArgs(args) |
| 27 |
then.Nil(t, cmd.Execute()) |
| 28 |
|
| 29 |
out := make([]byte, 1000) |
| 30 |
_, err := reader.Read(out) |
| 31 |
|
| 32 |
then.Nil(t, err) |
| 33 |
then.Contains(t, expect, string(out)) |
| 34 |
} |
| 35 |
|
| 36 |
func testInit(t *testing.T, cmd *cobra.Command) { |
| 37 |
cmd.SetArgs([]string{"init"}) |
| 38 |
then.Nil(t, cmd.Execute()) |
| 39 |
then.FileExists(t, ".changie.yaml") |
| 40 |
} |
| 41 |
|
| 42 |
func testNew(t *testing.T, cmd *cobra.Command, w io.Writer, body string) { |
| 43 |
cmd.SetArgs([]string{"new"}) |
| 44 |
then.DelayWrite( |
| 45 |
t, w, |
| 46 |
[]byte("j"), |
| 47 |
[]byte{13}, |
| 48 |
[]byte(body), |
| 49 |
[]byte{13}, |
| 50 |
) |
| 51 |
then.Nil(t, cmd.Execute()) |
| 52 |
} |
| 53 |
|
| 54 |
func testBatch(t *testing.T, cmd *cobra.Command) { |
| 55 |
cmd.SetArgs([]string{"batch", "v0.1.0"}) |
| 56 |
then.Nil(t, cmd.Execute()) |
| 57 |
|
| 58 |
date := time.Now().Format("2006-01-02") |
| 59 |
changeContents := fmt.Sprintf(`## v0.1.0 - %s |
| 60 |
### Changed |
| 61 |
* older |
| 62 |
* newer |
| 63 |
`, date) |
| 64 |
|
| 65 |
then.FileContents(t, changeContents, ".changes", "v0.1.0.md") |
| 66 |
} |
| 67 |
|
| 68 |
func testMerge(t *testing.T, cmd *cobra.Command) { |
| 69 |
cmd.SetArgs([]string{"merge"}) |
| 70 |
then.Nil(t, cmd.Execute()) |
| 71 |
|
| 72 |
date := time.Now().Format("2006-01-02") |
| 73 |
changeContents := fmt.Sprintf(`%s |
| 74 |
|
| 75 |
## v0.1.0 - %s |
| 76 |
### Changed |
| 77 |
* older |
| 78 |
* newer |
| 79 |
`, defaultHeader, date) |
| 80 |
then.FileContents(t, changeContents, "CHANGELOG.md") |
| 81 |
} |
| 82 |
|
| 83 |
func TestFullRun(t *testing.T) { |
| 84 |
// we need to override this value as it would fail in CI with the interactive system |
| 85 |
// but is ok here as we override stdin and stdout anyway |
| 86 |
t.Setenv("CI", "false") |
| 87 |
|
| 88 |
cmd := RootCmd() |
| 89 |
|
| 90 |
then.WithTempDir(t) |
| 91 |
inReader, inWriter, outReader, outWriter := then.WithStdInOut(t) |
| 92 |
swapInReaderOutWriter(t, cmd, inReader, outWriter) |
| 93 |
|
| 94 |
testInit(t, cmd) |
| 95 |
testEcho(t, cmd, outReader, []string{"latest"}, "0.0.0") |
| 96 |
testNew(t, cmd, inWriter, "older") |
| 97 |
time.Sleep(2 * time.Second) // let time pass for the next change |
| 98 |
testNew(t, cmd, inWriter, "newer") |
| 99 |
testBatch(t, cmd) |
| 100 |
testEcho(t, cmd, outReader, []string{"latest"}, "0.1.0") |
| 101 |
testEcho(t, cmd, outReader, []string{"next", "major"}, "1.0.0") |
| 102 |
testMerge(t, cmd) |
| 103 |
} |
| 104 |
|
| 105 |
func TestErrorNextBadInput(t *testing.T) { |
| 106 |
cmd := RootCmd() |
| 107 |
|
| 108 |
then.WithTempDir(t) |
| 109 |
testInit(t, cmd) |
| 110 |
|
| 111 |
cmd.SetArgs([]string{"next", "blah-blah-blah"}) |
| 112 |
then.NotNil(t, cmd.Execute()) |
| 113 |
} |
| 114 |
|
| 115 |
func TestErrorNextExactVersion(t *testing.T) { |
| 116 |
cmd := RootCmd() |
| 117 |
|
| 118 |
then.WithTempDir(t) |
| 119 |
testInit(t, cmd) |
| 120 |
|
| 121 |
cmd.SetArgs([]string{"next", "v1.2.3"}) |
| 122 |
then.NotNil(t, cmd.Execute()) |
| 123 |
} |