common.dart 2.55 KB
Newer Older
1 2 3 4 5
// 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.


6
import 'package:args/args.dart';
7
import 'package:conductor/stdio.dart';
8
import 'package:test/test.dart';
9

10
export 'package:test/test.dart' hide isInstanceOf;
11

12 13 14 15 16 17 18 19 20 21
Matcher throwsAssertionWith(String messageSubString) {
  return throwsA(
      isA<AssertionError>().having(
          (AssertionError e) => e.toString(),
          'description',
          contains(messageSubString),
      ),
  );
}

22 23 24 25 26 27 28 29 30 31
Matcher throwsExceptionWith(String messageSubString) {
  return throwsA(
      isA<Exception>().having(
          (Exception e) => e.toString(),
          'description',
          contains(messageSubString),
      ),
  );
}

32
class TestStdio extends Stdio {
33 34
  TestStdio({
    this.verbose = false,
35 36
    List<String>? stdin,
  }) : _stdin = stdin ?? <String>[];
37

38 39 40
  String get error => logs.where((String log) => log.startsWith(r'[error] ')).join('\n');

  String get stdout => logs.where((String log) {
41
    return log.startsWith(r'[status] ') || log.startsWith(r'[trace] ') || log.startsWith(r'[write] ');
42
  }).join('\n');
43 44

  final bool verbose;
45
  late final List<String> _stdin;
46
  List<String> get stdin => _stdin;
47 48 49 50 51 52 53 54 55 56 57 58

  @override
  String readLineSync() {
    if (_stdin.isEmpty) {
      throw Exception('Unexpected call to readLineSync!');
    }
    return _stdin.removeAt(0);
  }
}

class FakeArgResults implements ArgResults {
  FakeArgResults({
59
    required String? level,
60
    required String candidateBranch,
61
    String remote = 'upstream',
62 63 64 65 66 67 68
    bool justPrint = false,
    bool autoApprove = true, // so we don't have to mock stdin
    bool help = false,
    bool force = false,
    bool skipTagging = false,
  }) : _parsedArgs = <String, dynamic>{
    'increment': level,
69
    'candidate-branch': candidateBranch,
70 71 72 73 74 75 76 77 78
    'remote': remote,
    'just-print': justPrint,
    'yes': autoApprove,
    'help': help,
    'force': force,
    'skip-tagging': skipTagging,
  };

  @override
79
  String? name;
80 81

  @override
82
  ArgResults? command;
83 84 85 86 87

  @override
  final List<String> rest = <String>[];

  @override
88 89 90 91
  List<String> get arguments {
    assert(false, 'not yet implemented');
    return <String>[];
  }
92 93 94 95 96

  final Map<String, dynamic> _parsedArgs;

  @override
  Iterable<String> get options {
97 98
    assert(false, 'not yet implemented');
    return <String>[];
99 100 101 102 103 104 105 106 107
  }

  @override
  dynamic operator [](String name) {
    return _parsedArgs[name];
  }

  @override
  bool wasParsed(String name) {
108 109
    assert(false, 'not yet implemented');
    return false;
110 111
  }
}