enable method

void enable({
  1. ExecutionMode mode = ExecutionMode.widget,
  2. String? targetPlatform,
  3. bool fixIosDeploymentTarget = false,
})

Temporarily wires the app for declarative tests.

Validates prerequisites before mutating files. On any failure, every touched file is restored and created files are deleted — even when enable never reached the success path.

targetPlatform is the selected device platform (ios / android). iOS deployment require/fix runs only for integration mode when this is 'ios'. Null (widget mode / no device) skips the iOS gate.

Implementation

void enable({
  ExecutionMode mode = ExecutionMode.widget,
  String? targetPlatform,
  bool fixIosDeploymentTarget = false,
}) {
  if (_enabled) return;
  _mode = mode;

  try {
    final existingEntrySource = TestEntryValidator.validateForPatching(
      appDir: appDir,
      entry: File(_testEntryPath),
      usesApplicationEntry: usesApplicationEntry,
      mode: mode,
      configuredPath: configuredEntryRelativePath,
      activePath: activeEntryRelativePath,
    );

    // Integration generation may adapt the widget entry when there is no
    // configured integration entry. Validate and read this optional source
    // during preflight, before dependency configuration mutates the pubspec.
    final widgetEntrySource = !usesApplicationEntry &&
            mode == ExecutionMode.integration &&
            existingEntrySource == null
        ? TestEntryValidator.readOptionalEntrySource(
            appDir: appDir,
            relativePath: testEntryRelativePath,
          )
        : null;

    final pubspec = File(_pubspecPath).readAsStringSync();
    if (!usesApplicationEntry && existingEntrySource == null) {
      _mutations.captureValidatedContents(
        _testEntryPath,
        null,
      );
    }

    // The pubspec source is already in memory for dependency activation.
    // Reuse it when generating an entry instead of reopening the same file.
    String? resolvedPackageName;
    String packageName() => resolvedPackageName ??=
        TestEntryGenerator.packageNameFromPubspec(pubspec);
    final preparedIntegrationEntry =
        usesApplicationEntry && mode == ExecutionMode.integration
            ? IntegrationEntryMaterializer.prepare(
                appDir: appDir,
                sourceRelativePath: configuredEntryRelativePath,
                source: existingEntrySource!,
                packageName: packageName,
                sourceHasApplicationEntry: true,
              )
            : null;
    final activated = _dependencyConfigurator.activate(pubspec, mode: mode);
    if (activated != pubspec) {
      _mutations.writeValidated(
        _pubspecPath,
        activated,
        originalContents: pubspec,
      );
    }

    if (!usesApplicationEntry) {
      final testEntryExisted = existingEntrySource != null;

      if (!testEntryExisted) {
        _mutations.createDirectory(_testDirPath);
        _mutations.writeValidated(
          _testEntryPath,
          TestEntryGenerator.generate(
            mode: mode,
            packageName: packageName(),
            widgetEntrySource: mode == ExecutionMode.widget
                ? existingEntrySource
                : widgetEntrySource,
          ),
          originalContents: null,
        );
      }
    } else if (mode == ExecutionMode.integration) {
      _materializeApplicationIntegrationEntry(preparedIntegrationEntry);
    }

    _platformSupport.prepareForTestRun(
      mode: mode,
      targetPlatform: targetPlatform,
      fixIosDeploymentTarget: fixIosDeploymentTarget,
    );

    _enabled = true;
  } catch (_) {
    _rollbackMutations();
    rethrow;
  }
}