Skip to content

Commit

Permalink
chore: add test to ensure the package versions are added correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLewis committed Jul 14, 2024
1 parent ed74f16 commit 1ebe627
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
42 changes: 42 additions & 0 deletions pkg/anchor/docker_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package anchor

import (
"fmt"
"reflect"
"strings"
"testing"
)

Expand All @@ -13,3 +15,43 @@ func TestParseCommand(t *testing.T) {
t.Errorf("Expected %v but got %v", expected, actual)
}
}

func TestAppendPackageVersions(t *testing.T) {
file := `# hadolint ignore=DL3008
RUN apt-get update \
&& apt-get install \
--no-install-recommends -y \
# We just need curl and wget
curl wget \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean`
input := strings.NewReader(file)
nodes := Parse(input)
architecture := "amd64"

packageMap := map[string]string{
"curl": "7.68.0",
"wget": "1.20.3",
}

expected := fmt.Sprintf(`# hadolint ignore=DL3008
RUN dpkg --add-architecture %s && apt-get update && apt-get update \
&& apt-get install \
--no-install-recommends -y \
# We just need curl and wget
curl=%s wget=%s \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
`, architecture, packageMap["curl"], packageMap["wget"])

node := nodes[0]
appendPackageVersions(&node, packageMap, architecture)
nodes[0] = node

w := &strings.Builder{}
nodes.Write(w)
result := w.String()
if result != expected {
t.Errorf("Expected:\n%v\ngot:\n%v", expected, result)
}
}
4 changes: 2 additions & 2 deletions pkg/anchor/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func parsePackageVersions(s string) (map[string]string, error) {
}

if _, ok := versions[currentPackage]; ok {
// We have already seen this package, so we can skip it
currentPackage = ""
// We have already seen this package, so we can skip it
currentPackage = ""
continue
}
versions[currentPackage] = strings.Split(line, ": ")[1]
Expand Down
7 changes: 6 additions & 1 deletion pkg/anchor/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ RUN apt-get update \
input := strings.NewReader(tc.input)
result := Parse(input)
if len(result) != len(tc.expected) {
t.Fatalf("%s: Different lengths, expected:\n%v\n got:\n%v", tc.name, tc.expected, result)
t.Fatalf(
"%s: Different lengths, expected:\n%v\n got:\n%v",
tc.name,
tc.expected,
result,
)
}
for i := range result {
if tc.expected[i].Command != result[i].Command {
Expand Down

0 comments on commit 1ebe627

Please sign in to comment.