customer_test.dart 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

import 'package:meta/meta.dart';

@immutable
class CustomerTest {
  factory CustomerTest(File testFile) {
    final String errorPrefix = 'Could not parse: ${testFile.path}\n';
    final List<String> contacts = <String>[];
    final List<String> fetch = <String>[];
    final List<Directory> update = <Directory>[];
    final List<String> test = <String>[];
    bool hasTests = false;
    for (final String line in testFile.readAsLinesSync().map((String line) => line.trim())) {
      if (line.isEmpty) {
        // blank line
      } else if (line.startsWith('#')) {
        // comment
      } else if (line.startsWith('contact=')) {
        contacts.add(line.substring(8));
      } else if (line.startsWith('fetch=')) {
        fetch.add(line.substring(6));
      } else if (line.startsWith('update=')) {
        update.add(Directory(line.substring(7)));
      } else if (line.startsWith('test=')) {
        hasTests = true;
        test.add(line.substring(5));
      } else if (line.startsWith('test.windows=')) {
        hasTests = true;
34
        if (Platform.isWindows) {
35
          test.add(line.substring(13));
36
        }
37 38
      } else if (line.startsWith('test.macos=')) {
        hasTests = true;
39
        if (Platform.isMacOS) {
40
          test.add(line.substring(11));
41
        }
42 43
      } else if (line.startsWith('test.linux=')) {
        hasTests = true;
44
        if (Platform.isLinux) {
45
          test.add(line.substring(11));
46
        }
47 48
      } else if (line.startsWith('test.posix=')) {
        hasTests = true;
49
        if (Platform.isLinux || Platform.isMacOS) {
50
          test.add(line.substring(11));
51
        }
52 53 54 55
      } else {
        throw FormatException('${errorPrefix}Unexpected directive:\n$line');
      }
    }
56
    if (contacts.isEmpty) {
57
      throw FormatException('${errorPrefix}No contacts specified. At least one contact e-mail address must be specified.');
58
    }
59
    for (final String email in contacts) {
60
      if (!email.contains(_email) || email.endsWith('@example.com')) {
61
        throw FormatException('${errorPrefix}The following e-mail address appears to be an invalid e-mail address: $email');
62
      }
63
    }
64
    if (fetch.isEmpty) {
65
      throw FormatException('${errorPrefix}No "fetch" directives specified. Two lines are expected: "git clone https://github.com/USERNAME/REPOSITORY.git tests" and "git -C tests checkout HASH".');
66 67
    }
    if (fetch.length < 2) {
68
      throw FormatException('${errorPrefix}Only one "fetch" directive specified. Two lines are expected: "git clone https://github.com/USERNAME/REPOSITORY.git tests" and "git -C tests checkout HASH".');
69 70
    }
    if (!fetch[0].contains(_fetch1)) {
71
      throw FormatException('${errorPrefix}First "fetch" directive does not match expected pattern (expected "git clone https://github.com/USERNAME/REPOSITORY.git tests").');
72 73
    }
    if (!fetch[1].contains(_fetch2)) {
74
      throw FormatException('${errorPrefix}Second "fetch" directive does not match expected pattern (expected "git -C tests checkout HASH").');
75 76
    }
    if (update.isEmpty) {
77
      throw FormatException('${errorPrefix}No "update" directives specified. At least one directory must be specified. (It can be "." to just upgrade the root of the repository.)');
78 79
    }
    if (!hasTests) {
80
      throw FormatException('${errorPrefix}No "test" directives specified. At least one command must be specified to run tests.');
81
    }
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    return CustomerTest._(
      List<String>.unmodifiable(contacts),
      List<String>.unmodifiable(fetch),
      List<Directory>.unmodifiable(update),
      List<String>.unmodifiable(test),
    );
  }

  const CustomerTest._(this.contacts, this.fetch, this.update, this.tests);

  // (e-mail regexp from HTML standard)
  static final RegExp _email = RegExp(r"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
  static final RegExp _fetch1 = RegExp(r'^git(?: -c core.longPaths=true)? clone https://github.com/[-a-zA-Z0-9]+/[-_a-zA-Z0-9]+.git tests$');
  static final RegExp _fetch2 = RegExp(r'^git(?: -c core.longPaths=true)? -C tests checkout [0-9a-f]+$');

  final List<String> contacts;
  final List<String> fetch;
  final List<Directory> update;
  final List<String> tests;
}