diff --git a/Directory.Build.props b/Directory.Build.props index 065260c..c541d02 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -10,7 +10,6 @@ https://github.com/dotnet-campus/dotnetCampus.Configurations https://github.com/dotnet-campus/dotnetCampus.Configurations.git git - true latest enable diff --git a/src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs b/src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs index b7d0508..babfa5e 100644 --- a/src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs +++ b/src/dotnetCampus.Configurations/Concurrent/FileDictionarySynchronizer.cs @@ -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, diff --git a/tests/dotnetCampus.Configurations.Tests/FileConfigurationRepoTests.cs b/tests/dotnetCampus.Configurations.Tests/FileConfigurationRepoTests.cs index 90b0ce9..d656cec 100644 --- a/tests/dotnetCampus.Configurations.Tests/FileConfigurationRepoTests.cs +++ b/tests/dotnetCampus.Configurations.Tests/FileConfigurationRepoTests.cs @@ -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] diff --git a/tests/dotnetCampus.Configurations.Tests/Utils/TestUtil.cs b/tests/dotnetCampus.Configurations.Tests/Utils/TestUtil.cs index d43d8bb..99b9013 100644 --- a/tests/dotnetCampus.Configurations.Tests/Utils/TestUtil.cs +++ b/tests/dotnetCampus.Configurations.Tests/Utils/TestUtil.cs @@ -22,11 +22,13 @@ public static class TestUtil /// /// 如果指定临时文件的模板,则会确保生成的临时文件存在且与模板文件相同; /// 如果指定临时文件的模板为 null,则仅会返回一个临时文件的路径,而不会创建文件。 + /// 将此配置文件放入到某文件夹中。 /// 用于测试的临时文件。 - 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)) {