Skip to content

Commit f854793

Browse files
committed
Fix of endtoend tests
1 parent 383b620 commit f854793

File tree

8 files changed

+33
-13
lines changed

8 files changed

+33
-13
lines changed

internal/cmd/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql OutputPair,
350350
case plug.Process != nil:
351351
handler = &process.Runner{
352352
Cmd: plug.Process.Cmd,
353+
Dir: combo.Dir,
353354
Env: plug.Env,
354355
Format: plug.Process.Format,
355356
}

internal/cmd/process.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func processQuerySets(ctx context.Context, rp ResultProcessor, conf *config.Conf
6868
errout := &stderrs[i]
6969

7070
grp.Go(func() error {
71-
combo := config.Combine(*conf, sql.SQL)
71+
combo := config.Combine(*conf, sql.SQL, dir)
7272
if sql.Plugin != nil {
7373
combo.Codegen = *sql.Plugin
7474
}

internal/cmd/vet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func (c *checker) DSN(dsn string) (string, error) {
464464

465465
func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
466466
// TODO: Create a separate function for this logic so we can
467-
combo := config.Combine(*c.Conf, s)
467+
combo := config.Combine(*c.Conf, s, c.Dir)
468468

469469
// TODO: This feels like a hack that will bite us later
470470
joined := make([]string, 0, len(s.Schema))

internal/compiler/engine.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func NewCompiler(conf config.SQL, combo config.CombinedSettings, parserOpts opts
116116
default:
117117
// Check if this is a plugin engine
118118
if enginePlugin, found := config.FindEnginePlugin(&combo.Global, string(conf.Engine)); found {
119-
eng, err := createPluginEngine(enginePlugin)
119+
eng, err := createPluginEngine(enginePlugin, combo.Dir)
120120
if err != nil {
121121
return nil, err
122122
}
@@ -137,10 +137,10 @@ func NewCompiler(conf config.SQL, combo config.CombinedSettings, parserOpts opts
137137
}
138138

139139
// createPluginEngine creates an engine from an engine plugin configuration.
140-
func createPluginEngine(ep *config.EnginePlugin) (engine.Engine, error) {
140+
func createPluginEngine(ep *config.EnginePlugin, dir string) (engine.Engine, error) {
141141
switch {
142142
case ep.Process != nil:
143-
return plugin.NewPluginEngine(ep.Name, ep.Process.Cmd, ep.Env), nil
143+
return plugin.NewPluginEngine(ep.Name, ep.Process.Cmd, dir, ep.Env), nil
144144
case ep.WASM != nil:
145145
return plugin.NewWASMPluginEngine(ep.Name, ep.WASM.URL, ep.WASM.SHA256, ep.Env), nil
146146
default:

internal/config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,16 @@ type CombinedSettings struct {
326326

327327
// TODO: Combine these into a more usable type
328328
Codegen Codegen
329+
330+
// Dir is the directory containing the config file (for resolving relative paths)
331+
Dir string
329332
}
330333

331-
func Combine(conf Config, pkg SQL) CombinedSettings {
334+
func Combine(conf Config, pkg SQL, dir string) CombinedSettings {
332335
cs := CombinedSettings{
333336
Global: conf,
334337
Package: pkg,
338+
Dir: dir,
335339
}
336340
if pkg.Gen.Go != nil {
337341
cs.Go = *pkg.Gen.Go

internal/endtoend/endtoend_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func TestExamples(t *testing.T) {
5858
t.Parallel()
5959
path := filepath.Join(examples, tc)
6060
var stderr bytes.Buffer
61-
opts := &cmd.Options{
62-
Env: cmd.Env{},
61+
o := &cmd.Options{
62+
Env: cmd.Env{Debug: opts.DebugFromString("")},
6363
Stderr: &stderr,
6464
}
65-
output, err := cmd.Generate(ctx, path, "", opts)
65+
output, err := cmd.Generate(ctx, path, "", o)
6666
if err != nil {
6767
t.Fatalf("sqlc generate failed: %s", stderr.String())
6868
}
@@ -311,7 +311,7 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
311311
if file.IsDir() {
312312
return nil
313313
}
314-
if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".kt") && !strings.HasSuffix(path, ".py") && !strings.HasSuffix(path, ".json") && !strings.HasSuffix(path, ".txt") {
314+
if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".kt") && !strings.HasSuffix(path, ".py") && !strings.HasSuffix(path, ".json") && !strings.HasSuffix(path, ".txt") && !strings.HasSuffix(path, ".rs") {
315315
return nil
316316
}
317317
// TODO: Figure out a better way to ignore certain files
@@ -330,6 +330,10 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
330330
if strings.HasSuffix(path, "_test.go") || strings.Contains(path, "src/test/") {
331331
return nil
332332
}
333+
// Skip plugin source files - they are not generated by sqlc
334+
if strings.Contains(path, "/plugins/") {
335+
return nil
336+
}
333337
if strings.Contains(path, "/python/.venv") || strings.Contains(path, "/python/src/tests/") ||
334338
strings.HasSuffix(path, "__init__.py") || strings.Contains(path, "/python/src/dbtest/") ||
335339
strings.Contains(path, "/python/.mypy_cache") {

internal/engine/plugin/process.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
// ProcessRunner runs an engine plugin as an external process.
2525
type ProcessRunner struct {
2626
Cmd string
27+
Dir string // Working directory for the plugin (config file directory)
2728
Env []string
2829

2930
// Cached responses
@@ -32,9 +33,10 @@ type ProcessRunner struct {
3233
}
3334

3435
// NewProcessRunner creates a new ProcessRunner.
35-
func NewProcessRunner(cmd string, env []string) *ProcessRunner {
36+
func NewProcessRunner(cmd, dir string, env []string) *ProcessRunner {
3637
return &ProcessRunner{
3738
Cmd: cmd,
39+
Dir: dir,
3840
Env: env,
3941
}
4042
}
@@ -60,6 +62,10 @@ func (r *ProcessRunner) invoke(ctx context.Context, method string, req, resp pro
6062
args := append(cmdParts[1:], method)
6163
cmd := exec.CommandContext(ctx, path, args...)
6264
cmd.Stdin = bytes.NewReader(stdin)
65+
// Set working directory to config file directory for relative paths
66+
if r.Dir != "" {
67+
cmd.Dir = r.Dir
68+
}
6369
// Inherit the current environment and add SQLC_VERSION
6470
cmd.Env = append(os.Environ(), fmt.Sprintf("SQLC_VERSION=%s", info.Version))
6571

@@ -446,10 +452,10 @@ type PluginEngine struct {
446452
}
447453

448454
// NewPluginEngine creates a new engine from a process plugin.
449-
func NewPluginEngine(name, cmd string, env []string) *PluginEngine {
455+
func NewPluginEngine(name, cmd, dir string, env []string) *PluginEngine {
450456
return &PluginEngine{
451457
name: name,
452-
runner: NewProcessRunner(cmd, env),
458+
runner: NewProcessRunner(cmd, dir, env),
453459
}
454460
}
455461

internal/ext/process/gen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
type Runner struct {
2323
Cmd string
24+
Dir string // Working directory for the plugin (config file directory)
2425
Format string
2526
Env []string
2627
}
@@ -70,6 +71,10 @@ func (r *Runner) Invoke(ctx context.Context, method string, args any, reply any,
7071
cmdArgs := append(cmdParts[1:], method)
7172
cmd := exec.CommandContext(ctx, path, cmdArgs...)
7273
cmd.Stdin = bytes.NewReader(stdin)
74+
// Set working directory to config file directory for relative paths
75+
if r.Dir != "" {
76+
cmd.Dir = r.Dir
77+
}
7378
// Inherit the current environment (excluding SQLC_AUTH_TOKEN) and add SQLC_VERSION
7479
for _, env := range os.Environ() {
7580
if !strings.HasPrefix(env, "SQLC_AUTH_TOKEN=") {

0 commit comments

Comments
 (0)