Skip to content

Commit

Permalink
complete: fixes
Browse files Browse the repository at this point in the history
Took 34 minutes
  • Loading branch information
Nasfame committed Oct 20, 2023
1 parent 55d274e commit 5b4f389
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 5 additions & 5 deletions pkg/module/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,21 @@ func PrepareModule(module data.ModuleConfig) (string, error) {

func subst(format string, jsonEncodedInputs ...string) string {

jsonDecodedInputs := make([]string, len(jsonEncodedInputs))
jsonDecodedInputs := make([]any, 0, len(jsonEncodedInputs))

for _, input := range jsonEncodedInputs {
var s string

if err := json.Unmarshal([]byte(input), &s); err != nil {
log.Debug().AnErr("subst: json unmarshall", err).Msgf(input)
log.Printf("setting json encoded value for input:%s\n", input)
s = input
log.Debug().AnErr("subst: json unmarshall", err).Msgf("input:%s", input)
panic("subst: invalid input")
}

jsonDecodedInputs = append(jsonDecodedInputs, s)
}
log.Printf("jsonDecodedInputs:%v", jsonDecodedInputs)

return fmt.Sprintf(format, jsonDecodedInputs)
return fmt.Sprintf(format, jsonDecodedInputs...)
}

// - prepare the module - now we have the text of the template
Expand Down
28 changes: 27 additions & 1 deletion pkg/module/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package module

import (
"encoding/json"
"fmt"
"testing"

"github.com/bacalhau-project/lilypad/pkg/data"
"github.com/stretchr/testify/assert"

"github.com/bacalhau-project/lilypad/pkg/data"
)

func TestPrepareModule(t *testing.T) {
Expand All @@ -29,3 +31,27 @@ func TestLoadModule(t *testing.T) {
assert.Equal(t, "grycap/cowsay@sha256:fad516b39e3a587f33ce3dbbb1e646073ef35e0b696bcf9afb1a3e399ce2ab0b", module.Job.Spec.Docker.Image)
assert.Equal(t, "Hello, world!", module.Job.Spec.Docker.Entrypoint[1])
}

// TestSubst: [subst] can correctly substitute json encoded values into the template string.
func TestSubst(t *testing.T) {
format := "Hello, %s!"
inputs := []string{"hiro"}
expectedOutput := "Hello, hiro!"

jsonEncodedInputs := make([]string, 0, len(inputs))

for _, input := range inputs {
inputJ, err := json.Marshal(input)
if err != nil {
t.Fatalf("json marshall failed %v", err)
}
jsonEncodedInputs = append(jsonEncodedInputs, string(inputJ))
}
t.Logf("jsonEncodedInputs -%v %d", jsonEncodedInputs, len(jsonEncodedInputs))

actualOutput := subst(format, jsonEncodedInputs...)

if actualOutput != expectedOutput {
t.Errorf("Expected output: %s, but got: %s", expectedOutput, actualOutput)
}
}

0 comments on commit 5b4f389

Please sign in to comment.