public static void SgenCommandTest() { /* * The intent of this test is to verify that we have not inadvertently changed the * code output of Microsoft.XmlSerializer.Generator. To do this, we generate * code output using the current Microsoft.XmlSerializer.Generator and compare it * to code output from a previous version of Microsoft.XmlSerializer.Generator. * * There are times when we intentionally update the code output however. If the * change in code output is intentional - and correct - then update the * 'Expected.SerializableAssembly.XmlSerializers.cs' file with the new code output * to use for comparison. * * [dotnet.exe $(OutputPath)dotnet-Microsoft.XmlSerializer.Generator.dll $(OutputPath)SerializableAssembly.dll --force --quiet] */ const string CodeFile = "SerializableAssembly.XmlSerializers.cs"; const string LKGCodeFile = "Expected.SerializableAssembly.XmlSerializers.cs"; var type = Type.GetType("Microsoft.XmlSerializer.Generator.Sgen, dotnet-Microsoft.XmlSerializer.Generator"); MethodInfo md = type.GetMethod("Main", BindingFlags.Static | BindingFlags.Public); string[] args = new string[] { "SerializableAssembly.dll", "--force", "--quiet" }; int n = (int)md.Invoke(null, new object[] { args }); Assert.Equal(0, n); Assert.True(File.Exists(CodeFile), string.Format("Fail to generate {0}.", CodeFile)); // Compare the generated CodeFiles from the LKG with the live built shared framework one. // Not comparing byte per byte as the generated output isn't deterministic. Assert.Equal(LineEndingsHelper.Normalize(File.ReadAllText(LKGCodeFile)).Length, File.ReadAllText(CodeFile).Length); }
public static void AssertEqualLongString(string expected, string actual) { if (!LineEndingsHelper.IsNewLineConsistent) { expected = LineEndingsHelper.Normalize(expected); } if (actual != expected) { string message = "-- Expected -" + Environment.NewLine + expected + Environment.NewLine + "-- Actual -" + Environment.NewLine + actual + Environment.NewLine; throw new NotSupportedException(message); } }
protected void AssertEqual(CodeObject c, string expected) { // Validate all identifiers are valid CodeGenerator.ValidateIdentifiers(c); // Generate code CodeDomProvider provider = GetProvider(); string code = GenerateCode(c, provider); Assert.Equal( CoalesceWhitespace(LineEndingsHelper.Normalize(expected)), CoalesceWhitespace(code)); }
protected void AssertEqualPreserveLineBreaks(CodeObject c, string expected) { // Validate all identifiers are valid CodeGenerator.ValidateIdentifiers(c); // Generate code CodeDomProvider provider = GetProvider(); string code = GenerateCode(c, provider); // Make sure the code matches what we expected Assert.Equal( CoalesceWhitespace(LineEndingsHelper.Normalize(expected), preserveNewLines: true), CoalesceWhitespace(code, preserveNewLines: true)); }
private async Task VerifyAgainstBaselineUsingFile(string filename, string testSourceCode) { string baseline = LineEndingsHelper.Normalize(await File.ReadAllTextAsync(Path.Combine("Baselines", filename)).ConfigureAwait(false)); string[] expectedLines = baseline.Replace("%VERSION%", typeof(LoggerMessageGenerator).Assembly.GetName().Version?.ToString()) .Split(Environment.NewLine); var(d, r) = await RoslynTestUtils.RunGenerator( new LoggerMessageGenerator(), new[] { typeof(ILogger).Assembly, typeof(LoggerMessageAttribute).Assembly }, new[] { testSourceCode }).ConfigureAwait(false); Assert.Empty(d); Assert.Single(r); Assert.True(CompareLines(expectedLines, r[0].SourceText, out string errorMessage), errorMessage); }
public async Task RedactHeaderValueWithHeaderList_ValueIsRedactedBeforeLogging() { // Arrange var sink = new TestSink(); var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); serviceCollection.AddSingleton <ILoggerFactory>(new TestLoggerFactory(sink, enabled: true)); // Act serviceCollection .AddHttpClient("test") .ConfigurePrimaryHttpMessageHandler(() => new TestMessageHandler()) .RedactLoggedHeaders(new[] { "Authorization", "X-Sensitive", }); // Assert var services = serviceCollection.BuildServiceProvider(); var client = services.GetRequiredService <IHttpClientFactory>().CreateClient("test"); var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com"); request.Headers.Authorization = new AuthenticationHeaderValue("fake", "secret value"); request.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true, }; await client.SendAsync(request); var messages = sink.Writes.ToArray(); var message = Assert.Single(messages.Where(m => { return (m.EventId == LoggingScopeHttpMessageHandler.Log.EventIds.RequestHeader && m.LoggerName == "System.Net.Http.HttpClient.test.LogicalHandler"); })); Assert.StartsWith(LineEndingsHelper.Normalize( @"Request Headers: Authorization: * Cache-Control: no-cache "), message.Message); message = Assert.Single(messages.Where(m => { return (m.EventId == LoggingHttpMessageHandler.Log.EventIds.RequestHeader && m.LoggerName == "System.Net.Http.HttpClient.test.ClientHandler"); })); Assert.StartsWith(LineEndingsHelper.Normalize( @"Request Headers: Authorization: * Cache-Control: no-cache "), message.Message); message = Assert.Single(messages.Where(m => { return (m.EventId == LoggingHttpMessageHandler.Log.EventIds.ResponseHeader && m.LoggerName == "System.Net.Http.HttpClient.test.ClientHandler"); })); Assert.StartsWith(LineEndingsHelper.Normalize( @"Response Headers: X-Sensitive: * Y-Non-Sensitive: innocuous value "), message.Message); message = Assert.Single(messages.Where(m => { return (m.EventId == LoggingScopeHttpMessageHandler.Log.EventIds.ResponseHeader && m.LoggerName == "System.Net.Http.HttpClient.test.LogicalHandler"); })); Assert.StartsWith(LineEndingsHelper.Normalize( @"Response Headers: X-Sensitive: * Y-Non-Sensitive: innocuous value "), message.Message); }