flutter/packages/flutter_tools/test/general.shard/build_system/depfile_test.dart
Alex Li e8366e3334
🐛 Use consist slashes when generating dep files (#169570)
Continue with https://github.com/flutter/flutter/pull/169467. I've
noticed that paths in the depfile on Windows are generated with
non-normalized paths. This PR normalizes every single file path before
putting them into the output.

This might also fix https://github.com/flutter/flutter/issues/163591 as
a root cause and can be easily cherry-picked.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-05-28 15:59:20 +00:00

219 lines
8.1 KiB
Dart

// 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/base/logger.dart';
import 'package:flutter_tools/src/build_system/depfile.dart';
import '../../src/common.dart';
void main() {
late FileSystem fileSystem;
late DepfileService depfileService;
setUp(() {
fileSystem = MemoryFileSystem.test();
depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem);
});
testWithoutContext('Can parse depfile from file', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
a.txt: b.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs.single.path, 'b.txt');
expect(depfile.outputs.single.path, 'a.txt');
});
testWithoutContext('Can parse depfile with multiple inputs', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
a.txt: b.txt c.txt d.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs.map((File file) => file.path), <String>['b.txt', 'c.txt', 'd.txt']);
expect(depfile.outputs.single.path, 'a.txt');
});
testWithoutContext('Can parse depfile with multiple outputs', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
a.txt c.txt d.txt: b.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs.single.path, 'b.txt');
expect(depfile.outputs.map((File file) => file.path), <String>['a.txt', 'c.txt', 'd.txt']);
});
testWithoutContext('Can parse depfile with windows file paths', () {
final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
final DepfileService depfileService = DepfileService(
logger: BufferLogger.test(),
fileSystem: fileSystem,
);
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
C:\\a1.txt C:\\a2/a3.txt: C:\\b1.txt C:\\b2/b3.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs.map((File e) => e.path).toList(), <String>[
r'C:\b1.txt',
r'C:\b2\b3.txt',
]);
expect(depfile.outputs.map((File e) => e.path).toList(), <String>[
r'C:\a1.txt',
r'C:\a2\a3.txt',
]);
});
testWithoutContext(
'Can escape depfile with windows file paths and spaces in directory names',
() {
final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
final DepfileService depfileService = DepfileService(
logger: BufferLogger.test(),
fileSystem: fileSystem,
);
final File inputFile =
fileSystem.directory(r'Hello Flutter').childFile('a.txt').absolute
..createSync(recursive: true);
final File outputFile =
fileSystem.directory(r'Hello Flutter').childFile('b.txt').absolute..createSync();
final Depfile depfile = Depfile(<File>[inputFile], <File>[outputFile]);
final File outputDepfile = fileSystem.file('depfile');
depfileService.writeToFile(depfile, outputDepfile);
final String output = outputDepfile.readAsStringSync();
expect(output, contains(r'C:\\Hello\ Flutter\\a.txt'));
expect(output, contains(r'C:\\Hello\ Flutter\\b.txt'));
},
);
testWithoutContext('Can escape depfile with spaces in directory names', () {
final File inputFile =
fileSystem.directory(r'Hello Flutter').childFile('a.txt').absolute
..createSync(recursive: true);
final File outputFile =
fileSystem.directory(r'Hello Flutter').childFile('b.txt').absolute..createSync();
final Depfile depfile = Depfile(<File>[inputFile], <File>[outputFile]);
final File outputDepfile = fileSystem.file('depfile');
depfileService.writeToFile(depfile, outputDepfile);
final String output = outputDepfile.readAsStringSync();
expect(output, contains(r'/Hello\ Flutter/a.txt'));
expect(output, contains(r'/Hello\ Flutter/b.txt'));
});
testWithoutContext('Can produce normalized paths', () {
final List<(FileSystemStyle style, String input, String output, List<String> expects)> pairs =
<(FileSystemStyle style, String input, String output, List<String> expects)>[
(
FileSystemStyle.posix,
r'Hello Flutter\a.txt',
r'Hello Flutter\b.txt',
<String>[r'/Hello\ Flutter/a.txt', r'/Hello\ Flutter/b.txt'],
),
(
FileSystemStyle.windows,
r'Hello Flutter/a.txt',
r'Hello Flutter/b.txt',
<String>[r'\\Hello\ Flutter\\a.txt', r'\\Hello\ Flutter\\b.txt'],
),
];
for (final (FileSystemStyle style, String input, String output, List<String> expects)
in pairs) {
final FileSystem fileSystem = MemoryFileSystem.test(style: style);
final DepfileService depfileService = DepfileService(
logger: BufferLogger.test(),
fileSystem: fileSystem,
);
final File inputFile = fileSystem.file(input).absolute..createSync(recursive: true);
final File outputFile = fileSystem.file(output).absolute..createSync();
final Depfile depfile = Depfile(<File>[inputFile], <File>[outputFile]);
final File outputDepfile = fileSystem.file('depfile');
depfileService.writeToFile(depfile, outputDepfile);
final String outputString = outputDepfile.readAsStringSync();
for (final String path in expects) {
expect(outputString, contains(path));
}
}
});
testWithoutContext('Resilient to weird whitespace', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
a.txt
: b.txt c.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs, hasLength(2));
expect(depfile.outputs.single.path, 'a.txt');
});
testWithoutContext('Resilient to duplicate files', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
a.txt: b.txt b.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs.single.path, 'b.txt');
expect(depfile.outputs.single.path, 'a.txt');
});
testWithoutContext('Resilient to malformed file, missing :', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
a.text b.txt
''');
final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs, isEmpty);
expect(depfile.outputs, isEmpty);
});
testWithoutContext('Can parse dart2js output format', () {
final File dart2jsDependencyFile = fileSystem.file('main.dart.js.deps')..writeAsStringSync(r'''
file:///Users/foo/collection.dart
file:///Users/foo/algorithms.dart
file:///Users/foo/canonicalized_map.dart
''');
final Depfile depfile = depfileService.parseDart2js(
dart2jsDependencyFile,
fileSystem.file('foo.dart.js'),
);
expect(depfile.inputs.map((File file) => file.path), <String>[
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')),
]);
expect(depfile.outputs.single.path, 'foo.dart.js');
});
testWithoutContext('Can parse handle invalid uri', () {
final File dart2jsDependencyFile = fileSystem.file('main.dart.js.deps')..writeAsStringSync('''
file:///Users/foo/collection.dart
abcdevf
file:///Users/foo/canonicalized_map.dart
''');
final Depfile depfile = depfileService.parseDart2js(
dart2jsDependencyFile,
fileSystem.file('foo.dart.js'),
);
expect(depfile.inputs.map((File file) => file.path), <String>[
fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'collection.dart')),
fileSystem.path.absolute(fileSystem.path.join('Users', 'foo', 'canonicalized_map.dart')),
]);
expect(depfile.outputs.single.path, 'foo.dart.js');
});
}