Skip to content

Conversation

@jaredpar
Copy link
Member

@jaredpar jaredpar commented Apr 11, 2025

As part of #78024 I needed to change how output was handled by our unit tests. In looking at the test execution code I noticed that it was in need of a bit of a cleanup. The responsibilities of the layers was not clear and this lead to a lot of code duplication as well as subtle behavior differences between .NET Core and Framework. This change does the following:

  • Changes the responsibility of IRuntimeEnvironment to be only about how code is executed. No more emitting code, calling into Assert, etc ... This is just about running code in a specific environment.
  • Moves the responsibility of verifying the output is as expected to CompilationVerifier
  • Moves the responsibility of emitting the code to be CompilationVerifier
  • Enables NRT in several places to better track how null is handled. There is so much implicit null usage in the low test layers that it was making the hard to follow. Decided to just turn on NRT there and work through the problems.

@ghost ghost added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 11, 2025
item = newItem;
empty = false;
Console.Error.WriteLine(""{0} wrote {1}"", Thread.CurrentThread.Name, newItem);
Console.WriteLine(""{0} wrote {1}"", Thread.CurrentThread.Name, newItem);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test verification now verifies output and error output. This test was using error output for a reason that I cannot determine, no other test does this.

=> this with { ILVerifyMessage = message };
}

#nullable disable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is just 100% turning on NRT and chasing down the errors. There is so much implicit null usage in this layer and it was making it hard to understand what was / wasn't allowed in CompilationVerifier. Decided to take the time to get null clean here.

}
}

internal static class RuntimeEnvironmentUtilities
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods here were just moved to CompilationVerifier. They were all about emitting Compilation and references into ModuleData or dumping them to disk. That doesn't belong in the test execution harness hence moved it to CompilationVerifier which does have responsibility for emitting code.

{
public sealed partial class CompilationVerifier
{
internal readonly struct EmitOutput
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type moved from IRuntimeEnvironment.cs

return output.ToString();
}

public static string DumpAssemblyData(IEnumerable<ModuleData> modules)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from IRuntimeEnvironment.cs

.ToList();
AssertEx.SetEqual(expectedFields, members);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is moved from IRuntimeEnvironment.cs

Comment on lines -33 to -38
private readonly Compilation _compilation;
private CompilationTestData _testData;
private readonly IEnumerable<ModuleData> _dependencies;
private ImmutableArray<Diagnostic> _diagnostics;
private IModuleSymbol _lazyModuleSymbol;
private IList<ModuleData> _allModuleData;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutable fields here were only valid after consumers had called Emit. There was inconsistent validation in the type about checking for whether emit had occurred before accessing these values. The structure also made it very hard to enable NRT in this file. Moved all of those fields into a new type, EmitData and centralized the validation in GetEmitData.

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest just looking at the after state of this file vs. the diff.

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest looking at the after state of this file vs. the diff.

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is just turn on NRT and follow the diagnostics.

@jaredpar jaredpar marked this pull request as ready for review April 11, 2025 17:21
@jaredpar jaredpar requested review from a team as code owners April 11, 2025 17:21
@jaredpar
Copy link
Member Author

@dotnet/roslyn-compiler PTAL

@jaredpar jaredpar removed the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 11, 2025
jaredpar and others added 3 commits April 16, 2025 11:35
Co-authored-by: Jan Jones <jan.jones.cz@gmail.com>
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Roslyn.Test.Utilities
Imports Basic.Reference.Assemblies
Imports Microsoft.CodeAnalysis.Test.Utilities
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider sorting the usings

@arunchndr arunchndr merged commit 8968ec5 into dotnet:main Apr 18, 2025
28 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 18, 2025
@RikkiGibson RikkiGibson modified the milestones: Next, 18.0 P1 Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants