-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into users/joemay/PaYamlV3-updates-and-fixes
- Loading branch information
Showing
31 changed files
with
588 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Json.Schema; | ||
using Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; | ||
|
||
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 readonly JsonSchema _schema; | ||
private readonly Validator _yamlValidator; | ||
|
||
public ValidatorTest() | ||
{ | ||
var schemaFileLoader = new SchemaLoader(); | ||
_schema = schemaFileLoader.Load(_schemaPath); | ||
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) | ||
{ | ||
var rawYaml = Utility.ReadFileData($@"{filepath}"); | ||
var result = _yamlValidator.Validate(_schema, rawYaml); | ||
Assert.IsTrue(result.SchemaValid == expectedResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="_TestData\**\*"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-preview.5.24306.7" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\YamlValidator\YamlValidator.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
3 changes: 3 additions & 0 deletions
3
src/YamlValidator.Tests/_TestData/InvalidYaml/ControlWithInvalidProperty.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Screen2: | ||
Control: Screen | ||
InvalidProperty: "invalid" |
Empty file.
4 changes: 4 additions & 0 deletions
4
src/YamlValidator.Tests/_TestData/InvalidYaml/EmptyArray.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- | ||
- | ||
- | ||
- |
1 change: 1 addition & 0 deletions
1
src/YamlValidator.Tests/_TestData/InvalidYaml/NamelessObjectNoControl.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
: |
1 change: 1 addition & 0 deletions
1
src/YamlValidator.Tests/_TestData/InvalidYaml/ScreenWithNameNoColon.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
1 change: 1 addition & 0 deletions
1
src/YamlValidator.Tests/_TestData/InvalidYaml/ScreenWithNameNoValue.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test: |
2 changes: 2 additions & 0 deletions
2
src/YamlValidator.Tests/_TestData/InvalidYaml/ScreenWithoutControlProperty.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Screen: | ||
ComponentName: "test" |
2 changes: 2 additions & 0 deletions
2
src/YamlValidator.Tests/_TestData/InvalidYaml/WrongControlDefinition.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TestScreen: | ||
"abcd" |
2 changes: 2 additions & 0 deletions
2
src/YamlValidator.Tests/_TestData/ValidYaml/NamelessObjectWithControl.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
: | ||
Control: "bdbd" |
2 changes: 2 additions & 0 deletions
2
src/YamlValidator.Tests/_TestData/ValidYaml/SimpleNoRecursiveDefinition.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
LoginPage: | ||
Control: "Button" |
30 changes: 30 additions & 0 deletions
30
src/YamlValidator.Tests/_TestData/ValidYaml/ValidScreen1.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Screen2: | ||
Control: Screen | ||
Children: | ||
- ButtonCanvas2: | ||
Control: Button | ||
Properties: | ||
OnSelect: =Navigate(Screen1) | ||
Text: ="Back" | ||
Height: =53 | ||
Width: =172 | ||
X: =632 | ||
Y: =550 | ||
- TextCanvas1: | ||
Control: Text | ||
Properties: | ||
Align: ='TextCanvas.Align'.Center | ||
Size: =50 | ||
Text: ="Hello" | ||
Height: =91 | ||
Width: =368 | ||
X: =517 | ||
Y: =44 | ||
- Image1: | ||
Control: Image | ||
Properties: | ||
Image: ='pexels-pixabay-417173' | ||
Height: =361 | ||
Width: =466 | ||
X: =447 | ||
Y: =135 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; | ||
public class Constants | ||
{ | ||
public const string FileTypeName = "file"; | ||
public const string FolderTypeName = "folder"; | ||
public const string YamlFileExtension = ".yaml"; | ||
public const string YmlFileExtension = ".yml"; | ||
public const string JsonFileExtension = ".json"; | ||
|
||
public const string Verbose = "verbose"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.CommandLine; | ||
|
||
namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; | ||
public class InputProcessor | ||
{ | ||
|
||
private static void ProcessFiles(string path, string schema, string pathType) | ||
{ | ||
// read only records | ||
var filePathInfo = new ValidationRequest(path, schema, pathType); | ||
var verbosityInfo = new VerbosityData(Constants.Verbose); | ||
|
||
var validator = new Validator(verbosityInfo.EvalOptions, verbosityInfo.JsonOutputOptions); | ||
var schemaLoader = new SchemaLoader(); | ||
var fileLoader = new YamlLoader(); | ||
var orchestrator = new Orchestrator(fileLoader, schemaLoader, validator); | ||
orchestrator.RunValidation(filePathInfo); | ||
} | ||
public static RootCommand GetRootCommand() | ||
{ | ||
|
||
var pathOption = new Option<string>( | ||
name: "--path", | ||
description: "The path to the input yaml file or directory of yaml files" | ||
) | ||
{ IsRequired = true }; | ||
|
||
pathOption.AddValidator(result => | ||
{ | ||
var inputFilePath = result.GetValueForOption(pathOption); | ||
// either file or folder must be passed | ||
var pathType = string.Empty; | ||
if (string.IsNullOrEmpty(inputFilePath)) | ||
{ | ||
result.ErrorMessage = "The input is invalid, input must be a filepath to a yaml file \\" + | ||
"or a folder path to a folder of yaml files"; | ||
} | ||
else if (!Directory.Exists(inputFilePath) && !File.Exists(inputFilePath)) | ||
{ | ||
result.ErrorMessage = "The input path does not exist"; | ||
} | ||
else if (Directory.Exists(inputFilePath)) | ||
{ | ||
if (Directory.GetFiles(inputFilePath, $"*{Constants.YamlFileExtension}").Length == 0) | ||
{ | ||
result.ErrorMessage = "The input folder does not contain any yaml files"; | ||
} | ||
} | ||
else if (File.Exists(inputFilePath)) | ||
{ | ||
if (Path.GetExtension(inputFilePath) != Constants.YamlFileExtension) | ||
{ | ||
result.ErrorMessage = "The input file must be a yaml file"; | ||
} | ||
} | ||
}); | ||
|
||
// assume local schema file exists in nuget package, use relative filepath for now | ||
var schemaOption = new Option<string>( | ||
name: "--schema", | ||
description: "The path to the schema json file", | ||
getDefaultValue: () => @".\schema\pa.yaml-schema.json" | ||
); | ||
|
||
schemaOption.AddValidator(result => | ||
{ | ||
var schemaPath = result.GetValueForOption(schemaOption); | ||
if (string.IsNullOrEmpty(schemaPath)) | ||
{ | ||
result.ErrorMessage = "Schema option selected, but no schema was provided"; | ||
} | ||
else if (Path.GetExtension(schemaPath) != Constants.JsonFileExtension) | ||
{ | ||
result.ErrorMessage = "The schema file must be a json file"; | ||
} | ||
else if (!File.Exists(schemaPath)) | ||
{ | ||
result.ErrorMessage = "The schema file does not exist"; | ||
} | ||
}); | ||
|
||
// define root | ||
var rootCommand = new RootCommand("YAML validator cli-tool"); | ||
|
||
// validate command | ||
var validateCommand = new Command("validate", "Validate the input yaml file") | ||
{ | ||
pathOption, | ||
schemaOption | ||
}; | ||
|
||
validateCommand.SetHandler((pathOptionVal, schemaOptionVal) => | ||
{ | ||
var pathType = File.GetAttributes(pathOptionVal).HasFlag(FileAttributes.Directory) ? Constants.FolderTypeName : | ||
Constants.FileTypeName; | ||
ProcessFiles(pathOptionVal, schemaOptionVal, pathType); | ||
}, pathOption, schemaOption); | ||
|
||
rootCommand.AddCommand(validateCommand); | ||
|
||
return rootCommand; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; | ||
public class Orchestrator | ||
{ | ||
private readonly YamlLoader _fileLoader; | ||
private readonly SchemaLoader _schemaLoader; | ||
private readonly Validator _validator; | ||
|
||
public Orchestrator(YamlLoader fileLoader, SchemaLoader schemaLoader, Validator validator) | ||
{ | ||
_fileLoader = fileLoader; | ||
_schemaLoader = schemaLoader; | ||
_validator = validator; | ||
} | ||
|
||
public void RunValidation(ValidationRequest inputData) | ||
{ | ||
var schemaPath = inputData.SchemaPath; | ||
var path = inputData.FilePath; | ||
var pathType = inputData.FilePathType; | ||
|
||
var yamlData = _fileLoader.Load(path, pathType); | ||
var serializedSchema = _schemaLoader.Load(schemaPath); | ||
|
||
foreach (var yamlFileData in yamlData) | ||
{ | ||
Console.WriteLine($"Validation for {yamlFileData.Key}"); | ||
var result = _validator.Validate(serializedSchema, yamlFileData.Value); | ||
Console.WriteLine($"Validation Result: {result.SchemaValid}"); | ||
foreach (var error in result.TraversalResults) | ||
{ | ||
Console.WriteLine($"{error}"); | ||
} | ||
Console.WriteLine(); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.CommandLine; | ||
|
||
namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; | ||
|
||
public class Program | ||
{ | ||
private static void Main(string[] args) | ||
{ | ||
var inputProcessor = InputProcessor.GetRootCommand(); | ||
inputProcessor.Invoke(args); | ||
} | ||
} |
Oops, something went wrong.