From afa855ec31fa88252e4405b5d49ed98373e2044f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jul 2026 23:28:56 +0000 Subject: [PATCH] Add edge-case tests for SharedFileSystemPathInTestAnalyzer (MSTEST0077) - WhenTestMethodOpensFileForWriting_Diagnostic: verifies File.OpenWrite with a constant path fires the diagnostic. The analyzer comments mention OpenWrite as the only unambiguously-writing File.Open* variant but no test covered it. - WhenTestInitializeWritesConstantPath_Diagnostic: verifies the analyzer fires inside a [TestInitialize] fixture method, which runs before each test and can race under method-level parallelization. Fixture-method coverage was absent. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...SharedFileSystemPathInTestAnalyzerTests.cs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/SharedFileSystemPathInTestAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/SharedFileSystemPathInTestAnalyzerTests.cs index 538163f8b5..def0bb2382 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/SharedFileSystemPathInTestAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/SharedFileSystemPathInTestAnalyzerTests.cs @@ -432,4 +432,65 @@ await VerifyCS.VerifyAnalyzerAsync( .WithLocation(0) .WithArguments("C:\\temp\\source.txt")); } + + [TestMethod] + public async Task WhenTestMethodOpensFileForWriting_Diagnostic() + { + // File.OpenWrite is the only File.Open* overload that is unambiguously a mutation (it always opens for + // writing), so it must be flagged - unlike the general File.Open which can request a read-only handle. + string code = """ + using System.IO; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] + + [TestClass] + public class MyTestClass + { + [TestMethod] + public void MyTestMethod() + { + using FileStream stream = {|#0:File.OpenWrite("shared.log")|}; + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(SharedFileSystemPathInTestAnalyzer.Rule) + .WithLocation(0) + .WithArguments("shared.log")); + } + + [TestMethod] + public async Task WhenTestInitializeWritesConstantPath_Diagnostic() + { + // [TestInitialize] is a class-scoped fixture that runs before each test method. Under method-level + // parallelization it can race with other tests, so a constant-path write inside it must be flagged. + string code = """ + using System.IO; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] + + [TestClass] + public class MyTestClass + { + [TestInitialize] + public void Setup() + { + {|#0:File.WriteAllText("setup.txt", "data")|}; + } + + [TestMethod] + public void MyTestMethod() { } + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(SharedFileSystemPathInTestAnalyzer.Rule) + .WithLocation(0) + .WithArguments("setup.txt")); + } }