Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: introduce yamltest helper for stable test record output (#20044)

h3n4lcb6553c089ee
diff --git a/.sonarcloud.properties b/.sonarcloud.properties
index 514353bf83..99ef0b4846 100644
--- a/.sonarcloud.properties
+++ b/.sonarcloud.properties
@@ -19,6 +19,7 @@ sonar.test.inclusions=\
backend/**/testdata/**,\
backend/**/test-data/**,\
backend/common/testcontainer/**,\
+ backend/common/yamltest/**,\
backend/tests/**,\
frontend/src/**/*.test.ts,\
frontend/src/**/*.test.tsx,\
diff --git a/backend/common/yamltest/yamltest.go b/backend/common/yamltest/yamltest.go
new file mode 100644
index 0000000000..bb4eb0510d
--- /dev/null
+++ b/backend/common/yamltest/yamltest.go
@@ -0,0 +1,55 @@
+// Package yamltest provides yaml encoding helpers tuned for bytebase test
+// fixtures. The encoder is pinned to 2-space indentation to match the
+// existing on-disk format and avoid noisy diffs when test data is
+// re-recorded.
+//
+// yaml.v3's default Marshal uses 4-space sequence indentation, which differs
+// from the 2-space style used in bytebase's checked-in test fixtures. Every
+// re-record under the default produces hundreds of lines of pure whitespace
+// churn. The helpers here wrap yaml.NewEncoder with SetIndent(2) so that
+// re-recording is a no-op when the test data has not changed.
+//
+// Other yaml.v3 behaviors (block-scalar collapse, comment loss, blank-line
+// removal) are inherent to text-based marshalling and are not handled here.
+package yamltest
+
+import (
+ "bytes"
+ "io"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
+)
+
+// NewEncoder returns a yaml.Encoder configured with 2-space indentation.
+// Use directly when callers need custom buffering; most record-mode tests
+// should use Record or WriteFile instead.
+func NewEncoder(w io.Writer) *yaml.Encoder {
+ enc := yaml.NewEncoder(w)
+ enc.SetIndent(2)
+ return enc
+}
+
+// WriteFile marshals v with 2-space indentation and writes it to path,
+// returning any error. Use from helpers that propagate errors instead of
+// failing tests directly (e.g., backend/tests integration helpers).
+func WriteFile(path string, v any) error {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ if err := enc.Encode(v); err != nil {
+ return err
+ }
+ if err := enc.Close(); err != nil {
+ return err
+ }
+ return os.WriteFile(path, buf.Bytes(), 0644)
+}
+
+// Record marshals v with 2-space indentation and writes it to path,
+// failing the test on any error. Use in test record-mode branches.
+func Record(t *testing.T, path string, v any) {
+ t.Helper()
+ require.NoError(t, WriteFile(path, v))
+}
diff --git a/backend/common/yamltest/yamltest_test.go b/backend/common/yamltest/yamltest_test.go
new file mode 100644
index 0000000000..5edfc8faa3
--- /dev/null
+++ b/backend/common/yamltest/yamltest_test.go
@@ -0,0 +1,54 @@
+package yamltest
+
+import (
+ "bytes"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// goldenYAML is the expected output for the test fixture below. It pins
+// 2-space sequence indentation as the format guarantee.
+const goldenYAML = `- name: alpha
+ values:
+ - one
+ - two
+- name: beta
+ values:
+ - three
+`
+
+func fixture() any {
+ return []map[string]any{
+ {"name": "alpha", "values": []string{"one", "two"}},
+ {"name": "beta", "values": []string{"three"}},
+ }
+}
+
+func TestNewEncoderPins2SpaceIndent(t *testing.T) {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ require.NoError(t, enc.Encode(fixture()))
+ require.NoError(t, enc.Close())
+ require.Equal(t, goldenYAML, buf.String())
+}
+
+func TestWriteFileWritesGoldenBytes(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "out.yaml")
+ require.NoError(t, WriteFile(path, fixture()))
+ got, err := os.ReadFile(path)
+ require.NoError(t, err)
+ require.Equal(t, goldenYAML, string(got))
+}
+
+func TestRecordWritesGoldenBytes(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "out.yaml")
+ Record(t, path, fixture())
+ got, err := os.ReadFile(path)
+ require.NoError(t, err)
+ require.Equal(t, goldenYAML, string(got))
+}
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 067a1f2829..25c330a7c8 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/component/sheet"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
@@ -337,10 +338,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule *storepb.SQLReviewRule, dbType stor
if record {
err := yamlFile.Close()
require.NoError(t, err)
- byteValue, err := yaml.Marshal(tests)
- require.NoError(t, err)
- err = os.WriteFile(filepath, byteValue, 0644)
- require.NoError(t, err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/db/mssql/query_test.go b/backend/plugin/db/mssql/query_test.go
index 8f4319f67a..de93735150 100644
--- a/backend/plugin/db/mssql/query_test.go
+++ b/backend/plugin/db/mssql/query_test.go
@@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
func TestGetStatementWithResultLimit(t *testing.T) {
@@ -44,9 +46,6 @@ func runLimitTest(t *testing.T, file string, record bool) {
if record {
err := yamlFile.Close()
require.NoError(t, err)
- byteValue, err = yaml.Marshal(testCases)
- require.NoError(t, err)
- err = os.WriteFile(filepath, byteValue, 0644)
- require.NoError(t, err)
+ yamltest.Record(t, filepath, testCases)
}
}
diff --git a/backend/plugin/db/oracle/query_test.go b/backend/plugin/db/oracle/query_test.go
index e85b9fee1e..e6dabbbbb8 100644
--- a/backend/plugin/db/oracle/query_test.go
+++ b/backend/plugin/db/oracle/query_test.go
@@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
func TestGetStatementWithResultLimit(t *testing.T) {
@@ -44,9 +46,6 @@ func runLimitTest(t *testing.T, file string, record bool) {
if record {
err := yamlFile.Close()
require.NoError(t, err)
- byteValue, err = yaml.Marshal(testCases)
- require.NoError(t, err)
- err = os.WriteFile(filepath, byteValue, 0644)
- require.NoError(t, err)
+ yamltest.Record(t, filepath, testCases)
}
}
diff --git a/backend/plugin/db/snowflake/query_test.go b/backend/plugin/db/snowflake/query_test.go
index 049bfc500d..680c8ccee4 100644
--- a/backend/plugin/db/snowflake/query_test.go
+++ b/backend/plugin/db/snowflake/query_test.go
@@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
func TestGetStatementWithResultLimit(t *testing.T) {
@@ -44,9 +46,6 @@ func runLimitTest(t *testing.T, file string, record bool) {
if record {
err := yamlFile.Close()
require.NoError(t, err)
- byteValue, err = yaml.Marshal(testCases)
- require.NoError(t, err)
- err = os.WriteFile(filepath, byteValue, 0644)
- require.NoError(t, err)
+ yamltest.Record(t, filepath, testCases)
}
}
diff --git a/backend/plugin/parser/base/split_test_runner.go b/backend/plugin/parser/base/split_test_runner.go
index 67606cf1f5..5b12377957 100644
--- a/backend/plugin/parser/base/split_test_runner.go
+++ b/backend/plugin/parser/base/split_test_runner.go
@@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
// record is a flag to update expected results in YAML files.
@@ -186,11 +188,7 @@ func recordTestResults(t *testing.T, fullPath string, testCases []SplitTestCase,
}
}
- data, err := yaml.Marshal(updated)
- require.NoError(t, err, "failed to marshal updated test cases")
-
- err = os.WriteFile(fullPath, data, 0644)
- require.NoError(t, err, "failed to write updated test file: %s", fullPath)
+ yamltest.Record(t, fullPath, updated)
t.Logf("Updated %s with %d test cases", fullPath, len(updated))
}
diff --git a/backend/plugin/parser/bigquery/query_span_test.go b/backend/plugin/parser/bigquery/query_span_test.go
index 49dee3afd5..c7cbfd0691 100644
--- a/backend/plugin/parser/bigquery/query_span_test.go
+++ b/backend/plugin/parser/bigquery/query_span_test.go
@@ -14,6 +14,7 @@ import (
parser "github.com/bytebase/parser/googlesql"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -69,10 +70,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/cockroachdb/cockroachdb_test.go b/backend/plugin/parser/cockroachdb/cockroachdb_test.go
index 8869c8abc6..640b1cc2ea 100644
--- a/backend/plugin/parser/cockroachdb/cockroachdb_test.go
+++ b/backend/plugin/parser/cockroachdb/cockroachdb_test.go
@@ -6,6 +6,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
func TestSplitSQLStatement(t *testing.T) {
@@ -37,9 +39,6 @@ func TestSplitSQLStatement(t *testing.T) {
}
if record {
- content, err = yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testFilepath, content, 0644)
- a.NoError(err)
+ yamltest.Record(t, testFilepath, testCases)
}
}
diff --git a/backend/plugin/parser/cosmosdb/query_span_test.go b/backend/plugin/parser/cosmosdb/query_span_test.go
index c357203d61..46e4818d59 100644
--- a/backend/plugin/parser/cosmosdb/query_span_test.go
+++ b/backend/plugin/parser/cosmosdb/query_span_test.go
@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -49,10 +50,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/doris/query_span_extractor_system_table_test.go b/backend/plugin/parser/doris/query_span_extractor_system_table_test.go
index 53fdc1542c..b34c96ee63 100644
--- a/backend/plugin/parser/doris/query_span_extractor_system_table_test.go
+++ b/backend/plugin/parser/doris/query_span_extractor_system_table_test.go
@@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -64,10 +65,7 @@ func TestGetQuerySpan_SystemTable(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/doris/statement_ranges_test.go b/backend/plugin/parser/doris/statement_ranges_test.go
index 471da1c3f2..af096810ca 100644
--- a/backend/plugin/parser/doris/statement_ranges_test.go
+++ b/backend/plugin/parser/doris/statement_ranges_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -54,10 +55,7 @@ func TestGetStatementRange(t *testing.T) {
}
if record {
- yamlData, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(filepath, yamlData, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, testCases)
}
}
}
diff --git a/backend/plugin/parser/elasticsearch/parser_test.go b/backend/plugin/parser/elasticsearch/parser_test.go
index e4aef0181c..7937ff05ae 100644
--- a/backend/plugin/parser/elasticsearch/parser_test.go
+++ b/backend/plugin/parser/elasticsearch/parser_test.go
@@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
func TestParseElasticsearchREST(t *testing.T) {
@@ -42,9 +44,6 @@ func TestParseElasticsearchREST(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, testCases)
}
}
diff --git a/backend/plugin/parser/elasticsearch/query_span_test.go b/backend/plugin/parser/elasticsearch/query_span_test.go
index c81a8587c9..bcc314efc9 100644
--- a/backend/plugin/parser/elasticsearch/query_span_test.go
+++ b/backend/plugin/parser/elasticsearch/query_span_test.go
@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -50,10 +51,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
diff --git a/backend/plugin/parser/elasticsearch/statement_ranges_test.go b/backend/plugin/parser/elasticsearch/statement_ranges_test.go
index 297833fcbf..8483a9c9de 100644
--- a/backend/plugin/parser/elasticsearch/statement_ranges_test.go
+++ b/backend/plugin/parser/elasticsearch/statement_ranges_test.go
@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -45,9 +46,6 @@ func TestStatementRanges(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, testCases)
}
}
diff --git a/backend/plugin/parser/mongodb/completion_test.go b/backend/plugin/parser/mongodb/completion_test.go
index 93a78150c8..a1e9ecf374 100644
--- a/backend/plugin/parser/mongodb/completion_test.go
+++ b/backend/plugin/parser/mongodb/completion_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -73,10 +74,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/mysql/backup_test.go b/backend/plugin/parser/mysql/backup_test.go
index ffda8163eb..ebe3980e1e 100644
--- a/backend/plugin/parser/mysql/backup_test.go
+++ b/backend/plugin/parser/mysql/backup_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -71,10 +72,7 @@ func TestBackup(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/mysql/completion_test.go b/backend/plugin/parser/mysql/completion_test.go
index 8965a02bbd..c99a3426c3 100644
--- a/backend/plugin/parser/mysql/completion_test.go
+++ b/backend/plugin/parser/mysql/completion_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -86,10 +87,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/mysql/query_span_test.go b/backend/plugin/parser/mysql/query_span_test.go
index bd13167e23..e9175edb57 100644
--- a/backend/plugin/parser/mysql/query_span_test.go
+++ b/backend/plugin/parser/mysql/query_span_test.go
@@ -13,6 +13,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -77,10 +78,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/mysql/restore_test.go b/backend/plugin/parser/mysql/restore_test.go
index 9426265d51..d0cad91d39 100644
--- a/backend/plugin/parser/mysql/restore_test.go
+++ b/backend/plugin/parser/mysql/restore_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -75,9 +76,6 @@ func TestRestore(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/mysql/statement_type_test.go b/backend/plugin/parser/mysql/statement_type_test.go
index f009131789..1716cb10a8 100644
--- a/backend/plugin/parser/mysql/statement_type_test.go
+++ b/backend/plugin/parser/mysql/statement_type_test.go
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -59,9 +60,6 @@ func TestGetStatementType(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/partiql/completion_test.go b/backend/plugin/parser/partiql/completion_test.go
index 50a87e8c6f..34ea156d10 100644
--- a/backend/plugin/parser/partiql/completion_test.go
+++ b/backend/plugin/parser/partiql/completion_test.go
@@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -79,10 +80,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/pg/backup_test.go b/backend/plugin/parser/pg/backup_test.go
index a27afc47f8..16e58faa39 100644
--- a/backend/plugin/parser/pg/backup_test.go
+++ b/backend/plugin/parser/pg/backup_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -69,9 +70,6 @@ func TestBackup(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/pg/completion_test.go b/backend/plugin/parser/pg/completion_test.go
index a8460218ba..4ab2cbc3f0 100644
--- a/backend/plugin/parser/pg/completion_test.go
+++ b/backend/plugin/parser/pg/completion_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -86,10 +87,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
@@ -288,10 +286,7 @@ func TestCompletionQuotedIdentifiers(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/pg/query_span_test.go b/backend/plugin/parser/pg/query_span_test.go
index 3d72e82f71..9b12d1543e 100644
--- a/backend/plugin/parser/pg/query_span_test.go
+++ b/backend/plugin/parser/pg/query_span_test.go
@@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -64,10 +65,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/pg/restore_test.go b/backend/plugin/parser/pg/restore_test.go
index 9415b43010..abecd9773d 100644
--- a/backend/plugin/parser/pg/restore_test.go
+++ b/backend/plugin/parser/pg/restore_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -75,10 +76,7 @@ func TestRestore(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/pg/statement_type_antlr_test.go b/backend/plugin/parser/pg/statement_type_antlr_test.go
index f78cd6f6e3..9b496dc980 100644
--- a/backend/plugin/parser/pg/statement_type_antlr_test.go
+++ b/backend/plugin/parser/pg/statement_type_antlr_test.go
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -59,10 +60,7 @@ func TestGetStatementType(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/plsql/backup_test.go b/backend/plugin/parser/plsql/backup_test.go
index 81a95fd0ed..b18be8688d 100644
--- a/backend/plugin/parser/plsql/backup_test.go
+++ b/backend/plugin/parser/plsql/backup_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -66,9 +67,6 @@ func TestBackup(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/plsql/completion_test.go b/backend/plugin/parser/plsql/completion_test.go
index 274026344f..26b4f1f067 100644
--- a/backend/plugin/parser/plsql/completion_test.go
+++ b/backend/plugin/parser/plsql/completion_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -87,10 +88,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/plsql/query_span_test.go b/backend/plugin/parser/plsql/query_span_test.go
index faaa3bfee3..710943a184 100644
--- a/backend/plugin/parser/plsql/query_span_test.go
+++ b/backend/plugin/parser/plsql/query_span_test.go
@@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -75,10 +76,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
diff --git a/backend/plugin/parser/plsql/restore_test.go b/backend/plugin/parser/plsql/restore_test.go
index a8019d5cad..94be8db12e 100644
--- a/backend/plugin/parser/plsql/restore_test.go
+++ b/backend/plugin/parser/plsql/restore_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -73,10 +74,7 @@ func TestRestore(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/snowflake/query_span_test.go b/backend/plugin/parser/snowflake/query_span_test.go
index de037d34f4..da87e13b98 100644
--- a/backend/plugin/parser/snowflake/query_span_test.go
+++ b/backend/plugin/parser/snowflake/query_span_test.go
@@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -70,10 +71,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/snowflake/statement_ranges_test.go b/backend/plugin/parser/snowflake/statement_ranges_test.go
index 03bb37f83e..9ccd1625d0 100644
--- a/backend/plugin/parser/snowflake/statement_ranges_test.go
+++ b/backend/plugin/parser/snowflake/statement_ranges_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -54,10 +55,7 @@ func TestGetStatementRange(t *testing.T) {
}
if record {
- yamlData, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(filepath, yamlData, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, testCases)
}
}
}
diff --git a/backend/plugin/parser/spanner/query_span_test.go b/backend/plugin/parser/spanner/query_span_test.go
index 2cd94ac91e..863f00d7e9 100644
--- a/backend/plugin/parser/spanner/query_span_test.go
+++ b/backend/plugin/parser/spanner/query_span_test.go
@@ -14,6 +14,7 @@ import (
parser "github.com/bytebase/parser/googlesql"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -69,10 +70,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/tidb/backup_test.go b/backend/plugin/parser/tidb/backup_test.go
index 34df776760..683a42d38b 100644
--- a/backend/plugin/parser/tidb/backup_test.go
+++ b/backend/plugin/parser/tidb/backup_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -73,10 +74,7 @@ func TestBackup(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/tidb/query_span_test.go b/backend/plugin/parser/tidb/query_span_test.go
index d87696d4b7..fbb145e37a 100644
--- a/backend/plugin/parser/tidb/query_span_test.go
+++ b/backend/plugin/parser/tidb/query_span_test.go
@@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -68,10 +69,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/tidb/statement_type_test.go b/backend/plugin/parser/tidb/statement_type_test.go
index 9ee80fd3fe..9eef2b1e35 100644
--- a/backend/plugin/parser/tidb/statement_type_test.go
+++ b/backend/plugin/parser/tidb/statement_type_test.go
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -59,9 +60,6 @@ func TestGetStatementType(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/trino/completion_test.go b/backend/plugin/parser/trino/completion_test.go
index 9e3430bb7f..99ddb82f5f 100644
--- a/backend/plugin/parser/trino/completion_test.go
+++ b/backend/plugin/parser/trino/completion_test.go
@@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -104,10 +105,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/trino/query_span_test.go b/backend/plugin/parser/trino/query_span_test.go
index c647e64a7c..3e71981928 100644
--- a/backend/plugin/parser/trino/query_span_test.go
+++ b/backend/plugin/parser/trino/query_span_test.go
@@ -12,6 +12,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -200,10 +201,7 @@ func TestGetQuerySpanFromYAML(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(testCases)
- a.NoError(err)
- err = os.WriteFile(testDataPath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/tsql/backup_test.go b/backend/plugin/parser/tsql/backup_test.go
index b415b97339..00c48fdf54 100644
--- a/backend/plugin/parser/tsql/backup_test.go
+++ b/backend/plugin/parser/tsql/backup_test.go
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -66,10 +67,7 @@ func TestBackup(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/tsql/batch/batch_test.go b/backend/plugin/parser/tsql/batch/batch_test.go
index 1d2f2cc1a5..8068c9bba7 100644
--- a/backend/plugin/parser/tsql/batch/batch_test.go
+++ b/backend/plugin/parser/tsql/batch/batch_test.go
@@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
func TestBuildGoCommands(t *testing.T) {
@@ -87,9 +89,7 @@ func TestBatch(t *testing.T) {
}
}
if record {
- bytes, err := yaml.Marshal(testCases)
- a.NoError(err)
- a.NoError(os.WriteFile(filePath, bytes, 0644))
+ yamltest.Record(t, filePath, testCases)
}
}
diff --git a/backend/plugin/parser/tsql/completion_test.go b/backend/plugin/parser/tsql/completion_test.go
index 356e6bc9c7..411410a791 100644
--- a/backend/plugin/parser/tsql/completion_test.go
+++ b/backend/plugin/parser/tsql/completion_test.go
@@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -101,10 +102,7 @@ func TestCompletion(t *testing.T) {
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/parser/tsql/query_span_test.go b/backend/plugin/parser/tsql/query_span_test.go
index 5b7691dc30..76baf9918c 100644
--- a/backend/plugin/parser/tsql/query_span_test.go
+++ b/backend/plugin/parser/tsql/query_span_test.go
@@ -1,7 +1,6 @@
package tsql
import (
- "bytes"
"context"
"io"
"os"
@@ -12,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -75,12 +75,7 @@ func TestGetQuerySpan(t *testing.T) {
}
if record {
- var buf bytes.Buffer
- enc := yaml.NewEncoder(&buf)
- enc.SetIndent(2)
- a.NoError(enc.Encode(testCases))
- a.NoError(enc.Close())
- a.NoError(os.WriteFile(testDataPath, buf.Bytes(), 0644))
+ yamltest.Record(t, testDataPath, testCases)
}
}
}
diff --git a/backend/plugin/parser/tsql/restore_test.go b/backend/plugin/parser/tsql/restore_test.go
index 0fec3b3806..04666bf774 100644
--- a/backend/plugin/parser/tsql/restore_test.go
+++ b/backend/plugin/parser/tsql/restore_test.go
@@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
"github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
@@ -150,10 +151,7 @@ func TestRestore(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/schema/mssql/get_database_definition_test.go b/backend/plugin/schema/mssql/get_database_definition_test.go
index fdc5b4b87c..525ec07e4f 100644
--- a/backend/plugin/schema/mssql/get_database_definition_test.go
+++ b/backend/plugin/schema/mssql/get_database_definition_test.go
@@ -9,6 +9,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
)
@@ -51,9 +52,6 @@ func TestGetDatabaseDefinition(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/plugin/schema/mssql/get_database_metadata_test.go b/backend/plugin/schema/mssql/get_database_metadata_test.go
index 31f2347688..b9a12bc9c0 100644
--- a/backend/plugin/schema/mssql/get_database_metadata_test.go
+++ b/backend/plugin/schema/mssql/get_database_metadata_test.go
@@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/common/yamltest"
)
type getDatabaseMetadataCase struct {
@@ -48,9 +50,6 @@ func TestGetDatabaseMetadata(t *testing.T) {
}
}
if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
+ yamltest.Record(t, filepath, tests)
}
}
diff --git a/backend/tests/sql_review_test.go b/backend/tests/sql_review_test.go
index 68acbb02aa..174c1374e3 100644
--- a/backend/tests/sql_review_test.go
+++ b/backend/tests/sql_review_test.go
@@ -23,6 +23,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
@@ -461,15 +462,7 @@ func writeTestData(filepath string, tests []test) error {
yamlTests = append(yamlTests, yamlTest)
}
- byteValue, err := yaml.Marshal(yamlTests)
- if err != nil {
- return err
- }
- err = os.WriteFile(filepath, byteValue, 0644)
- if err != nil {
- return err
- }
- return nil
+ return yamltest.WriteFile(filepath, yamlTests)
}
func createIssueAndReturnSQLReviewResult(ctx context.Context, a *require.Assertions, ctl *controller, project *v1pb.Project, database *v1pb.Database, statement string, wait bool) []*v1pb.PlanCheckRun_Result {