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

5 6
// @dart = 2.8

7 8
import 'package:file/file.dart';
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/base/io.dart';
10
import 'package:flutter_tools/src/base/logger.dart';
11
import 'package:flutter_tools/src/devfs.dart';
12
import 'package:flutter_tools/src/project.dart';
13
import 'package:flutter_tools/src/reporting/github_template.dart';
14 15 16

import '../src/common.dart';
import '../src/context.dart';
17
import '../src/fake_http_client.dart';
18 19 20 21

const String _kShortURL = 'https://www.example.com/short';

void main() {
22 23 24 25
  BufferLogger logger;
  FileSystem fs;
  setUp(() {
    logger = BufferLogger.test();
26
    fs = MemoryFileSystem.test();
27 28
  });

29
  group('GitHub template creator', () {
30
    testWithoutContext('similar issues URL', () {
31
      expect(
32 33
        GitHubTemplateCreator.toolCrashSimilarIssuesURL('this is a 100% error'),
        'https://github.com/flutter/flutter/issues?q=is%3Aissue+this+is+a+100%25+error',
34 35 36
      );
    });

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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
    group('sanitized error message', () {
      testWithoutContext('ProcessException', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            const ProcessException('cd', <String>['path/to/something'])
          ),
          'ProcessException:  Command: cd, OS error code: 0',
        );
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            const ProcessException('cd', <String>['path/to/something'], 'message')
          ),
          'ProcessException: message Command: cd, OS error code: 0',
        );
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            const ProcessException('cd', <String>['path/to/something'], 'message', -19)
          ),
          'ProcessException: message Command: cd, OS error code: -19',
        );
      });

      testWithoutContext('FileSystemException', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            const FileSystemException('delete failed', 'path/to/something')
          ),
          'FileSystemException: delete failed, null',
        );
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            const FileSystemException('delete failed', 'path/to/something', OSError('message', -19))
          ),
          'FileSystemException: delete failed, OS Error: message, errno = -19',
        );
      });

      testWithoutContext('SocketException', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            SocketException(
              'message',
              osError: const OSError('message', -19),
              address: InternetAddress.anyIPv6,
              port: 2000
            )
          ),
          'SocketException: message, OS Error: message, errno = -19',
        );
      });

      testWithoutContext('DevFSException', () {
        final StackTrace stackTrace = StackTrace.fromString('''
#0      _File.open.<anonymous closure> (dart:io/file_impl.dart:366:9)
#1      _rootRunUnary (dart:async/zone.dart:1141:38)''');
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            DevFSException('message', ArgumentError('argument error message'), stackTrace)
          ),
          'DevFSException: message',
        );
      });

      testWithoutContext('ArgumentError', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            ArgumentError('argument error message')
          ),
          'ArgumentError: Invalid argument(s): argument error message',
        );
      });

      testWithoutContext('Error', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            FakeError()
          ),
          'FakeError: (#0      _File.open.<anonymous closure> (dart:io/file_impl.dart:366:9))',
        );
      });

      testWithoutContext('String', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
121 122 123 124 125 126 127 128 129 130 131 132 133
            'May have non-tool-internal info, very long string, 0b8abb4724aa590dd0f429683339b' // ignore: missing_whitespace_between_adjacent_strings
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
            '24aa590dd0f429683339b1e045a1594d0b8abb4724aa590dd0f429683339b1e045a1594d0b8abb'
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
          ),
          'String: <1,016 characters>',
        );
      });

      testWithoutContext('Exception', () {
        expect(
          GitHubTemplateCreator.sanitizedCrashException(
            Exception('May have non-tool-internal info')
          ),
          '_Exception',
        );
      });
    });

