Skip to content

Commit

Permalink
Fixes #4 and Fixes #5 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
JimPaine authored Mar 10, 2021
1 parent 4972250 commit d06d5c0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
28 changes: 13 additions & 15 deletions src/FileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.Threading.Tasks;
using Lignator.Interfaces;
using Microsoft.Extensions.Logging;

namespace Lignator
{
Expand All @@ -16,19 +15,17 @@ public class FileSink : IFileSink

private bool disposing = false;
private bool disposed = false;
private readonly ILogger<FileSink> logger;
private readonly string path;
private readonly bool clean;

public FileSink(ILogger<FileSink> logger)
{
this.logger = logger;
}

public IFileSink Start(string path, bool multiline = false)
public FileSink() {}
private FileSink(string path, bool multiline = false, bool clean = false)
{
this.path = path;
this.clean = clean;
string directory = Path.GetDirectoryName(path);
if (!Directory.Exists(directory))
{
this.logger.LogDebug($"Directory did not exist, creating directory {directory}");
Directory.CreateDirectory(directory);
}

Expand All @@ -46,20 +43,20 @@ public IFileSink Start(string path, bool multiline = false)
await task;
}
}
await writer.FlushAsync();
}
});
return this;
}

public void Sink(string content)
public IFileSink Start(string path, bool multiline = false, bool clean = false)
{
if (this.disposing) return;
this.queue.Enqueue(content);
return new FileSink(path, multiline, clean);
}

public void DeleteFile(string path)
public void Sink(string content)
{
File.Delete(path);
if (this.disposing) return;
this.queue.Enqueue(content);
}

public void Dispose()
Expand All @@ -69,6 +66,7 @@ public void Dispose()
this.disposed = true;
Task.WaitAll(new Task[] { this.sinker });
this.sinker.Dispose();
if (this.clean) File.Delete(this.path);
}
}
}
5 changes: 2 additions & 3 deletions src/Interfaces/IFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Licensed under the MIT License.

using System;
using Microsoft.Extensions.Logging;

namespace Lignator.Interfaces
{
public interface IFileSink: IDisposable
{
IFileSink Start(string path, bool multiline = false);
IFileSink Start(string path, bool multiline = false, bool clean = false);

void Sink(string content);

void DeleteFile(string path);
}
}
9 changes: 1 addition & 8 deletions src/LogGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ public async Task Generate(Options options)
List<Task> tasks = new List<Task>();
for (int n = 0; n < options.Runs; n++)
{
ConcurrentBag<string> fileLocations = new ConcurrentBag<string>();
IEnumerable<IGrouping<string, Extraction>> grouping = extractions.GroupBy(x => x.SourceFileName);
foreach (IGrouping<string, Extraction> group in grouping)
{
tasks.Add(Task.Run(() =>
{
string fileName = !string.IsNullOrEmpty(group.Key) ? group.Key : "lignator";
string path = $"{options.Output}/{fileName}.{options.Extension}";
fileLocations.Add(path);
int seed = 0;
lock (this.randomLock)
Expand All @@ -72,7 +70,7 @@ public async Task Generate(Options options)
}
Random taskRandom = new Random(seed);
using (IFileSink fileSink = this.fileSink.Start(path, options.MultiLine))
using (IFileSink fileSink = this.fileSink.Start(path, options.MultiLine, options.Clean))
{
if (head != null) fileSink.Sink(this.transformer.Transform(head.Template, head.Tokens, taskRandom, variables));
Expand All @@ -88,11 +86,6 @@ public async Task Generate(Options options)
if (tail != null) fileSink.Sink(this.transformer.Transform(tail.Template, tail.Tokens, taskRandom, variables));
}
if (options.Clean)
{
this.fileSink.DeleteFile(path);
}
}));
}

Expand Down
24 changes: 12 additions & 12 deletions tests/LogGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task Generate_WithValidParams_CallsExtractor()
Mock<ITokenTransformer> transformer = new Mock<ITokenTransformer>();
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer.Object, fileSink.Object, logger.Object);
string template = "Hello %{randomitem(world,universe)}%";
Expand Down Expand Up @@ -68,7 +68,7 @@ public async Task Generate_WithValidParams_CallsTransformer()
Mock<ITokenTransformer> transformer = new Mock<ITokenTransformer>();
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer.Object, fileSink.Object, logger.Object);
string template = "Hello %{randomitem(world,universe)}%";
Expand Down Expand Up @@ -107,7 +107,7 @@ public async Task Generate_WithValidParams_CallsTransformerPerRun()
Mock<ITokenTransformer> transformer = new Mock<ITokenTransformer>();
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer.Object, fileSink.Object, logger.Object);

