| // 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:async'; |
| |
| import 'package:file/file.dart'; |
| import 'package:file/memory.dart'; |
| import 'package:test/test.dart'; |
| import 'package:deps/package.dart'; |
| import 'package:deps/useful_directory.dart'; |
| |
| class TestPackage extends Package { |
| TestPackage(String name, Uri source, {this.checkResult = true}) |
| : super(name, source); |
| |
| late Directory artifacts; |
| bool checkResult; |
| |
| @override |
| Future<List<FileSystemEntity>> fetch(Directory workingDir) async { |
| final file = await workingDir.resolveFile('file').create(); |
| final directory = await workingDir.resolve('directory').create(); |
| return [file, directory]; |
| } |
| |
| @override |
| Future<Directory> build( |
| List<FileSystemEntity> inputs, Directory workingDir) async { |
| final output = await workingDir.resolve('build').create(); |
| for (var input in inputs) { |
| final path = |
| '${input.uri.pathSegments.lastWhere((s) => s.isNotEmpty)}.out'; |
| if (input is Directory) { |
| await output.resolve(path).create(); |
| } else { |
| await output.resolveFile(path).create(); |
| } |
| } |
| return output; |
| } |
| |
| @override |
| Future<bool> check(Directory artifacts) async => checkResult; |
| |
| @override |
| Future<void> upload(Directory artifacts, bool provenance) async { |
| expect(checkResult, isTrue); |
| this.artifacts = artifacts; |
| } |
| } |
| |
| void main() { |
| test('create', () async { |
| final fileSystem = MemoryFileSystem.test(); |
| final temp = await fileSystem.directory('/temp').create(); |
| |
| final package = TestPackage('dart/test', Uri.parse('scheme://path')); |
| await package.create(temp, false); |
| |
| expect( |
| temp.listSync(recursive: true).map((f) => f.path), |
| unorderedEquals([ |
| '/temp/dart', |
| '/temp/dart/test', |
| '/temp/dart/test/file', |
| '/temp/dart/test/directory', |
| '/temp/dart/test/build', |
| '/temp/dart/test/build/file.out', |
| '/temp/dart/test/build/directory.out', |
| ])); |
| expect( |
| package.artifacts.listSync(recursive: true).map((f) => f.path), |
| unorderedEquals([ |
| '/temp/dart/test/build/file.out', |
| '/temp/dart/test/build/directory.out', |
| ])); |
| }); |
| |
| test('check', () async { |
| final fileSystem = MemoryFileSystem.test(); |
| final temp = await fileSystem.directory('/temp').create(); |
| |
| final package = TestPackage('dart/test', Uri.parse('scheme://path'), |
| checkResult: false); |
| expect(package.create(temp, false), throwsStateError); |
| }); |
| |
| test('dry run', () { |
| final package = TestPackage('dart/test', Uri.parse('scheme://path')); |
| expect(package.isDryRun, false); |
| expect(dryRun(() => package.isDryRun, true), isTrue); |
| }); |
| } |