| // Copyright 2024, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| import 'dart:io' as io; |
| |
| import 'package:deps/fetch.dart'; |
| import 'package:deps/package.dart'; |
| import 'package:file/local.dart'; |
| import 'package:file/src/interface/file_system_entity.dart'; |
| import 'package:file/src/interface/directory.dart'; |
| import 'package:test/test.dart'; |
| |
| import 'command_test.dart'; |
| |
| class TestGsutilPackage extends Package with GsUtilDownloader { |
| TestGsutilPackage(Uri source) : super('test', source); |
| |
| @override |
| Future<Directory> build(List<FileSystemEntity> inputs, Directory workingDir) { |
| throw UnimplementedError(); |
| } |
| |
| @override |
| Future<bool> check(Directory artifacts) { |
| throw UnimplementedError(); |
| } |
| |
| @override |
| Future<void> upload(Directory artifacts, bool provenance) { |
| throw UnimplementedError(); |
| } |
| } |
| |
| void main() { |
| final fileSystem = LocalFileSystem(); |
| late Directory workingDir; |
| |
| setUp(() async => |
| workingDir = await fileSystem.systemTempDirectory.createTemp()); |
| tearDown(() async => await workingDir.delete(recursive: true)); |
| |
| test('fetch gsutil package', () async { |
| final source = |
| await workingDir.childFile('source.zip').writeAsString('contents'); |
| final target = await workingDir.childDirectory('target').create(); |
| final targetFile = target.childFile('source.zip'); |
| final package = TestGsutilPackage(Uri.file(source.path)); |
| final result = await package.fetch(target); |
| expect(result.map((e) => e.path), [targetFile.path]); |
| expect(await targetFile.readAsString(), 'contents'); |
| }); |
| |
| test('fetch gsutil package unauthenticated', () async { |
| final source = workingDir.childFile('source.zip'); |
| final package = TestGsutilPackage(Uri.file(source.path)); |
| final future = mockedRun( |
| io.ProcessResult(123, 1, null, '401 Anonymous caller'), |
| 'gsutil.py', |
| null, |
| () => package.fetch(workingDir)); |
| expect(future, throwsStateError); |
| try { |
| await future; |
| } catch (e) { |
| expect(workingDir.listSync(), isEmpty); |
| } |
| }); |
| |
| test('fetch gsutil package failed', () async { |
| final source = workingDir.childFile('source.zip'); |
| final package = TestGsutilPackage(Uri.file(source.path)); |
| final future = mockedRun(io.ProcessResult(123, 1, null, 'failed to copy'), |
| 'gsutil.py', null, () => package.fetch(workingDir)); |
| expect(future, throwsA(isA<io.ProcessException>())); |
| try { |
| await future; |
| } catch (e) { |
| expect(workingDir.listSync(), isEmpty); |
| } |
| }); |
| } |