Expand Down Expand Up @@ -146,7 +146,7 @@ public async Task Generate_WithValidParams_CallsTransformerPerRunPerLog()
Mock<ITokenTransformer> transformer = new Mock<ITokenTransformer>();
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer.Object, fileSink.Object, logger.Object);

Expand Down Expand Up @@ -187,7 +187,7 @@ public async Task Generate_WithValidParams_CallsTransformerOnceForAllRunsAndLogs
Mock<ITokenTransformer> transformer = new Mock<ITokenTransformer>();
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer.Object, fileSink.Object, logger.Object);
string template = "Hello %{randomitem(world,universe)}%";
Expand Down Expand Up @@ -224,7 +224,7 @@ public async Task Generate_ExtractionWithSourceName_CreateFileIncludesItInOutput
Mock<ITokenTransformer> transformer = new Mock<ITokenTransformer>();
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer.Object, fileSink.Object, logger.Object);
string template = "Hello %{randomitem(world,universe)}%";
Expand All @@ -239,7 +239,7 @@ public async Task Generate_ExtractionWithSourceName_CreateFileIncludesItInOutput
await generator.Generate(options);

// Assert
fileSink.Verify(o => o.Start(It.IsAny<string>(), It.IsAny<bool>()), Times.Exactly(1));
fileSink.Verify(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()), Times.Exactly(1));
}

[Fact]
Expand All @@ -264,7 +264,7 @@ public async Task Generate_ExtractionWithNewLine_CreateLogWithMultipleLines()
ITokenTransformer transformer = new TokenTransformer(new Mock<ILogger<TokenTransformer>>().Object);
Mock<IFileSink> fileSink = new Mock<IFileSink>();
IFileSink instance = fileSink.Object;
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(instance);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(instance);

ILogGenerator generator = new LogGenerator(extractor.Object, transformer, instance, logger.Object);

Expand Down Expand Up @@ -308,7 +308,7 @@ public async Task Generate_ExtractionWithHead_CreateLogWithHead()
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
ITokenTransformer transformer = new TokenTransformer(new Mock<ILogger<TokenTransformer>>().Object);
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);
MockSequence sequence = new MockSequence();
fileSink.InSequence(sequence).Setup(o => o.Sink("Say Hello!"));
fileSink.InSequence(sequence).Setup(o => o.Sink(It.Is<string>(s => s == "Hello world" || s == "Hello universe")));
Expand Down Expand Up @@ -357,7 +357,7 @@ public async Task Generate_ExtractionWithTail_CreateLogWithTail()
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
ITokenTransformer transformer = new TokenTransformer(new Mock<ILogger<TokenTransformer>>().Object);
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);
MockSequence sequence = new MockSequence();
fileSink.InSequence(sequence).Setup(o => o.Sink(It.Is<string>(s => s == "Hello world" || s == "Hello universe")));
fileSink.InSequence(sequence).Setup(o => o.Sink("Say Hello!"));
Expand Down Expand Up @@ -408,7 +408,7 @@ public async Task Generate_ExtractionWithHeadAndTail_CreateLogWithHeadAndTail()
ITokenTransformer transformer = new TokenTransformer(new Mock<ILogger<TokenTransformer>>().Object);
Mock<IFileSink> fileSink = new Mock<IFileSink>();
IFileSink instance = fileSink.Object;
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(instance);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(instance);
MockSequence sequence = new MockSequence();
fileSink.InSequence(sequence).Setup(o => o.Sink("Say Hello!"));
fileSink.InSequence(sequence).Setup(o => o.Sink(It.Is<string>(s => s == "Hello world" || s == "Hello universe")));
Expand Down Expand Up @@ -461,7 +461,7 @@ public async Task Generate_ExtractionWithHeadAndTailTransforms_CreateLogWithHead
Mock<ILogger<LogGenerator>> logger = new Mock<ILogger<LogGenerator>>();
ITokenTransformer transformer = new TokenTransformer(new Mock<ILogger<TokenTransformer>>().Object);
Mock<IFileSink> fileSink = new Mock<IFileSink>();
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>())).Returns(fileSink.Object);
fileSink.Setup(o => o.Start(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(fileSink.Object);
MockSequence sequence = new MockSequence();
fileSink.InSequence(sequence).Setup(o => o.Sink(It.Is<string>(s => s == "Head: Hi" || s == "Head: Hello")));
fileSink.InSequence(sequence).Setup(o => o.Sink(It.Is<string>(s => s == "Hello world" || s == "Hello universe")));
Expand Down

0 comments on commit d06d5c0

Please sign in to comment.