|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.Build.BackEnd.Logging; |
| 9 | +using Microsoft.Build.Framework; |
| 10 | +using Microsoft.Build.Logging; |
| 11 | +using Shouldly; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Microsoft.Build.UnitTests; |
| 15 | + |
| 16 | +public class AssemblyConflictLogger_Tests |
| 17 | +{ |
| 18 | + [Fact] |
| 19 | + public void ConsoleLoggerLocalizesStructuredConflictMessages() |
| 20 | + { |
| 21 | + (AssemblyConflictDependencyDetailsMessageEventArgs details, AssemblyConflictWarningEventArgs warning) = CreateEvents(); |
| 22 | + var output = new StringBuilder(); |
| 23 | + var eventSource = new EventSourceSink(); |
| 24 | + var logger = new ConsoleLogger(LoggerVerbosity.Diagnostic, message => output.Append(message), null, null); |
| 25 | + logger.Initialize(eventSource); |
| 26 | + |
| 27 | + WithFrenchUICulture(() => |
| 28 | + { |
| 29 | + eventSource.Consume(details); |
| 30 | + AssertFrenchDetailsOutput(output.ToString(), details); |
| 31 | + output.Clear(); |
| 32 | + eventSource.Consume(warning); |
| 33 | + }); |
| 34 | + |
| 35 | + AssertFrenchWarningOutput(output.ToString(), warning); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void TerminalLoggerLocalizesStructuredConflictMessages() |
| 40 | + { |
| 41 | + (AssemblyConflictDependencyDetailsMessageEventArgs details, AssemblyConflictWarningEventArgs warning) = CreateEvents(); |
| 42 | + using var output = new StringWriter(); |
| 43 | + using var terminal = new Terminal(output); |
| 44 | + var eventSource = new EventSourceSink(); |
| 45 | + var logger = new TerminalLogger(terminal) |
| 46 | + { |
| 47 | + Verbosity = LoggerVerbosity.Diagnostic, |
| 48 | + }; |
| 49 | + logger.Initialize(eventSource, nodeCount: 1); |
| 50 | + |
| 51 | + WithFrenchUICulture(() => |
| 52 | + { |
| 53 | + eventSource.Consume(details); |
| 54 | + eventSource.Consume(warning); |
| 55 | + }); |
| 56 | + |
| 57 | + AssertFrenchWarningOutput(output.ToString(), warning); |
| 58 | + details.IsMessageMaterialized.ShouldBeFalse(); |
| 59 | + } |
| 60 | + |
| 61 | + private static void AssertFrenchDetailsOutput( |
| 62 | + string output, |
| 63 | + AssemblyConflictDependencyDetailsMessageEventArgs details) |
| 64 | + { |
| 65 | + output.ShouldContain("Références qui dépendent de"); |
| 66 | + output.ShouldContain("Item Include d'un fichier projet"); |
| 67 | + output.ShouldNotContain("References which depend on"); |
| 68 | + output.ShouldNotContain("Project file item includes"); |
| 69 | + details.IsMessageMaterialized.ShouldBeFalse(); |
| 70 | + } |
| 71 | + |
| 72 | + private static void AssertFrenchWarningOutput( |
| 73 | + string output, |
| 74 | + AssemblyConflictWarningEventArgs warning) |
| 75 | + { |
| 76 | + output.ShouldContain("Conflit existant entre"); |
| 77 | + output.ShouldContain("détection de conflits non résolus"); |
| 78 | + output.ShouldContain("Références qui dépendent de"); |
| 79 | + output.ShouldContain("Item Include d'un fichier projet"); |
| 80 | + output.ShouldNotContain("There was a conflict between"); |
| 81 | + output.ShouldNotContain("Found conflicts between different versions"); |
| 82 | + warning.IsMessageMaterialized.ShouldBeFalse(); |
| 83 | + } |
| 84 | + |
| 85 | + private static void WithFrenchUICulture(Action action) |
| 86 | + { |
| 87 | + CultureInfo originalUICulture = CultureInfo.CurrentUICulture; |
| 88 | + try |
| 89 | + { |
| 90 | + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR"); |
| 91 | + action(); |
| 92 | + } |
| 93 | + finally |
| 94 | + { |
| 95 | + CultureInfo.CurrentUICulture = originalUICulture; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + internal static (AssemblyConflictDependencyDetailsMessageEventArgs, AssemblyConflictWarningEventArgs) CreateEvents() |
| 100 | + { |
| 101 | + var victor = new AssemblyConflictReferenceDetails( |
| 102 | + "D, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null", |
| 103 | + @"C:\libs\v2\D.dll", |
| 104 | + isPrimary: true, |
| 105 | + isResolved: true, |
| 106 | + unresolvedPrimaryItemSpec: null, |
| 107 | + primarySourceItemSpecs: ["D"], |
| 108 | + dependees: []); |
| 109 | + |
| 110 | + var victim = new AssemblyConflictReferenceDetails( |
| 111 | + "D, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", |
| 112 | + @"C:\libs\v1\D.dll", |
| 113 | + isPrimary: false, |
| 114 | + isResolved: true, |
| 115 | + unresolvedPrimaryItemSpec: null, |
| 116 | + primarySourceItemSpecs: [], |
| 117 | + dependees: [new AssemblyConflictDependee(@"C:\libs\B.dll", ["B"])]); |
| 118 | + |
| 119 | + var details = new AssemblyConflictDependencyDetailsMessageEventArgs( |
| 120 | + victor, |
| 121 | + victim, |
| 122 | + "ResolveAssemblyReference", |
| 123 | + MessageImportance.Low, |
| 124 | + DateTime.UtcNow); |
| 125 | + |
| 126 | + var warning = new AssemblyConflictWarningEventArgs( |
| 127 | + "D", |
| 128 | + AssemblyConflictLossReason.WasNotPrimary, |
| 129 | + victor, |
| 130 | + victim, |
| 131 | + "MSB3277", |
| 132 | + file: null, |
| 133 | + lineNumber: 0, |
| 134 | + columnNumber: 0, |
| 135 | + helpKeyword: "MSBuild.ResolveAssemblyReference.FoundConflicts", |
| 136 | + senderName: "ResolveAssemblyReference", |
| 137 | + eventTimestamp: DateTime.UtcNow); |
| 138 | + |
| 139 | + var context = new BuildEventContext(1, 2, 3, 4); |
| 140 | + details.BuildEventContext = context; |
| 141 | + warning.BuildEventContext = context; |
| 142 | + |
| 143 | + return (details, warning); |
| 144 | + } |
| 145 | +} |
0 commit comments