Skip to content

Commit

Permalink
Reconcile AcceptanceTestAssembly and friends with v3
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Nov 1, 2023
1 parent f244cc8 commit 801790c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 36 deletions.
29 changes: 21 additions & 8 deletions test/test.utility/Compilation/AcceptanceTestAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

public abstract class AcceptanceTestAssembly : IDisposable
{
protected AcceptanceTestAssembly(string basePath)
protected static readonly Task CompletedTask = Task.FromResult(0);

protected AcceptanceTestAssembly(string basePath = null)
{
BasePath = basePath ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetLocalCodeBase());
FileName = Path.Combine(BasePath, Path.GetRandomFileName() + ".dll");
BasePath = basePath ?? Path.GetDirectoryName(typeof(AcceptanceTestAssembly).Assembly.GetLocalCodeBase())!;
FileName = Path.Combine(BasePath, Path.GetRandomFileName() + AssemblyFileExtension);
PdbName = Path.Combine(BasePath, Path.GetFileNameWithoutExtension(FileName) + ".pdb");

AssemblyName = new AssemblyName()
Expand All @@ -20,6 +23,8 @@ protected AcceptanceTestAssembly(string basePath)
};
}

protected virtual string AssemblyFileExtension => ".dll";

public AssemblyName AssemblyName { get; protected set; }

public string BasePath { get; }
Expand All @@ -30,14 +35,22 @@ protected AcceptanceTestAssembly(string basePath)

public virtual void Dispose()
{
if (File.Exists(FileName))
File.Delete(FileName);
try
{
if (File.Exists(FileName))
File.Delete(FileName);
}
catch { }

if (File.Exists(PdbName))
File.Delete(PdbName);
try
{
if (File.Exists(PdbName))
File.Delete(PdbName);
}
catch { }
}

protected abstract void Compile(string code, string[] references);
protected abstract Task Compile(string[] code, params string[] references);

protected virtual IEnumerable<string> GetStandardReferences()
=> new[] {
Expand Down
20 changes: 13 additions & 7 deletions test/test.utility/Compilation/CSharpAcceptanceTestAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CSharp;

public abstract class CSharpAcceptanceTestAssembly : AcceptanceTestAssembly
{
protected CSharpAcceptanceTestAssembly(string basePath)
: base(basePath) { }
protected CSharpAcceptanceTestAssembly(string basePath = null) :
base(basePath)
{ }

protected override void Compile(string code, string[] references)
protected override Task Compile(string[] code, params string[] references)
{
var parameters = new CompilerParameters()
{
Expand All @@ -20,9 +22,11 @@ protected override void Compile(string code, string[] references)
};

parameters.ReferencedAssemblies.AddRange(
GetStandardReferences().Concat(references ?? new string[0])
.Select(ResolveReference)
.ToArray());
GetStandardReferences()
.Concat(references ?? new string[0])
.Select(ResolveReference)
.ToArray()
);

var compilerOptions = new Dictionary<string, string> { { "CompilerVersion", "v4.0" } };
var provider = new CSharpCodeProvider(compilerOptions);
Expand All @@ -32,11 +36,13 @@ protected override void Compile(string code, string[] references)
{
var errors = new List<string>();

foreach (CompilerError error in results.Errors)
foreach (var error in results.Errors.Cast<CompilerError>().Where(x => x != null))
errors.Add($"{error.FileName}({error.Line},{error.Column}): error {error.ErrorNumber}: {error.ErrorText}");

throw new InvalidOperationException($"Compilation Failed:{Environment.NewLine}{string.Join(Environment.NewLine, errors.ToArray())}");
}

return CompletedTask;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static CSharpAcceptanceTestV1Assembly Create(string code, params string[]
{
var basePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
var assembly = new CSharpAcceptanceTestV1Assembly(basePath);
assembly.Compile(code, references);
assembly.Compile(new[] { code }, references);
return assembly;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static CSharpAcceptanceTestV2Assembly Create(string code, params string[]
{
var basePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
var assembly = new CSharpAcceptanceTestV2Assembly(basePath);
assembly.Compile(code, references);
assembly.Compile(new[] { code }, references);
return assembly;
}
}
Expand Down
40 changes: 22 additions & 18 deletions test/test.utility/Compilation/FSharpAcceptanceTestAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.FSharp.Compiler.SimpleSourceCodeServices;
using Microsoft.FSharp.Core;

Expand All @@ -23,34 +24,37 @@ protected override IEnumerable<string> GetStandardReferences()
return new[] { mscorlib, sysRuntime, "xunit.abstractions.dll" };
}

protected override void Compile(string code, string[] references)
protected override Task Compile(string[] code, params string[] references)
{
var sourcePath = Path.GetTempFileName() + ".fs";
File.WriteAllText(sourcePath, code);

var compilerArgs =
new[] {
"fsc",
"--noframework",
sourcePath,
$"--out:{FileName}",
$"--pdb:{PdbName}",
$"--lib:\"{BasePath}\"",
"--debug",
"--target:library"
}
.Concat(GetStandardReferences().Concat(references).Select(r => $"--reference:{r}"))
.ToArray();
var compilerArgs = new List<string> { "fsc", "--noframework" };

foreach (var codeText in code)
{
var sourcePath = Path.GetTempFileName() + ".fs";
File.WriteAllText(sourcePath, codeText);
compilerArgs.Add(sourcePath);
}

compilerArgs.AddRange(new[] {
$"--out:{FileName}",
$"--pdb:{PdbName}",
$"--lib:\"{BasePath}\"",
"--debug",
"--target:library"
});
compilerArgs.AddRange(GetStandardReferences().Concat(references).Select(r => $"--reference:{r}"));

var compiler = new SimpleSourceCodeServices(FSharpOption<bool>.Some(false));
var result = compiler.Compile(compilerArgs);
var result = compiler.Compile(compilerArgs.ToArray());
if (result.Item2 != 0)
{
var errors = result.Item1
.Select(e => $"{e.FileName}({e.StartLineAlternate},{e.StartColumn}): {(e.Severity.IsError ? "error" : "warning")} {e.ErrorNumber}: {e.Message}");

throw new InvalidOperationException($"Compilation Failed:{Environment.NewLine}{string.Join(Environment.NewLine, errors)}");
}

return CompletedTask;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static FSharpAcceptanceTestV2Assembly Create(string code, params string[]
{
var basePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
var assembly = new FSharpAcceptanceTestV2Assembly(basePath);
assembly.Compile(code, references);
assembly.Compile(new[] { code }, references);
return assembly;
}
}
Expand Down

0 comments on commit 801790c

Please sign in to comment.