From 8d29ce55b23db5c3035236fe44aadef155020fc1 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Sat, 17 Dec 2022 09:45:27 +0200 Subject: [PATCH 1/6] Run test on Linux and Mac --- .../AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs index 5eaa129e66fe..bed0236b50fa 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs @@ -147,9 +147,7 @@ public static IEnumerable FixedWithAppendAddsUsingDirectiveTestData() }; } - [ConditionalTheory] - [OSSkipCondition(OperatingSystems.Linux)] - [OSSkipCondition(OperatingSystems.MacOSX)] + [Theory] [MemberData(nameof(FixedWithAppendAddsUsingDirectiveTestData))] public async Task IHeaderDictionary_WithAdd_FixedWithAppend_AddsUsingDirective(string source, string fixedSource) { From a3522ef8cedd3f06f802a406a7d27235ad08bf77 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Sat, 14 Jan 2023 10:04:21 +0200 Subject: [PATCH 2/6] Use `ReplaceLineEndings` --- .../AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs index bed0236b50fa..0c79673f3921 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs @@ -153,14 +153,14 @@ public async Task IHeaderDictionary_WithAdd_FixedWithAppend_AddsUsingDirective(s { // Arrange & Act & Assert await VerifyCS.VerifyCodeFixAsync( - source.TrimStart(), + source.TrimStart().ReplaceLineEndings(), new[] { new DiagnosticResult(DiagnosticDescriptors.DoNotUseIHeaderDictionaryAdd) .WithLocation(0) .WithMessage(Resources.Analyzer_HeaderDictionaryAdd_Message) }, - fixedSource.TrimStart(), + fixedSource.TrimStart().ReplaceLineEndings(), codeActionEquivalenceKey: AppendCodeActionEquivalenceKey); } From 617956ca1df1fdc4384bdbe2e7e0aaf7d8242faf Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 19 Jan 2023 09:31:36 +0200 Subject: [PATCH 3/6] Always use lf --- .../test/Http/HeaderDictionaryAddTest.cs | 4 ++-- .../test/Verifiers/CSharpCodeFixVerifier.cs | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs index 0c79673f3921..bed0236b50fa 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs @@ -153,14 +153,14 @@ public async Task IHeaderDictionary_WithAdd_FixedWithAppend_AddsUsingDirective(s { // Arrange & Act & Assert await VerifyCS.VerifyCodeFixAsync( - source.TrimStart().ReplaceLineEndings(), + source.TrimStart(), new[] { new DiagnosticResult(DiagnosticDescriptors.DoNotUseIHeaderDictionaryAdd) .WithLocation(0) .WithMessage(Resources.Analyzer_HeaderDictionaryAdd_Message) }, - fixedSource.TrimStart().ReplaceLineEndings(), + fixedSource.TrimStart(), codeActionEquivalenceKey: AppendCodeActionEquivalenceKey); } diff --git a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs index 124b40083885..aa69f720a6c5 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs @@ -59,8 +59,16 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] ex // We need to set the output type to an exe to properly // support top-level programs in the tests. Otherwise, // the test infra will assume we are trying to build a library. - TestState = { Sources = { source }, OutputKind = OutputKind.ConsoleApplication }, - FixedState = { Sources = { fixedSource } }, + TestState = + { + Sources = { source.ReplaceLineEndings("\n") }, + OutputKind = OutputKind.ConsoleApplication, + AnalyzerConfigFiles = + { + ("/.editorconfig", "end_of_line = lf"), + }, + }, + FixedState = { Sources = { fixedSource.ReplaceLineEndings("\n") } }, ReferenceAssemblies = CSharpAnalyzerVerifier.GetReferenceAssemblies(), NumberOfFixAllIterations = expectedIterations, CodeActionEquivalenceKey = codeActionEquivalenceKey From 7eb3c1af55de115f6673b89603514502984ec517 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 19 Jan 2023 11:28:18 +0200 Subject: [PATCH 4/6] Different approach --- .../test/Verifiers/CSharpCodeFixVerifier.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs index aa69f720a6c5..dd592519f201 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs @@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis.Testing.Verifiers; @@ -63,15 +64,15 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] ex { Sources = { source.ReplaceLineEndings("\n") }, OutputKind = OutputKind.ConsoleApplication, - AnalyzerConfigFiles = - { - ("/.editorconfig", "end_of_line = lf"), - }, }, FixedState = { Sources = { fixedSource.ReplaceLineEndings("\n") } }, ReferenceAssemblies = CSharpAnalyzerVerifier.GetReferenceAssemblies(), NumberOfFixAllIterations = expectedIterations, - CodeActionEquivalenceKey = codeActionEquivalenceKey + CodeActionEquivalenceKey = codeActionEquivalenceKey, + OptionsTransforms = + { + optionSet => optionSet.WithChangedOption(new CodeAnalysis.Options.OptionKey(FormattingOptions.NewLine, LanguageNames.CSharp), "\n"), + }, }; if (usageSource != null) From b0c873a44413eaca23f9178074f9bd25bce1b5ea Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 19 Jan 2023 13:32:58 +0200 Subject: [PATCH 5/6] Revert --- .../test/Http/HeaderDictionaryAddTest.cs | 4 ++-- .../test/Verifiers/CSharpCodeFixVerifier.cs | 15 +++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs index bed0236b50fa..0c79673f3921 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs @@ -153,14 +153,14 @@ public async Task IHeaderDictionary_WithAdd_FixedWithAppend_AddsUsingDirective(s { // Arrange & Act & Assert await VerifyCS.VerifyCodeFixAsync( - source.TrimStart(), + source.TrimStart().ReplaceLineEndings(), new[] { new DiagnosticResult(DiagnosticDescriptors.DoNotUseIHeaderDictionaryAdd) .WithLocation(0) .WithMessage(Resources.Analyzer_HeaderDictionaryAdd_Message) }, - fixedSource.TrimStart(), + fixedSource.TrimStart().ReplaceLineEndings(), codeActionEquivalenceKey: AppendCodeActionEquivalenceKey); } diff --git a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs index dd592519f201..124b40083885 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpCodeFixVerifier.cs @@ -5,7 +5,6 @@ using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis.Testing.Verifiers; @@ -60,19 +59,11 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] ex // We need to set the output type to an exe to properly // support top-level programs in the tests. Otherwise, // the test infra will assume we are trying to build a library. - TestState = - { - Sources = { source.ReplaceLineEndings("\n") }, - OutputKind = OutputKind.ConsoleApplication, - }, - FixedState = { Sources = { fixedSource.ReplaceLineEndings("\n") } }, + TestState = { Sources = { source }, OutputKind = OutputKind.ConsoleApplication }, + FixedState = { Sources = { fixedSource } }, ReferenceAssemblies = CSharpAnalyzerVerifier.GetReferenceAssemblies(), NumberOfFixAllIterations = expectedIterations, - CodeActionEquivalenceKey = codeActionEquivalenceKey, - OptionsTransforms = - { - optionSet => optionSet.WithChangedOption(new CodeAnalysis.Options.OptionKey(FormattingOptions.NewLine, LanguageNames.CSharp), "\n"), - }, + CodeActionEquivalenceKey = codeActionEquivalenceKey }; if (usageSource != null) From 0cc8bec0670199e2d108897820baafccc117066b Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Thu, 19 Jan 2023 23:00:09 +0200 Subject: [PATCH 6/6] Doc --- .../AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs index 0c79673f3921..13684257b8b8 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs @@ -151,6 +151,11 @@ public static IEnumerable FixedWithAppendAddsUsingDirectiveTestData() [MemberData(nameof(FixedWithAppendAddsUsingDirectiveTestData))] public async Task IHeaderDictionary_WithAdd_FixedWithAppend_AddsUsingDirective(string source, string fixedSource) { + // Source is cloned on Windows with CRLF line endings, then the test is run by Helix in Windows/Linux/macOS. + // When Roslyn adds a new `using`, it gets added followed by Environment.NewLine. + // For Linux/macOS, the actual result is `\n`, however, the source is cloned on Windows with CRLF expectation. + // We replace all line endings with Environment.NewLine to avoid this. + // Arrange & Act & Assert await VerifyCS.VerifyCodeFixAsync( source.TrimStart().ReplaceLineEndings(),