depfile_test.dart 6.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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';
7
import 'package:flutter_tools/src/base/logger.dart';
8 9 10 11 12
import 'package:flutter_tools/src/build_system/depfile.dart';

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

void main() {
13 14
  FileSystem fileSystem;
  DepfileService depfileService;
15 16

  setUp(() {
17 18
    fileSystem = MemoryFileSystem.test();
    depfileService = DepfileService(
19
      logger: BufferLogger.test(),
20 21
      fileSystem: fileSystem,
    );
22
  });
23 24
  testWithoutContext('Can parse depfile from file', () {
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
25 26
a.txt: b.txt
''');
27
    final Depfile depfile = depfileService.parse(depfileSource);
28 29 30

    expect(depfile.inputs.single.path, 'b.txt');
    expect(depfile.outputs.single.path, 'a.txt');
31
  });
32

33 34
  testWithoutContext('Can parse depfile with multiple inputs', () {
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
35 36
a.txt: b.txt c.txt d.txt
''');
37
    final Depfile depfile = depfileService.parse(depfileSource);
38 39 40 41 42 43 44

    expect(depfile.inputs.map((File file) => file.path), <String>[
      'b.txt',
      'c.txt',
      'd.txt',
    ]);
    expect(depfile.outputs.single.path, 'a.txt');
45
  });
46

47 48
  testWithoutContext('Can parse depfile with multiple outputs', () {
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
49 50
a.txt c.txt d.txt: b.txt
''');
51
    final Depfile depfile = depfileService.parse(depfileSource);
52 53 54 55 56 57 58

    expect(depfile.inputs.single.path, 'b.txt');
    expect(depfile.outputs.map((File file) => file.path), <String>[
      'a.txt',
      'c.txt',
      'd.txt',
    ]);
59
  });
60

61 62 63
  testWithoutContext('Can parse depfile with windows file paths', () {
    fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
    depfileService = DepfileService(
64
      logger: BufferLogger.test(),
65 66 67
      fileSystem: fileSystem,
    );
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
68 69
C:\\a.txt: C:\\b.txt
''');
70
    final Depfile depfile = depfileService.parse(depfileSource);
71 72 73

    expect(depfile.inputs.single.path, r'C:\b.txt');
    expect(depfile.outputs.single.path, r'C:\a.txt');
74
  });
75

76 77 78
  testWithoutContext('Can escape depfile with windows file paths and spaces in directory names', () {
    fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
    depfileService = DepfileService(
79
      logger: BufferLogger.test(),
80 81 82
      fileSystem: fileSystem,
    );
    final File inputFile = fileSystem.directory(r'Hello Flutter').childFile('a.txt').absolute
83
      ..createSync(recursive: true);
84
    final File outputFile = fileSystem.directory(r'Hello Flutter').childFile('b.txt').absolute
85 86
      ..createSync();
    final Depfile depfile = Depfile(<File>[inputFile], <File>[outputFile]);
87 88
    final File outputDepfile = fileSystem.file('depfile');
    depfileService.writeToFile(depfile, outputDepfile);
89 90 91

    expect(outputDepfile.readAsStringSync(), contains(r'C:\\Hello\ Flutter\\a.txt'));
    expect(outputDepfile.readAsStringSync(), contains(r'C:\\Hello\ Flutter\\b.txt'));
92
  });
93

94 95
  testWithoutContext('Can escape depfile with spaces in directory names', () {
    final File inputFile = fileSystem.directory(r'Hello Flutter').childFile('a.txt').absolute
96
      ..createSync(recursive: true);
97
    final File outputFile = fileSystem.directory(r'Hello Flutter').childFile('b.txt').absolute
98 99
      ..createSync();
    final Depfile depfile = Depfile(<File>[inputFile], <File>[outputFile]);
100 101
    final File outputDepfile = fileSystem.file('depfile');
    depfileService.writeToFile(depfile, outputDepfile);
102 103 104

    expect(outputDepfile.readAsStringSync(), contains(r'/Hello\ Flutter/a.txt'));
    expect(outputDepfile.readAsStringSync(), contains(r'/Hello\ Flutter/b.txt'));
105
  });
106 107


108 109
  testWithoutContext('Resillient to weird whitespace', () {
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
110 111 112 113 114
a.txt
  : b.txt    c.txt


''');
115
    final Depfile depfile = depfileService.parse(depfileSource);
116 117 118

    expect(depfile.inputs, hasLength(2));
    expect(depfile.outputs.single.path, 'a.txt');
119
  });
120

121 122
  testWithoutContext('Resillient to duplicate files', () {
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
123 124
a.txt: b.txt b.txt
''');
125
    final Depfile depfile = depfileService.parse(depfileSource);
126 127 128

    expect(depfile.inputs.single.path, 'b.txt');
    expect(depfile.outputs.single.path, 'a.txt');
129
  });
130

131 132
  testWithoutContext('Resillient to malformed file, missing :', () {
    final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
133 134
a.text b.txt
''');
135
    final Depfile depfile = depfileService.parse(depfileSource);
136 137 138

    expect(depfile.inputs, isEmpty);
    expect(depfile.outputs, isEmpty);
139
  });
140

141 142
  testWithoutContext('Can parse dart2js output format', () {
    final File dart2jsDependencyFile = fileSystem.file('main.dart.js.deps')..writeAsStringSync(r'''
143 144 145 146 147
file:///Users/foo/collection.dart
file:///Users/foo/algorithms.dart
file:///Users/foo/canonicalized_map.dart
''');

148
    final Depfile depfile = depfileService.parseDart2js(dart2jsDependencyFile, fileSystem.file('foo.dart.js'));
149 150

    expect(depfile.inputs.map((File file) => file.path), <String>[
151 152 153
      fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'collection.dart')),
      fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'algorithms.dart')),
      fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'canonicalized_map.dart')),
154 155
    ]);
    expect(depfile.outputs.single.path, 'foo.dart.js');
156
  });
157

158 159
  testWithoutContext('Can parse handle invalid uri', () {
    final File dart2jsDependencyFile = fileSystem.file('main.dart.js.deps')..writeAsStringSync('''
160 161 162 163 164
file:///Users/foo/collection.dart
abcdevf
file:///Users/foo/canonicalized_map.dart
''');

165
    final Depfile depfile = depfileService.parseDart2js(dart2jsDependencyFile, fileSystem.file('foo.dart.js'));
166 167

    expect(depfile.inputs.map((File file) => file.path), <String>[
168 169
      fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'collection.dart')),
      fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'canonicalized_map.dart')),
170 171
    ]);
    expect(depfile.outputs.single.path, 'foo.dart.js');
172
  });
173
}