Skip to content

Commit

Permalink
Merge pull request #69 from dotnet-campus/t/walterlv/directory-save
Browse files Browse the repository at this point in the history
支持在配置文件所在文件夹不存在时,自动创建文件夹
  • Loading branch information
kkwpsv authored Feb 29, 2024
2 parents bafa8d7 + 3c91102 commit a8b5126
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
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 @@ -356,6 +356,10 @@ private void WriteAllText(string text)
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

0 comments on commit a8b5126

Please sign in to comment.