From ccbc3d95dcd11affd10bf8912ee962f42395d0ea Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Mon, 11 Sep 2023 18:26:15 +0000 Subject: [PATCH] [analysis_server] Add additional tests for file delete/create with overlays Change-Id: Iec6a158f394611a84bce6e4bf107039fbf82241e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/325340 Reviewed-by: Brian Wilkerson Commit-Queue: Brian Wilkerson --- .../test/domain_analysis_test.dart | 63 ++++++++++++++++++- .../test/lsp/document_changes_test.dart | 53 +++++++++++++++- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/pkg/analysis_server/test/domain_analysis_test.dart b/pkg/analysis_server/test/domain_analysis_test.dart index 6bf9383d891..252643247e9 100644 --- a/pkg/analysis_server/test/domain_analysis_test.dart +++ b/pkg/analysis_server/test/domain_analysis_test.dart @@ -755,6 +755,61 @@ analyzer: _assertAnalyzedFiles(hasErrors: [], notAnalyzed: [path]); } + /// Tests that deleting and re-creating a file while an overlay is active + /// keeps the diagnotics when the overlay is then removed, then removes them + /// when the file is deleted. + /// + /// https://github.com/dart-lang/sdk/issues/53475 + Future test_fileSystem_deleteFile_createFile_withOverlay_dart() async { + var a_path = convertPath('$testPackageLibPath/a.dart'); + + _createFilesWithErrors([a_path]); + + await setRoots(included: [workspaceRootPath], excluded: []); + await server.onAnalysisComplete; + + // Initial file has errors. + assertHasErrors(a_path); + + // Overlay still has errors. + await handleSuccessfulRequest( + AnalysisUpdateContentParams({ + a_path: AddContentOverlay('error'), + }).toRequest('0'), + ); + await pumpEventQueue(); + await server.onAnalysisComplete; + assertHasErrors(a_path); + + // After deleting the file, still has errors. + deleteFile(a_path); + await pumpEventQueue(); + await server.onAnalysisComplete; + assertHasErrors(a_path); + + // After re-creating the file, still has errors. + _createFilesWithErrors([a_path]); + await pumpEventQueue(); + await server.onAnalysisComplete; + assertHasErrors(a_path); + + // After removing the overlay, still has errors. + await handleSuccessfulRequest( + AnalysisUpdateContentParams({ + a_path: RemoveContentOverlay(), + }).toRequest('1'), + ); + await pumpEventQueue(); + await server.onAnalysisComplete; + assertHasErrors(a_path); + + // After deleting the file again, errors are now gone. + deleteFile(a_path); + await pumpEventQueue(); + await server.onAnalysisComplete; + assertNoErrorsNotification(a_path); + } + Future test_fileSystem_deleteFile_dart() async { var a_path = '$testPackageLibPath/a.dart'; @@ -873,6 +928,10 @@ void f(A a) {} assertNoErrorsNotification(a_path); } + /// Tests that deleting a file does not clear diagnostics if there's still + /// an active overlay for the file. + /// + /// https://github.com/dart-lang/sdk/issues/53475 Future test_fileSystem_deleteFile_withOverlay_dart() async { var a_path = convertPath('$testPackageLibPath/a.dart'); @@ -894,13 +953,13 @@ void f(A a) {} await server.onAnalysisComplete; assertHasErrors(a_path); - // After deleting files, still has errors. + // After deleting the file, still has errors. deleteFile(a_path); await pumpEventQueue(); await server.onAnalysisComplete; assertHasErrors(a_path); - // After removing overlay, errors are gone. + // After removing the overlay, errors are gone. await handleSuccessfulRequest( AnalysisUpdateContentParams({ a_path: RemoveContentOverlay(), diff --git a/pkg/analysis_server/test/lsp/document_changes_test.dart b/pkg/analysis_server/test/lsp/document_changes_test.dart index 41980a299d3..0babb0fdbea 100644 --- a/pkg/analysis_server/test/lsp/document_changes_test.dart +++ b/pkg/analysis_server/test/lsp/document_changes_test.dart @@ -168,7 +168,7 @@ class Bar { equals(content)); } - /// Checks that deleting a file does not clear diagnostics while there's an + /// Tests that deleting a file does not clear diagnostics while there's an /// overlay, and that removing the overlay later clears the diagnostics. /// /// https://github.com/dart-lang/sdk/issues/53475 @@ -197,13 +197,62 @@ class Bar { await pumpEventQueue(times: 5000); expect(latestDiagnostics[mainFilePath], isNotEmpty); - // Expect diagnostics to be removed after we cloes the file (which removes + // Expect diagnostics to be removed after we close the file (which removes // the overlay). await closeFile(mainFileUri); await pumpEventQueue(times: 5000); expect(latestDiagnostics[mainFilePath], isEmpty); } + /// Tests that deleting and re-creating a file while an overlay is active + /// keeps the diagnotics when the overlay is then removed, then removes them + /// when the file is deleted. + /// + /// https://github.com/dart-lang/sdk/issues/53475 + Future + test_documentOpen_fileDeleted_fileCreated_documentClosed_fileDeleted() async { + const content = 'error'; + newFile(mainFilePath, content); + + // Track the latest diagnostics as the client would. + Map> latestDiagnostics = {}; + trackDiagnostics(latestDiagnostics); + + // Expect diagnostics after initial analysis because file has invalid + // content. + await initialize(); + await pumpEventQueue(times: 5000); + expect(latestDiagnostics[mainFilePath], isNotEmpty); + + // Expect diagnostics after opening the file with the same contents. + await openFile(mainFileUri, content); + await pumpEventQueue(times: 5000); + expect(latestDiagnostics[mainFilePath], isNotEmpty); + + // Expect diagnostics after deleting the file because the overlay is still + // active. + deleteFile(mainFilePath); + await pumpEventQueue(times: 5000); + expect(latestDiagnostics[mainFilePath], isNotEmpty); + + // Expect diagnostics remain after re-creating the file (the overlay is still + // active). + newFile(mainFilePath, content); + await pumpEventQueue(times: 5000); + expect(latestDiagnostics[mainFilePath], isNotEmpty); + + // Expect diagnostics remain after we close the file because the file still + //exists on disk. + await closeFile(mainFileUri); + await pumpEventQueue(times: 5000); + expect(latestDiagnostics[mainFilePath], isNotEmpty); + + // Finally, expect deleteing the file clears the diagnostics. + deleteFile(mainFilePath); + await pumpEventQueue(times: 5000); + expect(latestDiagnostics[mainFilePath], isEmpty); + } + Future test_documentOpen_notifiesPlugins() async { if (!AnalysisServer.supportsPlugins) return; await _initializeAndOpen();