From a36e01d43bb03e25883bb2582f054cb4eea0bc06 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Sat, 2 Dec 2023 21:31:21 +0000 Subject: [PATCH] [analyzer] Reuse analysis context collection in verify_docs_test Perhaps there's a good reason not do this, but it seems to work fine for now, and doesn't hide errors. On my computer it brought the test down from 35 seconds to 600 ms. Closes https://github.com/dart-lang/sdk/issues/54183 Bug: https://github.com/dart-lang/sdk/issues/54183 Change-Id: I15351ce71897534a82da1f649d33b0b7b6180f4c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339440 Reviewed-by: Brian Wilkerson Reviewed-by: Samuel Rawlins Auto-Submit: Parker Lougheed Commit-Queue: Brian Wilkerson --- pkg/analyzer/test/verify_docs_test.dart | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/analyzer/test/verify_docs_test.dart b/pkg/analyzer/test/verify_docs_test.dart index caaf5020f3b..d9a9c8e6fdc 100644 --- a/pkg/analyzer/test/verify_docs_test.dart +++ b/pkg/analyzer/test/verify_docs_test.dart @@ -25,6 +25,7 @@ class SnippetTester { final Folder docFolder; final String snippetDirPath; final String snippetPath; + final AnalysisContextCollection collection; final StringBuffer output = StringBuffer(); @@ -41,7 +42,9 @@ class SnippetTester { } SnippetTester._( - this.provider, this.docFolder, this.snippetDirPath, this.snippetPath); + this.provider, this.docFolder, this.snippetDirPath, this.snippetPath) + : collection = AnalysisContextCollection( + resourceProvider: provider, includedPaths: [snippetPath]); /// Return `true` if the given error is a diagnostic produced by a lint that /// is allowed to occur in documentation. @@ -91,7 +94,7 @@ class SnippetTester { if (output.isNotEmpty) { fail(output.toString()); } - }, timeout: Timeout.factor(6)); + }, timeout: Timeout.factor(4)); } } else if (child is Folder) { await verifyFolder(child); @@ -103,7 +106,7 @@ class SnippetTester { // TODO(brianwilkerson): When the files outside of 'src' contain only public // API, write code to compute the list of imports so that new public API // will automatically be allowed. - String imports = ''' + const String imports = ''' import 'dart:math' as math; import 'package:analyzer/dart/analysis/analysis_context.dart'; @@ -127,13 +130,16 @@ $snippet ''', modificationStamp: 1); try { - AnalysisContextCollection collection = AnalysisContextCollection( - includedPaths: [snippetDirPath], resourceProvider: provider); List contexts = collection.contexts; if (contexts.length != 1) { fail('The snippets directory contains multiple analysis contexts.'); } - var results = await contexts[0].currentSession.getErrors(snippetPath); + var context = contexts[0]; + // Mark the snippet as changed since we reuse the same path + // for each snippet found. + context.changeFile(snippetPath); + await context.applyPendingFileChanges(); + var results = await context.currentSession.getErrors(snippetPath); if (results is ErrorsResult) { Iterable errors = results.errors.where((error) { ErrorCode errorCode = error.errorCode;