Skip to content

Commit 78b27e2

Browse files
committed
refactor: add version tests to be more defensive
1 parent 09c0599 commit 78b27e2

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lint:
88
golangci-lint run
99

1010
tests:
11-
go test ./...
11+
go test -v ./...
1212

1313
coverage:
1414
go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out

internal/app/gitops-commit/slackhttp/handler_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ func Test_handleSlackCommand(t *testing.T) {
1313
s := &server{}
1414
r := &NamedRepositoryRegistry{}
1515

16-
tests := []struct{
17-
name string
16+
tests := []struct {
17+
name string
1818
command string
19-
expect string
19+
expect string
2020
}{
2121
{
22-
name: "when using missing bits",
22+
name: "when using missing bits",
2323
command: "totally wrong",
24-
expect: "Incorrect usage, expected /gitops-commit [command] [name] [tag]",
24+
expect: "Incorrect usage, expected /gitops-commit [command] [name] [tag]",
2525
},
2626
{
27-
name: "when using an invalid command",
27+
name: "when using an invalid command",
2828
command: "wrong thing v1.2.3",
29-
expect: "Unknown command 'wrong', expected /gitops-commit [command] [name] [tag]",
29+
expect: "Unknown command 'wrong', expected /gitops-commit [command] [name] [tag]",
3030
},
3131
{
32-
name: "when using a valid command ",
32+
name: "when using a valid command ",
3333
command: "deploy thing v1.2.3",
34-
expect: "Unknown named repository, cannot handle \"thing\", availabe options ()",
34+
expect: "Unknown named repository, cannot handle \"thing\", availabe options ()",
3535
},
3636
}
3737

@@ -64,4 +64,4 @@ func Test_handleSlackCommand(t *testing.T) {
6464
})
6565
}
6666
})
67-
}
67+
}

internal/pkg/gitops/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func ReadCurrentVersion(f []byte, notation string) (string, error) {
1515
data := make(map[string]interface{})
1616
err := yaml.Unmarshal(f, &data)
1717

18-
if err != nil {
18+
if err != nil || len(data) <= 0 {
1919
return "", fmt.Errorf("unvalid valid: %w", err)
2020
}
2121

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package gitops
2+
3+
import (
4+
"fmt"
5+
"gopkg.in/yaml.v3"
6+
"testing"
7+
)
8+
9+
func TestReadCurrentVersion(t *testing.T) {
10+
type args struct {
11+
f []byte
12+
notation string
13+
}
14+
15+
tests := []struct {
16+
name string
17+
args args
18+
want string
19+
wantErr bool
20+
}{
21+
{
22+
name: "empty file",
23+
args: args{
24+
f: []byte(""),
25+
notation: "test.test",
26+
},
27+
want: "",
28+
wantErr: true,
29+
},
30+
{
31+
name: "invalid yaml",
32+
args: args{
33+
f: []byte("wibble&&3..\nfff"),
34+
notation: "image.tag",
35+
},
36+
want: "",
37+
wantErr: true,
38+
},
39+
{
40+
name: "simple yaml",
41+
args: args{
42+
f: createSimpleYaml("v1.2.99"),
43+
notation: "image.tag",
44+
},
45+
want: "v1.2.99",
46+
wantErr: false,
47+
},
48+
{
49+
name: "invalid notation to yaml",
50+
args: args{
51+
f: createSimpleYaml("v1.2.99"),
52+
notation: "image.nope",
53+
},
54+
want: "",
55+
wantErr: true,
56+
},
57+
}
58+
59+
fmt.Println(string(createSimpleYaml("v1.2.99")))
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
got, err := ReadCurrentVersion(tt.args.f, tt.args.notation)
64+
if (err != nil) != tt.wantErr {
65+
t.Errorf("ReadCurrentVersion() error = %v, wantErr %v", err, tt.wantErr)
66+
return
67+
}
68+
if got != tt.want {
69+
t.Errorf("ReadCurrentVersion() got = %v, want %v", got, tt.want)
70+
}
71+
})
72+
}
73+
}
74+
75+
func createSimpleYaml(t string) []byte {
76+
type tag struct {
77+
Tag string `yaml:"tag"`
78+
}
79+
80+
marshal, err := yaml.Marshal(&struct {
81+
Image tag `yaml:"image"`
82+
}{
83+
Image: tag{Tag: t},
84+
})
85+
if err != nil {
86+
return nil
87+
}
88+
89+
return marshal
90+
}

0 commit comments

Comments
 (0)