33// BSD-style license that can be found in the LICENSE file.
44
55import 'dart:convert' ;
6- import 'dart:io' ;
76
8- import 'package:clock/clock.dart' ;
9- import 'package:http/http.dart' as http;
10- import 'package:path/path.dart' as p;
7+ import 'package:pub_dev/search/backend.dart' ;
118import 'package:pub_dev/search/sdk_mem_index.dart' ;
12- import 'package:pub_dev/shared/versions.dart' ;
13- import 'package:retry/retry.dart' ;
149import 'package:test/test.dart' ;
1510
11+ import '../shared/test_services.dart' ;
12+
1613void main () {
1714 group ('dartdoc index.json parsing' , () {
18- /// Downloads [url and creates a cached file in the .dart_tool/pub-search-data directory.
19- ///
20- /// Reuses the same file up to a week.
21- Future <File > getCachedFile (String name, String url) async {
22- final file = File (p.join ('.dart_tool' , 'pub-search-data' , name));
23- if (await file.exists ()) {
24- final lastModified = await file.lastModified ();
25- final age = clock.now ().difference (lastModified);
26- if (age.inDays < 7 ) {
27- return file;
28- }
29- }
30- await file.parent.create (recursive: true );
31- return retry (() async {
32- final rs = await http.get (Uri .parse (url));
33- if (rs.statusCode != 200 ) {
34- throw Exception ('Unexpected status code for $url : ${rs .statusCode }' );
35- }
36- await file.writeAsBytes (rs.bodyBytes);
37- return file;
38- });
39- }
40-
41- test ('parse Dart SDK index.json' , () async {
42- final file = await getCachedFile ('dart-sdk-$runtimeSdkVersion .json' ,
43- 'https://api.dart.dev/stable/$runtimeSdkVersion /index.json' );
44- final textContent = await file.readAsString ();
45- final index = DartdocIndex .parseJsonText (await file.readAsString ());
15+ testWithProfile ('parse Dart SDK index.json' , fn: () async {
16+ final textContent = await searchBackend.loadOrFetchSdkIndexJsonAsString (
17+ Uri .parse ('https://api.dart.dev/stable/latest/index.json' ),
18+ ttl: Duration (days: 1 ),
19+ );
20+ final index = DartdocIndex .parseJsonText (textContent);
4621 expect (index.entries, hasLength (greaterThan (10000 )));
4722
4823 final libraries =
@@ -59,11 +34,12 @@ void main() {
5934 expect (json.decode (index.toJsonText ()), json.decode (textContent));
6035 });
6136
62- test ('parse Flutter SDK index.json' , () async {
63- final file = await getCachedFile (
64- 'flutter-sdk.json' , 'https://api.flutter.dev/flutter/index.json' );
65- final textContent = await file.readAsString ();
66- final index = DartdocIndex .parseJsonText (await file.readAsString ());
37+ testWithProfile ('parse Flutter SDK index.json' , fn: () async {
38+ final textContent = await searchBackend.loadOrFetchSdkIndexJsonAsString (
39+ Uri .parse ('https://api.flutter.dev/flutter/index.json' ),
40+ ttl: Duration (days: 1 ),
41+ );
42+ final index = DartdocIndex .parseJsonText (textContent);
6743 expect (index.entries, hasLength (greaterThan (10000 )));
6844
6945 final libraries =
0 commit comments