feat(scripts): support external file embedding
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package sqlexec
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/assetloader"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/writers"
|
||||
)
|
||||
@@ -216,3 +220,36 @@ func TestWriter_WriteSchema_EmptyScripts(t *testing.T) {
|
||||
// // Verify results
|
||||
// // Cleanup
|
||||
// }
|
||||
|
||||
func TestProcessEmbedDirectives_UsesScriptSourcePath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
sqlPath := filepath.Join(dir, "1_001_seed.sql")
|
||||
if err := os.WriteFile(filepath.Join(dir, "body.txt"), []byte("writer text"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
script := models.InitScript("seed")
|
||||
script.SQL = "-- @embed: path=body.txt var=:body mode=text\nSELECT :body;"
|
||||
script.Metadata[assetloader.ScriptSourcePathMetadataKey] = sqlPath
|
||||
|
||||
got, err := processEmbedDirectives(script)
|
||||
if err != nil {
|
||||
t.Fatalf("processEmbedDirectives failed: %v", err)
|
||||
}
|
||||
if !strings.Contains(got, "SELECT 'writer text';") {
|
||||
t.Fatalf("script embed directive was not processed:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessEmbedDirectives_NoSourcePathLeavesSQLUnchanged(t *testing.T) {
|
||||
script := models.InitScript("seed")
|
||||
script.SQL = "-- @embed: path=missing.txt var=:body mode=text\nSELECT :body;"
|
||||
|
||||
got, err := processEmbedDirectives(script)
|
||||
if err != nil {
|
||||
t.Fatalf("processEmbedDirectives failed: %v", err)
|
||||
}
|
||||
if got != script.SQL {
|
||||
t.Fatalf("expected unchanged SQL without source path, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user