summaryrefslogtreecommitdiff
path: root/cmd/template_test.go
blob: 3d9e1275fc6698e3012782ce2a376dada431da72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main

import (
	"bytes"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestNewTemplateCommandWithEmptyArgs(t *testing.T) {

	defaultExpectedOutput := `build: automatic update of example-app

updates image example/example tag '1.0.0' to '1.0.1'
updates image example/updater tag 'sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee' to 'sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c'

`
	cmd := newTemplateCommand()
	buf := new(bytes.Buffer)
	args := []string{}
	cmd.SetOut(buf)
	cmd.SetArgs(args)
	err := cmd.Execute()
	if err != nil {
		t.Fatalf("could not execute command: %v", err)
	}
	assert.NoError(t, err)
	assert.Equal(t, defaultExpectedOutput, buf.String())

}

func TestNewTemplateCommandWithCustomTemplate(t *testing.T) {

	testTemplate := `Custom Commit Message:
App: {{.AppName}}
{{- range .AppChanges }}
- {{.Image}}: {{.OldTag}} -> {{.NewTag}}
{{- end }}`

	expectedOutput := `Custom Commit Message:
App: example-app
- example/example: 1.0.0 -> 1.0.1
- example/updater: sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee -> sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c
`

	// Create a temporary file to hold the test template
	tempFile, err := os.CreateTemp("", "test-template.tmpl")
	if err != nil {
		t.Fatalf("could not create temp file: %v", err)
	}
	defer os.Remove(tempFile.Name())
	_, err = tempFile.WriteString(testTemplate)
	if err != nil {
		t.Fatalf("could not write to temp file: %v", err)
	}
	tempFile.Close()

	cmd := newTemplateCommand()
	buf := new(bytes.Buffer)
	args := []string{tempFile.Name()}
	cmd.SetOut(buf)
	cmd.SetArgs(args)
	err = cmd.Execute()
	if err != nil {
		t.Fatalf("could not execute command: %v", err)
	}
	assert.Equal(t, expectedOutput, buf.String())
}

func TestNewTemplateCommandWithInvalidTemplate(t *testing.T) {

	testTemplate := `Custom Commit Message:
App: {{.AppName}}
{{- range .AppChanges }}
- {{.Image}}: {{.OldTag}} -> {{.NewTag}}
{{- end`

	// Create a temporary file to hold the test template
	tempFile, err := os.CreateTemp("", "test-template.tmpl")
	if err != nil {
		t.Fatalf("could not create temp file: %v", err)
	}
	defer os.Remove(tempFile.Name())
	_, err = tempFile.WriteString(testTemplate)
	if err != nil {
		t.Fatalf("could not write to temp file: %v", err)
	}
	tempFile.Close()

	cmd := newTemplateCommand()
	buf := new(bytes.Buffer)
	args := []string{tempFile.Name()}
	cmd.SetErr(buf)
	cmd.SetArgs(args)
	err = cmd.Execute()
	if err != nil {
		t.Fatalf("could not execute command: %v", err)
	}
	assert.Contains(t, buf.String(), "could not parse commit message template")
}

func TestNewTemplateCommandWithInvalidPath(t *testing.T) {

	cmd := newTemplateCommand()
	buf := new(bytes.Buffer)
	args := []string{"test-template.tmpl"}
	cmd.SetErr(buf)
	cmd.SetArgs(args)
	err := cmd.Execute()
	if err != nil {
		t.Fatalf("could not execute command: %v", err)
	}
	assert.Contains(t, buf.String(), "no such file or directory")
}