file_system_test.dart 4.58 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 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.

import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
7
import 'package:platform/platform.dart';
8

9 10
import '../src/common.dart';
import '../src/context.dart';
11

12
void main() {
13 14 15 16
  group('ensureDirectoryExists', () {
    MemoryFileSystem fs;

    setUp(() {
17
      fs = MemoryFileSystem();
18 19 20 21 22
    });

    testUsingContext('recursively creates a directory if it does not exist', () async {
      ensureDirectoryExists('foo/bar/baz.flx');
      expect(fs.isDirectorySync('foo/bar'), true);
23
    }, overrides: <Type, Generator>{ FileSystem: () => fs });
24 25 26 27

    testUsingContext('throws tool exit on failure to create', () async {
      fs.file('foo').createSync();
      expect(() => ensureDirectoryExists('foo/bar.flx'), throwsToolExit());
28
    }, overrides: <Type, Generator>{ FileSystem: () => fs });
29 30 31
  });

  group('copyDirectorySync', () {
32 33 34
    /// Test file_systems.copyDirectorySync() using MemoryFileSystem.
    /// Copies between 2 instances of file systems which is also supported by copyDirectorySync().
    test('test directory copy', () async {
35
      final MemoryFileSystem sourceMemoryFs = MemoryFileSystem();
36
      const String sourcePath = '/some/origin';
37
      final Directory sourceDirectory = await sourceMemoryFs.directory(sourcePath).create(recursive: true);
38
      sourceMemoryFs.currentDirectory = sourcePath;
39 40
      final File sourceFile1 = sourceMemoryFs.file('some_file.txt')..writeAsStringSync('bleh');
      final DateTime writeTime = sourceFile1.lastModifiedSync();
41 42 43 44
      sourceMemoryFs.file('sub_dir/another_file.txt').createSync(recursive: true);
      sourceMemoryFs.directory('empty_directory').createSync();

      // Copy to another memory file system instance.
45
      final MemoryFileSystem targetMemoryFs = MemoryFileSystem();
46
      const String targetPath = '/some/non-existent/target';
47
      final Directory targetDirectory = targetMemoryFs.directory(targetPath);
48
      copyDirectorySync(sourceDirectory, targetDirectory);
49

50 51 52 53 54 55 56 57 58 59 60 61
      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);
    });
  });
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

  group('canonicalizePath', () {
    test('does not lowercase on Windows', () {
      String path = 'C:\\Foo\\bAr\\cOOL.dart';
      expect(canonicalizePath(path), path);
      // fs.path.canonicalize does lowercase on Windows
      expect(fs.path.canonicalize(path), isNot(path));

      path = '..\\bar\\.\\\\Foo';
      final String expected = fs.path.join(fs.currentDirectory.parent.absolute.path, 'bar', 'Foo');
      expect(canonicalizePath(path), expected);
      // fs.path.canonicalize should return the same result (modulo casing)
      expect(fs.path.canonicalize(path), expected.toLowerCase());
    }, testOn: 'windows');

    test('does not lowercase on posix', () {
      String path = '/Foo/bAr/cOOL.dart';
      expect(canonicalizePath(path), path);
      // fs.path.canonicalize and canonicalizePath should be the same on Posix
      expect(fs.path.canonicalize(path), path);

      path = '../bar/.//Foo';
      final String expected = fs.path.join(fs.currentDirectory.parent.absolute.path, 'bar', 'Foo');
      expect(canonicalizePath(path), expected);
    }, testOn: 'posix');
  });
88 89 90 91 92 93 94

  group('escapePath', () {
    testUsingContext('on Windows', () {
      expect(escapePath('C:\\foo\\bar\\cool.dart'), 'C:\\\\foo\\\\bar\\\\cool.dart');
      expect(escapePath('foo\\bar\\cool.dart'), 'foo\\\\bar\\\\cool.dart');
      expect(escapePath('C:/foo/bar/cool.dart'), 'C:/foo/bar/cool.dart');
    }, overrides: <Type, Generator>{
95
      Platform: () => FakePlatform(operatingSystem: 'windows')
96 97 98 99 100 101 102
    });

    testUsingContext('on Linux', () {
      expect(escapePath('/foo/bar/cool.dart'), '/foo/bar/cool.dart');
      expect(escapePath('foo/bar/cool.dart'), 'foo/bar/cool.dart');
      expect(escapePath('foo\\cool.dart'), 'foo\\cool.dart');
    }, overrides: <Type, Generator>{
103
      Platform: () => FakePlatform(operatingSystem: 'linux')
104 105
    });
  });
106
}