Skip to content

Commit ef6d73d

Browse files
committed
Tests passing
1 parent c0ed090 commit ef6d73d

15 files changed

+148
-132
lines changed

src/Dotnet.Script.DependencyModel/Compilation/CompilationDependencyResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public IEnumerable<CompilationDependency> GetDependencies(string targetDirectory
6565
foreach (var compilationLibrary in compileLibraries)
6666
{
6767
var resolvedReferencePaths = new HashSet<string>();
68-
_logger.Debug($"Resolving compilation reference paths for {compilationLibrary.Name}");
68+
_logger.Trace($"Resolving compilation reference paths for {compilationLibrary.Name}");
6969
var referencePaths = ResolveReferencePaths(compilationLibrary, compilationAssemblyResolvers);
7070
var scripts =
7171
_scriptFilesDependencyResolver.GetScriptFileDependencies(compilationLibrary.Path, dependencyInfo.NugetPackageFolders);
@@ -94,7 +94,7 @@ private IEnumerable<string> ResolveReferencePaths(CompilationLibrary compilation
9494

9595
foreach (var referencePath in referencePaths)
9696
{
97-
_logger.Debug($"{compilationLibrary.Name} => {referencePath}");
97+
_logger.Trace($"{compilationLibrary.Name} => {referencePath}");
9898
}
9999

100100
return referencePaths;
Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Dotnet.Script.DependencyModel.Logging
45
{
56
public delegate Logger LogFactory(Type type);
67

7-
public delegate void Logger(LogLevel level, string message);
8-
8+
public delegate void Logger(LogLevel level, string message, Exception ex = null);
9+
910
public enum LogLevel
1011
{
1112
Trace,
1213
Debug,
13-
Info
14+
Info,
15+
Warning,
16+
Error,
17+
Critical
1418
}
1519

1620
public static class LogExtensions
@@ -19,6 +23,47 @@ public static class LogExtensions
1923

2024
public static void Trace(this Logger logger, string message) => logger(LogLevel.Trace, message);
2125
public static void Debug(this Logger logger, string message) => logger(LogLevel.Debug, message);
22-
public static void Info(this Logger logger, string message) => logger(LogLevel.Info, message);
26+
public static void Info(this Logger logger, string message) => logger(LogLevel.Info, message);
27+
public static void Warning(this Logger logger, string message) => logger(LogLevel.Warning, message);
28+
public static void Error(this Logger logger, string message, Exception exception = null) => logger(LogLevel.Error, message, exception);
29+
public static void Critical(this Logger logger, string message, Exception exception = null) => logger(LogLevel.Critical, message, exception);
30+
}
31+
32+
public static class LevelMapper
33+
{
34+
private static Dictionary<string, LogLevel> _levelMap = CreateMap();
35+
36+
private static Dictionary<string, LogLevel> CreateMap()
37+
{
38+
var map = new Dictionary<string, LogLevel>(StringComparer.InvariantCultureIgnoreCase);
39+
map.Add("t", LogLevel.Trace);
40+
map.Add("trace", LogLevel.Trace);
41+
map.Add("d", LogLevel.Debug);
42+
map.Add("debug", LogLevel.Debug);
43+
map.Add("i", LogLevel.Info);
44+
map.Add("info", LogLevel.Info);
45+
map.Add("w", LogLevel.Warning);
46+
map.Add("warning", LogLevel.Warning);
47+
map.Add("e", LogLevel.Error);
48+
map.Add("error", LogLevel.Error);
49+
map.Add("c", LogLevel.Critical);
50+
map.Add("critical", LogLevel.Critical);
51+
return map;
52+
}
53+
54+
public static LogLevel FromString(string levelName)
55+
{
56+
if (string.IsNullOrWhiteSpace(levelName))
57+
{
58+
return LogLevel.Warning;
59+
}
60+
61+
if (!_levelMap.TryGetValue(levelName, out var level))
62+
{
63+
throw new InvalidOperationException($"Unknown level name : {levelName}");
64+
}
65+
66+
return level;
67+
}
2368
}
2469
}

src/Dotnet.Script.Shared.Tests/InteractiveRunnerTestsBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ private InteractiveTestContext GetRunner(string[] commands)
4040

4141
var console = new ScriptConsole(writer, reader, error);
4242

43-
var runtimeDependencyResolver = new RuntimeDependencyResolver(TestOutputHelper.TestLogFactory);
43+
var logFactory = TestOutputHelper.CreateTestLogFactory();
44+
var runtimeDependencyResolver = new RuntimeDependencyResolver(logFactory);
4445

45-
var compiler = new ScriptCompiler(TestOutputHelper.TestLogFactory, runtimeDependencyResolver);
46-
var runner = new InteractiveRunner(compiler, TestOutputHelper.TestLogFactory, console, Array.Empty<string>());
46+
var compiler = new ScriptCompiler(logFactory, runtimeDependencyResolver);
47+
var runner = new InteractiveRunner(compiler, logFactory, console, Array.Empty<string>());
4748
return new InteractiveTestContext(console, runner);
4849
}
4950

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using System.Threading;
1+
using System.Threading;
52
using Dotnet.Script.DependencyModel.Logging;
63
using Xunit.Abstractions;
74

85
namespace Dotnet.Script.Shared.Tests
96
{
107
public static class TestOutputHelper
118
{
12-
private static readonly AsyncLocal<ITestOutputHelper> CurrentTestOutputHelper
13-
= new AsyncLocal<ITestOutputHelper>();
14-
15-
public static void Capture(this ITestOutputHelper outputHelper)
9+
private static readonly AsyncLocal<CapturedTestOutput> CurrentCapturedTestOutput
10+
= new AsyncLocal<CapturedTestOutput>();
11+
12+
public static void Capture(this ITestOutputHelper outputHelper, LogLevel minimumLogLevel = LogLevel.Debug)
1613
{
17-
CurrentTestOutputHelper.Value = outputHelper;
14+
CurrentCapturedTestOutput.Value = new CapturedTestOutput(outputHelper, minimumLogLevel);
1815
}
1916

20-
public static ITestOutputHelper Current => CurrentTestOutputHelper.Value;
21-
22-
public static LogFactory TestLogFactory =>
23-
type => (level, message) => Current.WriteLine($"{level} {message}");
17+
public static CapturedTestOutput Current => CurrentCapturedTestOutput.Value;
18+
19+
public static LogFactory CreateTestLogFactory()
20+
{
21+
return type => (level, message, exception) =>
22+
{
23+
#if DEBUG
24+
if (level >= Current.MinimumLogLevel)
25+
{
26+
Current.TestOutputHelper.WriteLine($"{level,-7} {type}");
27+
Current.TestOutputHelper.WriteLine($" {message}");
28+
}
29+
#endif
30+
};
31+
}
2432
}
2533

26-
34+
2735

36+
public class CapturedTestOutput
37+
{
38+
public CapturedTestOutput(ITestOutputHelper testOutputHelper, LogLevel minimumLogLevel)
39+
{
40+
TestOutputHelper = testOutputHelper;
41+
MinimumLogLevel = minimumLogLevel;
42+
}
43+
44+
public ITestOutputHelper TestOutputHelper { get; }
45+
public LogLevel MinimumLogLevel { get; }
46+
}
2847
}

src/Dotnet.Script.Tests/CompilationDependencyResolverTests.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using Dotnet.Script.DependencyModel.Compilation;
22
using Dotnet.Script.DependencyModel.Environment;
3+
using Dotnet.Script.Shared.Tests;
34
using Xunit;
45
using Xunit.Abstractions;
56

67
namespace Dotnet.Script.Tests
78
{
89
[Collection("IntegrationTests")]
910
public class CompilationDependencyResolverTests
10-
{
11-
private readonly ITestOutputHelper _testOutputHelper;
12-
11+
{
1312
private readonly ScriptEnvironment _scriptEnvironment;
1413

1514
public CompilationDependencyResolverTests(ITestOutputHelper testOutputHelper)
1615
{
17-
_testOutputHelper = testOutputHelper;
16+
testOutputHelper.Capture();
1817
_scriptEnvironment = ScriptEnvironment.Default;
1918
}
2019

@@ -40,16 +39,10 @@ public void ShouldGetCompilationDependenciesForIssue129()
4039
var resolver = CreateResolver();
4140
var dependencies = resolver.GetDependencies(TestPathUtils.GetPathToTestFixtureFolder("Issue129"), true, _scriptEnvironment.TargetFramework);
4241
Assert.Contains(dependencies, d => d.Name == "Auth0.ManagementApi");
43-
}
44-
45-
46-
42+
}
4743
private CompilationDependencyResolver CreateResolver()
48-
{
49-
var resolver = new CompilationDependencyResolver(type => ((level, message) =>
50-
{
51-
_testOutputHelper.WriteLine($"{level}:{message ?? ""}");
52-
}) );
44+
{
45+
var resolver = new CompilationDependencyResolver(TestOutputHelper.CreateTestLogFactory());
5346
return resolver;
5447
}
5548
}

src/Dotnet.Script.Tests/PackageSourceTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public void ShouldHandleSpecifyingPackageSource()
1818
{
1919
var fixture = "ScriptPackage/WithNoNuGetConfig";
2020
var pathToScriptPackages = TestPathUtils.GetPathToScriptPackages(fixture);
21-
var result = ScriptTestRunner.Default.ExecuteFixtureInProcess(fixture, "-s", pathToScriptPackages);
22-
//Assert.Contains("Hello", result.output);
23-
//Assert.Equal(0, result.exitCode);
21+
var result = ScriptTestRunner.Default.ExecuteFixture(fixture, "-s", pathToScriptPackages);
22+
Assert.Contains("Hello", result.output);
23+
Assert.Equal(0, result.exitCode);
2424
}
2525

2626
[Fact]

src/Dotnet.Script.Tests/ScaffoldingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void ShouldCreateDefaultTargetFrameworkSetting()
4949
var result = Execute("init", scriptFolder.Path);
5050
Assert.Equal(0, result.exitCode);
5151
dynamic settings = JObject.Parse(File.ReadAllText(Path.Combine(scriptFolder.Path, "omnisharp.json")));
52-
Assert.Equal(_scriptEnvironment.TargetFramework, settings.script.defaultTargetFramework.Value);
52+
Assert.Equal(_scriptEnvironment.TargetFramework, settings.script.defaultTargetFramework.Value);
5353
}
5454
}
5555

