Skip to content

Commit

Permalink
Make paren tests run 2–3× as fast
Browse files Browse the repository at this point in the history
* This optimization could in theory be used for all tests in this
  project if we set up some kind of solution cache keyed by project &
  editor settings, but it looks like it would require some refactoring
  in RoslynTestHelpers.
  • Loading branch information
brianrourkeboll committed Nov 22, 2023
1 parent e02f1a4 commit d1773e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ module Xunit =
/// <exception cref="T:FSharp.Editor.Tests.CodeFixes.CodeFixTestFramework.Xunit.UnexpectedCodeFixException">
/// Thrown if a code fix is applied.
/// </exception>
let expectNoFix (tryFix: string -> Task<TestCodeFix option>) code =
let expectNoFix (tryFix: string -> CancellableTask<TestCodeFix option>) code =
cancellableTask {
match! tryFix code with
| None -> ()
Expand All @@ -246,7 +246,7 @@ module Xunit =
/// <exception cref="T:FSharp.Editor.Tests.CodeFixes.CodeFixTestFramework.Xunit.WrongCodeFixException">
/// Thrown if the generated fix does not match the expected fixed code.
/// </exception>
let expectFix tryFix code fixedCode =
let expectFix (tryFix: string -> CancellableTask<TestCodeFix option>) code fixedCode =
if code = fixedCode then
expectNoFix tryFix code
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module FSharp.Editor.Tests.CodeFixes.RemoveUnnecessaryParenthesesTests

open System.Text
open FSharp.Compiler.Text
open FSharp.Editor.Tests.Helpers
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Text
open Microsoft.VisualStudio.FSharp.Editor
open Microsoft.VisualStudio.FSharp.Editor.CancellableTasks
Expand All @@ -13,17 +14,45 @@ open CodeFixTestFramework
[<AutoOpen>]
module private TopLevel =
let private fixer = FSharpRemoveUnnecessaryParenthesesCodeFixProvider()
let private fixAllProvider = fixer.RegisterFsharpFixAll()

// It is much (2–3×) faster to reuse the same solution
// rather than creating a new one for each test.
// Unfortunately, it is not safe to reuse the same solution across
// tests that may set different project or editor options,
// because they may run concurrently on other threads,
// and the in-memory settings store used for tests is global,
// so we restrict this optimization to this file.
let private sln, projId =
let projInfo =
RoslynTestHelpers.CreateProjectInfo (ProjectId.CreateNewId()) "C:\\test.fsproj" []

let sln = RoslynTestHelpers.CreateSolution [ projInfo ]

let projectOptions =
{ RoslynTestHelpers.DefaultProjectOptions with
OtherOptions =
[|
"--targetprofile:netcore" // without this lib some symbols are not loaded
"--nowarn:3384" // The .NET SDK for this script could not be determined
|]
}

RoslynTestHelpers.SetProjectOptions projInfo.Id sln projectOptions

RoslynTestHelpers.SetEditorOptions
sln
{ CodeFixesOptions.Default with
RemoveParens = true
}

sln, projInfo.Id

let private tryFix (code: string) =
cancellableTask {
let mode =
WithSettings
{ CodeFixesOptions.Default with
RemoveParens = true
}
let document =
let docInfo = RoslynTestHelpers.CreateDocumentInfo projId "C:\\test.fs" code
(sln.AddDocument docInfo).GetDocument docInfo.Id

let document = Document.create mode code
let sourceText = SourceText.From code

let! diagnostics = FSharpDocumentDiagnosticAnalyzer.GetDiagnostics(document, DiagnosticsType.Syntax)
Expand All @@ -34,7 +63,6 @@ module private TopLevel =
|> ValueOption.either (fixer :> IFSharpCodeFixProvider).GetCodeFixIfAppliesAsync (CancellableTask.singleton ValueNone)
|> CancellableTask.map (ValueOption.map (TestCodeFix.ofFSharpCodeFix sourceText) >> ValueOption.toOption)
}
|> CancellableTask.startWithoutCancellation

let expectFix = expectFix tryFix

Expand Down

0 comments on commit d1773e7

Please sign in to comment.