language_version_test.dart 9.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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.

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';

12 13 14 15 16 17 18 19 20 21
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);
}

22 23 24
void main() {
  testWithoutContext('detects language version in comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
25
    setUpLanguageVersion(fileSystem);
26 27 28 29 30 31 32
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart = 2.9
''');

33
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
34 35
  });

36 37
  testWithoutContext('detects language version in comment without spacing', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
38
    setUpLanguageVersion(fileSystem);
39 40 41 42 43 44 45
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart=2.9
''');

46
    expect(determineLanguageVersion(file, null, flutterRoot),  LanguageVersion(2, 9));
47 48 49 50
  });

  testWithoutContext('detects language version in comment with more numbers', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
51
    setUpLanguageVersion(fileSystem);
52 53 54 55 56 57 58
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart=2.12
''');

59
    expect(determineLanguageVersion(file, null, flutterRoot), nullSafeVersion);
60 61 62
  });

  testWithoutContext('does not detect invalid language version', () {
63
    final FileSystem fileSystem = MemoryFileSystem.test();
64
    setUpLanguageVersion(fileSystem);
65 66 67 68 69 70 71
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

// @dart
''');

72
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
73 74 75 76 77 78 79 80 81 82 83
  });

  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
''');

84
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
85 86 87 88 89 90 91 92 93 94 95
  });

  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
''');

96
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
97 98 99 100 101 102 103 104
  });

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

105
//        @dart       = 2.23
106 107
''');

108
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 23));
109 110 111 112
  });

  testWithoutContext('does not detect language version in dartdoc', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
113
    setUpLanguageVersion(fileSystem);
114 115 116 117 118 119 120
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

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

121
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
122 123 124 125
  });

  testWithoutContext('does not detect language version in block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
126
    setUpLanguageVersion(fileSystem);
127 128 129 130 131 132 133 134 135
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

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

136
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
137 138 139 140
  });

  testWithoutContext('does not detect language version in nested block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
141
    setUpLanguageVersion(fileSystem);
142 143 144 145 146 147 148 149 150 151 152
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

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

153
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  });

  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
''');

168
    expect(determineLanguageVersion(file, null, flutterRoot), LanguageVersion(2, 9));
169 170 171 172
  });

  testWithoutContext('does not crash with unbalanced opening block comments', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
173
    setUpLanguageVersion(fileSystem);
174 175 176 177 178 179 180 181 182 183
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

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

184
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
185 186 187 188
  });

  testWithoutContext('does not crash with unbalanced closing block comments', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
189
    setUpLanguageVersion(fileSystem);
190 191 192 193 194 195 196 197 198 199
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

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

200
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
201 202 203 204
  });

  testWithoutContext('does not detect language version in single line block comment', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
205
    setUpLanguageVersion(fileSystem);
206 207 208 209 210 211 212
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

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

213
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
214 215 216 217
  });

  testWithoutContext('does not detect language version after import declaration', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
218
    setUpLanguageVersion(fileSystem);
219 220 221 222 223 224 225 226 227
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

import 'dart:ui' as ui;

// @dart = 2.9
''');

228
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
229 230 231 232
  });

  testWithoutContext('does not detect language version after part declaration', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
233
    setUpLanguageVersion(fileSystem);
234 235 236 237 238 239 240 241 242
    final File file = fileSystem.file('example.dart')
      ..writeAsStringSync('''
// Some license

part of 'foo.dart';

// @dart = 2.9
''');

243
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
244 245 246 247 248 249 250 251 252 253 254 255 256
  });

  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
''');

257
    expect(determineLanguageVersion(file, null, flutterRoot), testCurrentLanguageVersion);
258 259 260 261 262 263 264 265 266 267 268 269 270 271
  });

  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),
    );

272
    expect(determineLanguageVersion(file, package, flutterRoot), LanguageVersion(2, 7));
273
  });
274

275
  testWithoutContext('defaults to current version if package lookup returns null', () {
276 277 278 279 280 281 282 283 284 285 286
    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,
    );

287
    expect(determineLanguageVersion(file, package, flutterRoot), testCurrentLanguageVersion);
288
  });
289 290

  testWithoutContext('Returns null safe error if reading the file throws a FileSystemException', () {
291 292 293 294 295 296
    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());

297 298 299 300 301 302
    final Package package = Package(
      'foo',
      Uri.parse('file://foo/'),
      languageVersion: LanguageVersion(2, 7),
    );

303
    expect(determineLanguageVersion(errorFile, package, flutterRoot), testCurrentLanguageVersion);
304 305
  });

306 307 308 309 310 311
  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));
  });
312
}