Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Gągor committed Dec 28, 2024
1 parent f576c71 commit bd0c700
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 6 deletions.
27 changes: 27 additions & 0 deletions pkg/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tgagor/template-dockerfiles/pkg/cmd"
)

func TestRunner(t *testing.T) {
// Arrange
input := []string{
cmd.New("echo").Arg("hello").Arg("world").String(),
cmd.New("cmd-only").String(),
cmd.New("").String(),
}
expected := []string{
"echo hello world",
"cmd-only",
"",
}

// Assert
for i, input := range input {
assert.Equal(t, expected[i], input)
}
}
8 changes: 4 additions & 4 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Run(workdir string, cfg *config.Config, flags config.Flags) error {
log.Debug().Str("dockerfile", dockerfile).Msg("Generating temporary")

// Template Dockerfile
if err := templateFile(dockerfileTemplate, dockerfile, configSet); err != nil {
if err := TemplateFile(dockerfileTemplate, dockerfile, configSet); err != nil {
log.Error().Err(err).Str("dockerfile", dockerfile).Msg("Failed to template Dockerfile")
return err
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func templateList(tagTemplates []string, configSet map[string]interface{}) ([]st
var tags []string

for _, label := range tagTemplates {
templated, err := templateString(label, configSet)
templated, err := TemplateString(label, configSet)
if err != nil {
return nil, err
}
Expand All @@ -305,11 +305,11 @@ func templateMap(labelTemplates map[string]string, configSet map[string]interfac
labels := map[string]string{}

for label, value := range labelTemplates {
templatedLabel, err := templateString(label, configSet)
templatedLabel, err := TemplateString(label, configSet)
if err != nil {
return nil, err
}
templatedValue, err := templateString(value, configSet)
templatedValue, err := TemplateString(value, configSet)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/rs/zerolog/log"
)

func templateString(pattern string, args map[string]interface{}) (string, error) {
func TemplateString(pattern string, args map[string]interface{}) (string, error) {
var output bytes.Buffer
t := template.Must(template.New(pattern).Funcs(sprig.TxtFuncMap()).Parse(pattern))
if err := t.Execute(&output, args); err != nil {
Expand All @@ -20,7 +20,7 @@ func templateString(pattern string, args map[string]interface{}) (string, error)
return output.String(), nil
}

func templateFile(templateFile string, destinationFile string, args map[string]interface{}) error {
func TemplateFile(templateFile string, destinationFile string, args map[string]interface{}) error {
t := template.Must(
template.New(filepath.Base(templateFile)).Funcs(sprig.TxtFuncMap()).ParseFiles(templateFile),
)
Expand Down
78 changes: 78 additions & 0 deletions pkg/parser/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package parser_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tgagor/template-dockerfiles/pkg/parser"
)

func TestTemplateString(t *testing.T) {
// Arrange
inputStrings := []string{
"{{ .key }}",
"{{ .key }}",
"{{ .key }}",
" {{ .key }} ",
"{{ .sprig | default \"works\" }}",
"{{range .loop}}{{.}}{{ end }}",
}
inputArgs := []map[string]interface{}{
{"key": "value"},
{"key": 1},
{"key": 1.43},
{"key": "value"},
{"sprig": ""},
{"loop": []int{1, 2, 3}},
}

expected := []string{
"value",
"1",
"1.43",
" value ",
"works",
"123",
}

// Assert
for i, input := range inputStrings {
result, _ := parser.TemplateString(input, inputArgs[i])
assert.Equal(t, expected[i], result)
}
}

// func TestTemplateTags(t *testing.T) {
// // Arrange
// input := []string{
// "{{ .key1 }}",
// "{{ .key2 }}",
// "{{ .key3 }}",
// " {{ .key4 }} ",
// "{{ .sprig | default \"works\" }}",
// "{{range .loop}}{{.}}{{ end }}",
// }
// args := map[string]interface{}{
// "key1": "value",
// "key2": 1,
// "key3": 1.43,
// "key4": "value",
// "sprig": "",
// "loop": []int{1, 2, 3},
// }

// expected := []string{
// "value",
// "1",
// "1.43",
// " value ",
// "works",
// "123",
// }

// // Assert
// for i, input := range inputStrings {
// result, _ := parser.TemplateTags(input)
// assert.Equal(t, expected[i], result)
// }
// }
58 changes: 58 additions & 0 deletions pkg/util/bytecount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package util_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tgagor/template-dockerfiles/pkg/util"
)

func TestByteCountIEC(t *testing.T) {
// Arrange
input := []uint64{
1000,
1024,
1000 * 1000,
1024 * 1024,
1000 * 1000 * 1000,
1024 * 1024 * 1024,
}
expected := []string{
"1000 B",
"1.0 KiB",
"976.6 KiB",
"1.0 MiB",
"953.7 MiB",
"1.0 GiB",
}

// Assert
for i, input := range input {
assert.Equal(t, expected[i], util.ByteCountIEC(input))
}
}

func TestByteCountSI(t *testing.T) {
// Arrange
input := []uint64{
1000,
1024,
1000 * 1000,
1024 * 1024,
1000 * 1000 * 1000,
1024 * 1024 * 1024,
}
expected := []string{
"1.0 kB",
"1.0 kB",
"1.0 MB",
"1.0 MB",
"1.0 GB",
"1.1 GB",
}

// Assert
for i, input := range input {
assert.Equal(t, expected[i], util.ByteCountSI(input))
}
}

0 comments on commit bd0c700

Please sign in to comment.