Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Unified Diff: sdk/lib/_internal/pub_generated/test/serve_packages.dart

Issue 657673002: Regenerate pub sources. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/pub_generated/test/serve_packages.dart
diff --git a/sdk/lib/_internal/pub_generated/test/serve_packages.dart b/sdk/lib/_internal/pub_generated/test/serve_packages.dart
index 7536a92389540292ee5eeee6650765d64191e80a..0b98cf7dd4430529b3cfefb9da29b3dfcf672f91 100644
--- a/sdk/lib/_internal/pub_generated/test/serve_packages.dart
+++ b/sdk/lib/_internal/pub_generated/test/serve_packages.dart
@@ -1,17 +1,53 @@
+// Copyright (c) 2014, 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.
+
library serve_packages;
+
import 'dart:async';
import 'dart:convert';
+
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:scheduled_test/scheduled_test.dart';
import 'package:yaml/yaml.dart';
+
import '../lib/src/io.dart';
import '../lib/src/utils.dart';
import 'descriptor.dart' as d;
import 'test_pub.dart';
+
+/// The [d.DirectoryDescriptor] describing the server layout of `/api/packages`
+/// on the test server.
+///
+/// This contains metadata for packages that are being served via
+/// [servePackages]. It's `null` if [servePackages] has not yet been called for
+/// this test.
d.DirectoryDescriptor _servedApiPackageDir;
+
+/// The [d.DirectoryDescriptor] describing the server layout of `/packages` on
+/// the test server.
+///
+/// This contains the tarballs for packages that are being served via
+/// [servePackages]. It's `null` if [servePackages] has not yet been called for
+/// this test.
d.DirectoryDescriptor _servedPackageDir;
+
+/// The current [PackageServerBuilder] that a user uses to specify which package
+/// to serve.
+///
+/// This is preserved over multiple calls to [servePackages] within the same
+/// test so that additional packages can be added.
PackageServerBuilder _builder;
+
+/// Creates an HTTP server that replicates the structure of pub.dartlang.org.
+///
+/// Calls [callback] with a [PackageServerBuilder] that's used to specify
+/// which packages to serve.
+///
+/// If [replace] is false, subsequent calls to [servePackages] will add to the
+/// set of packages that are being served. Previous packages will continue to be
+/// served. Otherwise, the previous packages will no longer be served.
void servePackages(void callback(PackageServerBuilder builder), {bool replace:
false}) {
if (_servedPackageDir == null) {
@@ -19,12 +55,14 @@ void servePackages(void callback(PackageServerBuilder builder), {bool replace:
_servedApiPackageDir = d.dir('packages', []);
_servedPackageDir = d.dir('packages', []);
serve([d.dir('api', [_servedApiPackageDir]), _servedPackageDir]);
+
currentSchedule.onComplete.schedule(() {
_builder = null;
_servedApiPackageDir = null;
_servedPackageDir = null;
}, 'cleaning up served packages');
}
+
schedule(() {
if (replace) _builder = new PackageServerBuilder();
callback(_builder);
@@ -42,6 +80,7 @@ void servePackages(void callback(PackageServerBuilder builder), {bool replace:
version.version.toString(),
JSON.encode(packageVersionApiMap(version.pubspec, full: true)));
}))])]);
+
_servedPackageDir.contents.add(
d.dir(
name,
@@ -54,36 +93,68 @@ void servePackages(void callback(PackageServerBuilder builder), {bool replace:
});
}, 'initializing the package server');
}
+
+/// Like [servePackages], but instead creates an empty server with no packages
+/// registered.
+///
+/// This will always replace a previous server.
void serveNoPackages() => servePackages((_) {}, replace: true);
+
+/// A builder for specifying which packages should be served by [servePackages].
class PackageServerBuilder {
+ /// A map from package names to a list of concrete packages to serve.
final _packages = new Map<String, List<_ServedPackage>>();
+
+ /// A group of futures from [serve] calls.
+ ///
+ /// This should be accessed by calling [_awair].
var _futures = new FutureGroup();
+
+ /// Specifies that a package named [name] with [version] should be served.
+ ///
+ /// If [deps] is passed, it's used as the "dependencies" field of the pubspec.
+ /// If [pubspec] is passed, it's used as the rest of the pubspec. Either of
+ /// these may recursively contain Futures.
+ ///
+ /// If [contents] is passed, it's used as the contents of the package. By
+ /// default, a package just contains a dummy lib directory.
void serve(String name, String version, {Map deps, Map pubspec,
Iterable<d.Descriptor> contents}) {
_futures.add(
Future.wait([awaitObject(deps), awaitObject(pubspec)]).then((pair) {
var resolvedDeps = pair.first;
var resolvedPubspec = pair.last;
+
var pubspecFields = {
"name": name,
"version": version
};
if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec);
if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps;
+
if (contents == null) contents = [d.libDir(name, "$name $version")];
contents =
[d.file("pubspec.yaml", yaml(pubspecFields))]..addAll(contents);
+
var packages = _packages.putIfAbsent(name, () => []);
packages.add(new _ServedPackage(pubspecFields, contents));
}));
}
+
+ /// Serves the versions of [package] and all its dependencies that are
+ /// currently checked into the Dart repository.
void serveRepoPackage(String package) {
_addPackage(name) {
if (_packages.containsKey(name)) return;
_packages[name] = [];
+
var pubspec = new Map.from(
loadYaml(readTextFile(p.join(repoRoot, 'pkg', name, 'pubspec.yaml'))));
+
+ // Remove any SDK constraints since we don't have a valid SDK version
+ // while testing.
pubspec.remove('environment');
+
_packages[name].add(
new _ServedPackage(
pubspec,
@@ -92,12 +163,17 @@ class PackageServerBuilder {
new d.DirectoryDescriptor.fromFilesystem(
'lib',
p.join(repoRoot, 'pkg', name, 'lib'))]));
+
if (pubspec.containsKey('dependencies')) {
pubspec['dependencies'].keys.forEach(_addPackage);
}
}
+
_addPackage(package);
}
+
+ /// Returns a Future that completes once all the [serve] calls have been fully
+ /// processed.
Future _await() {
if (_futures.futures.isEmpty) return new Future.value();
return _futures.future.then((_) {
@@ -105,9 +181,13 @@ class PackageServerBuilder {
});
}
}
+
+/// A package that's intended to be served.
class _ServedPackage {
final Map pubspec;
final List<d.Descriptor> contents;
+
Version get version => new Version.parse(pubspec['version']);
+
_ServedPackage(this.pubspec, this.contents);
}

Powered by Google App Engine
This is Rietveld 408576698