Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apps/cli-docs/src/fragments/commands/wizard.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ takes an optional `<org>/<project>` first, then an optional integration:
like an integration, write `my-org/nextjs`.
- `-f`/`--force` changes a project with uncommitted changes without asking,
and installs the SDK even when the package manager's checks fail.
- For `appleSnapshots`, `--app-target` and `--hosted-test-target` name the
Xcode app target and hosted XCTest target to use instead of picking them.
Without a terminal, the wizard uses the only target or stops with the list,
so pass these when the project has several.

It logs in with your `sentry auth login` account, or starts a login when you
have none, on the Sentry instance the rest of the CLI uses: `SENTRY_URL`, or
Expand All @@ -35,7 +39,7 @@ changes nothing.
## Coming from `@sentry/wizard`

`sentry wizard` takes the same integration ids as `npx @sentry/wizard -i`,
but not its flags or `SENTRY_WIZARD_*` environment variables:
but not most of its flags or its `SENTRY_WIZARD_*` environment variables:

| `@sentry/wizard` | `sentry wizard` |
| -------------------------------------- | ------------------------------------------------ |
Expand All @@ -46,6 +50,9 @@ but not its flags or `SENTRY_WIZARD_*` environment variables:
| `--force-install`, `--ignore-git-changes` | `-f`/`--force` |
| `--disable-telemetry` | `SENTRY_CLI_NO_TELEMETRY=1` or `DO_NOT_TRACK=1` |
| `--debug` | `--verbose` or `--log-level debug` |
| `--app-target`, `--hosted-test-target` | The same flags |
| `--non-interactive` | Automatic when stdin is not a terminal |
| `--xcode-project-dir` | Run the wizard in that directory |
| `--signup`, `--promo-code`, `--coming-from` | Sign up at https://sentry.io/signup/ first |

The Cordova and Electron wizards were retired. Follow their setup guides in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Set up the Sentry SDK in your project interactively

**Flags:**
- `-f, --force - Change a project with uncommitted changes without asking, and install the SDK even when the package manager's checks fail`
- `--app-target <value> - Xcode app target that SnapshotPreviews uses, instead of picking one (appleSnapshots only)`
- `--hosted-test-target <value> - Hosted XCTest target that renders the snapshots, instead of picking one (appleSnapshots only)`

**Examples:**

Expand Down
22 changes: 21 additions & 1 deletion packages/cli/src/commands/wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
parseOrgProjectInput,
} from "../lib/arg-parsing.js";
import { buildCommand } from "../lib/command.js";
import { ORG_PROJECT_TARGET } from "../lib/completion-target.js";
import { ORG_PROJECT_TARGET, parseText } from "../lib/completion-target.js";
import { getEnv } from "../lib/env.js";
import { ValidationError, WizardExitError } from "../lib/errors.js";
import { parseBoolValue } from "../lib/parse-bool.js";
Expand All @@ -39,6 +39,8 @@ const usageHint = () => sentry`wizard ${USAGE}`;

type WizardFlags = {
readonly force: boolean;
readonly "app-target"?: string;
readonly "hosted-test-target"?: string;
};

