package_uri_mapper_test.dart 3.5 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
import 'dart:typed_data';

7 8 9
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/compile.dart';
import 'package:flutter_tools/src/convert.dart';
10
import 'package:flutter_tools/src/globals.dart' as globals;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import 'package:mockito/mockito.dart';

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

const String packagesContents = r'''
xml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/xml-3.2.3/lib/
yaml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib/
example:file:///example/lib/
''';

const String multiRootPackagesContents = r'''
xml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/xml-3.2.3/lib/
yaml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib/
example:org-dartlang-app:/
''';

void main() {
  MockFileSystem mockFileSystem;
  MockFile mockFile;

  setUp(() {
    mockFileSystem = MockFileSystem();
    mockFile = MockFile();
35
    when(mockFileSystem.path).thenReturn(globals.fs.path);
36
    when(mockFileSystem.file(any)).thenReturn(mockFile);
37
    when(mockFile.readAsBytesSync()).thenReturn(utf8.encode(packagesContents) as Uint8List);
38 39 40 41 42 43 44 45
  });

  testUsingContext('Can map main.dart to correct package', () async {
    final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', null, null);
    expect(packageUriMapper.map('/example/lib/main.dart').toString(),
        'package:example/main.dart');
  }, overrides: <Type, Generator>{
    FileSystem: () => mockFileSystem,
46
    ProcessManager: () => FakeProcessManager.any(),
47 48 49 50 51 52 53
  });

  testUsingContext('single-root maps file from other package to null', () async {
    final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', null, null);
    expect(packageUriMapper.map('/xml/lib/xml.dart'), null);
  }, overrides: <Type, Generator>{
    FileSystem: () => mockFileSystem,
54
    ProcessManager: () => FakeProcessManager.any(),
55 56 57 58 59 60 61 62
  });

  testUsingContext('single-root maps non-main file from same package', () async {
    final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', null, null);
    expect(packageUriMapper.map('/example/lib/src/foo.dart').toString(),
        'package:example/src/foo.dart');
  }, overrides: <Type, Generator>{
    FileSystem: () => mockFileSystem,
63
    ProcessManager: () => FakeProcessManager.any(),
64 65 66 67 68
  });

  testUsingContext('multi-root maps main file from same package on multiroot scheme', () async {
    final MockFileSystem mockFileSystem = MockFileSystem();
    final MockFile mockFile = MockFile();
69
    when(mockFileSystem.path).thenReturn(globals.fs.path);
70 71
    when(mockFileSystem.file(any)).thenReturn(mockFile);
    when(mockFile.readAsBytesSync())
72
        .thenReturn(utf8.encode(multiRootPackagesContents) as Uint8List);
73 74 75 76 77 78 79 80 81
    final PackageUriMapper packageUriMapper = PackageUriMapper(
        '/example/lib/main.dart',
        '.packages',
        'org-dartlang-app',
        <String>['/example/lib/', '/gen/lib/']);
    expect(packageUriMapper.map('/example/lib/main.dart').toString(),
        'package:example/main.dart');
  }, overrides: <Type, Generator>{
    FileSystem: () => mockFileSystem,
82
    ProcessManager: () => FakeProcessManager.any(),
83 84 85 86 87
  });
}

class MockFileSystem extends Mock implements FileSystem {}
class MockFile extends Mock implements File {}