-
Notifications
You must be signed in to change notification settings - Fork 1.8k
AVRO-3446: Add avrogen unit tests #1595
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
Merged
RyanSkraba
merged 13 commits into
apache:master
from
zcsizmadia:avro-3446-add-avrogen-unit-tests
Mar 18, 2022
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5b0b7b0
Add avrogen unit tests
d3ceb1a
Hide dynamic compiler errors
9a34b18
Add asserts
a5d0bb6
Bump MSBUild package versions
0dc3033
Tweak dotnet 6.0 install
26b68f0
Add comments
192b863
Use 6.0.2xx channel
ad03776
Revert dotnet-install change
009a53a
Add codegen tests
0a8b12a
Add test for contextual keyword in namespace
8bccae8
Add AvroGenHelper
838868e
Update lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
zcsizmadia 5dcc633
Update lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
zcsizmadia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Text; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.Emit; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Avro.Test.AvroGen | ||
| { | ||
| class AvroGenToolResult | ||
| { | ||
| public int ExitCode { get; set; } | ||
| public string[] StdOut { get; set; } | ||
| public string[] StdErr { get; set; } | ||
| } | ||
|
|
||
| class AvroGenHelper | ||
| { | ||
| public static AvroGenToolResult RunAvroGenTool(params string[] args) | ||
| { | ||
| // Save stdout and stderr | ||
| TextWriter conOut = Console.Out; | ||
| TextWriter conErr = Console.Error; | ||
|
|
||
| try | ||
| { | ||
| AvroGenToolResult result = new AvroGenToolResult(); | ||
| StringBuilder strBuilderOut = new StringBuilder(); | ||
| StringBuilder strBuilderErr = new StringBuilder(); | ||
|
|
||
| using (StringWriter writerOut = new StringWriter(strBuilderOut)) | ||
| using (StringWriter writerErr = new StringWriter(strBuilderErr)) | ||
| { | ||
| writerOut.NewLine = "\n"; | ||
| writerErr.NewLine = "\n"; | ||
|
|
||
| // Overwrite stdout and stderr to be able to capture console output | ||
| Console.SetOut(writerOut); | ||
| Console.SetError(writerErr); | ||
|
|
||
| result.ExitCode = AvroGenTool.Main(args.ToArray()); | ||
|
|
||
| writerOut.Flush(); | ||
| writerErr.Flush(); | ||
|
|
||
| result.StdOut = strBuilderOut.Length == 0 ? Array.Empty<string>() : strBuilderOut.ToString().Split(writerOut.NewLine); | ||
| result.StdErr = strBuilderErr.Length == 0 ? Array.Empty<string>() : strBuilderErr.ToString().Split(writerErr.NewLine); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| finally | ||
| { | ||
| // Restore console | ||
| Console.SetOut(conOut); | ||
| Console.SetError(conErr); | ||
| } | ||
| } | ||
|
|
||
| public static Assembly CompileCSharpFilesIntoLibrary(IEnumerable<string> sourceFiles, string assemblyName = null, bool loadAssembly = true) | ||
| { | ||
| // CReate random assenbly name if not specified | ||
|
zcsizmadia marked this conversation as resolved.
Outdated
|
||
| if (assemblyName == null) | ||
| assemblyName = Path.GetRandomFileName(); | ||
|
|
||
| // Base path to assemblies .NET assemblies | ||
| var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); | ||
|
|
||
| using (var compilerStream = new MemoryStream()) | ||
| { | ||
| List<string> assemblies = new List<string>() | ||
| { | ||
| typeof(object).Assembly.Location, | ||
| typeof(Schema).Assembly.Location, | ||
| Path.Combine(assemblyPath, "System.Runtime.dll"), | ||
| Path.Combine(assemblyPath, "netstandard.dll") | ||
| }; | ||
|
|
||
| // Create compiler | ||
| CSharpCompilation compilation = CSharpCompilation | ||
| .Create(assemblyName) | ||
| .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) | ||
| .AddReferences(assemblies.Select(path => MetadataReference.CreateFromFile(path))) | ||
| .AddSyntaxTrees(sourceFiles.Select(sourceFile => | ||
| { | ||
| string sourceText = System.IO.File.ReadAllText(sourceFile); | ||
| return CSharpSyntaxTree.ParseText(sourceText); | ||
| })); | ||
|
|
||
| // Compile | ||
| EmitResult compilationResult = compilation.Emit(compilerStream); | ||
|
|
||
| //Note: Comment the following out to analyze the compiler errors if needed | ||
| //if (!compilationResult.Success) | ||
| //{ | ||
| // foreach (Diagnostic diagnostic in compilationResult.Diagnostics) | ||
| // { | ||
| // if (diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) | ||
| // { | ||
| // TestContext.WriteLine($"{diagnostic.Id} - {diagnostic.GetMessage()} - {diagnostic.Location}"); | ||
| // } | ||
| // } | ||
| //} | ||
|
|
||
| Assert.That(compilationResult.Success, Is.True); | ||
|
|
||
| if (!loadAssembly) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| compilerStream.Seek(0, SeekOrigin.Begin); | ||
| return Assembly.Load(compilerStream.ToArray()); | ||
| } | ||
| } | ||
|
|
||
| public static string CreateEmptyTemporyFolder(out string uniqueId, string path = null) | ||
| { | ||
| // Create unique id | ||
| uniqueId = Guid.NewGuid().ToString(); | ||
|
|
||
| // Temporary folder name in working folder or the specified path | ||
| string tempFolder = Path.Combine(path ?? TestContext.CurrentContext.WorkDirectory, uniqueId); | ||
|
|
||
| // Create folder | ||
| Directory.CreateDirectory(tempFolder); | ||
|
|
||
| // Make sure it is empty | ||
| Assert.That(new DirectoryInfo(tempFolder), Is.Empty); | ||
|
|
||
| return tempFolder; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.