Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持在配置文件所在文件夹不存在时,自动创建文件夹 #69

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageProjectUrl>https://github.com/dotnet-campus/dotnetCampus.Configurations</PackageProjectUrl>
<RepositoryUrl>https://github.com/dotnet-campus/dotnetCampus.Configurations.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
};

private readonly FileInfo _file;
private readonly Func<IReadOnlyDictionary<TKey, TValue>, string> _serializer;

Check warning on line 47 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.

Check warning on line 47 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.
private readonly Func<string, IReadOnlyDictionary<TKey, TValue>> _deserializer;

Check warning on line 48 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.

Check warning on line 48 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.
private readonly FileEqualsComparison _fileEqualsComparison;

/// <summary>
Expand Down Expand Up @@ -95,8 +95,8 @@
/// <param name="deserializer">指定如何从一个字符串反序列化成一个键值集合。</param>
/// <param name="fileEqualsComparison">指定如何表示文件内容相同或不同。</param>
public FileDictionarySynchronizer(FileInfo file,
Func<IReadOnlyDictionary<TKey, TValue>, string> serializer,

Check warning on line 98 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.

Check warning on line 98 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.
Func<string, IReadOnlyDictionary<TKey, TValue>> deserializer,

Check warning on line 99 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.

Check warning on line 99 in src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs

View workflow job for this annotation

GitHub Actions / TestOnMac

The type 'TKey' cannot be used as type parameter 'TKey' in the generic type or method 'IReadOnlyDictionary<TKey, TValue>'. Nullability of type argument 'TKey' doesn't match 'notnull' constraint.
FileEqualsComparison fileEqualsComparison = FileEqualsComparison.WholeTextEquals)
{
_file = file ?? throw new ArgumentNullException(nameof(file));
Expand Down Expand Up @@ -356,6 +356,10 @@
DoIOActionWithRetry(i =>
{
CT.Log($"正在写入文件(i):{text.Replace("\r\n", "\\n").Replace("\n", "\\n")}", _file.Name, "Sync");
if (!Directory.Exists(_file.Directory.FullName))
{
_file.Directory.Create();
}
using var fileStream = new FileStream(
_file.FullName, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ public void SaveAsync()
// Assert
Assert.IsTrue(File.Exists(coin.FullName));
});

"如果没有文件甚至连文件夹也不存在但需要存储数据,那么会创建文件夹和文件。".Test(async () =>
{
// Arrange
var coin = TestUtil.GetTempFile(null, ".coin", "Configs");
var directory = new DirectoryInfo(coin.DirectoryName!);
if (Directory.Exists(directory.FullName))
{
directory.Delete(true);
}
var repo = CreateIndependentRepo(coin);

// Act
await repo.WriteAsync("Test.Create", "True").ConfigureAwait(false);
await repo.SaveAsync().ConfigureAwait(false);

// Assert
Assert.IsTrue(File.Exists(coin.FullName));

// Clean
directory.Delete(true);
});
}

[ContractTestCase]
Expand Down
6 changes: 4 additions & 2 deletions tests/dotnetCampus.Configurations.Tests/Utils/TestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public static class TestUtil
/// <param name="templateFileName">
/// 如果指定临时文件的模板,则会确保生成的临时文件存在且与模板文件相同;
/// 如果指定临时文件的模板为 null,则仅会返回一个临时文件的路径,而不会创建文件。</param>
/// <param name="relativeFilePath">将此配置文件放入到某文件夹中。</param>
/// <returns>用于测试的临时文件。</returns>
public static FileInfo GetTempFile(string? templateFileName = null, string? extension = null)
public static FileInfo GetTempFile(string? templateFileName = null, string? extension = null, string? relativeFilePath = null)
{
var newFileName = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
relativeFilePath ?? "",
Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
if (!string.IsNullOrWhiteSpace(templateFileName))
{
Expand Down
Loading