Skip to content

Commit 801790c

Browse files
committed
Reconcile AcceptanceTestAssembly and friends with v3
1 parent f244cc8 commit 801790c

6 files changed

Lines changed: 59 additions & 36 deletions

test/test.utility/Compilation/AcceptanceTestAssembly.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Reflection;
7+
using System.Threading.Tasks;
78

89
public abstract class AcceptanceTestAssembly : IDisposable
910
{
10-
protected AcceptanceTestAssembly(string basePath)
11+
protected static readonly Task CompletedTask = Task.FromResult(0);
12+
13+
protected AcceptanceTestAssembly(string basePath = null)
1114
{
12-
BasePath = basePath ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetLocalCodeBase());
13-
FileName = Path.Combine(BasePath, Path.GetRandomFileName() + ".dll");
15+
BasePath = basePath ?? Path.GetDirectoryName(typeof(AcceptanceTestAssembly).Assembly.GetLocalCodeBase())!;
16+
FileName = Path.Combine(BasePath, Path.GetRandomFileName() + AssemblyFileExtension);
1417
PdbName = Path.Combine(BasePath, Path.GetFileNameWithoutExtension(FileName) + ".pdb");
1518

1619
AssemblyName = new AssemblyName()
@@ -20,6 +23,8 @@ protected AcceptanceTestAssembly(string basePath)
2023
};
2124
}
2225

26+
protected virtual string AssemblyFileExtension => ".dll";
27+
2328
public AssemblyName AssemblyName { get; protected set; }
2429

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

3136
public virtual void Dispose()
3237
{
33-
if (File.Exists(FileName))
34-
File.Delete(FileName);
38+
try
39+
{
40+
if (File.Exists(FileName))
41+
File.Delete(FileName);
42+
}
43+
catch { }
3544

36-
if (File.Exists(PdbName))
37-
File.Delete(PdbName);
45+
try
46+
{
47+
if (File.Exists(PdbName))
48+
File.Delete(PdbName);
49+
}
50+
catch { }
3851
}
3952

40-
protected abstract void Compile(string code, string[] references);
53+
protected abstract Task Compile(string[] code, params string[] references);
4154

4255
protected virtual IEnumerable<string> GetStandardReferences()
4356
=> new[] {

test/test.utility/Compilation/CSharpAcceptanceTestAssembly.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
using System.CodeDom.Compiler;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Threading.Tasks;
78
using Microsoft.CSharp;
89

910
public abstract class CSharpAcceptanceTestAssembly : AcceptanceTestAssembly
1011
{
11-
protected CSharpAcceptanceTestAssembly(string basePath)
12-
: base(basePath) { }
12+
protected CSharpAcceptanceTestAssembly(string basePath = null) :
13+
base(basePath)
14+
{ }
1315

14-
protected override void Compile(string code, string[] references)
16+
protected override Task Compile(string[] code, params string[] references)
1517
{
1618
var parameters = new CompilerParameters()
1719
{
@@ -20,9 +22,11 @@ protected override void Compile(string code, string[] references)
2022
};
2123

2224
parameters.ReferencedAssemblies.AddRange(
23-
GetStandardReferences().Concat(references ?? new string[0])
24-
.Select(ResolveReference)
25-
.ToArray());
25+
GetStandardReferences()
26+
.Concat(references ?? new string[0])
27+
.Select(ResolveReference)
28+
.ToArray()
29+
);
2630

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

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

3842
throw new InvalidOperationException($"Compilation Failed:{Environment.NewLine}{string.Join(Environment.NewLine, errors.ToArray())}");
3943
}
44+
45+
return CompletedTask;
4046
}
4147
}
4248

test/test.utility/Compilation/CSharpAcceptanceTestV1Assembly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static CSharpAcceptanceTestV1Assembly Create(string code, params string[]
1818
{
1919
var basePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
2020
var assembly = new CSharpAcceptanceTestV1Assembly(basePath);
21-
assembly.Compile(code, references);
21+
assembly.Compile(new[] { code }, references);
2222
return assembly;
2323
}
2424
}

test/test.utility/Compilation/CSharpAcceptanceTestV2Assembly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static CSharpAcceptanceTestV2Assembly Create(string code, params string[]
2020
{
2121
var basePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
2222
var assembly = new CSharpAcceptanceTestV2Assembly(basePath);
23-
assembly.Compile(code, references);
23+
assembly.Compile(new[] { code }, references);
2424
return assembly;
2525
}
2626
}

test/test.utility/Compilation/FSharpAcceptanceTestAssembly.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Threading.Tasks;
78
using Microsoft.FSharp.Compiler.SimpleSourceCodeServices;
89
using Microsoft.FSharp.Core;
910

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

26-
protected override void Compile(string code, string[] references)
27+
protected override Task Compile(string[] code, params string[] references)
2728
{
28-
var sourcePath = Path.GetTempFileName() + ".fs";
29-
File.WriteAllText(sourcePath, code);
30-
31-
var compilerArgs =
32-
new[] {
33-
"fsc",
34-
"--noframework",
35-
sourcePath,
36-
$"--out:{FileName}",
37-
$"--pdb:{PdbName}",
38-
$"--lib:\"{BasePath}\"",
39-
"--debug",
40-
"--target:library"
41-
}
42-
.Concat(GetStandardReferences().Concat(references).Select(r => $"--reference:{r}"))
43-
.ToArray();
29+
var compilerArgs = new List<string> { "fsc", "--noframework" };
30+
31+
foreach (var codeText in code)
32+
{
33+
var sourcePath = Path.GetTempFileName() + ".fs";
34+
File.WriteAllText(sourcePath, codeText);
35+
compilerArgs.Add(sourcePath);
36+
}
37+
38+
compilerArgs.AddRange(new[] {
39+
$"--out:{FileName}",
40+
$"--pdb:{PdbName}",
41+
$"--lib:\"{BasePath}\"",
42+
"--debug",
43+
"--target:library"
44+
});
45+
compilerArgs.AddRange(GetStandardReferences().Concat(references).Select(r => $"--reference:{r}"));
4446

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

5254
throw new InvalidOperationException($"Compilation Failed:{Environment.NewLine}{string.Join(Environment.NewLine, errors)}");
5355
}
56+
57+
return CompletedTask;
5458
}
5559
}
5660

test/test.utility/Compilation/FSharpAcceptanceTestV2Assembly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static FSharpAcceptanceTestV2Assembly Create(string code, params string[]
2020
{
2121
var basePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
2222
var assembly = new FSharpAcceptanceTestV2Assembly(basePath);
23-
assembly.Compile(code, references);
23+
assembly.Compile(new[] { code }, references);
2424
return assembly;
2525
}
2626
}

0 commit comments

Comments
 (0)