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

5
import 'package:args/args.dart';
6
import 'package:conductor_core/src/stdio.dart';
7
import 'package:test/test.dart';
8

9
export 'package:test/test.dart' hide isInstanceOf;
10
export '../../../../packages/flutter_tools/test/src/fake_process_manager.dart';
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
    List<String>? stdin,
36
  }) : 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
  final List<String> stdin;
46 47 48

  @override
  String readLineSync() {
49
    if (stdin.isEmpty) {
50
      throw Exception('Unexpected call to readLineSync! Last stdout was ${logs.last}');
51
    }
52
    return stdin.removeAt(0);
53 54 55 56 57
  }
}

class FakeArgResults implements ArgResults {
  FakeArgResults({
58
    required String? level,
59
    required String candidateBranch,
60
    String remote = 'upstream',
61 62 63 64 65 66 67
    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,
68
    'candidate-branch': candidateBranch,
69 70 71 72 73 74 75 76 77
    'remote': remote,
    'just-print': justPrint,
    'yes': autoApprove,
    'help': help,
    'force': force,
    'skip-tagging': skipTagging,
  };

  @override
78
  String? name;
79 80

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

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

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

  final Map<String, dynamic> _parsedArgs;

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

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

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