Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rebuild.UnitTests #51778

Merged
merged 17 commits into from
Mar 12, 2021
Merged

Add Rebuild.UnitTests #51778

merged 17 commits into from
Mar 12, 2021

Conversation

RikkiGibson
Copy link
Contributor

@RikkiGibson RikkiGibson commented Mar 10, 2021

  • Adds Rebuild.UnitTests project
  • Adds Platform round trip tests. Currently cases X86 and AnyCpu32BitPreferred are failing. (edit: now are all passing.)

I can't quite figure out how to implement GetPlatform to make all these values round trip. In particular struggling to figure out how to distinguish the AnyCpu case from the X86 case. It seems like when Platform.AnyCpu is provided as an input, we translate it to Machine.Unknown:

case Platform.AnyCpu:
case Platform.AnyCpu32BitPreferred:
machine = Machine.Unknown;

However, when I try to read the Machine value back out of the PE later on, it ends up being Machine.I386... I am really not sure what I'm missing or where the value is getting changed.

@jaredpar for review.

@RikkiGibson
Copy link
Contributor Author

After chatting with @jaredpar decided to just add the platform to the PDB compilation options. Let me know if you have objections @tmat or ideas on how to more properly implement this.

Compilers.sln Outdated Show resolved Hide resolved
src/Compilers/Core/Portable/Compilation/Platform.cs Outdated Show resolved Hide resolved
src/Compilers/Core/RebuildTest/CSharpRebuildTests.cs Outdated Show resolved Hide resolved
src/Compilers/Core/RebuildTest/CSharpRebuildTests.cs Outdated Show resolved Hide resolved
src/Compilers/Core/RebuildTest/CSharpRebuildTests.cs Outdated Show resolved Hide resolved
src/Compilers/Core/RebuildTest/CSharpRebuildTests.cs Outdated Show resolved Hide resolved
src/Compilers/Core/RebuildTest/CSharpRebuildTests.cs Outdated Show resolved Hide resolved
var (compilation, isError) = bc.CreateCompilation(optionsReader, "test", sources, references);

Assert.False(isError);
Assert.Equal(platform, compilation!.Options.Platform);
Copy link
Member

Choose a reason for hiding this comment

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

Think we need to take the next step and ensuring the original and rebuilt compilation have the same byte[] output. Consider doing as a helper so future tests can just build on top of that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is a good idea, but there are issues with it to iron out that I'm not sure I want to block on.

Copy link
Member

Choose a reason for hiding this comment

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

Fine on working it out later but let's file an issue to track.

namespace Rebuild.UnitTests
{
//[CompilerTrait(CompilerFeature.)]
public class CSharpRebuildTests : CSharpTestBase
Copy link
Member

Choose a reason for hiding this comment

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

What about VB tests here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be good to have a VB version of this test. I guess in that case we'd want to add a reference to the VB test helpers?

Copy link
Member

Choose a reason for hiding this comment

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

My instinct was to have an abstract base class and then have both a VB and C# derivation. Hoping that it would be easy to abstract over the languages this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to introduce a few more test cases and then try to figure out what test helpers would be most useful.

@RikkiGibson
Copy link
Contributor Author

@jaredpar have responded to the feedback.

@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Roslyn.Utilities;
Copy link
Member

Choose a reason for hiding this comment

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

Roslyn.Utilities [](start = 6, length = 16)

Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, thanks, have removed.

@@ -176,6 +177,11 @@ private static (OptimizationLevel, bool) GetOptimizationLevel(string? optimizati
_ => throw new InvalidDataException($"Optimization \"{optimizationLevel}\" level not recognized")
};

private static Platform GetPlatform(string? platform)
=> platform is null
Copy link
Member

@cston cston Mar 11, 2021

Choose a reason for hiding this comment

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

=> [](start = 8, length = 3)

Should this be indented? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

<ProjectReference Include="..\Rebuild\Rebuild.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="$(MoqVersion)" />
Copy link
Member

@cston cston Mar 11, 2021

Choose a reason for hiding this comment

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

[](start = 4, length = 58)

Is this package used? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.


var factory = LoggerFactory.Create(configure => { });
var logger = factory.CreateLogger(path);
// TODO: shouldn't need to pass a logger.
Copy link
Member

@cston cston Mar 11, 2021

Choose a reason for hiding this comment

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

If we need an ILogger instance, could we create a simple implementation of ILogger in this test class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but it feels like users of the public API shouldn't be required to provide a logger, and shouldn't provide it as its own parameter. Maybe a property on an options object that can be specified if the user wants, but not a parameter to public API that they have to deal with.

{
var text = st.GetText();
return new ResolvedSource(OnDiskPath: null, text, new SourceFileInfo(path, text.ChecksumAlgorithm, text.GetChecksum().ToArray(), text, embeddedCompressedHash: null));
}).ToImmutableArray();
Copy link
Member

Choose a reason for hiding this comment

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

ToImmutableArray [](start = 15, length = 16)

Could use SyntaxTrees.SelectAsArray(...) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this project is missing an IVT to MS.CA for this. I will punt on it for now if that's ok.

@RikkiGibson RikkiGibson merged commit 062355a into dotnet:main Mar 12, 2021
@RikkiGibson RikkiGibson deleted the rebuild-unittests branch March 12, 2021 03:53
@@ -888,6 +888,9 @@ private void EmbedCompilationOptions(BlobReader? pdbCompilationOptionsReader, Co
WriteValue(CompilationOptionNames.Optimization, optimizationLevel.ToPdbSerializedString(debugPlusMode));
}

var platform = module.CommonCompilation.Options.Platform;
WriteValue(CompilationOptionNames.Platform, platform.ToString());
Copy link
Member

@tmat tmat Mar 12, 2021

Choose a reason for hiding this comment

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

Platform can't be determined from the CLR/PE header flags of the dll? Seems like it should be possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It may be possible, but I wasn't able to get all the values to round trip. The CoffHeader.Machine, for example, is not enough information. There must be other bits that imply Platform.AnyCpu and Platform.AnyCpu32BitPreferred that I just wasn't able to find.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filed #51941 to make sure we get to this. Thanks

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.

5 participants