fake_process_manager_test.dart 3.82 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

8
import 'common.dart';
9 10 11 12 13 14 15 16 17 18 19 20 21

import 'fake_process_manager.dart';

void main() {
  group('ArchivePublisher', () {
    FakeProcessManager processManager;
    final List<String> stdinCaptured = <String>[];

    void _captureStdin(String item) {
      stdinCaptured.add(item);
    }

    setUp(() async {
22
      processManager = FakeProcessManager(stdinResults: _captureStdin);
23 24 25 26 27
    });

    tearDown(() async {});

    test('start works', () async {
28 29
      final Map<String, List<ProcessResult>> calls = <String, List<ProcessResult>>{
        'gsutil acl get gs://flutter_infra/releases/releases.json': <ProcessResult>[
30
          ProcessResult(0, 0, 'output1', ''),
31 32
        ],
        'gsutil cat gs://flutter_infra/releases/releases.json': <ProcessResult>[
33
          ProcessResult(0, 0, 'output2', ''),
34
        ],
35
      };
36
      processManager.fakeResults = calls;
37
      for (final String key in calls.keys) {
38 39 40 41 42 43
        final Process process = await processManager.start(key.split(' '));
        String output = '';
        process.stdout.listen((List<int> item) {
          output += utf8.decode(item);
        });
        await process.exitCode;
44
        expect(output, equals(calls[key][0].stdout));
45
      }
46
      processManager.verifyCalls(calls.keys.toList());
47 48 49
    });

    test('run works', () async {
50 51
      final Map<String, List<ProcessResult>> calls = <String, List<ProcessResult>>{
        'gsutil acl get gs://flutter_infra/releases/releases.json': <ProcessResult>[
52
          ProcessResult(0, 0, 'output1', ''),
53 54
        ],
        'gsutil cat gs://flutter_infra/releases/releases.json': <ProcessResult>[
55
          ProcessResult(0, 0, 'output2', ''),
56
        ],
57
      };
58
      processManager.fakeResults = calls;
59
      for (final String key in calls.keys) {
60
        final ProcessResult result = await processManager.run(key.split(' '));
61
        expect(result.stdout, equals(calls[key][0].stdout));
62
      }
63
      processManager.verifyCalls(calls.keys.toList());
64 65 66
    });

    test('runSync works', () async {
67 68
      final Map<String, List<ProcessResult>> calls = <String, List<ProcessResult>>{
        'gsutil acl get gs://flutter_infra/releases/releases.json': <ProcessResult>[
69
          ProcessResult(0, 0, 'output1', ''),
70 71
        ],
        'gsutil cat gs://flutter_infra/releases/releases.json': <ProcessResult>[
72
          ProcessResult(0, 0, 'output2', ''),
73
        ],
74
      };
75
      processManager.fakeResults = calls;
76
      for (final String key in calls.keys) {
77
        final ProcessResult result = processManager.runSync(key.split(' '));
78
        expect(result.stdout, equals(calls[key][0].stdout));
79
      }
80
      processManager.verifyCalls(calls.keys.toList());
81 82 83
    });

    test('captures stdin', () async {
84 85
      final Map<String, List<ProcessResult>> calls = <String, List<ProcessResult>>{
        'gsutil acl get gs://flutter_infra/releases/releases.json': <ProcessResult>[
86
          ProcessResult(0, 0, 'output1', ''),
87 88
        ],
        'gsutil cat gs://flutter_infra/releases/releases.json': <ProcessResult>[
89
          ProcessResult(0, 0, 'output2', ''),
90
        ],
91
      };
92
      processManager.fakeResults = calls;
93
      for (final String key in calls.keys) {
94 95 96 97 98
        final Process process = await processManager.start(key.split(' '));
        String output = '';
        process.stdout.listen((List<int> item) {
          output += utf8.decode(item);
        });
99
        final String testInput = '${calls[key][0].stdout} input';
100 101
        process.stdin.add(testInput.codeUnits);
        await process.exitCode;
102
        expect(output, equals(calls[key][0].stdout));
103 104
        expect(stdinCaptured.last, equals(testInput));
      }
105
      processManager.verifyCalls(calls.keys.toList());
106 107 108
    });
  });
}