|
| 1 | +//go:build cli_integration |
| 2 | + |
| 3 | +package yamlflow_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// TestMain builds the CLI binary once for all tests in this package. |
| 13 | +// If DEVTOOLS_CLI_BIN is already set, it skips the build step. |
| 14 | +func TestMain(m *testing.M) { |
| 15 | + if os.Getenv("RUN_CLI_INTEGRATION") != "true" { |
| 16 | + os.Exit(0) |
| 17 | + } |
| 18 | + |
| 19 | + binPath := os.Getenv("DEVTOOLS_CLI_BIN") |
| 20 | + cleanUp := false |
| 21 | + if binPath == "" { |
| 22 | + // Build CLI binary with cli tag |
| 23 | + binPath = filepath.Join(os.TempDir(), "devtools-cli-test") |
| 24 | + cmd := exec.Command("go", "build", "-tags", "cli", "-o", binPath, "../../.") |
| 25 | + cmd.Stdout = os.Stdout |
| 26 | + cmd.Stderr = os.Stderr |
| 27 | + if err := cmd.Run(); err != nil { |
| 28 | + panic("failed to build CLI binary: " + err.Error()) |
| 29 | + } |
| 30 | + os.Setenv("DEVTOOLS_CLI_BIN", binPath) |
| 31 | + cleanUp = true |
| 32 | + } |
| 33 | + |
| 34 | + code := m.Run() |
| 35 | + |
| 36 | + if cleanUp { |
| 37 | + os.Remove(binPath) |
| 38 | + } |
| 39 | + os.Exit(code) |
| 40 | +} |
| 41 | + |
| 42 | +func runCLI(t *testing.T, yamlFile string) { |
| 43 | + t.Helper() |
| 44 | + |
| 45 | + binPath := os.Getenv("DEVTOOLS_CLI_BIN") |
| 46 | + if binPath == "" { |
| 47 | + t.Fatal("DEVTOOLS_CLI_BIN not set") |
| 48 | + } |
| 49 | + |
| 50 | + cmd := exec.Command(binPath, "flow", "run", yamlFile) |
| 51 | + cmd.Env = append(os.Environ(), "DEVTOOLS_MODE=cli") |
| 52 | + out, err := cmd.CombinedOutput() |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("CLI failed for %s:\n%s", filepath.Base(yamlFile), string(out)) |
| 55 | + } |
| 56 | + |
| 57 | + t.Logf("CLI output for %s:\n%s", filepath.Base(yamlFile), string(out)) |
| 58 | +} |
| 59 | + |
| 60 | +func TestYAMLFlow_SimpleRun(t *testing.T) { |
| 61 | + runCLI(t, "simple_run_example.yaml") |
| 62 | +} |
| 63 | + |
| 64 | +func TestYAMLFlow_MultiFlowRun(t *testing.T) { |
| 65 | + runCLI(t, "multi_flow_run_example.yaml") |
| 66 | +} |
| 67 | + |
| 68 | +func TestYAMLFlow_ExampleRun(t *testing.T) { |
| 69 | + runCLI(t, "example_run_yamlflow.yaml") |
| 70 | +} |
| 71 | + |
| 72 | +func TestYAMLFlow_TestRunField(t *testing.T) { |
| 73 | + runCLI(t, "test_run_field.yaml") |
| 74 | +} |
| 75 | + |
| 76 | +func TestYAMLFlow_GraphQLRun(t *testing.T) { |
| 77 | + runCLI(t, "graphql_run_example.yaml") |
| 78 | +} |
0 commit comments