Skip to content

Commit

Permalink
QAction - CheckDataMinerDependency - Check DevPacks (#74)
Browse files Browse the repository at this point in the history
* Adding new check for checking DevPacks compared to the MinimumRequiredVersion tag.

* Smaller tweaks
  • Loading branch information
MichielOda authored Aug 1, 2024
1 parent f3af4ca commit 9192a49
Show file tree
Hide file tree
Showing 29 changed files with 792 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// <auto-generated>This is auto-generated code by Validator Management Tool. Do not modify.</auto-generated>
namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.QActions.QAction.CheckDataMinerDependency
{
using System;
using System.Collections.Generic;

using Skyline.DataMiner.CICD.Models.Protocol.Read;
using Skyline.DataMiner.CICD.Validators.Common.Interfaces;
using Skyline.DataMiner.CICD.Validators.Common.Model;
using Skyline.DataMiner.CICD.Validators.Protocol.Common;
using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces;

internal static class Error
{
public static IValidationResult MismatchDevPack(IValidate test, IReadable referenceNode, IReadable positionNode, string packageName, string packageVersion, string minimumRequiredVersion, string qactionId)
{
return new ValidationResult
{
Test = test,
CheckId = CheckId.CheckDataMinerDependency,
ErrorId = ErrorIds.MismatchDevPack,
FullId = "3.42.1",
Category = Category.QAction,
Severity = Severity.Major,
Certainty = Certainty.Certain,
Source = Source.Validator,
FixImpact = FixImpact.NonBreaking,
GroupDescription = "",
Description = String.Format("Package '{0}' version '{1}' does not meet the minimum required version '{2}'. QAction ID '{3}'.", packageName, packageVersion, minimumRequiredVersion, qactionId),
HowToFix = "Either increase version in the MinimumRequiredVersion tag or downgrade the package so it matches with the minimum required version.",
ExampleCode = "",
Details = "Using a higher version of the DevPack may introduce new features that are not compatible with the specified version of DataMiner." + Environment.NewLine + "This can lead to unexpected behavior or errors. Make sure that the DevPack version used in the solution does not exceed the MinimumRequiredVersion.",
HasCodeFix = false,

PositionNode = positionNode,
ReferenceNode = referenceNode,
};
}
}

internal static class ErrorIds
{
public const uint MismatchDevPack = 1;
}

/// <summary>
/// Contains the identifiers of the checks.
/// </summary>
public static class CheckId
{
/// <summary>
/// The check identifier.
/// </summary>
public const uint CheckDataMinerDependency = 42;
}
}
26 changes: 26 additions & 0 deletions Protocol/ErrorMessages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12313,6 +12313,32 @@
</ErrorMessage>
</ErrorMessages>
</Check>
<Check id="42">
<Name namespace="Protocol.QActions.QAction">CheckDataMinerDependency</Name>
<ErrorMessages>
<ErrorMessage id="1">
<Name>MismatchDevPack</Name>
<GroupDescription />
<Description>
<Format>Package '{0}' version '{1}' does not meet the minimum required version '{2}'. QAction ID '{3}'.</Format>
<InputParameters>
<InputParameter id="0">packageName</InputParameter>
<InputParameter id="1">packageVersion</InputParameter>
<InputParameter id="2">minimumRequiredVersion</InputParameter>
<InputParameter id="3">qactionId</InputParameter>
</InputParameters>
</Description>
<Severity>Major</Severity>
<Certainty>Certain</Certainty>
<Source>Validator</Source>
<FixImpact>NonBreaking</FixImpact>
<HasCodeFix>False</HasCodeFix>
<HowToFix><![CDATA[Either increase version in the MinimumRequiredVersion tag or downgrade the package so it matches with the minimum required version.]]></HowToFix>
<ExampleCode><![CDATA[]]></ExampleCode>
<Details><![CDATA[Using a higher version of the DevPack may introduce new features that are not compatible with the specified version of DataMiner.[NewLine]This can lead to unexpected behavior or errors. Make sure that the DevPack version used in the solution does not exceed the MinimumRequiredVersion.]]></Details>
</ErrorMessage>
</ErrorMessages>
</Check>
</Checks>
</Category>
<Category id="4">
Expand Down
127 changes: 127 additions & 0 deletions Protocol/Tests/Protocol/QActions/QAction/CheckDataMinerDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.QActions.QAction.CheckDataMinerDependency
{
using System;
using System.Collections.Generic;
using System.Linq;

using Skyline.DataMiner.CICD.Common;
using Skyline.DataMiner.CICD.Models.Protocol;
using Skyline.DataMiner.CICD.Models.Protocol.Read;
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects;
using Skyline.DataMiner.CICD.Validators.Common.Interfaces;
using Skyline.DataMiner.CICD.Validators.Common.Model;
using Skyline.DataMiner.CICD.Validators.Protocol.Common;
using Skyline.DataMiner.CICD.Validators.Protocol.Common.Attributes;
using Skyline.DataMiner.CICD.Validators.Protocol.Common.Extensions;
using Skyline.DataMiner.CICD.Validators.Protocol.Helpers;
using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces;

[Test(CheckId.CheckDataMinerDependency, Category.QAction)]
internal class CheckDataMinerDependency : IValidate/*, ICodeFix, ICompare*/
{
public List<IValidationResult> Validate(ValidatorContext context)
{
List<IValidationResult> results = new List<IValidationResult>();

if (!context.HasQActionsAndIsSolution)
{
// Early skip when no QActions are present or when it is not solution based.
return results;
}

ValidateHelper helper = new ValidateHelper(this, context, results);
foreach ((CompiledQActionProject projectData, IQActionsQAction qaction) in context.EachQActionProject(true))
{
// Load csproj
var project = Project.Load(projectData.Project.FilePath, projectData.Project.Name);
if (!project.PackageReferences.Any())
{
// No NuGet packages being used
// Should not happen, but is covered by the banner in DIS.
continue;
}

helper.CheckDevPack(project.PackageReferences, qaction);
}

return results;
}

////public ICodeFixResult Fix(CodeFixContext context)
////{
//// CodeFixResult result = new CodeFixResult();

//// switch (context.Result.ErrorId)
//// {

//// default:
//// result.Message = $"This error ({context.Result.ErrorId}) isn't implemented.";
//// break;
//// }

//// return result;
////}

////public List<IValidationResult> Compare(MajorChangeCheckContext context)
////{
//// List<IValidationResult> results = new List<IValidationResult>();

//// return results;
////}
}

internal class ValidateHelper : ValidateHelperBase
{
private readonly string minReqVersionTag;
private readonly Version minVersion;

public ValidateHelper(IValidate test, ValidatorContext context, List<IValidationResult> results) : base(test, context, results)
{
minReqVersionTag = context.ProtocolModel.Protocol?.Compliancies?.MinimumRequiredVersion?.Value;
if (!DataMinerVersion.TryParse(minReqVersionTag, out DataMinerVersion version))
{
// Default in case tag is not present.
version = context.ValidatorSettings.MinimumSupportedDataMinerVersion;
}

minVersion = new Version(version.Major, version.Minor, version.Build);
}

public void CheckDevPack(IEnumerable<PackageReference> packageReferences, IQActionsQAction qAction)
{
// Filter out DevPacks & the Files
var dmPacks = packageReferences.Where(IsDevPackOrPartOf).ToList();

foreach (PackageReference dmPack in dmPacks)
{
if (!Version.TryParse(dmPack.Version, out Version v))
{
// Unable to parse the version of the DevPack.
continue;
}

Version packageVersion = new Version(v.Major, v.Minor, v.Build);

/*
* MinimumRequiredVersion = 10.2.0.0 - XXXXX
* minVersion == 10.2.0
*
* Package Reference = 10.2.0.5 (CU or rerun of package creation)
* packageVersion == 10.2.0
*/

if (minVersion < packageVersion)
{
results.Add(Error.MismatchDevPack(test, qAction, qAction, dmPack.Name, dmPack.Version, minReqVersionTag, qAction.Id?.RawValue));
}
}

return;
bool IsDevPackOrPartOf(PackageReference packageReference)
{
return packageReference.Name.StartsWith("Skyline.DataMiner.Dev.") ||
packageReference.Name.StartsWith("Skyline.DataMiner.Files");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
namespace ProtocolTests.Protocol.QActions.QAction.CheckDataMinerDependency
{
using System;
using System.Collections.Generic;

using FluentAssertions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Skyline.DataMiner.CICD.Validators.Common.Interfaces;
using Skyline.DataMiner.CICD.Validators.Common.Model;
using Skyline.DataMiner.CICD.Validators.Protocol.Common;
using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces;
using Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.QActions.QAction.CheckDataMinerDependency;

[TestClass]
public class Validate
{
private readonly IValidate check = new CheckDataMinerDependency();

#region Valid Checks

[TestMethod]
[Ignore("Check can only be done on a solution")]
public void QAction_CheckDataMinerDependency_Valid_XmlBased()
{
Generic.ValidateData data = new Generic.ValidateData
{
TestType = Generic.TestType.Valid,
FileName = "Valid",
ExpectedResults = new List<IValidationResult>()
};

Generic.Validate(check, data);
}

[TestMethod]
public void QAction_CheckDataMinerDependency_Valid()
{
Generic.ValidateData data = new Generic.ValidateData
{
TestType = Generic.TestType.Valid,
FileName = "Valid",
IsSolution = true,
ExpectedResults = new List<IValidationResult>()
};

Generic.Validate(check, data);
}

#endregion

#region Invalid Checks

[TestMethod]
[Ignore("Check can only be done on a solution")]
public void QAction_CheckDataMinerDependency_MismatchDevPack_Xml()
{
Generic.ValidateData data = new Generic.ValidateData
{
TestType = Generic.TestType.Invalid,
FileName = "MismatchDevPack",
ExpectedResults = new List<IValidationResult>()
};

Generic.Validate(check, data);
}

[TestMethod]
public void QAction_CheckDataMinerDependency_MismatchDevPack()
{
Generic.ValidateData data = new Generic.ValidateData
{
TestType = Generic.TestType.Invalid,
FileName = "MismatchDevPack",
IsSolution = true,
ExpectedResults = new List<IValidationResult>
{
Error.MismatchDevPack(null, null, null, "Skyline.DataMiner.Dev.Protocol", "10.4.7", "10.2.0.0 - 11517", "1"),

Error.MismatchDevPack(null, null, null, "Skyline.DataMiner.Dev.Protocol", "10.4.7", "10.2.0.0 - 11517", "2"),
Error.MismatchDevPack(null, null, null, "Skyline.DataMiner.Files.SLMediationSnippets", "10.4.7", "10.2.0.0 - 11517", "2"),
}
};

Generic.Validate(check, data);
}

#endregion
}

[TestClass]
public class ErrorMessages
{
[TestMethod]
public void QAction_CheckDataMinerDependency_MismatchDevPack()
{
// Create ErrorMessage
var message = Error.MismatchDevPack(null, null, null, "packageName", "packageVersion", "minimumRequiredVersion", "qactionId");

var expected = new ValidationResult
{
Severity = Severity.Major,
Certainty = Certainty.Certain,
FixImpact = FixImpact.NonBreaking,
GroupDescription = "",
Description = "Package 'packageName' version 'packageVersion' does not meet the minimum required version 'minimumRequiredVersion'. QAction ID 'qactionId'.",
Details = "Using a higher version of the DevPack may introduce new features that are not compatible with the specified version of DataMiner." + Environment.NewLine +
"This can lead to unexpected behavior or errors. Make sure that the DevPack version used in the solution does not exceed the MinimumRequiredVersion.",
HasCodeFix = false,
};

// Assert
message.Should().BeEquivalentTo(expected, Generic.ExcludePropertiesForErrorMessages);
}
}

[TestClass]
public class Attribute
{
private readonly IRoot check = new CheckDataMinerDependency();

[TestMethod]
public void QAction_CheckDataMinerDependency_CheckCategory() => Generic.CheckCategory(check, Category.QAction);

[TestMethod]
public void QAction_CheckDataMinerDependency_CheckId() => Generic.CheckId(check, CheckId.CheckDataMinerDependency);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Protocol xmlns="http://www.skyline.be/validatorProtocolUnitTest">
<QActions>
<QAction>
</QAction>
</QActions>
</Protocol>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<PropertyGroup>
<PlatformTarget>x86</PlatformTarget>
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectName), '^QAction_([0-9]+)$'))">
<DefineConstants>$(DefineConstants);DCFv1;DBInfo;ALARM_SQUASHING</DefineConstants>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = tab
indent_size = 4
tab_width = 4
end_of_line = crlf
trim_trailing_whitespace = true

[*.cs]
dotnet_sort_system_directives_first = true
Loading

0 comments on commit 9192a49

Please sign in to comment.