149 150
    group('new issue template URL', () {
      StackTrace stackTrace;
151
      Error error;
152 153
      const String command = 'flutter test';
      const String doctorText = ' [✓] Flutter (Channel report';
154 155 156

      setUp(() async {
        stackTrace = StackTrace.fromString('trace');
157
        error = ArgumentError('argument error message');
158 159 160
      });

      testUsingContext('shortened', () async {
161 162 163
        final GitHubTemplateCreator creator = GitHubTemplateCreator(
          fileSystem: fs,
          logger: logger,
164 165 166 167 168 169 170 171
          client: FakeHttpClient.list(<FakeRequest>[
            FakeRequest(Uri.parse('https://git.io'), method: HttpMethod.post, response: const FakeResponse(
              statusCode: 201,
              headers: <String, List<String>>{
                HttpHeaders.locationHeader: <String>[_kShortURL],
              }
            ))
          ]),
172 173 174 175
          flutterProjectFactory: FlutterProjectFactory(
            fileSystem: fs,
            logger: logger,
          ),
176
        );
177
        expect(
178
            await creator.toolCrashIssueTemplateGitHubURL(command, error, stackTrace, doctorText),
179 180 181
            _kShortURL
        );
      }, overrides: <Type, Generator>{
182
        FileSystem: () => MemoryFileSystem.test(),
183 184 185 186
        ProcessManager: () => FakeProcessManager.any(),
      });

      testUsingContext('with network failure', () async {
187 188 189
        final GitHubTemplateCreator creator = GitHubTemplateCreator(
          fileSystem: fs,
          logger: logger,
190 191 192 193 194
          client: FakeHttpClient.list(<FakeRequest>[
            FakeRequest(Uri.parse('https://git.io'), method: HttpMethod.post, response: const FakeResponse(
              statusCode: 500,
            ))
          ]),
195 196 197 198
          flutterProjectFactory: FlutterProjectFactory(
            fileSystem: fs,
            logger: logger,
          ),
199
        );
200
        expect(
201 202 203 204 205 206 207 208
            await creator.toolCrashIssueTemplateGitHubURL(command, error, stackTrace, doctorText),
          'https://github.com/flutter/flutter/issues/new?title=%5Btool_crash%5D+ArgumentError%3A+'
            'Invalid+argument%28s%29%3A+argument+error+message&body=%23%23+Command%0A%60%60%60%0A'
            'flutter+test%0A%60%60%60%0A%0A%23%23+Steps+to+Reproduce%0A1.+...%0A2.+...%0A3.+...%0'
            'A%0A%23%23+Logs%0AArgumentError%3A+Invalid+argument%28s%29%3A+argument+error+message'
            '%0A%60%60%60%0Atrace%0A%60%60%60%0A%60%60%60%0A+%5B%E2%9C%93%5D+Flutter+%28Channel+r'
            'eport%0A%60%60%60%0A%0A%23%23+Flutter+Application+Metadata%0ANo+pubspec+in+working+d'
            'irectory.%0A&labels=tool%2Csevere%3A+crash'
209
        );
210
        expect(logger.traceText, contains('Failed to shorten GitHub template URL'));
211
      }, overrides: <Type, Generator>{
212
        FileSystem: () => MemoryFileSystem.test(),
213 214 215 216
        ProcessManager: () => FakeProcessManager.any(),
      });

      testUsingContext('app metadata', () async {
217 218 219
        final GitHubTemplateCreator creator = GitHubTemplateCreator(
          fileSystem: fs,
          logger: logger,
220
          client: FakeHttpClient.any(),
221 222 223 224
          flutterProjectFactory: FlutterProjectFactory(
            fileSystem: fs,
            logger: logger,
          ),
225
        );
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        final Directory projectDirectory = fs.currentDirectory;

        projectDirectory
            .childFile('pubspec.yaml')
            .writeAsStringSync('''
name: failing_app
version: 2.0.1+100
flutter:
  uses-material-design: true
  module:
    androidX: true
    androidPackage: com.example.failing.android
    iosBundleIdentifier: com.example.failing.ios
''');

241
        final File pluginsFile = projectDirectory.childFile('.flutter-plugins');
242 243 244 245 246 247
        pluginsFile
            .writeAsStringSync('''
camera=/fake/pub.dartlang.org/camera-0.5.7+2/
device_info=/fake/pub.dartlang.org/pub.dartlang.org/device_info-0.4.1+4/
        ''');

248 249 250 251 252 253 254 255 256 257
        final File metadataFile = projectDirectory.childFile('.metadata');
        metadataFile
          .writeAsStringSync('''
version:
  revision: 0b8abb4724aa590dd0f429683339b1e045a1594d
  channel: stable

project_type: app
        ''');

258
        final String actualURL = await creator.toolCrashIssueTemplateGitHubURL(command, error, stackTrace, doctorText);
259
        final String actualBody = Uri.parse(actualURL).queryParameters['body'];
260 261
        const String expectedBody = '''
## Command
262 263 264 265 266 267 268 269 270 271
```
flutter test
```

## Steps to Reproduce
1. ...
2. ...
3. ...

## Logs
272
ArgumentError: Invalid argument(s): argument error message
273 274 275 276 277 278 279 280
```
trace
```
```
 [✓] Flutter (Channel report
```

## Flutter Application Metadata
281
**Type**: app
282 283 284 285 286 287 288
**Version**: 2.0.1+100
**Material**: true
**Android X**: true
**Module**: true
**Plugin**: false
**Android package**: com.example.failing.android
**iOS bundle identifier**: com.example.failing.ios
289 290
**Creation channel**: stable
**Creation framework version**: 0b8abb4724aa590dd0f429683339b1e045a1594d
291 292 293 294 295 296 297 298 299 300 301
### Plugins
camera-0.5.7+2
device_info-0.4.1+4

''';

        expect(actualBody, expectedBody);
      }, overrides: <Type, Generator>{
        FileSystem: () => fs,
        ProcessManager: () => FakeProcessManager.any(),
      });
302 303 304 305
    });
  });
}

306
class FakeError extends Error {
307 308 309 310 311
  @override
  StackTrace get stackTrace => StackTrace.fromString('''
#0      _File.open.<anonymous closure> (dart:io/file_impl.dart:366:9)
#1      _rootRunUnary (dart:async/zone.dart:1141:38)''');
}