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 6cb5399f2..8eb2b9e7d 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); } @@ -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/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 38561c55b..692aac57c 100644 --- a/ArchUnitNETTests/Fluent/FreezeTests.cs +++ b/ArchUnitNETTests/Fluent/FreezeTests.cs @@ -1,7 +1,11 @@ -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; +using ArchUnitNET.Library.Rules; using ArchUnitNET.Loader; using ArchUnitNET.xUnit; using Xunit; @@ -110,6 +114,87 @@ 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 + ); + } + } + + [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)); + } + } + + [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 { } 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); + } + } + } +}