| // 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 'dart:convert'; |
| import 'dart:io' as io; |
| |
| import 'package:deps/command.dart'; |
| import 'package:deps/cipd.dart'; |
| import 'package:deps/package.dart'; |
| import 'package:file/file.dart'; |
| import 'package:file/memory.dart'; |
| import 'package:test/test.dart'; |
| |
| import 'command_test.dart'; |
| import 'test_logger.dart'; |
| |
| class TestPackage extends CipdPackage { |
| TestPackage(String name, String version) |
| : super(name, Uri.parse('https://example.com'), version); |
| |
| @override |
| Future<List<FileSystemEntity>> fetch(Directory workingDir) { |
| throw UnimplementedError(); |
| } |
| |
| @override |
| Future<Directory> build(List<FileSystemEntity> inputs, Directory workingDir) { |
| throw UnimplementedError(); |
| } |
| |
| @override |
| Future<bool> check(Directory artifacts) { |
| throw UnimplementedError(); |
| } |
| |
| @override |
| Future<bool> isAlreadyUploaded() => Future.value(false); |
| } |
| |
| void dryRunTest(String name, Function() body) => |
| test(name, () => dryRun(body, true)); |
| |
| void runTest(String name, Function() body) => |
| test(name, () => dryRun(body, false)); |
| |
| void main() { |
| final outputJson = jsonEncode({ |
| "result": { |
| "package": "name", |
| "instance_id": "iid", |
| } |
| }); |
| |
| dryRunTest('cipd upload dry run', () async { |
| final fileSystem = MemoryFileSystem.test(); |
| final temp = await fileSystem.directory('/temp').create(); |
| await temp.childFile('a').create(); |
| await temp.childFile('b').create(); |
| await fileSystem.file('/temp.json').writeAsString(outputJson); |
| await fileSystem.file('/temp.zip').create(); |
| |
| final package = TestPackage('dart/path/package/platform', '1.2.3'); |
| package.log = createTestLogger(); |
| final messages = []; |
| package.log.onRecord.listen((event) { |
| messages.add(event.message); |
| }); |
| |
| await mockedRun(io.ProcessResult(123, 0, null, ''), 'cipd', null, |
| () => package.upload(temp, true)); |
| expect(messages, [ |
| 'Running cipd pkg-build -name dart/path/package/platform -in /temp -install-mode copy -out /temp.zip -json-output /temp.json...', |
| 'Produced dart/path/package/platform instance iid with digest e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', |
| 'Running cipd pkg-register /temp.zip -ref latest -tag version:1.2.3...', |
| "Files that would've been included:\n ${temp.listSync()}", |
| 'Finished running cipd create for dart/path/package/platform.', |
| ]); |
| }); |
| |
| runTest('cipd upload', () async { |
| final fileSystem = MemoryFileSystem.test(); |
| final temp = await fileSystem.directory('/temp').create(); |
| await fileSystem.file('/temp.json').writeAsString(outputJson); |
| await fileSystem.file('/temp.zip').create(); |
| |
| final package = TestPackage('dart/path/package/platform', '1.2.3'); |
| package.log = createTestLogger(); |
| final expectedBuildArgs = [ |
| 'pkg-build', |
| '-name', |
| 'dart/path/package/platform', |
| '-in', |
| '/temp', |
| '-install-mode', |
| 'copy', |
| '-out', |
| '/temp.zip', |
| '-json-output', |
| '/temp.json', |
| ]; |
| final expectedRegisterArgs = [ |
| 'pkg-register', |
| '/temp.zip', |
| '-ref', |
| 'latest', |
| '-tag', |
| 'version:1.2.3', |
| ]; |
| final expectedReportArgs = [ |
| '-report-cipd', |
| '-digest', |
| 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', |
| '-pkg-name', |
| 'dart/path/package/platform', |
| '-iid', |
| 'iid', |
| ]; |
| await overrideRun((executable, arguments, {runInShell = false}) async { |
| if (executable != 'snoopy_broker') { |
| expect(executable, 'cipd'); |
| if (arguments[0] != 'pkg-register') { |
| expect(arguments, expectedBuildArgs); |
| } else { |
| expect(arguments, expectedRegisterArgs); |
| } |
| } else { |
| expect(arguments, expectedReportArgs); |
| } |
| return io.ProcessResult(123, 0, null, ''); |
| }, () => package.upload(temp, true)); |
| }); |
| |
| runTest('cipd upload fail', () async { |
| final fileSystem = MemoryFileSystem.test(); |
| final temp = await fileSystem.directory('/temp').create(); |
| |
| final package = TestPackage('dart/path/package/platform', '1.2.3'); |
| package.log = createTestLogger(); |
| final future = mockedRun(io.ProcessResult(123, 1, null, ''), 'cipd', null, |
| () => package.upload(temp, false)); |
| expect(future, throwsA(isA<io.ProcessException>())); |
| }); |
| } |