symbolize_test.dart 4.21 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 'dart:async';
6 7 8 9
import 'dart:typed_data';

import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/symbolize.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:mockito/mockito.dart';

import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/mocks.dart';


void main() {
  MemoryFileSystem fileSystem;
  MockStdio stdio;
  SymbolizeCommand command;
  MockDwarfSymbolizationService mockDwarfSymbolizationService;

  setUpAll(() {
    Cache.disableLocking();
  });

  setUp(() {
    fileSystem = MemoryFileSystem.test();
    stdio = MockStdio();
    mockDwarfSymbolizationService = MockDwarfSymbolizationService();
    command = SymbolizeCommand(
      stdio: stdio,
      fileSystem: fileSystem,
      dwarfSymbolizationService: mockDwarfSymbolizationService,
    );
  });

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  testUsingContext('Regression test for type error in codec', () async {
    final DwarfSymbolizationService symbolizationService = DwarfSymbolizationService.test();
    final StreamController<List<int>> output = StreamController<List<int>>();

    unawaited(symbolizationService.decode(
      input: Stream<Uint8List>.fromIterable(<Uint8List>[
        utf8.encode('Hello, World\n') as Uint8List,
      ]),
      symbols: Uint8List(0),
      output: IOSink(output.sink),
    ));

    await expectLater(
      output.stream.transform(utf8.decoder),
      emits('Hello, World'),
    );
  });

60 61 62 63 64

  testUsingContext('symbolize exits when --debug-info argument is missing', () async {
    final Future<void> result = createTestCommandRunner(command)
      .run(const <String>['symbolize']);

65
    expect(result, throwsToolExit(message: '"--debug-info" is required to symbolize stack traces.'));
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  });

  testUsingContext('symbolize exits when --debug-info file is missing', () async {
    final Future<void> result = createTestCommandRunner(command)
      .run(const <String>['symbolize', '--debug-info=app.debug']);

    expect(result, throwsToolExit(message: 'app.debug does not exist.'));
  });

  testUsingContext('symbolize exits when --input file is missing', () async {
    fileSystem.file('app.debug').createSync();
    final Future<void> result = createTestCommandRunner(command)
      .run(const <String>['symbolize', '--debug-info=app.debug', '--input=foo.stack', '--output=results/foo.result']);

    expect(result, throwsToolExit(message: ''));
  });

83
  testUsingContext('symbolize succeeds when DwarfSymbolizationService does not throw', () async {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    fileSystem.file('app.debug').writeAsBytesSync(<int>[1, 2, 3]);
    fileSystem.file('foo.stack').writeAsStringSync('hello');

    when(mockDwarfSymbolizationService.decode(
      input: anyNamed('input'),
      output: anyNamed('output'),
      symbols: anyNamed('symbols'))
    ).thenAnswer((Invocation invocation) async {
      // Data is passed correctly to service
      expect((await (invocation.namedArguments[#input] as Stream<List<int>>).toList()).first,
        utf8.encode('hello'));
      expect(invocation.namedArguments[#symbols] as Uint8List, <int>[1, 2, 3,]);
      return;
    });

    await createTestCommandRunner(command)
      .run(const <String>['symbolize', '--debug-info=app.debug', '--input=foo.stack', '--output=results/foo.result']);
  });

  testUsingContext('symbolize throws when DwarfSymbolizationService throws', () async {
    fileSystem.file('app.debug').writeAsBytesSync(<int>[1, 2, 3]);
    fileSystem.file('foo.stack').writeAsStringSync('hello');

    when(mockDwarfSymbolizationService.decode(
      input: anyNamed('input'),
      output: anyNamed('output'),
      symbols: anyNamed('symbols'))
    ).thenThrow(ToolExit('test'));

    expect(
      createTestCommandRunner(command).run(const <String>[
        'symbolize', '--debug-info=app.debug', '--input=foo.stack', '--output=results/foo.result']),
      throwsToolExit(message: 'test'),
    );
  });
}

class MockDwarfSymbolizationService extends Mock implements DwarfSymbolizationService {}