2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2017-03-21 17:23:44 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-04-14 16:42:26 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
import 'package:file/file.dart';
|
|
|
|
import 'package:file/memory.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
2020-05-06 15:15:39 +00:00
|
|
|
import 'package:flutter_tools/src/base/io.dart';
|
2020-01-30 01:51:31 +00:00
|
|
|
import 'package:flutter_tools/src/base/logger.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
import 'package:flutter_tools/src/base/os.dart';
|
2020-05-06 15:15:39 +00:00
|
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
import 'package:mockito/mockito.dart';
|
|
|
|
import 'package:process/process.dart';
|
|
|
|
|
2019-07-13 18:51:44 +00:00
|
|
|
import '../../src/common.dart';
|
|
|
|
import '../../src/context.dart';
|
2017-03-21 17:23:44 +00:00
|
|
|
|
|
|
|
const String kExecutable = 'foo';
|
|
|
|
const String kPath1 = '/bar/bin/$kExecutable';
|
|
|
|
const String kPath2 = '/another/bin/$kExecutable';
|
|
|
|
|
|
|
|
void main() {
|
2020-02-15 00:10:47 +00:00
|
|
|
MockProcessManager mockProcessManager;
|
|
|
|
|
|
|
|
setUp(() {
|
|
|
|
mockProcessManager = MockProcessManager();
|
|
|
|
});
|
2017-03-21 17:23:44 +00:00
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
OperatingSystemUtils createOSUtils(Platform platform) {
|
|
|
|
return OperatingSystemUtils(
|
|
|
|
fileSystem: MemoryFileSystem(),
|
2020-03-03 00:23:56 +00:00
|
|
|
logger: BufferLogger.test(),
|
2020-01-30 01:51:31 +00:00
|
|
|
platform: platform,
|
2020-02-15 00:10:47 +00:00
|
|
|
processManager: mockProcessManager,
|
2020-01-30 01:51:31 +00:00
|
|
|
);
|
|
|
|
}
|
2017-03-21 17:23:44 +00:00
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
group('which on POSIX', () {
|
|
|
|
testWithoutContext('returns null when executable does not exist', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['which', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 1, null, null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable), isNull);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns exactly one result', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['which', 'foo']))
|
|
|
|
.thenReturn(ProcessResult(0, 0, kPath1, null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable).path, kPath1);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns all results for whichAll', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
|
2017-03-21 17:23:44 +00:00
|
|
|
final List<File> result = utils.whichAll(kExecutable);
|
|
|
|
expect(result, hasLength(2));
|
|
|
|
expect(result[0].path, kPath1);
|
|
|
|
expect(result[1].path, kPath2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('which on Windows', () {
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns null when executable does not exist', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['where', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 1, null, null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable), isNull);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns exactly one result', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['where', 'foo']))
|
|
|
|
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
|
2017-03-21 17:23:44 +00:00
|
|
|
expect(utils.which(kExecutable).path, kPath1);
|
|
|
|
});
|
|
|
|
|
2020-01-30 01:51:31 +00:00
|
|
|
testWithoutContext('returns all results for whichAll', () async {
|
2020-02-15 00:10:47 +00:00
|
|
|
when(mockProcessManager.runSync(<String>['where', kExecutable]))
|
|
|
|
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
|
|
|
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
|
2017-03-21 17:23:44 +00:00
|
|
|
final List<File> result = utils.whichAll(kExecutable);
|
|
|
|
expect(result, hasLength(2));
|
|
|
|
expect(result[0].path, kPath1);
|
|
|
|
expect(result[1].path, kPath2);
|
|
|
|
});
|
|
|
|
});
|
2020-04-08 16:07:02 +00:00
|
|
|
|
2020-04-14 16:42:26 +00:00
|
|
|
group('gzip on Windows:', () {
|
|
|
|
testWithoutContext('verifyGzip returns false on a FileSystemException', () {
|
|
|
|
final MockFileSystem fileSystem = MockFileSystem();
|
|
|
|
final MockFile mockFile = MockFile();
|
|
|
|
when(fileSystem.file(any)).thenReturn(mockFile);
|
|
|
|
when(mockFile.readAsBytesSync()).thenThrow(
|
|
|
|
const FileSystemException('error'),
|
|
|
|
);
|
|
|
|
final OperatingSystemUtils osUtils = OperatingSystemUtils(
|
|
|
|
fileSystem: fileSystem,
|
|
|
|
logger: BufferLogger.test(),
|
|
|
|
platform: FakePlatform(operatingSystem: 'windows'),
|
|
|
|
processManager: mockProcessManager,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(osUtils.verifyGzip(mockFile), isFalse);
|
|
|
|
});
|
|
|
|
|
|
|
|
testWithoutContext('verifyGzip returns false on an ArchiveException', () {
|
|
|
|
final MockFileSystem fileSystem = MockFileSystem();
|
|
|
|
final MockFile mockFile = MockFile();
|
|
|
|
when(fileSystem.file(any)).thenReturn(mockFile);
|
|
|
|
when(mockFile.readAsBytesSync()).thenReturn(Uint8List.fromList(<int>[
|
|
|
|
// Anything other than the magic header: 0x1f, 0x8b.
|
|
|
|
0x01,
|
|
|
|
0x02,
|
|
|
|
]));
|
|
|
|
final OperatingSystemUtils osUtils = OperatingSystemUtils(
|
|
|
|
fileSystem: fileSystem,
|
|
|
|
logger: BufferLogger.test(),
|
|
|
|
platform: FakePlatform(operatingSystem: 'windows'),
|
|
|
|
processManager: mockProcessManager,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(osUtils.verifyGzip(mockFile), isFalse);
|
|
|
|
});
|
|
|
|
|
|
|
|
testWithoutContext('verifyGzip returns false on an empty file', () {
|
|
|
|
final MockFileSystem fileSystem = MockFileSystem();
|
|
|
|
final MockFile mockFile = MockFile();
|
|
|
|
when(fileSystem.file(any)).thenReturn(mockFile);
|
|
|
|
when(mockFile.readAsBytesSync()).thenReturn(Uint8List(0));
|
|
|
|
final OperatingSystemUtils osUtils = OperatingSystemUtils(
|
|
|
|
fileSystem: fileSystem,
|
|
|
|
logger: BufferLogger.test(),
|
|
|
|
platform: FakePlatform(operatingSystem: 'windows'),
|
|
|
|
processManager: mockProcessManager,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(osUtils.verifyGzip(mockFile), isFalse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-08 16:07:02 +00:00
|
|
|
testWithoutContext('stream compression level', () {
|
|
|
|
expect(OperatingSystemUtils.gzipLevel1.level, equals(1));
|
|
|
|
});
|
2017-03-21 17:23:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 15:44:39 +00:00
|
|
|
class MockProcessManager extends Mock implements ProcessManager {}
|
2020-04-14 16:42:26 +00:00
|
|
|
class MockFileSystem extends Mock implements FileSystem {}
|
|
|
|
class MockFile extends Mock implements File {}
|