2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2017-01-25 19:41:48 +00:00
|
|
|
// 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';
|
2020-01-14 17:40:39 +00:00
|
|
|
import 'package:mockito/mockito.dart';
|
2017-04-26 17:02:22 +00:00
|
|
|
import 'package:platform/platform.dart';
|
2017-01-25 19:41:48 +00:00
|
|
|
|
2019-07-13 18:51:44 +00:00
|
|
|
import '../../src/common.dart';
|
2020-01-14 17:40:39 +00:00
|
|
|
|
|
|
|
class MockPlatform extends Mock implements Platform {}
|
2017-02-04 01:34:12 +00:00
|
|
|
|
2017-01-25 19:41:48 +00:00
|
|
|
void main() {
|
2017-02-04 01:34:12 +00:00
|
|
|
group('ensureDirectoryExists', () {
|
|
|
|
MemoryFileSystem fs;
|
2020-01-14 17:40:39 +00:00
|
|
|
FileSystemUtils fsUtils;
|
2017-02-04 01:34:12 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2018-09-12 06:29:29 +00:00
|
|
|
fs = MemoryFileSystem();
|
2020-01-14 17:40:39 +00:00
|
|
|
fsUtils = FileSystemUtils(
|
|
|
|
fileSystem: fs,
|
|
|
|
platform: MockPlatform(),
|
|
|
|
);
|
2017-02-04 01:34:12 +00:00
|
|
|
});
|
|
|
|
|
2020-01-14 17:40:39 +00:00
|
|
|
testWithoutContext('recursively creates a directory if it does not exist', () async {
|
|
|
|
fsUtils.ensureDirectoryExists('foo/bar/baz.flx');
|
2017-02-04 01:34:12 +00:00
|
|
|
expect(fs.isDirectorySync('foo/bar'), true);
|
2019-10-11 18:23:12 +00:00
|
|
|
});
|
2017-02-04 01:34:12 +00:00
|
|
|
|
2020-01-14 17:40:39 +00:00
|
|
|
testWithoutContext('throws tool exit on failure to create', () async {
|
2017-02-04 01:34:12 +00:00
|
|
|
fs.file('foo').createSync();
|
2020-01-14 17:40:39 +00:00
|
|
|
expect(() => fsUtils.ensureDirectoryExists('foo/bar.flx'), throwsToolExit());
|
2019-10-11 18:23:12 +00:00
|
|
|
});
|
2017-02-04 01:34:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('copyDirectorySync', () {
|
2017-01-25 19:41:48 +00:00
|
|
|
/// Test file_systems.copyDirectorySync() using MemoryFileSystem.
|
|
|
|
/// Copies between 2 instances of file systems which is also supported by copyDirectorySync().
|
2020-01-14 17:40:39 +00:00
|
|
|
testWithoutContext('test directory copy', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
final MemoryFileSystem sourceMemoryFs = MemoryFileSystem();
|
2018-02-01 06:51:26 +00:00
|
|
|
const String sourcePath = '/some/origin';
|
2017-03-04 01:50:46 +00:00
|
|
|
final Directory sourceDirectory = await sourceMemoryFs.directory(sourcePath).create(recursive: true);
|
2017-01-25 19:41:48 +00:00
|
|
|
sourceMemoryFs.currentDirectory = sourcePath;
|
2017-03-04 01:50:46 +00:00
|
|
|
final File sourceFile1 = sourceMemoryFs.file('some_file.txt')..writeAsStringSync('bleh');
|
|
|
|
final DateTime writeTime = sourceFile1.lastModifiedSync();
|
2017-01-25 19:41:48 +00:00
|
|
|
sourceMemoryFs.file('sub_dir/another_file.txt').createSync(recursive: true);
|
|
|
|
sourceMemoryFs.directory('empty_directory').createSync();
|
|
|
|
|
|
|
|
// Copy to another memory file system instance.
|
2018-09-12 06:29:29 +00:00
|
|
|
final MemoryFileSystem targetMemoryFs = MemoryFileSystem();
|
2018-02-01 06:51:26 +00:00
|
|
|
const String targetPath = '/some/non-existent/target';
|
2017-03-04 01:50:46 +00:00
|
|
|
final Directory targetDirectory = targetMemoryFs.directory(targetPath);
|
2020-01-14 17:40:39 +00:00
|
|
|
|
|
|
|
final FileSystemUtils fsUtils = FileSystemUtils(
|
|
|
|
fileSystem: sourceMemoryFs,
|
|
|
|
platform: MockPlatform(),
|
|
|
|
);
|
|
|
|
fsUtils.copyDirectorySync(sourceDirectory, targetDirectory);
|
2017-01-26 23:43:28 +00:00
|
|
|
|
2017-01-25 19:41:48 +00:00
|
|
|
expect(targetDirectory.existsSync(), true);
|
|
|
|
targetMemoryFs.currentDirectory = targetPath;
|
|
|
|
expect(targetMemoryFs.directory('empty_directory').existsSync(), true);
|
|
|
|
expect(targetMemoryFs.file('sub_dir/another_file.txt').existsSync(), true);
|
|
|
|
expect(targetMemoryFs.file('some_file.txt').readAsStringSync(), 'bleh');
|
|
|
|
|
|
|
|
// Assert that the copy operation hasn't modified the original file in some way.
|
|
|
|
expect(sourceMemoryFs.file('some_file.txt').lastModifiedSync(), writeTime);
|
|
|
|
// There's still 3 things in the original directory as there were initially.
|
|
|
|
expect(sourceMemoryFs.directory(sourcePath).listSync().length, 3);
|
|
|
|
});
|
2019-08-28 21:52:08 +00:00
|
|
|
|
2020-01-14 17:40:39 +00:00
|
|
|
testWithoutContext('Skip files if shouldCopyFile returns false', () {
|
|
|
|
final MemoryFileSystem fileSystem = MemoryFileSystem();
|
|
|
|
final FileSystemUtils fsUtils = FileSystemUtils(
|
|
|
|
fileSystem: fileSystem,
|
|
|
|
platform: MockPlatform(),
|
|
|
|
);
|
|
|
|
final Directory origin = fileSystem.directory('/origin');
|
2019-08-28 21:52:08 +00:00
|
|
|
origin.createSync();
|
2020-01-14 17:40:39 +00:00
|
|
|
fileSystem.file(fileSystem.path.join('origin', 'a.txt')).writeAsStringSync('irrelevant');
|
|
|
|
fileSystem.directory('/origin/nested').createSync();
|
|
|
|
fileSystem.file(fileSystem.path.join('origin', 'nested', 'a.txt')).writeAsStringSync('irrelevant');
|
|
|
|
fileSystem.file(fileSystem.path.join('origin', 'nested', 'b.txt')).writeAsStringSync('irrelevant');
|
2019-08-28 21:52:08 +00:00
|
|
|
|
2020-01-14 17:40:39 +00:00
|
|
|
final Directory destination = fileSystem.directory('/destination');
|
|
|
|
fsUtils.copyDirectorySync(origin, destination, shouldCopyFile: (File origin, File dest) {
|
2019-08-28 21:52:08 +00:00
|
|
|
return origin.basename == 'b.txt';
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(destination.existsSync(), isTrue);
|
|
|
|
expect(destination.childDirectory('nested').existsSync(), isTrue);
|
|
|
|
expect(destination.childDirectory('nested').childFile('b.txt').existsSync(), isTrue);
|
|
|
|
|
|
|
|
expect(destination.childFile('a.txt').existsSync(), isFalse);
|
|
|
|
expect(destination.childDirectory('nested').childFile('a.txt').existsSync(), isFalse);
|
|
|
|
});
|
2017-01-25 19:41:48 +00:00
|
|
|
});
|
2017-04-25 17:34:43 +00:00
|
|
|
|
2017-04-26 17:02:22 +00:00
|
|
|
group('escapePath', () {
|
2020-01-14 17:40:39 +00:00
|
|
|
testWithoutContext('on Windows', () {
|
|
|
|
final MemoryFileSystem fileSystem = MemoryFileSystem();
|
|
|
|
final FileSystemUtils fsUtils = FileSystemUtils(
|
|
|
|
fileSystem: fileSystem,
|
|
|
|
platform: FakePlatform(operatingSystem: 'windows'),
|
|
|
|
);
|
2020-02-07 15:35:55 +00:00
|
|
|
expect(fsUtils.escapePath(r'C:\foo\bar\cool.dart'), r'C:\\foo\\bar\\cool.dart');
|
|
|
|
expect(fsUtils.escapePath(r'foo\bar\cool.dart'), r'foo\\bar\\cool.dart');
|
2020-01-14 17:40:39 +00:00
|
|
|
expect(fsUtils.escapePath('C:/foo/bar/cool.dart'), 'C:/foo/bar/cool.dart');
|
2017-04-26 17:02:22 +00:00
|
|
|
});
|
|
|
|
|
2020-01-14 17:40:39 +00:00
|
|
|
testWithoutContext('on Linux', () {
|
|
|
|
final MemoryFileSystem fileSystem = MemoryFileSystem();
|
|
|
|
final FileSystemUtils fsUtils = FileSystemUtils(
|
|
|
|
fileSystem: fileSystem,
|
|
|
|
platform: FakePlatform(operatingSystem: 'linux'),
|
|
|
|
);
|
|
|
|
expect(fsUtils.escapePath('/foo/bar/cool.dart'), '/foo/bar/cool.dart');
|
|
|
|
expect(fsUtils.escapePath('foo/bar/cool.dart'), 'foo/bar/cool.dart');
|
2020-02-07 15:35:55 +00:00
|
|
|
expect(fsUtils.escapePath(r'foo\cool.dart'), r'foo\cool.dart');
|
2017-04-26 17:02:22 +00:00
|
|
|
});
|
|
|
|
});
|
2017-01-25 19:41:48 +00:00
|
|
|
}
|