From 837fd3b459d1f0ad0d253a53340a4eb6020b6b50 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 29 Feb 2024 15:14:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=97=A0=E9=9C=80=E6=96=87=E6=A1=A3=E8=A6=81?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 1 - 1 file changed, 1 deletion(-) 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 From 5a357e3556e6c917437c7c9884ac96d93a8419e8 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 29 Feb 2024 15:14:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=9A=E5=86=99=E5=85=A5=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=89=80=E5=9C=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FileConfigurationRepoTests.cs | 22 +++++++++++++++++++ .../Utils/TestUtil.cs | 6 +++-- 2 files changed, 26 insertions(+), 2 deletions(-) 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)) { From 3c91102312aac487a2767e4b9d54a9103e4fbeb0 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 29 Feb 2024 15:14:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=89=80=E5=9C=A8=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E6=97=B6=EF=BC=8C=E4=B9=9F=E8=83=BD=E5=86=99=E5=85=A5?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Concurrent/FileDictionarySynchronizer.cs | 4 ++++ 1 file changed, 4 insertions(+) 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,