Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/SLNX-validator.Core/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static IServiceCollection AddSlnxValidator(this IServiceCollection servic
{
services.AddSingleton<IFileSystem, RealFileSystem>();
services.AddSingleton<ISonarReporter, SonarReporter>();
services.AddSingleton<ISlnxXsdProvider, SlnxXsdProvider>();
services.AddSingleton<IXsdValidator, XsdValidator>();
services.AddSingleton<Validation.SlnxValidator>();
services.AddSingleton<SlnxFileResolver>();
Expand Down
6 changes: 6 additions & 0 deletions src/SLNX-validator.Core/Validation/ISlnxXsdProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace JulianVerdurmen.SlnxValidator.Core.Validation;

public interface ISlnxXsdProvider
{
Stream GetXsdStream();
}
16 changes: 16 additions & 0 deletions src/SLNX-validator.Core/Validation/SlnxXsdProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Reflection;

namespace JulianVerdurmen.SlnxValidator.Core.Validation;

public sealed class SlnxXsdProvider : ISlnxXsdProvider
{
// Source: https://github.com/microsoft/vs-solutionpersistence/blob/main/src/Microsoft.VisualStudio.SolutionPersistence/Serializer/Xml/Slnx.xsd
private const string XsdResourceName = "JulianVerdurmen.SlnxValidator.Slnx.xsd";

public Stream GetXsdStream()
{
return Assembly
.GetExecutingAssembly()
.GetManifestResourceStream(XsdResourceName) ?? throw new InvalidOperationException($"XSD resource '{XsdResourceName}' not found in assembly");
}
}
10 changes: 2 additions & 8 deletions src/SLNX-validator.Core/Validation/XsdValidator.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using JulianVerdurmen.SlnxValidator.Core.ValidationResults;

namespace JulianVerdurmen.SlnxValidator.Core.Validation;

public sealed class XsdValidator : IXsdValidator
public sealed class XsdValidator(ISlnxXsdProvider xsdProvider) : IXsdValidator
{
// Source: https://github.com/microsoft/vs-solutionpersistence/blob/main/src/Microsoft.VisualStudio.SolutionPersistence/Serializer/Xml/Slnx.xsd
private const string XsdResourceName = "JulianVerdurmen.SlnxValidator.Slnx.xsd";

public async Task ValidateAsync(string slnxContent, ValidationResult result, CancellationToken cancellationToken)
{
var schemas = new XmlSchemaSet();

await using var xsdStream = Assembly
.GetExecutingAssembly()
.GetManifestResourceStream(XsdResourceName) ?? throw new InvalidOperationException("XSD file not found");
await using var xsdStream = xsdProvider.GetXsdStream();

using var xsdReader = XmlReader.Create(xsdStream);
schemas.Add(null, xsdReader);
Expand Down
2 changes: 1 addition & 1 deletion tests/SLNX-validator.Core.Tests/SlnxValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace JulianVerdurmen.SlnxValidator.Core.Tests;
public class SlnxValidatorTests
{
private static Validation.SlnxValidator ValidatorWithFiles(params string[] existingPaths)
=> new(new MockFileSystem(existingPaths), new XsdValidator());
=> new(new MockFileSystem(existingPaths), new XsdValidator(new SlnxXsdProvider()));

private static readonly string RepoRoot = OperatingSystem.IsWindows() ? @"C:\repo" : "/repo";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task OwnSlnxFile_HasNoValidationErrors()
var slnxFile = directory!.EnumerateFiles("*.slnx").First();
var content = await File.ReadAllTextAsync(slnxFile.FullName);

var validator = new CoreSlnxValidator(new RealFileSystem(), new XsdValidator());
var validator = new CoreSlnxValidator(new RealFileSystem(), new XsdValidator(new SlnxXsdProvider()));
var result = await validator.ValidateAsync(content, slnxFile.DirectoryName!);

result.Errors.Should().BeEmpty();
Expand Down
2 changes: 1 addition & 1 deletion tests/SLNX-validator.Tests/ValidatorRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ValidatorRunnerTests
private static ValidatorRunner CreateRunner(IFileSystem fileSystem)
{
var resolver = new SlnxFileResolver(fileSystem);
var validator = new CoreSlnxValidator(fileSystem, new XsdValidator());
var validator = new CoreSlnxValidator(fileSystem, new XsdValidator(new SlnxXsdProvider()));
var collector = new ValidationCollector(fileSystem, validator);
var sonarReporter = new SonarReporter(fileSystem);
return new ValidatorRunner(resolver, collector, sonarReporter);
Expand Down
Loading