/
main_test.go
69 lines (56 loc) · 1.82 KB
/
main_test.go
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
package main
import (
"testing"
"github.com/asottile/dockerfile"
"github.com/stretchr/testify/assert"
)
func TestParsing(t *testing.T) {
expected := []Image{
{Name: "golang", Tag: "1.17.0-alpine"},
{Name: "common", Tag: " * "},
{Name: "common", Tag: " * "},
{Name: "common", Tag: " * "},
{Name: "viaductoss/ksops", Tag: "v3.0.0"},
{Name: "quay.io/argoproj/argocd", Tag: "$ARGOCD_VERSION"},
}
commands, err := dockerfile.ParseFile("tests/Dockerfile.1")
if err != nil {
t.Errorf("Could not open Dockerfile.1: %s", err)
}
tags := getImages(commands, " * ")
assert.Equal(t, expected, tags)
}
func TestQuery(t *testing.T) {
cases := []struct {
query string
occurrence int
match bool
tag string
}{
{query: "foo", occurrence: 0, match: false, tag: ""},
{query: "viaductoss/ksops", occurrence: 0, match: true, tag: "v3.0.0"},
{query: "golang", occurrence: 0, match: true, tag: "1.17.0-alpine"},
{query: "common", occurrence: 0, match: true, tag: "?"},
{query: "foo", occurrence: 1, match: false, tag: ""},
{query: "viaductoss/ksops", occurrence: 1, match: true, tag: "v3.0.0"},
{query: "golang", occurrence: 1, match: true, tag: "1.17.0-alpine"},
{query: "common", occurrence: 1, match: true, tag: "?"},
{query: "viaductoss/ksops", occurrence: 2, match: false, tag: ""},
{query: "common", occurrence: 3, match: true, tag: "?"},
}
commands, err := dockerfile.ParseFile("tests/Dockerfile.1")
if err != nil {
t.Errorf("Could not open Dockerfile.1: %s", err)
}
tags := getImages(commands, "?")
for _, c := range cases {
result, err := getSingleTag(tags, c.query, c.occurrence)
if c.match {
assert.NoError(t, err, "must match %v", c.query)
assert.Equal(t, result, c.tag)
} else {
assert.Error(t, err, "must not match %v", c.query)
assert.Equal(t, result, "")
}
}
}