blob: c98389643ab9b6b5cb49c52bcea73375a167395e [file] [log] [blame]
// Wrapper around the `tar` command, for testing.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:tar/tar.dart' as tar;
import 'package:test/test.dart';
Future<Process> startTar(List<String> args, {String? baseDir}) {
return Process.start('tar', args, workingDirectory: baseDir).then((proc) {
expect(proc.exitCode, completion(0),
reason: 'tar ${args.join(' ')} should complete normally');
// Attach stderr listener, we don't expect any output on that
late List<int> data;
final sink = ByteConversionSink.withCallback((result) => data = result);
proc.stderr.forEach(sink.add).then((Object? _) {
const LineSplitter().convert(utf8.decode(data)).forEach(print);
return proc;
Stream<List<int>> createTarStream(Iterable<String> files,
{String archiveFormat = 'gnu',
String? sparseVersion,
String? baseDir}) async* {
final args = [
if (sparseVersion != null) {
final tar = await startTar(args, baseDir: baseDir);
yield* tar.stdout;
Future<Process> writeToTar(List<String> args, Stream<tar.TarEntry> entries,
{tar.OutputFormat format = tar.OutputFormat.pax}) async {
final proc = await startTar(args);
await entries.pipe(tar.tarWritingSink(proc.stdin, format: format));
return proc;
extension ProcessUtils on Process {
Stream<String> get lines {
return this.stdout.transform(utf8.decoder).transform(const LineSplitter());