generated from kritikos-io/templates-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/Configuration.Persistence/Converters/FileInfoToStringConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace Kritikos.Configuration.Persistence.Converters | ||
{ | ||
using System.IO; | ||
|
||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Converts from <seealso cref="FileInfo"/> to and from string. | ||
/// </summary> | ||
public class FileInfoToStringConverter : ValueConverter<FileInfo, string> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <seealso cref="FileInfoToStringConverter"/> class. | ||
/// </summary> | ||
/// <param name="separator">Character used as directory separator in the persistence layer.</param> | ||
/// <param name="basePath"><seealso cref="DirectoryInfo"/> used as path base when handling relative paths.</param> | ||
/// <param name="mappingHints"> | ||
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate | ||
/// facets for the converted data. | ||
/// </param> | ||
public FileInfoToStringConverter( | ||
char separator, | ||
FileSystemInfo? basePath = null, | ||
ConverterMappingHints? mappingHints = null) | ||
: base( | ||
v => FromFileInfo(basePath, v, separator), | ||
v => FromPath(basePath, v, separator), | ||
mappingHints) | ||
{ | ||
} | ||
|
||
private static FileInfo FromPath(FileSystemInfo? basePath, string filePath, char separator) | ||
{ | ||
var path = (basePath == null | ||
? filePath | ||
: Path.Combine(basePath.FullName, filePath)) | ||
.Replace(separator, Path.DirectorySeparatorChar); | ||
|
||
return new FileInfo(path); | ||
} | ||
|
||
private static string FromFileInfo(FileSystemInfo? basePath, FileSystemInfo file, char separator) | ||
{ | ||
var path = file.FullName; | ||
path = basePath != null | ||
? path | ||
.Replace(basePath?.FullName ?? string.Empty, string.Empty)[1..] | ||
: path; | ||
|
||
path = separator != '\\' && Path.DirectorySeparatorChar == '\\' && Path.GetPathRoot(path).Length - 1 > 0 | ||
? path[(Path.GetPathRoot(path).Length - 1)..] | ||
: path; | ||
path = path.Replace(Path.DirectorySeparatorChar, separator); | ||
|
||
return path; | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/Configuration.PersistenceTests/ConverterTests/FileInfoToStringConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace Kritikos.Configuration.PersistenceTests.ConverterTests | ||
{ | ||
using System.IO; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
using Xunit; | ||
|
||
public class FileInfoToStringConverter | ||
{ | ||
private const string WindowsBase = @"C:\Windows\System32"; | ||
private const string WindowsRelative = @"drivers\etc\hosts"; | ||
|
||
private const string WindowsPath = @"C:\Windows\System32\drivers\etc\hosts"; | ||
|
||
private const string LinuxBase = @"/srv/http"; | ||
private const string LinuxRelative = @"root/index.html"; | ||
private const string LinuxPath = @"/srv/http/root/index.html"; | ||
|
||
private static readonly ConverterMappingHints MappingHints = new(unicode: true); | ||
|
||
[Fact] | ||
public void Relative_path_windows() | ||
{ | ||
var converter = | ||
new Persistence.Converters.FileInfoToStringConverter('\\', new DirectoryInfo(WindowsBase), MappingHints); | ||
|
||
var file = converter.ConvertFromProvider(WindowsRelative) as FileInfo; | ||
var foo = converter.ConvertToProvider(file) as string; | ||
|
||
file.FullName.Should().Be(WindowsPath); | ||
foo.Should().Be(WindowsRelative); | ||
} | ||
|
||
[Fact] | ||
public void Absolute_path_windows() | ||
{ | ||
var converter = | ||
new Persistence.Converters.FileInfoToStringConverter('\\', mappingHints: MappingHints); | ||
|
||
var file = converter.ConvertFromProvider(WindowsPath) as FileInfo; | ||
var foo = converter.ConvertToProvider(file) as string; | ||
|
||
file.FullName.Should().Be(WindowsPath); | ||
foo.Should().Be(WindowsPath); | ||
} | ||
|
||
[Fact] | ||
public void Relative_path_linux() | ||
{ | ||
var converter = | ||
new Persistence.Converters.FileInfoToStringConverter('/', new DirectoryInfo(LinuxBase), | ||
mappingHints: MappingHints); | ||
|
||
var file = converter.ConvertFromProvider(LinuxRelative) as FileInfo; | ||
var foo = converter.ConvertToProvider(file) as string; | ||
|
||
foo.Should().Be(LinuxRelative); | ||
} | ||
|
||
[Fact] | ||
public void Absolute_path_linux() | ||
{ | ||
var converter = | ||
new Persistence.Converters.FileInfoToStringConverter('/', mappingHints: MappingHints); | ||
|
||
var file = converter.ConvertFromProvider(LinuxPath) as FileInfo; | ||
var foo = converter.ConvertToProvider(file) as string; | ||
|
||
foo.Should().Be(LinuxPath); | ||
} | ||
} | ||
} |