Skip to content
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 @@ -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