-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlnxValidatorTests.cs
More file actions
157 lines (129 loc) · 4.87 KB
/
SlnxValidatorTests.cs
File metadata and controls
157 lines (129 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using AwesomeAssertions;
using JulianVerdurmen.SlnxValidator.Core.Validation;
using JulianVerdurmen.SlnxValidator.Core.ValidationResults;
namespace JulianVerdurmen.SlnxValidator.Core.Tests;
public class SlnxValidatorTests
{
private static Validation.SlnxValidator ValidatorWithFiles(params string[] existingPaths)
=> new(new MockFileSystem(existingPaths), new XsdValidator());
private static readonly string RepoRoot = OperatingSystem.IsWindows() ? @"C:\repo" : "/repo";
[Test]
public async Task ValidateAsync_EmptySolution_IsValid()
{
var slnx = """
<Solution>
</Solution>
""";
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeTrue();
}
[Test]
public async Task ValidateAsync_InvalidXml_ReturnsInvalidXmlError()
{
var slnx = """
this is not xml at all
""";
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeFalse();
result.Errors[0].Code.Should().Be(ValidationErrorCode.InvalidXml);
result.Errors[0].Message.Should().Contain("Invalid XML");
}
[Test]
public async Task ValidateAsync_XsdViolation_ReturnsXsdViolationError()
{
var slnx = """
<Solution>
<UnknownElement />
</Solution>
""";
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeFalse();
result.Errors[0].Code.Should().Be(ValidationErrorCode.XsdViolation);
}
[Test]
public async Task ValidateAsync_ProjectWithoutPathAttribute_ReturnsXsdViolationError()
{
var slnx = """
<Solution>
<Project />
</Solution>
""";
// Path is use="required" in the XSD, so this is caught as an XSD violation
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeFalse();
result.Errors[0].Code.Should().Be(ValidationErrorCode.XsdViolation);
}
[Test]
public async Task ValidateAsync_MissingFileInFolder_ReturnsFileNotFoundError()
{
var slnx = """
<Solution>
<Folder Name="docs">
<File Path="README.md" />
</Folder>
</Solution>
""";
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeFalse();
result.Errors[0].Code.Should().Be(ValidationErrorCode.ReferencedFileNotFound);
result.Errors[0].Message.Should().Contain("README.md");
}
[Test]
public async Task ValidateAsync_ExistingFileInFolder_IsValid()
{
var slnx = """
<Solution>
<Folder Name="docs">
<File Path="README.md" />
</Folder>
</Solution>
""";
var result = await ValidatorWithFiles(Path.Combine(RepoRoot, "README.md"))
.ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeTrue();
}
[Test]
public async Task ValidateAsync_MultipleErrors_AllReported()
{
var slnx = """
<Solution>
<Folder Name="docs">
<File Path="missing/One.md" />
<File Path="missing/Two.md" />
</Folder>
</Solution>
""";
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.Errors.Should().HaveCount(2);
foreach (var error in result.Errors)
{
error.Code.Should().Be(ValidationErrorCode.ReferencedFileNotFound);
}
}
[Test]
public async Task ValidateAsync_WildcardInFilePath_ReturnsInvalidWildcardUsageError()
{
var slnx = """
<Solution>
<Folder Name="docs">
<File Path="docs/*.md" />
</Folder>
</Solution>
""";
var result = await ValidatorWithFiles().ValidateAsync(slnx, RepoRoot);
result.IsValid.Should().BeFalse();
result.Errors[0].Code.Should().Be(ValidationErrorCode.InvalidWildcardUsage);
result.Errors[0].Message.Should().Contain("docs/*.md");
}
[Test]
public async Task ValidationErrorCode_ToCode_ReturnsPrefixedCode()
{
ValidationErrorCode.FileNotFound.ToCode().Should().Be("SLNX001");
ValidationErrorCode.InvalidExtension.ToCode().Should().Be("SLNX002");
ValidationErrorCode.NotATextFile.ToCode().Should().Be("SLNX003");
ValidationErrorCode.InvalidXml.ToCode().Should().Be("SLNX010");
ValidationErrorCode.ReferencedFileNotFound.ToCode().Should().Be("SLNX011");
ValidationErrorCode.InvalidWildcardUsage.ToCode().Should().Be("SLNX012");
ValidationErrorCode.XsdViolation.ToCode().Should().Be("SLNX013");
}
}