blob: 2b2e3216060f496957bb8ad0f3cd09329ab37c6e [file] [log] [blame]
// Copyright (c) 2013, 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:io';
import 'package:path/path.dart' as path;
import 'package:pub/src/log.dart' as log;
import 'package:pub/src/io.dart';
import 'package:test_descriptor/test_descriptor.dart';
/// Describes a tar file and its contents.
class TarFileDescriptor extends FileDescriptor {
final List<Descriptor> contents;
TarFileDescriptor(String name, Iterable<Descriptor> contents)
: contents = contents.toList(),
/// Creates the files and directories within this tar file, then archives
/// them, compresses them, and saves the result to [parentDir].
Future create([String parent]) {
return withTempDir((tempDir) async {
await Future.wait( => entry.create(tempDir)));
var createdContents = listDir(tempDir,
recursive: true, includeHidden: true, includeDirs: false);
var bytes =
await createTarGz(createdContents, baseDir: tempDir).toBytes();
var file = path.join(parent ?? sandbox, name);
_writeBinaryFile(file, bytes);
return file;
/// Validates that the `.tar.gz` file at [path] contains the expected
/// contents.
Future validate([String parent]) {
throw UnimplementedError('TODO(nweiz): implement this');
Future<String> read() =>
throw UnsupportedError(' is not supported.');
Stream<List<int>> readAsBytes() {
return Stream<List<int>>.fromFuture(withTempDir((tempDir) async {
await create(tempDir);
return readBinaryFile(path.join(tempDir, name));
/// Creates [file] and writes [contents] to it.
String _writeBinaryFile(String file, List<int> contents) {'Writing ${contents.length} bytes to binary file $file.');
File(file).openSync(mode: FileMode.write)
log.fine('Wrote text file $file.');
return file;