language_version_test.dart 9.41 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 6 7 8 9 10
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=456"
@Tags(<String>['no-shuffle'])

11 12 13 14 15 16 17
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/dart/language_version.dart';
import 'package:package_config/package_config.dart';

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

18 19 20 21 22 23 24 25 26 27
const String flutterRoot = '';
const String testVersionString = '2.13';
final LanguageVersion testCurrentLanguageVersion = LanguageVersion(2, 13);

void setUpLanguageVersion(FileSystem fileSystem, [String version = testVersionString]) {
  fileSystem.file(fileSystem.path.join('bin', 'cache', 'dart-sdk', 'version'))
    ..createSync(recursive: true)
    ..writeAsStringSync(version);
}

28 29 30
void main() {
  testWithoutContext('detects language version in comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
31
    setUpLanguageVersion(fileSystem);
32 33 34 35 36 37 38
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart = 2.9
''');

39
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
40 41
  });

42 43
  testWithoutContext('detects language version in comment without spacing', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
44
    setUpLanguageVersion(fileSystem);
45 46 47 48 49 50 51
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart=2.9
''');

52
    expect(determineLanguageVersion(file, null, flutterRoot),  LanguageVersion(2, 9));
53 54 55 56
  });

  testWithoutContext('detects language version in comment with more numbers', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
57
    setUpLanguageVersion(fileSystem);
58 59 60 61 62 63 64
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart=2.12
''');

65
    expect(determineLanguageVersion(file, null, flutterRoot), nullSafeVersion);
66 67 68
  });

  testWithoutContext('does not detect invalid language version', () {
69
    final FileSystem fileSystem = MemoryFileSystem.test();
70
    setUpLanguageVersion(fileSystem);
71 72 73 74 75 76 77
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart
''');

78
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
79 80 81 82 83 84 85 86 87 88 89
  });

  testWithoutContext('detects language version with leading whitespace', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

    // @dart = 2.9
''');

90
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
91 92 93 94 95 96 97 98 99 100 101
  });

  testWithoutContext('detects language version with tabs', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

//\t@dart = 2.9
''');

102
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
103 104 105 106 107 108 109 110
  });

  testWithoutContext('detects language version with tons of whitespace', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

111
//        @dart       = 2.23
112 113
''');

114
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 23));
115 116 117 118
  });

  testWithoutContext('does not detect language version in dartdoc', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
119
    setUpLanguageVersion(fileSystem);
120 121 122 123 124 125 126
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/// @dart = 2.9
''');

127
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
128 129 130 131
  });

  testWithoutContext('does not detect language version in block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
132
    setUpLanguageVersion(fileSystem);
133 134 135 136 137 138 139 140 141
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/*
// @dart = 2.9
*/
''');

142
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
143 144 145 146
  });

  testWithoutContext('does not detect language version in nested block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
147
    setUpLanguageVersion(fileSystem);
148 149 150 151 152 153 154 155 156 157 158
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/*
/*
// @dart = 2.9
*/
*/
''');

159
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
160 161 162 163 164 165 166 167 168 169 170 171 172 173
  });

  testWithoutContext('detects language version after nested block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/* /*
*/
*/
// @dart = 2.9
''');

174
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
175 176 177 178
  });

  testWithoutContext('does not crash with unbalanced opening block comments', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
179
    setUpLanguageVersion(fileSystem);
180 181 182 183 184 185 186 187 188 189
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/*
/*
*/
// @dart = 2.9
''');

190
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
191 192 193 194
  });

  testWithoutContext('does not crash with unbalanced closing block comments', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
195
    setUpLanguageVersion(fileSystem);
196 197 198 199 200 201 202 203 204 205
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/*
*/
*/
// @dart = 2.9
''');

206
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
207 208 209 210
  });

  testWithoutContext('does not detect language version in single line block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
211
    setUpLanguageVersion(fileSystem);
212 213 214 215 216 217 218
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

/* // @dart = 2.9 */
''');

219
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
220 221 222 223
  });

  testWithoutContext('does not detect language version after import declaration', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
224
    setUpLanguageVersion(fileSystem);
225 226 227 228 229 230 231 232 233
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

import 'dart:ui' as ui;

// @dart = 2.9
''');

234
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
235 236 237 238
  });

  testWithoutContext('does not detect language version after part declaration', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
239
    setUpLanguageVersion(fileSystem);
240 241 242 243 244 245 246 247 248
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

part of 'foo.dart';

// @dart = 2.9
''');

249
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
250 251 252 253 254 255 256 257 258 259 260 261 262
  });

  testWithoutContext('does not detect language version after library declaration', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

library funstuff;

// @dart = 2.9
''');

263
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
264 265 266 267 268 269 270 271 272 273 274 275 276 277
  });

  testWithoutContext('looks up language version from package if not found in file', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license
''');
    final Package package = Package(
      'foo',
      Uri.parse('file://foo/'),
      languageVersion: LanguageVersion(2, 7),
    );

278
    expect(determineLanguageVersion(file, package, flutterRoot), LanguageVersion(2, 7));
279
  });
280

281
  testWithoutContext('defaults to current version if package lookup returns null', () {
282 283 284 285 286 287 288 289 290 291 292
    final FileSystem fileSystem = MemoryFileSystem.test();
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license
''');
    final Package package = Package(
      'foo',
      Uri.parse('file://foo/'),
      languageVersion: null,
    );

293
    expect(determineLanguageVersion(file, package, flutterRoot), testCurrentLanguageVersion);
294
  });
295 296

  testWithoutContext('Returns null safe error if reading the file throws a FileSystemException', () {
297 298 299 300 301 302
    final FileExceptionHandler handler = FileExceptionHandler();
    final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
    setUpLanguageVersion(fileSystem);
    final File errorFile = fileSystem.file('foo');
    handler.addError(errorFile, FileSystemOp.read, const FileSystemException());

303 304 305 306 307 308
    final Package package = Package(
      'foo',
      Uri.parse('file://foo/'),
      languageVersion: LanguageVersion(2, 7),
    );

309
    expect(determineLanguageVersion(errorFile, package, flutterRoot), testCurrentLanguageVersion);
310 311
  });

312 313 314 315 316 317
  testWithoutContext('Can parse Dart language version with pre/post suffix', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    setUpLanguageVersion(fileSystem, '2.13.0-150.0.dev');

    expect(currentLanguageVersion(fileSystem, flutterRoot), LanguageVersion(2, 13));
  });
318
}