Skip to content

Commit 1765d13

Browse files
fix: missing imports in generated golang code fron testcatse (#457)
1 parent 775aab1 commit 1765d13

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pkg/generator/golang_generator.go

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"bytes"
2020
"html/template"
2121
"net/http"
22+
"strings"
2223

2324
_ "embed"
2425

@@ -41,6 +42,13 @@ func (g *golangGenerator) Generate(testSuite *testing.TestSuite, testcase *testi
4142
buf := new(bytes.Buffer)
4243
if err = tpl.Execute(buf, testcase); err == nil {
4344
result = buf.String()
45+
// Check if "bytes." is used in the generated code
46+
if strings.Contains(result, "bytes.") {
47+
// Only insert "bytes" import if it's not already included
48+
if !strings.Contains(result, "\"bytes\"") {
49+
result = strings.Replace(result, "import (", "import (\n\t\"bytes\"", 1)
50+
}
51+
}
4452
}
4553
}
4654
return

pkg/generator/testdata/expected_go_code.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ limitations under the License.
1313
package main
1414

1515
import (
16+
"bytes"
1617
"io"
1718
"net/http"
1819
)

0 commit comments

Comments
 (0)