Skip to content

Test package APIs rather than resource APIs. #1451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions test/global/run/package_api_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2016, 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 'package:path/path.dart' as p;
import 'package:scheduled_test/scheduled_test.dart';

import '../../descriptor.dart' as d;
import '../../test_pub.dart';

main() {
integration('an immutable application sees a file: package config', () {
servePackages((builder) {
builder.serve("bar", "1.0.0");

builder.serve("foo", "1.0.0",
deps: {"bar": "1.0.0"},
contents: [
d.dir("bin", [
d.file("script.dart", """
import 'dart:isolate';

main() async {
print(await Isolate.packageRoot);
print(await Isolate.packageConfig);
print(await Isolate.resolvePackageUri(
Uri.parse('package:foo/resource.txt')));
print(await Isolate.resolvePackageUri(
Uri.parse('package:bar/resource.txt')));
}
""")
])
]);
});

schedulePub(args: ["global", "activate", "foo"]);

var pub = pubRun(global: true, args: ["foo:script"]);

pub.stdout.expect("null");

var packageConfigPath =
p.join(sandboxDir, cachePath, "global_packages/foo/.packages");
pub.stdout.expect(p.toUri(packageConfigPath).toString());

schedule(() async {
var fooResourcePath = p.join(
await globalPackageServer.pathInCache('foo', '1.0.0'),
"lib/resource.txt");
pub.stdout.expect(p.toUri(fooResourcePath).toString());
});

schedule(() async {
var barResourcePath = p.join(
await globalPackageServer.pathInCache('bar', '1.0.0'),
"lib/resource.txt");
pub.stdout.expect(p.toUri(barResourcePath).toString());
});
pub.shouldExit(0);
});

integration('a mutable application sees an http: package root', () {
d.dir("foo", [
d.libPubspec("foo", "1.0.0")
]).create();

d.dir(appPath, [
d.appPubspec({"foo": {"path": "../foo"}}),
d.dir("bin", [
d.file("script.dart", """
import 'dart:isolate';

main() async {
print(await Isolate.packageRoot);
print(await Isolate.packageConfig);
print(await Isolate.resolvePackageUri(
Uri.parse('package:myapp/resource.txt')));
print(await Isolate.resolvePackageUri(
Uri.parse('package:foo/resource.txt')));
}
""")
Copy link
Member

@munificent munificent Sep 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same string as the above script, right? If so, declare a constant for it. Ideally stick it somewhere that the run/package_api_test can get to it too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I didn't share it across files, though, because I couldn't think of a nice semantic thing to call it.

])
]).create();

schedulePub(args: ["global", "activate", "-s", "path", "."]);

var pub = pubRun(global: true, args: ["myapp:script"]);

pub.stdout.expect(
allOf(startsWith("http://localhost:"), endsWith("/packages/")));
pub.stdout.expect("null");
pub.stdout.expect(allOf(
startsWith("http://localhost:"),
endsWith("/packages/myapp/resource.txt")));
pub.stdout.expect(allOf(
startsWith("http://localhost:"),
endsWith("/packages/foo/resource.txt")));
pub.shouldExit(0);
});
}
111 changes: 0 additions & 111 deletions test/global/run/resource_test.dart

This file was deleted.

7 changes: 7 additions & 0 deletions test/package_server.dart
Original file line number Diff line number Diff line change
@@ -124,6 +124,13 @@ class PackageServer {
}, 'adding packages to the package server');
}

/// Returns the path of [package] at [version], installed from this server, in
/// the pub cache.
Future<String> pathInCache(String package, String version) async => p.join(
sandboxDir,
cachePath,
"hosted/localhost%58${await port}/$package-$version");

/// Replace the current set of packages that are being served.
void replace(void callback(PackageServerBuilder builder)) {
schedule(() => _builder._clear(), "clearing builder");
142 changes: 142 additions & 0 deletions test/run/package_api_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2016, 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 'package:path/path.dart' as p;
import 'package:scheduled_test/scheduled_test.dart';

import 'package:pub/src/io.dart';

import '../descriptor.dart' as d;
import '../test_pub.dart';

final _transformer = """
import 'dart:async';

import 'package:barback/barback.dart';

class MyTransformer extends Transformer {
MyTransformer.asPlugin();

String get allowedExtensions => '.in';

Future apply(Transform transform) async {
transform.addOutput(new Asset.fromString(
transform.primaryInput.id.changeExtension('.txt'),
await transform.primaryInput.readAsString()));
}
}
""";

final _script = """
import 'dart:isolate';

main() async {
print(await Isolate.packageRoot);
print(await Isolate.packageConfig);
print(await Isolate.resolvePackageUri(
Uri.parse('package:myapp/resource.txt')));
print(await Isolate.resolvePackageUri(
Uri.parse('package:foo/resource.txt')));
}
""";

main() {
integration('an untransformed application sees a file: package config', () {
d.dir("foo", [
d.libPubspec("foo", "1.0.0")
]).create();

d.dir(appPath, [
d.appPubspec({"foo": {"path": "../foo"}}),
d.dir("bin", [
d.file("script.dart", _script)
])
]).create();

pubGet();
var pub = pubRun(args: ["bin/script"]);

pub.stdout.expect("null");
pub.stdout.expect(
p.toUri(p.join(sandboxDir, "myapp/.packages")).toString());
pub.stdout.expect(
p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt")).toString());
pub.stdout.expect(
p.toUri(p.join(sandboxDir, "foo/lib/resource.txt")).toString());
pub.shouldExit(0);
});

integration('a transformed application sees an http: package root', () {
serveBarback();

d.dir("foo", [
d.libPubspec("foo", "1.0.0")
]).create();

d.dir(appPath, [
d.pubspec({
"name": "myapp",
"transformers": ["myapp/src/transformer"],
"dependencies": {"barback": "any"}
}),
d.dir("lib", [
d.file("resource.in", "hello!"),
d.dir("src", [
d.file("transformer.dart", _transformer)
])
]),
d.dir("bin", [
d.file("script.dart", _script)
])
]).create();

pubGet();
var pub = pubRun(args: ["bin/script"]);

pub.stdout.expect(
allOf(startsWith("http://localhost:"), endsWith("/packages/")));
pub.stdout.expect("null");
pub.stdout.expect(allOf(
startsWith("http://localhost:"),
endsWith("/packages/myapp/resource.txt")));
pub.stdout.expect(allOf(
startsWith("http://localhost:"),
endsWith("/packages/foo/resource.txt")));
pub.shouldExit(0);
});

integration('a snapshotted application sees a file: package root', () {
servePackages((builder) {
builder.serve("foo", "1.0.0",
contents: [
d.dir("bin", [
d.file("script.dart", _script)
])
]);
});

d.dir(appPath, [
d.appPubspec({
"foo": "any"
})
]).create();

pubGet(output: contains("Precompiled foo:script."));

var pub = pubRun(args: ["foo:script"]);

pub.stdout.expect("null");
pub.stdout.expect(
p.toUri(p.join(sandboxDir, "myapp/.packages")).toString());
pub.stdout.expect(
p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt")).toString());
schedule(() async {
var fooResourcePath = p.join(
await globalPackageServer.pathInCache('foo', '1.0.0'),
"lib/resource.txt");
pub.stdout.expect(p.toUri(fooResourcePath).toString());
});
pub.shouldExit(0);
});
}
175 changes: 0 additions & 175 deletions test/run/resource_test.dart

This file was deleted.