From c311fc227e446588c023ce98bdd87ca0ce01c8e1 Mon Sep 17 00:00:00 2001 From: abaskk-msft Date: Mon, 1 Jul 2024 12:46:55 -0700 Subject: [PATCH] Make YamlValidator filepaths platform agnostic (#687) Co-authored-by: Crash Collison <3751389+tehcrashxor@users.noreply.github.com> --- src/YamlValidator.Tests/ValidatorTest.cs | 49 +++++++++++++++--------- src/YamlValidator/Constants.cs | 4 ++ src/YamlValidator/InputProcessor.cs | 2 +- src/YamlValidator/SchemaLoader.cs | 2 +- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/YamlValidator.Tests/ValidatorTest.cs b/src/YamlValidator.Tests/ValidatorTest.cs index 3408da83..e236964f 100644 --- a/src/YamlValidator.Tests/ValidatorTest.cs +++ b/src/YamlValidator.Tests/ValidatorTest.cs @@ -9,9 +9,12 @@ namespace Persistence.Tests.YamlValidator; [TestClass] public class ValidatorTest { - private const string _validPath = @".\_TestData\ValidYaml"; - private const string _invalidPath = @".\_TestData\InvalidYaml"; - private const string _schemaPath = @"..\YamlValidator\schema\pa.yaml-schema.json"; + + private static readonly string _validPath = Path.Combine(".", "_TestData", "ValidYaml") + + Path.DirectorySeparatorChar; + + private static readonly string _invalidPath = Path.Combine(".", "_TestData", "InvalidYaml") + + Path.DirectorySeparatorChar; private readonly JsonSchema _schema; private readonly Validator _yamlValidator; @@ -19,28 +22,36 @@ public class ValidatorTest public ValidatorTest() { var schemaFileLoader = new SchemaLoader(); - _schema = schemaFileLoader.Load(_schemaPath); + _schema = schemaFileLoader.Load(Constants.DefaultSchemaPath); var resultVerbosity = new VerbosityData(Constants.Verbose); _yamlValidator = new Validator(resultVerbosity.EvalOptions, resultVerbosity.JsonOutputOptions); } [TestMethod] - [DataRow($@"{_invalidPath}\ScreenWithNameNoColon.yaml", false)] - [DataRow($@"{_invalidPath}\ScreenWithNameNoValue.yaml", false)] - [DataRow($@"{_invalidPath}\ScreenWithoutControlProperty.yaml", false)] - [DataRow($@"{_invalidPath}\WrongControlDefinition.yaml", false)] - [DataRow($@"{_invalidPath}\ControlWithInvalidProperty.yaml", false)] - [DataRow($@"{_invalidPath}\EmptyArray.yaml", false)] - [DataRow($@"{_invalidPath}\Empty.yaml", false)] - [DataRow($@"{_invalidPath}\NamelessObjectNoControl.yaml", false)] - [DataRow($@"{_validPath}\NamelessObjectWithControl.yaml", true)] - [DataRow($@"{_validPath}\ValidScreen1.yaml", true)] - [DataRow($@"{_validPath}\SimpleNoRecursiveDefinition.yaml", true)] - - public void TestValidation(string filepath, bool expectedResult) + [DataRow("NamelessObjectWithControl.yaml")] + [DataRow("ValidScreen1.yaml")] + [DataRow("SimpleNoRecursiveDefinition.yaml")] + + public void TestValidationValidYaml(string filename) + { + var rawYaml = Utility.ReadFileData($@"{_validPath}{filename}"); + var result = _yamlValidator.Validate(_schema, rawYaml); + Assert.IsTrue(result.SchemaValid); + } + + [TestMethod] + [DataRow("ScreenWithNameNoColon.yaml")] + [DataRow("ScreenWithNameNoValue.yaml")] + [DataRow("ScreenWithoutControlProperty.yaml")] + [DataRow("WrongControlDefinition.yaml")] + [DataRow("ControlWithInvalidProperty.yaml")] + [DataRow("EmptyArray.yaml")] + [DataRow("Empty.yaml")] + [DataRow("NamelessObjectNoControl.yaml")] + public void TestValidationInvalidYaml(string filename) { - var rawYaml = Utility.ReadFileData($@"{filepath}"); + var rawYaml = Utility.ReadFileData($@"{_invalidPath}{filename}"); var result = _yamlValidator.Validate(_schema, rawYaml); - Assert.IsTrue(result.SchemaValid == expectedResult); + Assert.IsFalse(result.SchemaValid); } } diff --git a/src/YamlValidator/Constants.cs b/src/YamlValidator/Constants.cs index c5c252a9..c750ab6a 100644 --- a/src/YamlValidator/Constants.cs +++ b/src/YamlValidator/Constants.cs @@ -11,4 +11,8 @@ public static class Constants public const string JsonFileExtension = ".json"; public const string Verbose = "verbose"; + + // runtime constants + // default schema path + public static readonly string DefaultSchemaPath = Path.Combine(".", "schema", "pa.yaml-schema.json"); } diff --git a/src/YamlValidator/InputProcessor.cs b/src/YamlValidator/InputProcessor.cs index b3d2e107..a8b3fd7a 100644 --- a/src/YamlValidator/InputProcessor.cs +++ b/src/YamlValidator/InputProcessor.cs @@ -63,7 +63,7 @@ public static RootCommand GetRootCommand() var schemaOption = new Option( name: "--schema", description: "The path to the schema json file", - getDefaultValue: () => @".\schema\pa.yaml-schema.json" + getDefaultValue: () => Constants.DefaultSchemaPath ); schemaOption.AddValidator(result => diff --git a/src/YamlValidator/SchemaLoader.cs b/src/YamlValidator/SchemaLoader.cs index 8130ecc8..05e93e21 100644 --- a/src/YamlValidator/SchemaLoader.cs +++ b/src/YamlValidator/SchemaLoader.cs @@ -13,7 +13,7 @@ public JsonSchema Load(string schemaPath) { var node = JsonSchema.FromFile(schemaPath); var schemaFolder = Path.GetDirectoryName(schemaPath); - var subschemaPaths = Directory.GetFiles($@"{schemaFolder}\{_schemaFolderPath}", + var subschemaPaths = Directory.GetFiles($@"{schemaFolder}{Path.DirectorySeparatorChar}{_schemaFolderPath}", $"*{Constants.JsonFileExtension}"); foreach (var path in subschemaPaths)