OLD | NEW |
| (Empty) |
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. | |
6 library descriptor; | |
7 | |
8 import 'package:oauth2/oauth2.dart' as oauth2; | |
9 import 'package:scheduled_test/scheduled_server.dart'; | |
10 import 'package:scheduled_test/descriptor.dart'; | |
11 | |
12 import '../lib/src/io.dart'; | |
13 import '../lib/src/utils.dart'; | |
14 import 'descriptor/git.dart'; | |
15 import 'descriptor/tar.dart'; | |
16 import 'test_pub.dart'; | |
17 | |
18 export 'package:scheduled_test/descriptor.dart'; | |
19 export 'descriptor/git.dart'; | |
20 export 'descriptor/tar.dart'; | |
21 | |
22 /// Creates a new [GitRepoDescriptor] with [name] and [contents]. | |
23 GitRepoDescriptor git(String name, [Iterable<Descriptor> contents]) => | |
24 new GitRepoDescriptor(name, contents == null ? <Descriptor>[] : contents); | |
25 | |
26 /// Creates a new [TarRepoDescriptor] with [name] and [contents]. | |
27 TarFileDescriptor tar(String name, [Iterable<Descriptor> contents]) => | |
28 new TarFileDescriptor(name, contents == null ? <Descriptor>[] : contents); | |
29 | |
30 /// Describes a package that passes all validation. | |
31 Descriptor get validPackage => | |
32 dir( | |
33 appPath, | |
34 [ | |
35 libPubspec("test_pkg", "1.0.0"), | |
36 file("LICENSE", "Eh, do what you want."), | |
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. | |
43 FileDescriptor outOfDateSnapshot(String name) => | |
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. | |
51 Descriptor pubspec(Map contents) { | |
52 return async( | |
53 awaitObject( | |
54 contents).then( | |
55 (resolvedContents) => file("pubspec.yaml", yaml(resolvedContents))
)); | |
56 } | |
57 | |
58 /// Describes a file named `pubspec.yaml` for an application package with the | |
59 /// given [dependencies]. | |
60 Descriptor appPubspec([Map dependencies]) { | |
61 var map = { | |
62 "name": "myapp" | |
63 }; | |
64 if (dependencies != null) map["dependencies"] = dependencies; | |
65 return pubspec(map); | |
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. | |
71 Descriptor libPubspec(String name, String version, {Map deps, String sdk}) { | |
72 var map = packageMap(name, version, deps); | |
73 if (sdk != null) map["environment"] = { | |
74 "sdk": sdk | |
75 }; | |
76 return pubspec(map); | |
77 } | |
78 | |
79 /// Describes a directory named `lib` containing a single dart file named | |
80 /// `<name>.dart` that contains a line of Dart code. | |
81 Descriptor libDir(String name, [String code]) { | |
82 // Default to printing the name if no other code was given. | |
83 if (code == null) code = name; | |
84 return dir("lib", [file("$name.dart", 'main() => "$code";')]); | |
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. | |
89 Descriptor gitPackageRevisionCacheDir(String name, [int modifier]) { | |
90 var value = name; | |
91 if (modifier != null) value = "$name $modifier"; | |
92 return pattern( | |
93 new RegExp("$name${r'-[a-f0-9]+'}"), | |
94 (dirName) => dir(dirName, [libDir(name, value)])); | |
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. | |
99 Descriptor gitPackageRepoCacheDir(String name) { | |
100 return pattern( | |
101 new RegExp("$name${r'-[a-f0-9]+'}"), | |
102 (dirName) => | |
103 dir(dirName, [dir('hooks'), dir('info'), dir('objects'), dir('refs')])
); | |
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. | |
111 Descriptor packagesDir(Map<String, String> packages) { | |
112 var contents = <Descriptor>[]; | |
113 packages.forEach((name, version) { | |
114 if (version == null) { | |
115 contents.add(nothing(name)); | |
116 } else { | |
117 contents.add( | |
118 dir(name, [file("$name.dart", 'main() => "$name $version";')])); | |
119 } | |
120 }); | |
121 return dir(packagesPath, contents); | |
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. | |
136 Descriptor cacheDir(Map packages, {bool includePubspecs: false}) { | |
137 var contents = <Descriptor>[]; | |
138 packages.forEach((name, versions) { | |
139 if (versions is! List) versions = [versions]; | |
140 for (var version in versions) { | |
141 var packageContents = [libDir(name, '$name $version')]; | |
142 if (includePubspecs) { | |
143 packageContents.add(libPubspec(name, version)); | |
144 } | |
145 contents.add(dir("$name-$version", packageContents)); | |
146 } | |
147 }); | |
148 | |
149 return hostedCache(contents); | |
150 } | |
151 | |
152 /// Describes the main cache directory containing cached hosted packages | |
153 /// downloaded from the mock package server. | |
154 Descriptor hostedCache(Iterable<Descriptor> contents) { | |
155 return dir( | |
156 cachePath, | |
157 [dir('hosted', [async(port.then((p) => dir('localhost%58$p', contents)))])
]); | |
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. | |
163 Descriptor credentialsFile(ScheduledServer server, String accessToken, | |
164 {String refreshToken, DateTime expiration}) { | |
165 return async(server.url.then((url) { | |
166 return dir( | |
167 cachePath, | |
168 [ | |
169 file( | |
170 'credentials.json', | |
171 new oauth2.Credentials( | |
172 accessToken, | |
173 refreshToken, | |
174 url.resolve('/token'), | |
175 ['https://www.googleapis.com/auth/userinfo.email'], | |
176 expiration).toJson())]); | |
177 })); | |
178 } | |
179 | |
180 /// Describes the application directory, containing only a pubspec specifying | |
181 /// the given [dependencies]. | |
182 DirectoryDescriptor appDir([Map dependencies]) => | |
183 dir(appPath, [appPubspec(dependencies)]); | |
OLD | NEW |