diff --git a/source/Commands/BaseGeneratorCommand.cs b/source/Commands/BaseGeneratorCommand.cs
index 6eba9d381f5ef77475f19804c6f46f67d5b8cc63..0c4a8e836ca86bd36d7c53eb304001be759b836b 100644
--- a/source/Commands/BaseGeneratorCommand.cs
+++ b/source/Commands/BaseGeneratorCommand.cs
@@ -36,20 +36,27 @@ public abstract class BaseGeneratorCommand
///
protected ILogger logger { get; }
+ ///
+ /// File system functions (file and directory)
+ ///
+ protected readonly IFileSystem fs;
+
///
/// Initializes a new instance of the class.
///
/// The generate options.
/// The logger instance. Injectable for testing
- protected BaseGeneratorCommand(IGenerateOptions options, ILogger logger)
+ ///
+ protected BaseGeneratorCommand(IGenerateOptions options, ILogger logger, IFileSystem fs)
{
ArgumentNullException.ThrowIfNull(options);
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
stopwatch = new(logger);
+ this.fs = fs;
logger.Information("Source path: {source}", propertyValue: options.Source);
- site = SiteHelper.Init(configFile, options, Parser, logger, stopwatch);
+ site = SiteHelper.Init(configFile, options, Parser, logger, stopwatch, fs);
}
}
diff --git a/source/Commands/BuildCommand.cs b/source/Commands/BuildCommand.cs
index e023d280762e6c1945a0d8af8b7114ffb3c64d9f..92983d6edc9573c910ba2983b17a1b27b84ce97a 100644
--- a/source/Commands/BuildCommand.cs
+++ b/source/Commands/BuildCommand.cs
@@ -10,17 +10,24 @@ namespace SuCoS;
public class BuildCommand : BaseGeneratorCommand
{
private readonly BuildOptions options;
-
///
/// Entry point of the build command. It will be called by the main program
/// in case the build command is invoked (which is by default).
///
/// Command line options
/// The logger instance. Injectable for testing
- public BuildCommand(BuildOptions options, ILogger logger) : base(options, logger)
+ ///
+ public BuildCommand(BuildOptions options, ILogger logger, IFileSystem fs)
+ : base(options, logger, fs)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
+ }
+ ///
+ /// Run the commmand
+ ///
+ public int Run()
+ {
logger.Information("Output path: {output}", options.Output);
// Generate the site pages
@@ -37,6 +44,8 @@ public class BuildCommand : BaseGeneratorCommand
// Generate the build report
stopwatch.LogReport(site.Title);
+
+ return 0;
}
private void CreateOutputFiles()
@@ -57,11 +66,11 @@ public class BuildCommand : BaseGeneratorCommand
var outputAbsolutePath = Path.Combine(options.Output, path);
var outputDirectory = Path.GetDirectoryName(outputAbsolutePath);
- _ = Directory.CreateDirectory(outputDirectory!);
+ fs.DirectoryCreateDirectory(outputDirectory!);
// Save the processed output to the final file
var result = page.CompleteContent;
- File.WriteAllText(outputAbsolutePath, result);
+ fs.FileWriteAllText(outputAbsolutePath, result);
// Log
logger.Debug("Page created: {Permalink}", outputAbsolutePath);
@@ -74,10 +83,10 @@ public class BuildCommand : BaseGeneratorCommand
var outputAbsolutePath = Path.Combine(options.Output, resource.Permalink!.TrimStart('/'));
var outputDirectory = Path.GetDirectoryName(outputAbsolutePath);
- _ = Directory.CreateDirectory(outputDirectory!);
+ fs.DirectoryCreateDirectory(outputDirectory!);
// Copy the file to the output folder
- File.Copy(resource.SourceFullPath, outputAbsolutePath, overwrite: true);
+ fs.FileCopy(resource.SourceFullPath, outputAbsolutePath, overwrite: true);
}
});
@@ -90,19 +99,19 @@ public class BuildCommand : BaseGeneratorCommand
///
/// The source folder to copy from.
/// The output folder to copy to.
- private static void CopyFolder(string source, string output)
+ public void CopyFolder(string source, string output)
{
// Check if the source folder even exists
- if (!Directory.Exists(source))
+ if (!fs.DirectoryExists(source))
{
return;
}
// Create the output folder if it doesn't exist
- _ = Directory.CreateDirectory(output);
+ fs.DirectoryCreateDirectory(output);
// Get all files in the source folder
- var files = Directory.GetFiles(source);
+ var files = fs.DirectoryGetFiles(source);
foreach (var fileFullPath in files)
{
@@ -113,7 +122,7 @@ public class BuildCommand : BaseGeneratorCommand
var destinationFullPath = Path.Combine(output, fileName);
// Copy the file to the output folder
- File.Copy(fileFullPath, destinationFullPath, overwrite: true);
+ fs.FileCopy(fileFullPath, destinationFullPath, overwrite: true);
}
}
}
diff --git a/source/Commands/NewSiteCommand.cs b/source/Commands/NewSiteCommand.cs
index 3b9ae662ea27fe8f99e09afb83ef7eb89e9e6f2e..1f2c73a196684dc3af48e51a6a6a7f5185c9fd18 100644
--- a/source/Commands/NewSiteCommand.cs
+++ b/source/Commands/NewSiteCommand.cs
@@ -40,8 +40,8 @@ public sealed partial class NewSiteCommand(NewSiteOptions options, ILogger logge
///
public int Run()
{
- var outputPath = fileSystem.GetFullPath(options.Output);
- var siteSettingsPath = fileSystem.Combine(outputPath, "sucos.yaml");
+ var outputPath = Path.GetFullPath(options.Output);
+ var siteSettingsPath = Path.Combine(outputPath, "sucos.yaml");
if (fileSystem.FileExists(siteSettingsPath) && !options.Force)
{
@@ -75,7 +75,7 @@ public sealed partial class NewSiteCommand(NewSiteOptions options, ILogger logge
foreach (var folder in folders)
{
logger.Information("Creating {folder}", folder);
- fileSystem.CreateDirectory(folder);
+ fileSystem.DirectoryCreateDirectory(folder);
}
}
}
\ No newline at end of file
diff --git a/source/Commands/ServeCommand.cs b/source/Commands/ServeCommand.cs
index fa6d7422031afa68c0c65cb6ef1af3899e85b79a..17fa49207e56e3b63931e7115bc75f083a3eba5b 100644
--- a/source/Commands/ServeCommand.cs
+++ b/source/Commands/ServeCommand.cs
@@ -60,7 +60,9 @@ public sealed class ServeCommand : BaseGeneratorCommand, IDisposable
/// ServeOptions object specifying the serve options.
/// The logger instance. Injectable for testing
///
- public ServeCommand(ServeOptions options, ILogger logger, IFileWatcher fileWatcher) : base(options, logger)
+ ///
+ public ServeCommand(ServeOptions options, ILogger logger, IFileWatcher fileWatcher, IFileSystem fs)
+ : base(options, logger, fs)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.fileWatcher = fileWatcher ?? throw new ArgumentNullException(nameof(fileWatcher));
@@ -161,7 +163,7 @@ public sealed class ServeCommand : BaseGeneratorCommand, IDisposable
}
// Reinitialize the site
- site = SiteHelper.Init(configFile, options, Parser, logger, stopwatch);
+ site = SiteHelper.Init(configFile, options, Parser, logger, stopwatch, fs);
StartServer(baseURLDefault, portDefault);
}).ConfigureAwait(false);
diff --git a/source/Helpers/IFileSystem.cs b/source/Helpers/IFileSystem.cs
index b6e5eeb531d5d6b2b09dae02e84cef47572ba6ac..1100c9069771ffe9775c525335c716eb5cbd9206 100644
--- a/source/Helpers/IFileSystem.cs
+++ b/source/Helpers/IFileSystem.cs
@@ -6,19 +6,39 @@ namespace SuCoS;
public interface IFileSystem
{
///
- /// Path.GetFullPath
+ /// Directory.CreateDirectory
+ ///
+ ///
+ void DirectoryCreateDirectory(string path);
+
+ ///
+ /// Directory.Exists
///
///
///
- string GetFullPath(string path);
+ bool DirectoryExists(string path);
///
- /// Path.Combine
+ /// Directory.GetFiles
///
- ///
- ///
+ ///
///
- string Combine(string path1, string path2);
+ string[] DirectoryGetFiles(string path);
+
+ ///
+ /// Directory.GetFiles
+ ///
+ ///
+ ///
+ ///
+ string[] DirectoryGetFiles(string path, string searchPattern);
+
+ ///
+ /// Directory.GetDirectories
+ ///
+ ///
+ ///
+ string[] DirectoryGetDirectories(string path);
///
/// File.Exists
@@ -28,10 +48,28 @@ public interface IFileSystem
bool FileExists(string path);
///
- /// Directory.CreateDirectory
+ /// File.Copy
+ ///
+ ///
+ ///
+ ///
+ ///
+ void FileCopy(string sourceFileName, string destFileName, bool overwrite);
+
+ ///
+ /// File.WriteAllText
///
///
- void CreateDirectory(string path);
+ ///
+ ///
+ void FileWriteAllText(string path, string? contents);
+
+ ///
+ /// File.ReadAllText
+ ///
+ ///
+ ///
+ string FileReadAllText(string path);
}
///
@@ -40,14 +78,38 @@ public interface IFileSystem
public class FileSystem : IFileSystem
{
///
- public string GetFullPath(string path) => Path.GetFullPath(path);
+ public void DirectoryCreateDirectory(string path)
+ => Directory.CreateDirectory(path);
+
+ ///
+ public bool DirectoryExists(string path)
+ => Directory.Exists(path);
+
+ ///
+ public string[] DirectoryGetFiles(string path)
+ => Directory.GetFiles(path);
+
+ ///
+ public string[] DirectoryGetFiles(string path, string searchPattern)
+ => Directory.GetFiles(path, searchPattern);
+
+ ///
+ public string[] DirectoryGetDirectories(string path)
+ => Directory.GetDirectories(path);
+
+ ///
+ public bool FileExists(string path)
+ => File.Exists(path);
///
- public string Combine(string path1, string path2) => Path.Combine(path1, path2);
+ public void FileCopy(string sourceFileName, string destFileName, bool overwrite)
+ => File.Copy(sourceFileName, destFileName, overwrite);
///
- public bool FileExists(string path) => File.Exists(path);
+ public void FileWriteAllText(string path, string? contents)
+ => File.WriteAllText(path, contents);
///
- public void CreateDirectory(string path) => Directory.CreateDirectory(path);
+ public string FileReadAllText(string path)
+ => File.ReadAllText(path);
}
diff --git a/source/Helpers/SiteHelper.cs b/source/Helpers/SiteHelper.cs
index 5f0bcddfe0aeb1e3deadd5b381ca799f330aeea5..39e8873450f67cf3b7d06f1c0fe1c75a5aa18ee0 100644
--- a/source/Helpers/SiteHelper.cs
+++ b/source/Helpers/SiteHelper.cs
@@ -23,14 +23,15 @@ public static class SiteHelper
/// Creates the pages dictionary.
///
///
- public static Site Init(string configFile, IGenerateOptions options, IMetadataParser parser, ILogger logger, StopwatchReporter stopwatch)
+ public static Site Init(string configFile, IGenerateOptions options, IMetadataParser parser, ILogger logger, StopwatchReporter stopwatch, IFileSystem fs)
{
ArgumentNullException.ThrowIfNull(stopwatch);
+ ArgumentNullException.ThrowIfNull(fs);
SiteSettings siteSettings;
try
{
- siteSettings = ParseSettings(configFile, options, parser);
+ siteSettings = ParseSettings(configFile, options, parser, fs);
}
catch
{
@@ -43,11 +44,11 @@ public static class SiteHelper
stopwatch.Start("Parse");
- site.ParseAndScanSourceFiles(site.SourceContentPath);
+ site.ParseAndScanSourceFiles(fs, site.SourceContentPath);
stopwatch.Stop("Parse", site.FilesParsedToReport);
- if (Directory.Exists(Path.GetFullPath(site.SourceThemePath)))
+ if (fs.DirectoryExists(Path.GetFullPath(site.SourceThemePath)))
{
site.TemplateEngine.Initialize(site);
}
@@ -87,21 +88,23 @@ public static class SiteHelper
///
/// The generate options.
/// The front matter parser.
+ ///
/// The site settings file.
/// The site settings.
- public static SiteSettings ParseSettings(string configFile, IGenerateOptions options, IMetadataParser parser)
+ public static SiteSettings ParseSettings(string configFile, IGenerateOptions options, IMetadataParser parser, IFileSystem fs)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(parser);
+ ArgumentNullException.ThrowIfNull(fs);
// Read the main configation
var filePath = Path.Combine(options.Source, configFile);
- if (!File.Exists(filePath))
+ if (!fs.FileExists(filePath))
{
throw new FileNotFoundException($"The {configFile} file was not found in the specified source directory: {options.Source}");
}
- var fileContent = File.ReadAllText(filePath);
+ var fileContent = fs.FileReadAllText(filePath);
var siteSettings = parser.Parse(fileContent)
?? throw new FormatException($"Error reading app config {configFile}");
return siteSettings;
diff --git a/source/Models/ISite.cs b/source/Models/ISite.cs
index cdbc88be69429de864f4948ce585904660c9cc38..3c92051a6ab983cb897d46928c2e8c1c671b2692 100644
--- a/source/Models/ISite.cs
+++ b/source/Models/ISite.cs
@@ -86,11 +86,12 @@ public interface ISite : ISiteSettings, IParams
/// Search recursively for all markdown files in the content folder, then
/// parse their content for front matter meta data and markdown.
///
+ ///
/// Folder to scan
/// Folder recursive level
/// Page of the upper directory
///
- public void ParseAndScanSourceFiles(string? directory, int level = 0, IPage? parent = null);
+ public void ParseAndScanSourceFiles(IFileSystem fs, string? directory, int level = 0, IPage? parent = null);
///
/// Extra calculation and automatic data for each page.
diff --git a/source/Models/Site.cs b/source/Models/Site.cs
index c315c2b70a55200431dda55f2ecf099425bcefd0..27c326312413a640ba42042ebb9165ee21f01d47 100644
--- a/source/Models/Site.cs
+++ b/source/Models/Site.cs
@@ -14,7 +14,6 @@ public class Site : ISite
{
#region IParams
- ///
///
public Dictionary Params
{
@@ -187,19 +186,14 @@ public class Site : ISite
OutputReferences.Clear();
}
- ///
- /// Search recursively for all markdown files in the content folder, then
- /// parse their content for front matter meta data and markdown.
- ///
- /// Folder to scan
- /// Folder recursive level
- /// Page of the upper directory
- ///
- public void ParseAndScanSourceFiles(string? directory, int level = 0, IPage? parent = null)
+ ///
+ public void ParseAndScanSourceFiles(IFileSystem fs, string? directory, int level = 0, IPage? parent = null)
{
+ ArgumentNullException.ThrowIfNull(fs);
+
directory ??= SourceContentPath;
- var markdownFiles = Directory.GetFiles(directory, "*.md");
+ var markdownFiles = fs.DirectoryGetFiles(directory, "*.md");
ParseIndexPage(directory, level, ref parent, ref markdownFiles);
@@ -208,10 +202,10 @@ public class Site : ISite
_ = ParseSourceFile(filePath, parent);
});
- var subdirectories = Directory.GetDirectories(directory);
+ var subdirectories = fs.DirectoryGetDirectories(directory);
_ = Parallel.ForEach(subdirectories, subdirectory =>
{
- ParseAndScanSourceFiles(subdirectory, level + 1, parent);
+ ParseAndScanSourceFiles(fs, subdirectory, level + 1, parent);
});
}
diff --git a/source/Program.cs b/source/Program.cs
index c6cf3f078d7443a21c5bfa7c14ad11bca3230cb6..b8a7da5d353780cdc14b845d5d8801f558ead89b 100644
--- a/source/Program.cs
+++ b/source/Program.cs
@@ -64,20 +64,20 @@ public class Program(ILogger logger)
{
try
{
- _ = new BuildCommand(options, logger);
+ var command = new BuildCommand(options, logger, new FileSystem());
+ return Task.FromResult(command.Run());
}
catch (Exception ex)
{
logger.Error($"Build failed: {ex.Message}");
return Task.FromResult(1);
}
- return Task.FromResult(0);
},
async (ServeOptions options) =>
{
try
{
- var serveCommand = new ServeCommand(options, logger, new SourceFileWatcher());
+ var serveCommand = new ServeCommand(options, logger, new SourceFileWatcher(), new FileSystem());
serveCommand.StartServer();
await Task.Delay(-1).ConfigureAwait(false); // Wait forever.
}
diff --git a/test/Commands/BaseGeneratorCommandTests.cs b/test/Commands/BaseGeneratorCommandTests.cs
index 28040d2a418ba264bdbb6e0f0a62b3d63465d30d..ecfd6d2ca2ad22cf90a7c8e72791cd872ef7cd85 100644
--- a/test/Commands/BaseGeneratorCommandTests.cs
+++ b/test/Commands/BaseGeneratorCommandTests.cs
@@ -1,3 +1,4 @@
+using NSubstitute;
using Serilog;
using SuCoS;
using SuCoS.Models.CommandLineOptions;
@@ -16,19 +17,26 @@ public class BaseGeneratorCommandTests
private static readonly ILogger testLogger = new LoggerConfiguration().CreateLogger();
- private class BaseGeneratorCommandStub(IGenerateOptions options, ILogger logger)
- : BaseGeneratorCommand(options, logger);
+ private class BaseGeneratorCommandStub(IGenerateOptions options, ILogger logger, IFileSystem fs)
+ : BaseGeneratorCommand(options, logger, fs);
+
+ readonly IFileSystem fs;
+
+ public BaseGeneratorCommandTests()
+ {
+ fs = Substitute.For();
+ }
[Fact]
public void Constructor_ShouldThrowArgumentNullException_WhenOptionsIsNull()
{
- _ = Assert.Throws(() => new BaseGeneratorCommandStub(null!, testLogger));
+ _ = Assert.Throws(() => new BaseGeneratorCommandStub(null!, testLogger, fs));
}
[Fact]
public void Constructor_ShouldThrowArgumentNullException_WhenLoggerIsNull()
{
- _ = Assert.Throws(() => new BaseGeneratorCommandStub(testOptions, null!));
+ _ = Assert.Throws(() => new BaseGeneratorCommandStub(testOptions, null!, fs));
}
[Fact]
diff --git a/test/Commands/BuildCommandTests.cs b/test/Commands/BuildCommandTests.cs
new file mode 100644
index 0000000000000000000000000000000000000000..eb1aef0b9e9f0832d696ef02dd526b8e02092fb1
--- /dev/null
+++ b/test/Commands/BuildCommandTests.cs
@@ -0,0 +1,121 @@
+using SuCoS.Models.CommandLineOptions;
+using Xunit;
+using NSubstitute;
+using Serilog;
+using SuCoS;
+
+namespace Tests.Commands;
+
+public class BuildCommandTests
+{
+ readonly ILogger logger;
+ readonly IFileSystem fileSystem;
+ readonly BuildOptions options;
+
+ public BuildCommandTests()
+ {
+ logger = Substitute.For();
+ fileSystem = Substitute.For();
+ fileSystem.FileExists("./sucos.yaml").Returns(true);
+ fileSystem.FileReadAllText("./sucos.yaml").Returns("""
+Ttile: test
+""");
+ options = new BuildOptions { Output = "test" };
+ }
+
+ [Fact]
+ public void Constructor_ShouldNotThrowException_WhenParametersAreValid()
+ {
+ // Act
+ var result = new BuildCommand(options, logger, fileSystem);
+
+ // Assert
+ Assert.IsType(result);
+ }
+
+ [Fact]
+ public void Constructor_ShouldThrowArgumentNullException_WhenOptionsIsNull()
+ {
+ // Act and Assert
+ Assert.Throws(() => new BuildCommand(null!, logger, fileSystem));
+ }
+
+ [Fact]
+ public void Run()
+ {
+ // Act
+ var command = new BuildCommand(options, logger, fileSystem);
+ var result = command.Run();
+
+ // Assert
+ Assert.Equal(0, result);
+ }
+
+ // [Fact]
+ // public void CreateOutputFiles_ShouldCallFileWriteAllText_WhenPageIsValid()
+ // {
+ // // Arrange
+ // site.OutputReferences.Returns(new Dictionary
+ // {
+ // { "test", Substitute.For() }
+ // });
+ // Path.Combine(options.Output, "test").Returns("testPath");
+
+ // var buildCommand = new BuildCommand(options, logger, fileSystem);
+
+ // // Act
+ // buildCommand.CreateOutputFiles();
+
+ // // Assert
+ // fileSystem.Received(1).FileWriteAllText("testPath", Arg.Any());
+ // }
+
+ // [Fact]
+ // public void CreateOutputFiles_ShouldCallFileCopy_WhenResourceIsValid()
+ // {
+ // // Arrange
+ // var resource = Substitute.For();
+ // resource.Permalink.Returns("testPermalink");
+ // resource.SourceFullPath.Returns("testSourcePath");
+ // site.OutputReferences.Returns(new Dictionary
+ // {
+ // { "test", resource }
+ // });
+ // Path.Combine(options.Output, "testPermalink").Returns("testDestinationPath");
+ // var buildCommand = new BuildCommand(options, logger, fileSystem);
+
+ // // Act
+ // buildCommand.CreateOutputFiles();
+
+ // // Assert
+ // fileSystem.Received(1).FileCopy("testSourcePath", "testDestinationPath", overwrite: true);
+ // }
+
+ [Fact]
+ public void CopyFolder_ShouldCallCreateDirectory_WhenSourceFolderExists()
+ {
+ // Arrange
+ fileSystem.DirectoryExists("sourceFolder").Returns(true);
+ var buildCommand = new BuildCommand(options, logger, fileSystem);
+
+ // Act
+ buildCommand.CopyFolder("sourceFolder", "outputFolder");
+
+ // Assert
+ fileSystem.Received(1).DirectoryCreateDirectory("outputFolder");
+ }
+
+ [Fact]
+ public void CopyFolder_ShouldNotCallCreateDirectory_WhenSourceFolderDoesNotExist()
+ {
+ // Arrange
+ fileSystem.DirectoryExists("sourceFolder").Returns(false);
+ var buildCommand = new BuildCommand(options, logger, fileSystem);
+
+ // Act
+ buildCommand.CopyFolder("sourceFolder", "outputFolder");
+
+ // Assert
+ fileSystem.DidNotReceive().DirectoryCreateDirectory(Arg.Any());
+ }
+}
diff --git a/test/Commands/NewSiteCommandTests.cs b/test/Commands/NewSiteCommandTests.cs
index af1c3712911aea8620fa4b2068347669e61af4a4..a14823a23f9c912a277d899dc5f93d99f3d52fa8 100644
--- a/test/Commands/NewSiteCommandTests.cs
+++ b/test/Commands/NewSiteCommandTests.cs
@@ -84,8 +84,8 @@ public class NewSiteCommandTests
newSiteCommand.Run();
// Assert
- fileSystem.Received(1).CreateDirectory("folder1");
- fileSystem.Received(1).CreateDirectory("folder2");
+ fileSystem.Received(1).DirectoryCreateDirectory("folder1");
+ fileSystem.Received(1).DirectoryCreateDirectory("folder2");
}
[Fact]
@@ -96,7 +96,7 @@ public class NewSiteCommandTests
var site = Substitute.For();
site.SourceFolders.Returns(["folder1", "folder2"]);
fileSystem.FileExists(Arg.Any()).Returns(false);
- fileSystem.When(x => x.CreateDirectory(Arg.Any()))
+ fileSystem.When(x => x.DirectoryCreateDirectory(Arg.Any()))
.Do(x => { throw new ArgumentNullException(); });
var newSiteCommand = new NewSiteCommand(options, logger, fileSystem, site);
@@ -189,6 +189,6 @@ public class NewSiteCommandTests
newSiteCommand.Run();
// Assert
- fileSystem.Received(2).CreateDirectory(Arg.Any());
+ fileSystem.Received(2).DirectoryCreateDirectory(Arg.Any());
}
}
diff --git a/test/Models/SiteTests.cs b/test/Models/SiteTests.cs
index 9207cb46ff53453cd52b72a3f65f5e2c15835c8f..8b6532c662501beb7703020b50744aed528f0f60 100644
--- a/test/Models/SiteTests.cs
+++ b/test/Models/SiteTests.cs
@@ -1,3 +1,4 @@
+using SuCoS;
using SuCoS.Helpers;
using SuCoS.Models;
using SuCoS.Models.CommandLineOptions;
@@ -10,6 +11,13 @@ namespace Tests.Models;
///
public class SiteTests : TestSetup
{
+ readonly IFileSystem fs;
+
+ public SiteTests()
+ {
+ fs = new FileSystem();
+ }
+
[Theory]
[InlineData("test01.md")]
[InlineData("date-ok.md")]
@@ -23,7 +31,7 @@ public class SiteTests : TestSetup
};
// Act
- site.ParseAndScanSourceFiles(Path.Combine(siteFullPath, "content"));
+ site.ParseAndScanSourceFiles(fs, Path.Combine(siteFullPath, "content"));
// Assert
Assert.Contains(site.Pages, page => page.SourceRelativePathDirectory!.Length == 0);
@@ -42,7 +50,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(site.SourceContentPath);
+ site.ParseAndScanSourceFiles(fs, site.SourceContentPath);
// Assert
Assert.NotNull(site.Home);
@@ -63,7 +71,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
Assert.Equal(expectedQuantity, site.OutputReferences.Values.Where(output => output is IPage page && page.IsSection).Count());
@@ -83,7 +91,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
Assert.Equal(expectedQuantity, site.OutputReferences.Values.Where(output => output is IPage page).Count());
@@ -103,7 +111,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
Assert.Equal(expectedQuantity, site.OutputReferences.Values.Where(output => output is IPage page && page.IsPage).Count());
@@ -119,7 +127,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
Assert.Equal(100, site.RegularPages.First().Weight);
@@ -136,7 +144,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
Assert.Equal(0, site.RegularPages.First().Weight);
@@ -153,7 +161,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
_ = site.OutputReferences.TryGetValue("/tags", out var output);
@@ -176,7 +184,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
_ = site.OutputReferences.TryGetValue("/tags/tag1", out var output);
@@ -201,7 +209,7 @@ public class SiteTests : TestSetup
site.Options = options;
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
_ = site.OutputReferences.TryGetValue(url, out var output);
@@ -234,11 +242,11 @@ public class SiteTests : TestSetup
SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST05))
};
var parser = new SuCoS.Parser.YAMLParser();
- var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser);
+ var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser, fs);
site = new Site(options, siteSettings, parser, loggerMock, null);
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
_ = site.OutputReferences.TryGetValue(url, out var output);
@@ -262,11 +270,11 @@ public class SiteTests : TestSetup
SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST07))
};
var parser = new SuCoS.Parser.YAMLParser();
- var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser);
+ var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser, fs);
site = new Site(options, siteSettings, parser, loggerMock, null);
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
_ = site.OutputReferences.TryGetValue(url, out var output);
@@ -304,11 +312,11 @@ public class SiteTests : TestSetup
SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST06))
};
var parser = new SuCoS.Parser.YAMLParser();
- var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser);
+ var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser, fs);
site = new Site(options, siteSettings, parser, loggerMock, null);
// Act
- site.ParseAndScanSourceFiles(null);
+ site.ParseAndScanSourceFiles(fs, null);
// Assert
_ = site.OutputReferences.TryGetValue(url, out var output);
diff --git a/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs b/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs
index a0125e89ca4455f886627751b2a1dcbf26ba6d3e..33c7653474d84e051ddb075b11f38e2090bafc09 100644
--- a/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs
+++ b/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs
@@ -1,4 +1,5 @@
using NSubstitute;
+using SuCoS;
using SuCoS.Helpers;
using SuCoS.Models;
using SuCoS.Models.CommandLineOptions;
@@ -9,6 +10,13 @@ namespace Tests.ServerHandlers;
public class RegisteredPageRequestHandlerTests : TestSetup
{
+ readonly IFileSystem fs;
+
+ public RegisteredPageRequestHandlerTests()
+ {
+ fs = new FileSystem();
+ }
+
[Theory]
[InlineData("/", true)]
[InlineData("/testPage", false)]
@@ -23,7 +31,7 @@ public class RegisteredPageRequestHandlerTests : TestSetup
var registeredPageRequest = new RegisteredPageRequest(site);
// Act
- site.ParseAndScanSourceFiles(Path.Combine(siteFullPath, "content"));
+ site.ParseAndScanSourceFiles(fs, Path.Combine(siteFullPath, "content"));
// Assert
Assert.Equal(exist, registeredPageRequest.Check(requestPath));
@@ -41,7 +49,9 @@ public class RegisteredPageRequestHandlerTests : TestSetup
SourceArgument = siteFullPath
};
var parser = new SuCoS.Parser.YAMLParser();
- var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser);
+ // FIXME: make it an argument
+ var fs = new FileSystem();
+ var siteSettings = SiteHelper.ParseSettings("sucos.yaml", options, parser, fs);
site = new Site(options, siteSettings, parser, loggerMock, null);
var registeredPageRequest = new RegisteredPageRequest(site);
@@ -51,7 +61,7 @@ public class RegisteredPageRequestHandlerTests : TestSetup
_ = response.OutputStream.Returns(stream);
// Act
- site.ParseAndScanSourceFiles(Path.Combine(siteFullPath, "content"));
+ site.ParseAndScanSourceFiles(fs, Path.Combine(siteFullPath, "content"));
_ = registeredPageRequest.Check(requestPath);
var code = await registeredPageRequest.Handle(response, requestPath, DateTime.Now).ConfigureAwait(true);