Skip to content

Commit

Permalink
Make YamlValidator filepaths platform agnostic (#687)
Browse files Browse the repository at this point in the history
Co-authored-by: Crash Collison <[email protected]>
  • Loading branch information
abaskk-msft and tehcrashxor authored Jul 1, 2024
1 parent 4d7b081 commit c311fc2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
49 changes: 30 additions & 19 deletions src/YamlValidator.Tests/ValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,49 @@ 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;

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);
}
}
4 changes: 4 additions & 0 deletions src/YamlValidator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
2 changes: 1 addition & 1 deletion src/YamlValidator/InputProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static RootCommand GetRootCommand()
var schemaOption = new Option<string>(
name: "--schema",
description: "The path to the schema json file",
getDefaultValue: () => @".\schema\pa.yaml-schema.json"
getDefaultValue: () => Constants.DefaultSchemaPath
);

schemaOption.AddValidator(result =>
Expand Down
2 changes: 1 addition & 1 deletion src/YamlValidator/SchemaLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c311fc2

Please sign in to comment.