From 64a4813f3c7b74759ec2db3f3ae4e9c312d0d9b8 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum <nex342@gmail.com> Date: Thu, 29 Sep 2016 12:48:38 -0700 Subject: [PATCH] Test package APIs rather than resource APIs. Test package APIs rather than resource APIs. Closes #1446 --- test/global/run/package_api_test.dart | 100 +++++++++++++++ test/global/run/resource_test.dart | 111 ---------------- test/package_server.dart | 7 ++ test/run/package_api_test.dart | 142 +++++++++++++++++++++ test/run/resource_test.dart | 175 -------------------------- 5 files changed, 249 insertions(+), 286 deletions(-) create mode 100644 test/global/run/package_api_test.dart delete mode 100644 test/global/run/resource_test.dart create mode 100644 test/run/package_api_test.dart delete mode 100644 test/run/resource_test.dart diff --git a/test/global/run/package_api_test.dart b/test/global/run/package_api_test.dart new file mode 100644 index 00000000..144d8adb --- /dev/null +++ b/test/global/run/package_api_test.dart @@ -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'))); +} +""") + ]) + ]).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); + }); +} diff --git a/test/global/run/resource_test.dart b/test/global/run/resource_test.dart deleted file mode 100644 index ad0edb70..00000000 --- a/test/global/run/resource_test.dart +++ /dev/null @@ -1,111 +0,0 @@ -// 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. - -import '../../descriptor.dart' as d; -import '../../test_pub.dart'; - -main() { - integration('the spawned application can load its own resource', () { - servePackages((builder) { - builder.serve("foo", "1.0.0", contents: [ - d.dir("lib", [ - d.file("resource.txt", "hello!") - ]), - d.dir("bin", [ - d.file("script.dart", """ -main() async { - var resource = new Resource("package:foo/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]); - }); - - schedulePub(args: ["global", "activate", "foo"]); - - var pub = pubRun(global: true, args: ["foo:script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); - - integration("the spawned application can load a dependency's resource", () { - servePackages((builder) { - builder.serve("bar", "1.0.0", contents: [ - d.dir("lib", [ - d.file("resource.txt", "hello!") - ]) - ]); - - builder.serve("foo", "1.0.0", deps: { - "bar": "any" - }, contents: [ - d.dir("bin", [ - d.file("script.dart", """ -main() async { - var resource = new Resource("package:bar/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]); - }); - - schedulePub(args: ["global", "activate", "foo"]); - - var pub = pubRun(global: true, args: ["foo:script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); - - integration('a mutable application can load its own resource', () { - d.dir("foo", [ - d.libPubspec("foo", "1.0.0"), - d.dir("lib", [ - d.file("resource.txt", "hello!") - ]), - d.dir("bin", [ - d.file("script.dart", """ -main() async { - var resource = new Resource("package:foo/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]).create(); - - schedulePub(args: ["global", "activate", "--source", "path", "../foo"]); - - var pub = pubRun(global: true, args: ["foo:script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); -} diff --git a/test/package_server.dart b/test/package_server.dart index 6fedf61f..95983648 100644 --- a/test/package_server.dart +++ b/test/package_server.dart @@ -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"); diff --git a/test/run/package_api_test.dart b/test/run/package_api_test.dart new file mode 100644 index 00000000..e5ad69b5 --- /dev/null +++ b/test/run/package_api_test.dart @@ -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); + }); +} diff --git a/test/run/resource_test.dart b/test/run/resource_test.dart deleted file mode 100644 index 08a4b133..00000000 --- a/test/run/resource_test.dart +++ /dev/null @@ -1,175 +0,0 @@ -// 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. - -import 'package:scheduled_test/scheduled_test.dart'; - -import '../descriptor.dart' as d; -import '../test_pub.dart'; - -const 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())); - } -} -"""; - -main() { - integration('the spawned application can load its own resource', () { - d.dir(appPath, [ - d.appPubspec(), - d.dir("lib", [ - d.file("resource.txt", "hello!") - ]), - d.dir("bin", [ - d.file("script.dart", """ -main() async { - var resource = new Resource("package:myapp/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]).create(); - - pubGet(); - var pub = pubRun(args: ["bin/script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); - - integration("the spawned application can load a dependency's resource", () { - d.dir("foo", [ - d.libPubspec("foo", "1.0.0"), - d.dir("lib", [ - d.file("resource.txt", "hello!") - ]) - ]).create(); - - d.dir(appPath, [ - d.appPubspec({ - "foo": {"path": "../foo"} - }), - d.dir("bin", [ - d.file("script.dart", """ -main() async { - var resource = new Resource("package:foo/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]).create(); - - pubGet(); - var pub = pubRun(args: ["bin/script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "foo/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); - - integration('the spawned application can load a transformed resource', () { - serveBarback(); - - 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", """ -main() async { - var resource = new Resource("package:myapp/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]).create(); - - pubGet(); - var pub = pubRun(args: ["bin/script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "myapp/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); - - integration('a snapshotted application can load a resource', () { - servePackages((builder) { - builder.serve("foo", "1.0.0", contents: [ - d.dir("lib", [ - d.file("resource.txt", "hello!") - ]), - d.dir("bin", [ - d.file("script.dart", """ -main() async { - var resource = new Resource("package:foo/resource.txt"); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // print(resource.uri); - - print(await resource.readAsString()); -} -""") - ]) - ]); - }); - - d.dir(appPath, [ - d.appPubspec({ - "foo": "any" - }) - ]).create(); - - pubGet(output: contains("Precompiled foo:script.")); - - var pub = pubRun(args: ["foo:script"]); - - // TODO(nweiz): Enable this when sdk#23990 is fixed. - // pub.stdout.expect(p.toUri(p.join(sandboxDir, "foo/lib/resource.txt"))); - - pub.stdout.expect("hello!"); - pub.shouldExit(0); - }, - skip: "Issue https://github.com/dart-lang/pub/issues/1446"); -} -- GitLab