Skip to content

Commit f094fc5

Browse files
committed
Added tests from core.
1 parent b9672e8 commit f094fc5

14 files changed

+535
-0
lines changed

Serilog.Sinks.File.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5
1616
EndProject
1717
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.File", "src\Serilog.Sinks.File\Serilog.Sinks.File.xproj", "{57E0ED0E-0F45-48AB-A73D-6A92B7C32095}"
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7B927378-9F16-4F6F-B3F6-156395136646}"
20+
EndProject
21+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Tests", "test\Serilog.Tests\Serilog.Tests.xproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
22+
EndProject
1923
Global
2024
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2125
Debug|Any CPU = Debug|Any CPU
@@ -26,11 +30,16 @@ Global
2630
{57E0ED0E-0F45-48AB-A73D-6A92B7C32095}.Debug|Any CPU.Build.0 = Debug|Any CPU
2731
{57E0ED0E-0F45-48AB-A73D-6A92B7C32095}.Release|Any CPU.ActiveCfg = Release|Any CPU
2832
{57E0ED0E-0F45-48AB-A73D-6A92B7C32095}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.Build.0 = Release|Any CPU
2937
EndGlobalSection
3038
GlobalSection(SolutionProperties) = preSolution
3139
HideSolutionNode = FALSE
3240
EndGlobalSection
3341
GlobalSection(NestedProjects) = preSolution
3442
{57E0ED0E-0F45-48AB-A73D-6A92B7C32095} = {037440DE-440B-4129-9F7A-09B42D00397E}
43+
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {7B927378-9F16-4F6F-B3F6-156395136646}
3544
EndGlobalSection
3645
EndGlobal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"test": {
4+
"commandName": "test"
5+
},
6+
"test-dnxcore50": {
7+
"commandName": "test",
8+
"sdkVersion": "dnx-coreclr-win-x86.1.0.0-rc1-final"
9+
}
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>3c2d8e01-5580-426a-bdd9-ec59cd98e618</ProjectGuid>
10+
<RootNamespace>Serilog.Tests</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
<TypeScriptCompileBlocked>True</TypeScriptCompileBlocked>
17+
</PropertyGroup>
18+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
19+
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
20+
</PropertyGroup>
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
22+
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
23+
</PropertyGroup>
24+
<ItemGroup>
25+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
26+
</ItemGroup>
27+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
28+
</Project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#if FILE_IO
2+
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using Xunit;
7+
using Serilog.Events;
8+
using Serilog.Tests.Support;
9+
10+
namespace Serilog.Tests.Sinks.IOFile
11+
{
12+
public class FileSinkTests
13+
{
14+
[Fact]
15+
public void FileIsWrittenIfNonexistent()
16+
{
17+
var path = Some.NonexistentTempFilePath();
18+
TestLoggingAndDelete(path);
19+
}
20+
21+
[Fact]
22+
public void FileIsAppendedToWhenAlreadyCreated()
23+
{
24+
var path = Some.TempFilePath();
25+
TestLoggingAndDelete(path);
26+
}
27+
28+
[Fact]
29+
public void WhenLimitIsSpecifiedFileSizeIsRestricted()
30+
{
31+
const int maxBytes = 100;
32+
var path = Some.NonexistentTempFilePath();
33+
ExecuteAndCleanUpFile(
34+
path,
35+
p => new LoggerConfiguration()
36+
.WriteTo.File(p, fileSizeLimitBytes: maxBytes)
37+
.CreateLogger(),
38+
log =>
39+
{
40+
log.Information(new string('n', maxBytes + 1));
41+
var size = new FileInfo(path).Length;
42+
Assert.True(size > 0);
43+
Assert.True(size < maxBytes);
44+
});
45+
}
46+
47+
static void TestLoggingAndDelete(string path)
48+
{
49+
ExecuteAndCleanUpFile(
50+
path,
51+
p => new LoggerConfiguration()
52+
.WriteTo.File(p)
53+
.CreateLogger(),
54+
log =>
55+
{
56+
var message = Some.MessageTemplate();
57+
58+
log.Write(new LogEvent(
59+
DateTimeOffset.Now,
60+
LogEventLevel.Information,
61+
null,
62+
message,
63+
Enumerable.Empty<LogEventProperty>()));
64+
65+
var refile = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
66+
var content = new StreamReader(refile).ReadToEnd();
67+
refile.Dispose();
68+
69+
Assert.True(content.Contains(message.Text));
70+
});
71+
}
72+
73+
static void ExecuteAndCleanUpFile(string path, Func<string, ILogger> configure, Action<ILogger> test)
74+
{
75+
ILogger log = null;
76+
77+
try
78+
{
79+
log = configure(path);
80+
test(log);
81+
}
82+
finally
83+
{
84+
var disposable = (IDisposable) log;
85+
if (disposable != null) disposable.Dispose();
86+
File.Delete(path);
87+
}
88+
}
89+
}
90+
}
91+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Serilog.Core;
4+
using Serilog.Events;
5+
6+
namespace Serilog.Tests.Support
7+
{
8+
class CollectingSink : ILogEventSink
9+
{
10+
readonly List<LogEvent> _events = new List<LogEvent>();
11+
12+
public List<LogEvent> Events { get { return _events; } }
13+
14+
public LogEvent SingleEvent { get { return _events.Single(); } }
15+
16+
public void Emit(LogEvent logEvent)
17+
{
18+
_events.Add(logEvent);
19+
}
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace Serilog.Tests.Support
4+
{
5+
public class DelegateDisposable : IDisposable
6+
{
7+
private readonly Action _disposeAction;
8+
private bool _disposed;
9+
10+
public DelegateDisposable(Action disposeAction)
11+
{
12+
_disposeAction = disposeAction;
13+
}
14+
15+
public void Dispose()
16+
{
17+
if (_disposed)
18+
return;
19+
20+
_disposeAction();
21+
_disposed = true;
22+
}
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Serilog.Tests.Support
6+
{
7+
class DelegatingEnricher : ILogEventEnricher
8+
{
9+
readonly Action<LogEvent, ILogEventPropertyFactory> _enrich;
10+
11+
public DelegatingEnricher(Action<LogEvent, ILogEventPropertyFactory> enrich)
12+
{
13+
if (enrich == null) throw new ArgumentNullException(nameof(enrich));
14+
_enrich = enrich;
15+
}
16+
17+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
18+
{
19+
_enrich(logEvent, propertyFactory);
20+
}
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Serilog.Tests.Support
6+
{
7+
public class DelegatingSink : ILogEventSink
8+
{
9+
readonly Action<LogEvent> _write;
10+
11+
public DelegatingSink(Action<LogEvent> write)
12+
{
13+
if (write == null) throw new ArgumentNullException(nameof(write));
14+
_write = write;
15+
}
16+
17+
public void Emit(LogEvent logEvent)
18+
{
19+
_write(logEvent);
20+
}
21+
22+
public static LogEvent GetLogEvent(Action<ILogger> writeAction)
23+
{
24+
LogEvent result = null;
25+
var l = new LoggerConfiguration()
26+
.WriteTo.Sink(new DelegatingSink(le => result = le))
27+
.CreateLogger();
28+
29+
writeAction(l);
30+
return result;
31+
}
32+
}
33+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Serilog.Core;
4+
using Serilog.Events;
5+
6+
namespace Serilog.Tests.Support
7+
{
8+
public class DisposableLogger : ILogger, IDisposable
9+
{
10+
public bool Disposed { get; set; }
11+
12+
public void Dispose()
13+
{
14+
Disposed = true;
15+
}
16+
17+
public ILogger ForContext(IEnumerable<ILogEventEnricher> enrichers)
18+
{
19+
throw new NotImplementedException();
20+
}
21+
22+
public ILogger ForContext(string propertyName, object value, bool destructureObjects = false)
23+
{
24+
throw new NotImplementedException();
25+
}
26+
27+
public ILogger ForContext<TSource>()
28+
{
29+
throw new NotImplementedException();
30+
}
31+
32+
public ILogger ForContext(Type source)
33+
{
34+
throw new NotImplementedException();
35+
}
36+
37+
public void Write(LogEvent logEvent)
38+
{
39+
throw new NotImplementedException();
40+
}
41+
42+
public void Write(LogEventLevel level, string messageTemplate, params object[] propertyValues)
43+
{
44+
throw new NotImplementedException();
45+
}
46+
47+
public void Write(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues)
48+
{
49+
throw new NotImplementedException();
50+
}
51+
52+
public bool IsEnabled(LogEventLevel level)
53+
{
54+
throw new NotImplementedException();
55+
}
56+
57+
public void Verbose(string messageTemplate, params object[] propertyValues)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
62+
public void Verbose(Exception exception, string messageTemplate, params object[] propertyValues)
63+
{
64+
throw new NotImplementedException();
65+
}
66+
67+
public void Debug(string messageTemplate, params object[] propertyValues)
68+
{
69+
throw new NotImplementedException();
70+
}
71+
72+
public void Debug(Exception exception, string messageTemplate, params object[] propertyValues)
73+
{
74+
throw new NotImplementedException();
75+
}
76+
77+
public void Information(string messageTemplate, params object[] propertyValues)
78+
{
79+
throw new NotImplementedException();
80+
}
81+
82+
public void Information(Exception exception, string messageTemplate, params object[] propertyValues)
83+
{
84+
throw new NotImplementedException();
85+
}
86+
87+
public void Warning(string messageTemplate, params object[] propertyValues)
88+
{
89+
throw new NotImplementedException();
90+
}
91+
92+
public void Warning(Exception exception, string messageTemplate, params object[] propertyValues)
93+
{
94+
throw new NotImplementedException();
95+
}
96+
97+
public void Error(string messageTemplate, params object[] propertyValues)
98+
{
99+
throw new NotImplementedException();
100+
}
101+
102+
public void Error(Exception exception, string messageTemplate, params object[] propertyValues)
103+
{
104+
throw new NotImplementedException();
105+
}
106+
107+
public void Fatal(string messageTemplate, params object[] propertyValues)
108+
{
109+
throw new NotImplementedException();
110+
}
111+
112+
public void Fatal(Exception exception, string messageTemplate, params object[] propertyValues)
113+
{
114+
throw new NotImplementedException();
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)