OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Pub-specific scheduled_test descriptors. |
1 library descriptor; | 6 library descriptor; |
| 7 |
2 import 'package:oauth2/oauth2.dart' as oauth2; | 8 import 'package:oauth2/oauth2.dart' as oauth2; |
3 import 'package:scheduled_test/scheduled_server.dart'; | 9 import 'package:scheduled_test/scheduled_server.dart'; |
4 import 'package:scheduled_test/descriptor.dart'; | 10 import 'package:scheduled_test/descriptor.dart'; |
| 11 |
5 import '../lib/src/io.dart'; | 12 import '../lib/src/io.dart'; |
6 import '../lib/src/utils.dart'; | 13 import '../lib/src/utils.dart'; |
7 import 'descriptor/git.dart'; | 14 import 'descriptor/git.dart'; |
8 import 'descriptor/tar.dart'; | 15 import 'descriptor/tar.dart'; |
9 import 'test_pub.dart'; | 16 import 'test_pub.dart'; |
| 17 |
10 export 'package:scheduled_test/descriptor.dart'; | 18 export 'package:scheduled_test/descriptor.dart'; |
11 export 'descriptor/git.dart'; | 19 export 'descriptor/git.dart'; |
12 export 'descriptor/tar.dart'; | 20 export 'descriptor/tar.dart'; |
| 21 |
| 22 /// Creates a new [GitRepoDescriptor] with [name] and [contents]. |
13 GitRepoDescriptor git(String name, [Iterable<Descriptor> contents]) => | 23 GitRepoDescriptor git(String name, [Iterable<Descriptor> contents]) => |
14 new GitRepoDescriptor(name, contents == null ? <Descriptor>[] : contents); | 24 new GitRepoDescriptor(name, contents == null ? <Descriptor>[] : contents); |
| 25 |
| 26 /// Creates a new [TarRepoDescriptor] with [name] and [contents]. |
15 TarFileDescriptor tar(String name, [Iterable<Descriptor> contents]) => | 27 TarFileDescriptor tar(String name, [Iterable<Descriptor> contents]) => |
16 new TarFileDescriptor(name, contents == null ? <Descriptor>[] : contents); | 28 new TarFileDescriptor(name, contents == null ? <Descriptor>[] : contents); |
| 29 |
| 30 /// Describes a package that passes all validation. |
17 Descriptor get validPackage => | 31 Descriptor get validPackage => |
18 dir( | 32 dir( |
19 appPath, | 33 appPath, |
20 [ | 34 [ |
21 libPubspec("test_pkg", "1.0.0"), | 35 libPubspec("test_pkg", "1.0.0"), |
22 file("LICENSE", "Eh, do what you want."), | 36 file("LICENSE", "Eh, do what you want."), |
23 dir("lib", [file("test_pkg.dart", "int i = 1;")])]); | 37 dir("lib", [file("test_pkg.dart", "int i = 1;")])]); |
| 38 |
| 39 /// Returns a descriptor of a snapshot that can't be run by the current VM. |
| 40 /// |
| 41 /// This snapshot was generated by the VM on r39611, the revision immediately |
| 42 /// before snapshot versioning was added. |
24 FileDescriptor outOfDateSnapshot(String name) => | 43 FileDescriptor outOfDateSnapshot(String name) => |
25 binaryFile(name, readBinaryFile(testAssetPath('out-of-date.snapshot'))); | 44 binaryFile(name, readBinaryFile(testAssetPath('out-of-date.snapshot'))); |
| 45 |
| 46 /// Describes a file named `pubspec.yaml` with the given YAML-serialized |
| 47 /// [contents], which should be a serializable object. |
| 48 /// |
| 49 /// [contents] may contain [Future]s that resolve to serializable objects, |
| 50 /// which may in turn contain [Future]s recursively. |
26 Descriptor pubspec(Map contents) { | 51 Descriptor pubspec(Map contents) { |
27 return async( | 52 return async( |
28 awaitObject( | 53 awaitObject( |
29 contents).then( | 54 contents).then( |
30 (resolvedContents) => file("pubspec.yaml", yaml(resolvedContents))
)); | 55 (resolvedContents) => file("pubspec.yaml", yaml(resolvedContents))
)); |
31 } | 56 } |
| 57 |
| 58 /// Describes a file named `pubspec.yaml` for an application package with the |
| 59 /// given [dependencies]. |
32 Descriptor appPubspec([Map dependencies]) { | 60 Descriptor appPubspec([Map dependencies]) { |
33 var map = { | 61 var map = { |
34 "name": "myapp" | 62 "name": "myapp" |
35 }; | 63 }; |
36 if (dependencies != null) map["dependencies"] = dependencies; | 64 if (dependencies != null) map["dependencies"] = dependencies; |
37 return pubspec(map); | 65 return pubspec(map); |
38 } | 66 } |
| 67 |
| 68 /// Describes a file named `pubspec.yaml` for a library package with the given |
| 69 /// [name], [version], and [deps]. If "sdk" is given, then it adds an SDK |
| 70 /// constraint on that version. |
39 Descriptor libPubspec(String name, String version, {Map deps, String sdk}) { | 71 Descriptor libPubspec(String name, String version, {Map deps, String sdk}) { |
40 var map = packageMap(name, version, deps); | 72 var map = packageMap(name, version, deps); |
41 if (sdk != null) map["environment"] = { | 73 if (sdk != null) map["environment"] = { |
42 "sdk": sdk | 74 "sdk": sdk |
43 }; | 75 }; |
44 return pubspec(map); | 76 return pubspec(map); |
45 } | 77 } |
| 78 |
| 79 /// Describes a directory named `lib` containing a single dart file named |
| 80 /// `<name>.dart` that contains a line of Dart code. |
46 Descriptor libDir(String name, [String code]) { | 81 Descriptor libDir(String name, [String code]) { |
| 82 // Default to printing the name if no other code was given. |
47 if (code == null) code = name; | 83 if (code == null) code = name; |
48 return dir("lib", [file("$name.dart", 'main() => "$code";')]); | 84 return dir("lib", [file("$name.dart", 'main() => "$code";')]); |
49 } | 85 } |
| 86 |
| 87 /// Describes a directory for a Git package. This directory is of the form |
| 88 /// found in the revision cache of the global package cache. |
50 Descriptor gitPackageRevisionCacheDir(String name, [int modifier]) { | 89 Descriptor gitPackageRevisionCacheDir(String name, [int modifier]) { |
51 var value = name; | 90 var value = name; |
52 if (modifier != null) value = "$name $modifier"; | 91 if (modifier != null) value = "$name $modifier"; |
53 return pattern( | 92 return pattern( |
54 new RegExp("$name${r'-[a-f0-9]+'}"), | 93 new RegExp("$name${r'-[a-f0-9]+'}"), |
55 (dirName) => dir(dirName, [libDir(name, value)])); | 94 (dirName) => dir(dirName, [libDir(name, value)])); |
56 } | 95 } |
| 96 |
| 97 /// Describes a directory for a Git package. This directory is of the form |
| 98 /// found in the repo cache of the global package cache. |
57 Descriptor gitPackageRepoCacheDir(String name) { | 99 Descriptor gitPackageRepoCacheDir(String name) { |
58 return pattern( | 100 return pattern( |
59 new RegExp("$name${r'-[a-f0-9]+'}"), | 101 new RegExp("$name${r'-[a-f0-9]+'}"), |
60 (dirName) => | 102 (dirName) => |
61 dir(dirName, [dir('hooks'), dir('info'), dir('objects'), dir('refs')])
); | 103 dir(dirName, [dir('hooks'), dir('info'), dir('objects'), dir('refs')])
); |
62 } | 104 } |
| 105 |
| 106 /// Describes the `packages/` directory containing all the given [packages], |
| 107 /// which should be name/version pairs. The packages will be validated against |
| 108 /// the format produced by the mock package server. |
| 109 /// |
| 110 /// A package with a null version should not be downloaded. |
63 Descriptor packagesDir(Map<String, String> packages) { | 111 Descriptor packagesDir(Map<String, String> packages) { |
64 var contents = <Descriptor>[]; | 112 var contents = <Descriptor>[]; |
65 packages.forEach((name, version) { | 113 packages.forEach((name, version) { |
66 if (version == null) { | 114 if (version == null) { |
67 contents.add(nothing(name)); | 115 contents.add(nothing(name)); |
68 } else { | 116 } else { |
69 contents.add( | 117 contents.add( |
70 dir(name, [file("$name.dart", 'main() => "$name $version";')])); | 118 dir(name, [file("$name.dart", 'main() => "$name $version";')])); |
71 } | 119 } |
72 }); | 120 }); |
73 return dir(packagesPath, contents); | 121 return dir(packagesPath, contents); |
74 } | 122 } |
| 123 |
| 124 /// Describes the global package cache directory containing all the given |
| 125 /// [packages], which should be name/version pairs. The packages will be |
| 126 /// validated against the format produced by the mock package server. |
| 127 /// |
| 128 /// A package's value may also be a list of versions, in which case all |
| 129 /// versions are expected to be downloaded. |
| 130 /// |
| 131 /// If [includePubspecs] is `true`, then pubspecs will be created for each |
| 132 /// package. Defaults to `false` so that the contents of pubspecs are not |
| 133 /// validated since they will often lack the dependencies section that the |
| 134 /// real pubspec being compared against has. You usually only need to pass |
| 135 /// `true` for this if you plan to call [create] on the resulting descriptor. |
75 Descriptor cacheDir(Map packages, {bool includePubspecs: false}) { | 136 Descriptor cacheDir(Map packages, {bool includePubspecs: false}) { |
76 var contents = <Descriptor>[]; | 137 var contents = <Descriptor>[]; |
77 packages.forEach((name, versions) { | 138 packages.forEach((name, versions) { |
78 if (versions is! List) versions = [versions]; | 139 if (versions is! List) versions = [versions]; |
79 for (var version in versions) { | 140 for (var version in versions) { |
80 var packageContents = [libDir(name, '$name $version')]; | 141 var packageContents = [libDir(name, '$name $version')]; |
81 if (includePubspecs) { | 142 if (includePubspecs) { |
82 packageContents.add(libPubspec(name, version)); | 143 packageContents.add(libPubspec(name, version)); |
83 } | 144 } |
84 contents.add(dir("$name-$version", packageContents)); | 145 contents.add(dir("$name-$version", packageContents)); |
85 } | 146 } |
86 }); | 147 }); |
| 148 |
87 return hostedCache(contents); | 149 return hostedCache(contents); |
88 } | 150 } |
| 151 |
| 152 /// Describes the main cache directory containing cached hosted packages |
| 153 /// downloaded from the mock package server. |
89 Descriptor hostedCache(Iterable<Descriptor> contents) { | 154 Descriptor hostedCache(Iterable<Descriptor> contents) { |
90 return dir( | 155 return dir( |
91 cachePath, | 156 cachePath, |
92 [dir('hosted', [async(port.then((p) => dir('localhost%58$p', contents)))])
]); | 157 [dir('hosted', [async(port.then((p) => dir('localhost%58$p', contents)))])
]); |
93 } | 158 } |
| 159 |
| 160 /// Describes the file in the system cache that contains the client's OAuth2 |
| 161 /// credentials. The URL "/token" on [server] will be used as the token |
| 162 /// endpoint for refreshing the access token. |
94 Descriptor credentialsFile(ScheduledServer server, String accessToken, | 163 Descriptor credentialsFile(ScheduledServer server, String accessToken, |
95 {String refreshToken, DateTime expiration}) { | 164 {String refreshToken, DateTime expiration}) { |
96 return async(server.url.then((url) { | 165 return async(server.url.then((url) { |
97 return dir( | 166 return dir( |
98 cachePath, | 167 cachePath, |
99 [ | 168 [ |
100 file( | 169 file( |
101 'credentials.json', | 170 'credentials.json', |
102 new oauth2.Credentials( | 171 new oauth2.Credentials( |
103 accessToken, | 172 accessToken, |
104 refreshToken, | 173 refreshToken, |
105 url.resolve('/token'), | 174 url.resolve('/token'), |
106 ['https://www.googleapis.com/auth/userinfo.email'], | 175 ['https://www.googleapis.com/auth/userinfo.email'], |
107 expiration).toJson())]); | 176 expiration).toJson())]); |
108 })); | 177 })); |
109 } | 178 } |
| 179 |
| 180 /// Describes the application directory, containing only a pubspec specifying |
| 181 /// the given [dependencies]. |
110 DirectoryDescriptor appDir([Map dependencies]) => | 182 DirectoryDescriptor appDir([Map dependencies]) => |
111 dir(appPath, [appPubspec(dependencies)]); | 183 dir(appPath, [appPubspec(dependencies)]); |
OLD | NEW |