|
| 1 | +defmodule Mix.Tasks.Test.Apps.Start do |
| 2 | + use Mix.Task |
| 3 | + |
| 4 | + @shortdoc "Start an integration test app for manual testing" |
| 5 | + |
| 6 | + @moduledoc """ |
| 7 | + Starts an integration test application for manual testing with a custom DSN. |
| 8 | +
|
| 9 | + ## Usage |
| 10 | +
|
| 11 | + $ mix test.apps.start --app phoenix_app --dsn YOUR_DSN |
| 12 | +
|
| 13 | + ## Options |
| 14 | +
|
| 15 | + * `--app` - The integration app to start. Available apps: |
| 16 | + * `phoenix_app` (default) - Phoenix LiveView application with Oban and OpenTelemetry |
| 17 | +
|
| 18 | + * `--dsn` - The Sentry DSN to use (required). Can be a full DSN URL or |
| 19 | + omitted to use the DSN from environment variables. |
| 20 | +
|
| 21 | + * `--environment` - The environment name to report to Sentry (default: "manual-test") |
| 22 | +
|
| 23 | + ## Examples |
| 24 | +
|
| 25 | + # Start phoenix_app with a custom DSN |
| 26 | + $ mix test.apps.start --dsn https://public@sentry.io/123 |
| 27 | +
|
| 28 | + # Use DSN from SENTRY_DSN environment variable |
| 29 | + $ export SENTRY_DSN=https://public@sentry.io/123 |
| 30 | + $ mix test.apps.start |
| 31 | +
|
| 32 | + """ |
| 33 | + |
| 34 | + @switches [ |
| 35 | + app: :string, |
| 36 | + dsn: :string, |
| 37 | + environment: :string |
| 38 | + ] |
| 39 | + |
| 40 | + @available_apps ["phoenix_app"] |
| 41 | + |
| 42 | + @impl true |
| 43 | + def run(args) when is_list(args) do |
| 44 | + {opts, _args} = OptionParser.parse!(args, strict: @switches) |
| 45 | + |
| 46 | + app = Keyword.get(opts, :app, "phoenix_app") |
| 47 | + dsn = Keyword.get(opts, :dsn) || System.get_env("SENTRY_DSN") |
| 48 | + environment = Keyword.get(opts, :environment, "manual-test") |
| 49 | + |
| 50 | + unless app in @available_apps do |
| 51 | + Mix.raise(""" |
| 52 | + Invalid app: #{app} |
| 53 | +
|
| 54 | + Available apps: |
| 55 | + #{Enum.map_join(@available_apps, "\n", &" - #{&1}")} |
| 56 | + """) |
| 57 | + end |
| 58 | + |
| 59 | + unless dsn do |
| 60 | + Mix.raise(""" |
| 61 | + No DSN provided. Please provide a DSN via: |
| 62 | + --dsn flag: mix test.apps.start --dsn YOUR_DSN |
| 63 | + Or set SENTRY_DSN environment variable |
| 64 | + """) |
| 65 | + end |
| 66 | + |
| 67 | + app_path = Path.join("test_integrations", app) |
| 68 | + |
| 69 | + unless File.dir?(app_path) do |
| 70 | + Mix.raise("Integration app not found: #{app_path}") |
| 71 | + end |
| 72 | + |
| 73 | + Mix.shell().info([ |
| 74 | + :cyan, |
| 75 | + :bright, |
| 76 | + "\n==> Starting integration app: #{app}", |
| 77 | + :reset |
| 78 | + ]) |
| 79 | + |
| 80 | + Mix.shell().info("DSN: #{mask_dsn(dsn)}") |
| 81 | + Mix.shell().info("Environment: #{environment}\n") |
| 82 | + |
| 83 | + # Set up dependencies |
| 84 | + Mix.shell().info("Installing dependencies...") |
| 85 | + |
| 86 | + case System.cmd("mix", ["deps.get"], cd: app_path, into: IO.stream(:stdio, :line)) do |
| 87 | + {_, 0} -> :ok |
| 88 | + {_, status} -> Mix.raise("Failed to install dependencies (exit status: #{status})") |
| 89 | + end |
| 90 | + |
| 91 | + # Check if overmind is available |
| 92 | + case System.cmd("which", ["overmind"], stderr_to_stdout: true) do |
| 93 | + {_, 0} -> |
| 94 | + # Set environment variables |
| 95 | + env = [ |
| 96 | + {"SENTRY_DSN", dsn}, |
| 97 | + {"SENTRY_ENVIRONMENT", environment} |
| 98 | + ] |
| 99 | + |
| 100 | + # Start the application |
| 101 | + Mix.shell().info([ |
| 102 | + :green, |
| 103 | + :bright, |
| 104 | + "\n==> Starting #{app} with Overmind...", |
| 105 | + :reset, |
| 106 | + "\n" |
| 107 | + ]) |
| 108 | + |
| 109 | + System.cmd("overmind", ["start"], |
| 110 | + cd: app_path, |
| 111 | + into: IO.stream(:stdio, :line), |
| 112 | + env: env |
| 113 | + ) |
| 114 | + |
| 115 | + _ -> |
| 116 | + Mix.raise(""" |
| 117 | + Overmind is not installed. Please install it: |
| 118 | +
|
| 119 | + macOS: brew install overmind tmux |
| 120 | + Linux: go install github.com/DarthSim/overmind/v2@latest |
| 121 | +
|
| 122 | + Then add to PATH: export PATH=$PATH:$(go env GOPATH)/bin |
| 123 | + """) |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + defp mask_dsn(dsn) do |
| 128 | + case URI.parse(dsn) do |
| 129 | + %URI{userinfo: userinfo} when is_binary(userinfo) -> |
| 130 | + String.replace(dsn, userinfo, "***") |
| 131 | + |
| 132 | + _ -> |
| 133 | + dsn |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments