Repository: googleapis/genai-toolbox
File: tests/dataform/dataform_integration_test.go (Go)

Code before:
	exampleSQLX := `config { type: "table" } SELECT 1 AS test_col`
	err = os.WriteFile(filepath.Join(definitionsDir, "example.sqlx"), []byte(exampleSQLX), 0644)
	if err != nil {
		cleanup()
		t.Fatalf("Failed to write example.sqlx: %v", err)
	}

	return tmpDir, cleanup
}

func TestDataformCompileTool(t *testing.T) {
	if _, err := exec.LookPath("dataform"); err != nil {
		t.Skip("dataform CLI not found in $PATH, skipping integration test")
	}

	projectDir, cleanupProject := setupTestProject(t)
	defer cleanupProject()

	toolConfig := map[string]any{
		"tools": map[string]any{
			"my-dataform-compiler": map[string]any{
				"kind":        "dataform-compile",
				"description": "Tool to compile dataform projects",
			},
		},
		"sources": map[string]any{}, // No source needed for this tool
	}

	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	cmd, cleanupServer, err := tests.StartCmd(ctx, toolConfig)
	if err != nil {
		t.Fatalf("command initialization returned an error: %s", err)
	}
	defer cleanupServer()

	waitCtx, cancelWait := context.WithTimeout(ctx, 30*time.Second)
	defer cancelWait()
	out, err := testutils.WaitForString(waitCtx, regexp.MustCompile(`Server ready to serve`), cmd.Out)
	if err != nil {
		t.Logf("toolbox command logs: \n%s", out)
		t.Fatalf("toolbox didn't start successfully: %s", err)
	}

	nonExistentDir := filepath.Join(os.TempDir(), "non-existent-dir-for-test-12345")
	os.Remove(nonExistentDir)

	testCases := []struct {
		name       string
		reqBody    string

Review comment (suggestion):
```suggestion
```

It should work without specifying `sources`.

Code after:
	err = os.WriteFile(filepath.Join(definitionsDir, "example.sqlx"), []byte(exampleSQLX), 0644)
	if err != nil {
		cleanup()
		t.Fatalf("Failed to write example.sqlx: %v", err)
	}

	return tmpDir, cleanup
}

func TestDataformCompileTool(t *testing.T) {
	if _, err := exec.LookPath("dataform"); err != nil {
		t.Skip("dataform CLI not found in $PATH, skipping integration test")
	}

	projectDir, cleanupProject := setupTestProject(t)
	defer cleanupProject()

	toolsFile := map[string]any{
		"tools": map[string]any{
			"my-dataform-compiler": map[string]any{
				"kind":        "dataform-compile-local",
				"description": "Tool to compile dataform projects",
			},
		},
	}

	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	cmd, cleanupServer, err := tests.StartCmd(ctx, toolsFile)
	if err != nil {
		t.Fatalf("command initialization returned an error: %s", err)
	}
	defer cleanupServer()

	waitCtx, cancelWait := context.WithTimeout(ctx, 30*time.Second)
	defer cancelWait()
	out, err := testutils.WaitForString(waitCtx, regexp.MustCompile(`Server ready to serve`), cmd.Out)
	if err != nil {
		t.Logf("toolbox command logs: \n%s", out)
		t.Fatalf("toolbox didn't start successfully: %s", err)
	}

	nonExistentDir := filepath.Join(os.TempDir(), "non-existent-dir")

	testCases := []struct {
		name       string
		reqBody    string
		wantStatus int
		wantBody   string // Substring to check for in the response
	}{