-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Rebuild compilations that use top-level statements #51845
Changes from 1 commit
e093816
c44eb97
a58b666
26e7d3e
8ab23fe
601e582
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System; | ||
using System.Reflection.PortableExecutable; | ||
using BuildValidator; | ||
using Castle.Core.Logging; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Test.Utilities; | ||
using Microsoft.CodeAnalysis.Emit; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using System.IO; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis.VisualBasic; | ||
using System.Text; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are all of these usings needed? #Closed |
||
|
||
namespace Microsoft.CodeAnalysis.Rebuild.UnitTests | ||
{ | ||
public class CSharpRebuildTests : CSharpTestBase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the new class here vs. adding onto the existing test case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I felt like "round tripping various compilation options" was pretty well delineated as a category of tests from "using various C# language features" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What type of test do you imagine won't involve a round trip at the end though? That in many ways is the ultimate test of the code here 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They all will test that round tripping works. But some tests are primarily about passing "unusual" compilation options while other tests are primarily about using "unusual" language features. That said, if you want me to put this in the existing file, please let me know and I will. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll let you decide. |
||
{ | ||
[Fact] | ||
public void TopLevelStatements() | ||
{ | ||
const string path = "test"; | ||
|
||
var original = CreateCompilation( | ||
@"System.Console.WriteLine(""I'm using top-level statements!"");", | ||
options: TestOptions.DebugExe); | ||
|
||
original.VerifyDiagnostics(); | ||
|
||
var originalBytes = original.EmitToArray(new EmitOptions(debugInformationFormat: DebugInformationFormat.Embedded)); | ||
var peReader = new PEReader(originalBytes); | ||
Assert.True(peReader.TryOpenAssociatedPortablePdb(path, path => null, out var provider, out _)); | ||
var pdbReader = provider!.GetMetadataReader(); | ||
|
||
var factory = LoggerFactory.Create(configure => { }); | ||
var logger = factory.CreateLogger(path); | ||
var bc = new BuildConstructor(logger); | ||
|
||
var optionsReader = new CompilationOptionsReader(logger, pdbReader, peReader); | ||
|
||
var sources = original.SyntaxTrees.Select(st => | ||
{ | ||
var text = st.GetText(); | ||
return new ResolvedSource(OnDiskPath: null, text, new SourceFileInfo(path, text.ChecksumAlgorithm, text.GetChecksum().ToArray(), text, embeddedCompressedHash: null)); | ||
}).ToImmutableArray(); | ||
var references = original.References.ToImmutableArray(); | ||
var (compilation, isError) = bc.CreateCompilation(optionsReader, path, sources, references); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this same pattern used in another test change? If so, could this be extracted to a helper method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will add helpers in the near future after we get byte-for-byte equality working in unit tests. |
||
|
||
Assert.False(isError); | ||
compilation.VerifyEmitDiagnostics(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason you didn't make this an XML doc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this to an inline comment for now. I am fine with it being either inline or XML doc, but it seems like it would be more appropriate on the "root"
GetMethodMainInfo
if it were moved to XML doc.It's not currently clear to me that CompilationOptionsReader is going to be part of the public API in the long term. For example, perhaps we will go back to accepting the PEReader+MetadataReader combo directly out of the feeling that we don't need to provide the individual facilities of the CompilationOptionsReader to library consumers.