From 5e9cfa5be48a2aa66bfdbb6a39c9558210b1fe39 Mon Sep 17 00:00:00 2001 From: Alexander Linne Date: Wed, 4 Nov 2026 11:22:43 +0100 Subject: [PATCH 1/3] fix: write XmlViolationStore to its configured path StoreCurrentViolations wrote to the default storage path instead of the path passed to the constructor, so a custom XML store was read from but never written to. Add a regression test using a temporary store directory. Signed-off-by: Alexander Linne --- .../Fluent/Freeze/XmlViolationStore.cs | 2 +- ArchUnitNETTests/Fluent/FreezeTests.cs | 22 ++++++++++++- ArchUnitNETTests/Fluent/TempStoreDirectory.cs | 31 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 ArchUnitNETTests/Fluent/TempStoreDirectory.cs diff --git a/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs b/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs index 6cb5399f2..9701d9f44 100644 --- a/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs +++ b/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs @@ -75,7 +75,7 @@ public void StoreCurrentViolations(IArchRule rule, IEnumerable storedRule?.Remove(); storageDoc.Root.Add(ruleElement); - using (var writer = XmlWriter.Create(DefaultStoragePath, WriterSettings)) + using (var writer = XmlWriter.Create(_storagePath, WriterSettings)) { storageDoc.WriteTo(writer); } diff --git a/ArchUnitNETTests/Fluent/FreezeTests.cs b/ArchUnitNETTests/Fluent/FreezeTests.cs index 38561c55b..e35c1d94a 100644 --- a/ArchUnitNETTests/Fluent/FreezeTests.cs +++ b/ArchUnitNETTests/Fluent/FreezeTests.cs @@ -1,4 +1,7 @@ -using ArchUnitNET.Domain; +using System; +using System.IO; +using System.Xml.Linq; +using ArchUnitNET.Domain; using ArchUnitNET.Fluent; using ArchUnitNET.Fluent.Freeze; using ArchUnitNET.Fluent.Slices; @@ -110,6 +113,23 @@ public void PassFrozenRulesWithCustomViolationStorePath() .Check(StaticTestArchitectures.SlicesTestArchitecture); } + [Fact] + public void XmlViolationStoreWritesToCustomPath() + { + using (var dir = new TempStoreDirectory()) + { + var path = dir.PathFor("FrozenRules.xml"); + + Freeze(_frozenRule, new XmlViolationStore(path)).Check(Architecture); + + Assert.True(File.Exists(path)); + Assert.Contains( + XDocument.Load(path).Root.Elements(), + element => element.Attribute("ArchRule")?.Value == _frozenRule.Description + ); + } + } + private class Violation { } private class Violation2 { } diff --git a/ArchUnitNETTests/Fluent/TempStoreDirectory.cs b/ArchUnitNETTests/Fluent/TempStoreDirectory.cs new file mode 100644 index 000000000..df8d8c8e0 --- /dev/null +++ b/ArchUnitNETTests/Fluent/TempStoreDirectory.cs @@ -0,0 +1,31 @@ +using System; +using System.IO; + +namespace ArchUnitNETTests.Fluent +{ + internal sealed class TempStoreDirectory : IDisposable + { + private readonly string _directory = Path.Combine( + Path.GetTempPath(), + "archunitnet-freeze-" + Guid.NewGuid() + ); + + public string PathFor(string fileName) => Path.Combine(_directory, fileName); + + public string CreateFile(string fileName, string content) + { + Directory.CreateDirectory(_directory); + var path = PathFor(fileName); + File.WriteAllText(path, content); + return path; + } + + public void Dispose() + { + if (Directory.Exists(_directory)) + { + Directory.Delete(_directory, true); + } + } + } +} From a32bd48e6447122737b7e57a14d1455bbaed8bb5 Mon Sep 17 00:00:00 2001 From: Alexander Linne Date: Wed, 4 Nov 2026 11:23:18 +0100 Subject: [PATCH 2/3] fix: do not silently reset malformed violation stores LoadStorage caught every exception and returned an empty store, which the next write then persisted, so a single malformed or locked file silently un-froze all rules. Only a missing file or directory now counts as an empty store; parse errors and IO errors propagate. Signed-off-by: Alexander Linne --- .../Fluent/Freeze/JsonViolationStore.cs | 2 +- .../Fluent/Freeze/XmlViolationStore.cs | 2 +- ArchUnitNETTests/Fluent/FreezeTests.cs | 48 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/ArchUnitNET/Fluent/Freeze/JsonViolationStore.cs b/ArchUnitNET/Fluent/Freeze/JsonViolationStore.cs index 933917d44..efcbaa915 100644 --- a/ArchUnitNET/Fluent/Freeze/JsonViolationStore.cs +++ b/ArchUnitNET/Fluent/Freeze/JsonViolationStore.cs @@ -108,7 +108,7 @@ private List LoadStorage() return results ?? new List(); } - catch (Exception) + catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException) { return new List(); } diff --git a/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs b/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs index 9701d9f44..8eb2b9e7d 100644 --- a/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs +++ b/ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs @@ -100,7 +100,7 @@ private XDocument LoadStorage() ? doc : new XDocument(new XElement("FrozenRules")); } - catch (Exception) //file not found or invalid xml document + catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException) { return new XDocument(new XElement("FrozenRules")); } diff --git a/ArchUnitNETTests/Fluent/FreezeTests.cs b/ArchUnitNETTests/Fluent/FreezeTests.cs index e35c1d94a..4fadbbb37 100644 --- a/ArchUnitNETTests/Fluent/FreezeTests.cs +++ b/ArchUnitNETTests/Fluent/FreezeTests.cs @@ -130,6 +130,54 @@ public void XmlViolationStoreWritesToCustomPath() } } + [Fact] + public void FreezeThrowsOnMalformedJsonStoreAndLeavesFileUntouched() + { + using (var dir = new TempStoreDirectory()) + { + const string content = "{ this is not valid json"; + var path = dir.CreateFile("FrozenRules.json", content); + + Assert.ThrowsAny(() => + Freeze(_frozenRule, new JsonViolationStore(path)).Check(Architecture) + ); + + Assert.Equal(content, File.ReadAllText(path)); + } + } + + [Fact] + public void FreezeThrowsOnMalformedXmlStoreAndLeavesFileUntouched() + { + using (var dir = new TempStoreDirectory()) + { + const string content = "(() => + Freeze(_frozenRule, new XmlViolationStore(path)).Check(Architecture) + ); + + Assert.Equal(content, File.ReadAllText(path)); + } + } + + [Fact] + public void FreezeCreatesStoreInMissingDirectory() + { + using (var dir = new TempStoreDirectory()) + { + var jsonPath = dir.PathFor(Path.Combine("json", "FrozenRules.json")); + var xmlPath = dir.PathFor(Path.Combine("xml", "FrozenRules.xml")); + + Freeze(_frozenRule, new JsonViolationStore(jsonPath)).Check(Architecture); + Freeze(_frozenRule, new XmlViolationStore(xmlPath)).Check(Architecture); + + Assert.True(File.Exists(jsonPath)); + Assert.True(File.Exists(xmlPath)); + } + } + private class Violation { } private class Violation2 { } From 983b9b5ec969d3d4a705463368a7b6efabb5f166 Mon Sep 17 00:00:00 2001 From: Alexander Linne Date: Wed, 4 Nov 2026 11:23:32 +0100 Subject: [PATCH 3/3] fix: use simple assembly name in target framework rule violations Assembly.FullName includes Version, Culture and PublicKeyToken, so a frozen target framework rule stopped matching on every assembly version bump. Assembly.Name is the simple name set in ArchBuilder and is stable. Signed-off-by: Alexander Linne --- .../Library/Rules/TargetFrameworkRules.cs | 6 +++--- ArchUnitNETTests/Fluent/FreezeTests.cs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ArchUnitNET/Library/Rules/TargetFrameworkRules.cs b/ArchUnitNET/Library/Rules/TargetFrameworkRules.cs index 4a2df0867..8d1afc923 100644 --- a/ArchUnitNET/Library/Rules/TargetFrameworkRules.cs +++ b/ArchUnitNET/Library/Rules/TargetFrameworkRules.cs @@ -17,7 +17,7 @@ IEnumerable Evaluate(Architecture architecture, IArchRule arch var description = "has target framework " + assembly.GetTargetFramework(); yield return new EvaluationResult( assembly, - new StringIdentifier(assembly.FullName), + new StringIdentifier(assembly.Name), passed, description, archRule, @@ -39,7 +39,7 @@ IEnumerable Evaluate(Architecture architecture, IArchRule arch var description = "has target framework " + assembly.GetTargetFramework(); yield return new EvaluationResult( assembly, - new StringIdentifier(assembly.FullName), + new StringIdentifier(assembly.Name), passed, description, archRule, @@ -61,7 +61,7 @@ IEnumerable Evaluate(Architecture architecture, IArchRule arch var description = "has target framework " + assembly.GetTargetFramework(); yield return new EvaluationResult( assembly, - new StringIdentifier(assembly.FullName), + new StringIdentifier(assembly.Name), passed, description, archRule, diff --git a/ArchUnitNETTests/Fluent/FreezeTests.cs b/ArchUnitNETTests/Fluent/FreezeTests.cs index 4fadbbb37..692aac57c 100644 --- a/ArchUnitNETTests/Fluent/FreezeTests.cs +++ b/ArchUnitNETTests/Fluent/FreezeTests.cs @@ -5,6 +5,7 @@ using ArchUnitNET.Fluent; using ArchUnitNET.Fluent.Freeze; using ArchUnitNET.Fluent.Slices; +using ArchUnitNET.Library.Rules; using ArchUnitNET.Loader; using ArchUnitNET.xUnit; using Xunit; @@ -178,6 +179,22 @@ public void FreezeCreatesStoreInMissingDirectory() } } + [Fact] + public void FrozenTargetFrameworkRuleDoesNotStoreAssemblyVersion() + { + using (var dir = new TempStoreDirectory()) + { + var path = dir.PathFor("FrozenRules.json"); + var rule = TargetFrameworkRules.ShouldBeDotNetFramework(); + + Freeze(rule, new JsonViolationStore(path)).Check(Architecture); + + var stored = File.ReadAllText(path); + Assert.Contains(typeof(FreezeTests).Assembly.GetName().Name, stored); + Assert.DoesNotContain("Version=", stored); + } + } + private class Violation { } private class Violation2 { }