/** Parsed `sentry wizard` arguments. */
Expand Down Expand Up @@ -208,6 +210,20 @@ export const wizardCommand = buildCommand({
"Change a project with uncommitted changes without asking, and install the SDK even when the package manager's checks fail",
default: false,
},
"app-target": {
kind: "parsed",
parse: parseText,
optional: true,
brief:
"Xcode app target that SnapshotPreviews uses, instead of picking one (appleSnapshots only)",
},
"hosted-test-target": {
kind: "parsed",
parse: parseText,
optional: true,
brief:
"Hosted XCTest target that renders the snapshots, instead of picking one (appleSnapshots only)",
},
},
aliases: { f: "force" },
},
Expand All @@ -224,6 +240,10 @@ export const wizardCommand = buildCommand({
ignoreGitChanges: flags.force,
spotlight: isSpotlightEnabled(),
},
appleSnapshotsTargets: {
appTarget: flags["app-target"],
hostedTestTarget: flags["hosted-test-target"],
},
});
} catch (error) {
if (!(error instanceof WizardExitError)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/lib/wizard/apple/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ export interface AppleWizardOptions extends WizardOptions {
export interface AppleSnapshotsWizardOptions extends AppleWizardOptions {
/** Fail with the choices instead of prompting, such as when stdin is not a terminal. */
nonInteractive: boolean;
/** App target to use instead of picking one (`--app-target`). */
appTarget?: string | undefined;
/** Hosted XCTest target to use instead of picking one (`--hosted-test-target`). */
hostedTestTarget?: string | undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ async function resolveAppTargetName(
options: AppleSnapshotsWizardOptions,
): Promise<string | undefined> {
const appTargetNames = xcodeProject.getAllTargets();
if (options.appTarget) {
if (appTargetNames.includes(options.appTarget)) {
return options.appTarget;
}

clack.log.error(
`Xcode app target ${
options.appTarget
} was not found. Available app targets: ${formatList(appTargetNames)}.`,
);
return undefined;
}

if (appTargetNames.length === 0) {
clack.log.error("No application target found.");
return undefined;
Expand All @@ -197,7 +210,7 @@ async function resolveAppTargetName(
[
"Could not automatically select an Xcode app target in non-interactive mode.",
`Available app targets: ${formatList(appTargetNames)}.`,
"Run the wizard in a terminal to choose the app target that SnapshotPreviews should use.",
"Pass --app-target <target-name> to select the app target that SnapshotPreviews should use.",
].join(" "),
);
return undefined;
Expand All @@ -216,6 +229,22 @@ async function resolveHostedTestTargetName(
options: AppleSnapshotsWizardOptions,
): Promise<string | undefined> {
const hostedTestTargetNames = xcodeProject.getHostedUnitTestTargetNames();
if (options.hostedTestTarget) {
if (hostedTestTargetNames.includes(options.hostedTestTarget)) {
return options.hostedTestTarget;
}

clack.log.error(
[
`Hosted XCTest target ${options.hostedTestTarget} was not found or does not define TEST_HOST.`,
`Available hosted XCTest targets: ${formatList(
hostedTestTargetNames,
)}.`,
].join(" "),
);
return undefined;
}

const inferredHostedTestTargetNames =
xcodeProject.getHostedUnitTestTargetNamesForApplicationTarget(
appTargetName,
Expand Down Expand Up @@ -276,7 +305,7 @@ async function selectHostedTestTargetName({
`Available hosted XCTest targets: ${formatList(
hostedTestTargetNames,
)}.`,
"Run the wizard in a terminal to choose the target.",
"Pass --hosted-test-target <target-name> to select the target explicitly.",
manualAppleSnapshotsSetupInstructions({
appTargetName,
hostedTestTargetName: hostedTestTargetNames[0],
Expand Down
24 changes: 20 additions & 4 deletions packages/cli/src/lib/wizard/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { isatty } from "node:tty";
import { ValidationError } from "../errors.js";
import type { AppleSnapshotsWizardOptions } from "./apple/options.js";
import { abortIfCancelled } from "./utils/clack/index.js";
import { clack } from "./utils/clack/prompts.js";
import type { WizardOptions } from "./utils/types.js";
Expand Down Expand Up @@ -44,8 +45,17 @@ export type WizardIntegration = (typeof WIZARD_INTEGRATIONS)[number]["id"];
export const INTEGRATION_IDS: readonly WizardIntegration[] =
WIZARD_INTEGRATIONS.map((integration) => integration.id);

/** `--app-target` and `--hosted-test-target`, which only Apple Snapshots reads. */
type AppleSnapshotsTargets = Pick<
AppleSnapshotsWizardOptions,
"appTarget" | "hostedTestTarget"
>;

/** Runs the wizard of one integration. */
type WizardRunner = (options: WizardOptions) => Promise<void>;
type WizardRunner = (
options: WizardOptions,
appleSnapshotsTargets: AppleSnapshotsTargets,
) => Promise<void>;

/**
* The flow of each integration. Integrations without one are still being
Expand All @@ -60,12 +70,16 @@ const WIZARD_RUNNERS: Partial<Record<WizardIntegration, WizardRunner>> = {
const { runAppleWizard } = await import("./apple/apple-wizard.js");
await runAppleWizard(options);
},
appleSnapshots: async (options) => {
appleSnapshots: async (options, appleSnapshotsTargets) => {
const { runAppleSnapshotsWizard } =
await import("./apple/snapshots/apple-snapshots-wizard.js");
// Without a terminal to prompt on, pick the only target or fail with
// the choices, as the CLI does for auto-login (`isatty(0)`).
await runAppleSnapshotsWizard({ ...options, nonInteractive: !isatty(0) });
await runAppleSnapshotsWizard({
...options,
...appleSnapshotsTargets,
nonInteractive: !isatty(0),
});
},
reactNative: async (options) => {
const { runReactNativeWizard } =
Expand All @@ -80,6 +94,8 @@ export type RunWizardArgs = {
integration?: WizardIntegration | undefined;
/** The options every wizard flow takes. */
wizardOptions: WizardOptions;
/** The Xcode targets Apple Snapshots uses instead of picking them. */
appleSnapshotsTargets?: AppleSnapshotsTargets | undefined;
};

/**
Expand All @@ -99,7 +115,7 @@ export async function runWizard(args: RunWizardArgs): Promise<void> {
"integration",
);
}
await runner(args.wizardOptions);
await runner(args.wizardOptions, args.appleSnapshotsTargets ?? {});
}

/** Ask which integration to set up, out of the ones that have a flow. */
Expand Down
42 changes: 33 additions & 9 deletions packages/cli/test/commands/wizard.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
/**
* Tests for `sentry wizard`: its command line (`[<org>/<project>]
* [<integration>]` and `--force`, nothing else), what it passes to a wizard
* flow, and how a stopped wizard sets the exit code.
* [<integration>]`, `--force`, and the Apple Snapshots target flags), what it
* passes to a wizard flow, and how a stopped wizard sets the exit code.
*/

import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

const { runNextjsWizard, isTelemetryEnabled } = vi.hoisted(() => ({
runNextjsWizard: vi.fn<(options: unknown) => Promise<void>>(),
isTelemetryEnabled: vi.fn<() => boolean>(),
}));
const { runNextjsWizard, runAppleSnapshotsWizard, isTelemetryEnabled } =
vi.hoisted(() => ({
runNextjsWizard: vi.fn<(options: unknown) => Promise<void>>(),
runAppleSnapshotsWizard: vi.fn<(options: unknown) => Promise<void>>(),
isTelemetryEnabled: vi.fn<() => boolean>(),
}));
vi.mock("../../src/lib/wizard/nextjs/nextjs-wizard.js", () => ({
runNextjsWizard,
}));
vi.mock(
"../../src/lib/wizard/apple/snapshots/apple-snapshots-wizard.js",
() => ({ runAppleSnapshotsWizard }),
);
vi.mock("../../src/lib/telemetry.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../../src/lib/telemetry.js")>()),
isTelemetryEnabled,
Expand Down Expand Up @@ -80,7 +86,7 @@ describe("sentry wizard flags", () => {
},
}).parameters;

test("--force (-f) is the wizard's only flag, and it is not hidden", () => {
test("the wizard's flags are --force (-f) and the Apple Snapshots targets, none hidden", () => {
const { flags = {}, aliases = {} } = wizardCommand.parameters as {
flags?: Record<string, { hidden?: boolean }>;
aliases?: Record<string, string>;
Expand All @@ -92,8 +98,10 @@ describe("sentry wizard flags", () => {
([alias]) => !(alias in (injected.aliases ?? {})),
);

expect(ownFlags).toEqual(["force"]);
expect(flags["force"]?.hidden).toBeUndefined();
expect(ownFlags).toEqual(["force", "app-target", "hosted-test-target"]);
for (const name of ownFlags) {
expect(flags[name]?.hidden).toBeUndefined();
}
expect(Object.fromEntries(ownAliases)).toEqual({ f: "force" });
});
});
Expand Down Expand Up @@ -206,6 +214,22 @@ describe("sentry wizard", () => {
});
});

test("--app-target and --hosted-test-target reach the Apple Snapshots flow", async () => {
runAppleSnapshotsWizard.mockResolvedValue(undefined);

await runWizardCommand(
{ "app-target": "App", "hosted-test-target": "AppTests" },
"appleSnapshots",
);

expect(runAppleSnapshotsWizard).toHaveBeenCalledWith(
expect.objectContaining({
appTarget: "App",
hostedTestTarget: "AppTests",
}),
);
});

test("--force both force-installs and skips the uncommitted changes prompt", async () => {
await runWizardCommand({ force: true }, "nextjs");

Expand Down
Loading
Loading