Skip to content

Commit

Permalink
only use fmt.Fprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
b1ron committed Jun 9, 2023
1 parent 741509e commit 028b3d8
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions internal/jstest/jstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func runShellWithScript(dir, script string) ([]byte, error) {
dockerArgs := []string{"compose", "run", "-T", "--rm", "mongo"}
shellArgs := []string{
"--verbose", "--norc", "mongodb://host.docker.internal:27017/",
"--eval", evalBuilder(script, nil), script,
"--eval", evalBuilder(script), script,
}
dockerArgs = append(dockerArgs, shellArgs...)

Expand All @@ -165,33 +165,11 @@ func runShellWithScript(dir, script string) ([]byte, error) {
}

// evalBuilder creates the TestData object and sets the testName property for the shell.
// It optionally sets additional properties on the TestData object.
func evalBuilder(script string, obj map[string]string) string {
func evalBuilder(script string) string {
var eb bytes.Buffer
eb.WriteString("TestData = new Object();")
eb.WriteByte(' ')
fmt.Fprintf(&eb, "TestData = new Object(); ")
scriptName := filepath.Base(script)
fmt.Fprintf(&eb, "TestData.testName = %q;", strings.TrimSuffix(scriptName, filepath.Ext(scriptName)))

if obj == nil {
return eb.String()
}

eb.WriteByte(' ')

// avoids extraneous space
i := len(obj) - 1

for key, value := range obj {
fmt.Fprintf(&eb, "TestData.%s = %s;", key, value)

if i == 0 {
break
}
i--

eb.WriteByte(' ')
}

return eb.String()
}

0 comments on commit 028b3d8

Please sign in to comment.