parseTestPlan function

TestPlanV1? parseTestPlan([
  1. Map<String, String>? environment
])

Parses an Allure test plan from ALLURE_TESTPLAN_PATH.

Implementation

TestPlanV1? parseTestPlan([Map<String, String>? environment]) {
  final source = environment ?? Platform.environment;
  final path = source['ALLURE_TESTPLAN_PATH'];
  if (path == null || path.isEmpty) {
    return null;
  }

  final file = File(path);
  if (!file.existsSync()) {
    stderr.writeln('Allure: test plan file does not exist: $path');
    return null;
  }

  try {
    final decoded = jsonDecode(file.readAsStringSync());
    if (decoded is! Map<String, dynamic>) {
      stderr.writeln('Allure: test plan root must be a JSON object: $path');
      return null;
    }
    final version = decoded['version'];
    if (version != null && version.toString() != '1.0') {
      stderr.writeln('Allure: unsupported test plan version: $version');
      return null;
    }
    final tests = decoded['tests'];
    if (tests is! List) {
      stderr.writeln('Allure: test plan does not contain a tests array');
      return null;
    }
    final entries = tests
        .whereType<Map>()
        .where((entry) {
          final hasId = entry['id'] != null;
          final hasSelector = entry['selector'] != null &&
              entry['selector'].toString().isNotEmpty;
          if (!hasId && !hasSelector) {
            stderr.writeln('Allure: ignoring malformed test plan entry');
          }
          return hasId || hasSelector;
        })
        .map(
          (entry) => TestPlanEntry(
            id: entry['id'],
            selector: entry['selector']?.toString(),
          ),
        )
        .toList();
    if (entries.isEmpty) {
      return null;
    }
    return TestPlanV1(tests: entries);
  } catch (error) {
    stderr.writeln('Allure: unable to parse test plan: $error');
    return null;
  }
}