src/Dotnet.Script.Tests/ScriptPackagesTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System.Text.RegularExpressions;
77
using Dotnet.Script.DependencyModel.Compilation;
88
using Dotnet.Script.DependencyModel.Environment;
9+
using Dotnet.Script.Shared.Tests;
910
using Xunit;
11+
using Xunit.Abstractions;
1012

1113
namespace Dotnet.Script.Tests
1214
{
@@ -15,8 +17,9 @@ public class ScriptPackagesTests : IClassFixture<ScriptPackagesFixture>
1517
{
1618
private readonly ScriptEnvironment _scriptEnvironment;
1719

18-
public ScriptPackagesTests()
20+
public ScriptPackagesTests(ITestOutputHelper testOutputHelper)
1921
{
22+
testOutputHelper.Capture();
2023
_scriptEnvironment = ScriptEnvironment.Default;
2124
}
2225

@@ -82,10 +85,7 @@ private static string GetFullPathToTestFixture(string path)
8285

8386
private CompilationDependencyResolver CreateResolverCompilationDependencyResolver()
8487
{
85-
var resolver = new CompilationDependencyResolver(type => ((level, message) =>
86-
{
87-
88-
}));
88+
var resolver = new CompilationDependencyResolver(TestOutputHelper.CreateTestLogFactory());
8989
return resolver;
9090
}
9191

src/Dotnet.Script.Tests/ScriptParserTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
using System.Text;
33
using Dotnet.Script.DependencyModel.Environment;
44
using Dotnet.Script.DependencyModel.ProjectSystem;
5+
using Dotnet.Script.Shared.Tests;
56
using Xunit;
67
using Xunit.Abstractions;
78

89
namespace Dotnet.Script.Tests
910
{
1011
public class ScriptParserTests
11-
{
12-
private readonly ITestOutputHelper _testOutputHelper;
12+
{
1313
private readonly ScriptEnvironment _scriptEnvironment;
1414

1515
public ScriptParserTests(ITestOutputHelper testOutputHelper)
1616
{
17-
_testOutputHelper = testOutputHelper;
17+
testOutputHelper.Capture(minimumLogLevel:0);
1818
_scriptEnvironment = ScriptEnvironment.Default;
1919
}
2020

@@ -71,7 +71,7 @@ public void ShouldParseTargetFramework()
7171

7272
private ScriptParser CreateParser()
7373
{
74-
return new ScriptParser(type => ((level, message) => _testOutputHelper.WriteLine(message)));
74+
return new ScriptParser(TestOutputHelper.CreateTestLogFactory());
7575
}
7676
}
7777
}

src/Dotnet.Script.Tests/ScriptProjectProviderTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
using System.Text;
33
using Dotnet.Script.DependencyModel.Environment;
44
using Dotnet.Script.DependencyModel.ProjectSystem;
5+
using Dotnet.Script.Shared.Tests;
56
using Xunit;
67
using Xunit.Abstractions;
78

89
namespace Dotnet.Script.Tests
910
{
1011
[Collection("IntegrationTests")]
1112
public class ScriptProjectProviderTests
12-
{
13-
private readonly ITestOutputHelper _testOutputHelper;
13+
{
1414
private readonly ScriptEnvironment _scriptEnvironment;
1515

1616
public ScriptProjectProviderTests(ITestOutputHelper testOutputHelper)
1717
{
18-
_testOutputHelper = testOutputHelper;
18+
testOutputHelper.Capture();
1919
_scriptEnvironment = ScriptEnvironment.Default;
2020
}
2121

@@ -32,7 +32,7 @@ public void ShouldCopyLocalNuGetConfig()
3232
public void ShouldLogProjectFileContent()
3333
{
3434
StringBuilder log = new StringBuilder();
35-
var provider = new ScriptProjectProvider(type => ((level, message) => log.AppendLine(message)));
35+
var provider = new ScriptProjectProvider(type => ((level, message, exception) => log.AppendLine(message)));
3636

3737
provider.CreateProject(TestPathUtils.GetPathToTestFixtureFolder("HelloWorld"), _scriptEnvironment.TargetFramework, true);
3838
var output = log.ToString();
@@ -42,11 +42,7 @@ public void ShouldLogProjectFileContent()
4242

4343
private ScriptProjectProvider CreateProvider()
4444
{
45-
ScriptProjectProvider provider = new ScriptProjectProvider(type => ((level, message) =>
46-
{
47-
_testOutputHelper.WriteLine($"{level}:{message ?? ""}");
48-
}));
49-
45+
ScriptProjectProvider provider = new ScriptProjectProvider(TestOutputHelper.CreateTestLogFactory());
5046
return provider;
5147
}
5248
}

0 commit comments

Comments
 (0)