Skip to content
Draft
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
2 changes: 1 addition & 1 deletion ArchUnitNET/Fluent/Freeze/JsonViolationStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private List<FrozenRule> LoadStorage()

return results ?? new List<FrozenRule>();
}
catch (Exception)
catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException)
{
return new List<FrozenRule>();
}
Expand Down
4 changes: 2 additions & 2 deletions ArchUnitNET/Fluent/Freeze/XmlViolationStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void StoreCurrentViolations(IArchRule rule, IEnumerable<StringIdentifier>
storedRule?.Remove();
storageDoc.Root.Add(ruleElement);

using (var writer = XmlWriter.Create(DefaultStoragePath, WriterSettings))
using (var writer = XmlWriter.Create(_storagePath, WriterSettings))
{
storageDoc.WriteTo(writer);
}
Expand All @@ -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"));
}
Expand Down
6 changes: 3 additions & 3 deletions ArchUnitNET/Library/Rules/TargetFrameworkRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IEnumerable<EvaluationResult> 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,
Expand All @@ -39,7 +39,7 @@ IEnumerable<EvaluationResult> 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,
Expand All @@ -61,7 +61,7 @@ IEnumerable<EvaluationResult> 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,
Expand Down
87 changes: 86 additions & 1 deletion ArchUnitNETTests/Fluent/FreezeTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Exception>(() =>
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 = "<FrozenRules><FrozenRule";
var path = dir.CreateFile("FrozenRules.xml", content);

Assert.ThrowsAny<Exception>(() =>
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 { }
Expand Down
31 changes: 31 additions & 0 deletions ArchUnitNETTests/Fluent/TempStoreDirectory.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}