dependency_checker_test.dart 4.51 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium 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
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/devices.dart';
9 10 11 12 13 14
import 'package:flutter_tools/src/dart/dependencies.dart';
import 'package:flutter_tools/src/dependency_checker.dart';
import 'package:test/test.dart';
import 'src/common.dart';
import 'src/context.dart';

15
void main() {
16
  group('DependencyChecker', () {
17 18 19 20 21 22 23 24 25
    final String dataPath = fs.path.join(
        getFlutterRoot(),
        'packages',
        'flutter_tools',
        'test',
        'data',
        'dart_dependencies_test',
    );

26
    FileSystem testFileSystem;
27

28
    setUpAll(() {
29
      Cache.disableLocking();
30 31 32
    });

    setUp(() {
33 34 35
      testFileSystem = new MemoryFileSystem();
    });

36
    testUsingContext('good', () {
37 38 39 40 41
      final String testPath = fs.path.join(dataPath, 'good');
      final String mainPath = fs.path.join(testPath, 'main.dart');
      final String fooPath = fs.path.join(testPath, 'foo.dart');
      final String barPath = fs.path.join(testPath, 'lib', 'bar.dart');
      final String packagesPath = fs.path.join(testPath, '.packages');
42
      final DartDependencySetBuilder builder =
43
          new DartDependencySetBuilder(mainPath, packagesPath);
44
      final DependencyChecker dependencyChecker =
45 46 47
          new DependencyChecker(builder, null);

      // Set file modification time on all dependencies to be in the past.
48
      final DateTime baseTime = new DateTime.now();
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      updateFileModificationTime(packagesPath, baseTime, -10);
      updateFileModificationTime(mainPath, baseTime, -10);
      updateFileModificationTime(fooPath, baseTime, -10);
      updateFileModificationTime(barPath, baseTime, -10);
      expect(dependencyChecker.check(baseTime), isFalse);

      // Set .packages file modification time to be in the future.
      updateFileModificationTime(packagesPath, baseTime, 20);
      expect(dependencyChecker.check(baseTime), isTrue);

      // Reset .packages file modification time.
      updateFileModificationTime(packagesPath, baseTime, 0);
      expect(dependencyChecker.check(baseTime), isFalse);

      // Set 'package:self/bar.dart' file modification time to be in the future.
      updateFileModificationTime(barPath, baseTime, 10);
      expect(dependencyChecker.check(baseTime), isTrue);
66
    });
67

68
    testUsingContext('syntax error', () {
69 70 71 72
      final String testPath = fs.path.join(dataPath, 'syntax_error');
      final String mainPath = fs.path.join(testPath, 'main.dart');
      final String fooPath = fs.path.join(testPath, 'foo.dart');
      final String packagesPath = fs.path.join(testPath, '.packages');
73

74
      final DartDependencySetBuilder builder =
75
          new DartDependencySetBuilder(mainPath, packagesPath);
76
      final DependencyChecker dependencyChecker =
77 78
          new DependencyChecker(builder, null);

79
      final DateTime baseTime = new DateTime.now();
80 81 82 83 84 85 86 87 88 89

      // Set file modification time on all dependencies to be in the past.
      updateFileModificationTime(packagesPath, baseTime, -10);
      updateFileModificationTime(mainPath, baseTime, -10);
      updateFileModificationTime(fooPath, baseTime, -10);

      // Dependencies are considered dirty because there is a syntax error in
      // the .dart file.
      expect(dependencyChecker.check(baseTime), isTrue);
    });
90 91 92 93 94 95

    /// Test a flutter tool move.
    ///
    /// Tests that the flutter tool doesn't crash and displays a warning when its own location
    /// changed since it was last referenced to in a package's .packages file.
    testUsingContext('moved flutter sdk', () async {
96
      final Directory destinationPath = fs.systemTempDirectory.createTempSync('dependency_checker_test_');
97
      // Copy the golden input and let the test run in an isolated temporary in-memory file system.
98
      const LocalFileSystem localFileSystem = const LocalFileSystem();
99
      final Directory sourcePath = localFileSystem.directory(localFileSystem.path.join(dataPath, 'changed_sdk_location'));
100
      copyDirectorySync(sourcePath, destinationPath);
101 102 103 104 105 106 107 108
      fs.currentDirectory = destinationPath;

      // Doesn't matter what commands we run. Arbitrarily list devices here.
      await createTestCommandRunner(new DevicesCommand()).run(<String>['devices']);
      expect(testLogger.errorText, contains('.packages'));
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
    });
109
  });
110
}