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

Code before:
	cmd := exec.Command("dataform", "init", tmpDir, "test-project-id", "US")
	if output, err := cmd.CombinedOutput(); err != nil {
		cleanup()
		t.Fatalf("Failed to run 'dataform init': %v\nOutput: %s", err, string(output))
	}

	definitionsDir := filepath.Join(tmpDir, "definitions")
	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)
	}

Review comment (suggestion):
```suggestion
	toolsFile := map[string]any{
```

easier if we use the same name in all the integration tests.

Code after:
	if output, err := cmd.CombinedOutput(); err != nil {
		cleanup()
		t.Fatalf("Failed to run 'dataform init': %v\nOutput: %s", err, string(output))
	}

	definitionsDir := filepath.Join(tmpDir, "definitions")
	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()

	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")