Skip to content

Commit

Permalink
Version assertion improvements #714 #715 (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed May 16, 2021
1 parent e297dc3 commit fd02431
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 113 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -16,7 +16,8 @@
"yaml.schemas": {
"./schemas/PSRule-options.schema.json": [
"/tests/PSRule.Tests/PSRule.*.yml",
"/docs/scenarios/*/ps-rule.yaml"
"/docs/scenarios/*/ps-rule.yaml",
"/ps-rule.yaml"
],
"./schemas/PSRule-language.schema.json": [
"/tests/PSRule.Tests/**.Rule.yaml",
Expand Down
7 changes: 6 additions & 1 deletion docs/CHANGELOG-v1.md
Expand Up @@ -12,8 +12,13 @@ See [upgrade notes][upgrade-notes] for helpful information when upgrading from p

What's changed since pre-release v1.4.0-B2105004:

- General improvements:
- Improved support for version constraints by:
- Constraints can include prerelease versions of other matching versions. [#714](https://github.com/microsoft/PSRule/issues/714)
- Constraint sets allow multiple constraints to be joined together. [#715](https://github.com/microsoft/PSRule/issues/715)
- See [about_PSRule_Assert] for details.
- Bug fixes:
- Fixed pre-release constraint handling for pre-releases versions. [#712](https://github.com/microsoft/PSRule/issues/712)
- Fixed prerelease constraint handling for prerelease versions. [#712](https://github.com/microsoft/PSRule/issues/712)

## v1.4.0-B2105004 (pre-release)

Expand Down
20 changes: 18 additions & 2 deletions docs/concepts/PSRule/en-US/about_PSRule_Assert.md
Expand Up @@ -1178,6 +1178,8 @@ The following parameters are accepted:
- `field` - The name of the field to check.
This is a case insensitive compare.
- `constraint` (optional) - A version constraint, see below for details of version constrain format.
- `includePrerelease` (optional) - Determines if prerelease versions are included.
Unless specified this defaults to `$False`.

The following are supported constraints:

Expand All @@ -1198,15 +1200,29 @@ The following are supported constraints:

An empty, null or `*` constraint matches all valid semantic versions.

Multiple constraints can be joined together:

- Use a _space_ to separate multiple constraints, each must be true (_logical AND_).
- Separates constraint sets with the double pipe `||`.
Only one constraint set must be true (_logical OR_).

By example:

- `1.2.3 || >=3.4.5 <5.0.0` results in:
- Pass: `1.2.3`, `3.4.5`, `3.5.0`, `4.9.9`.
- Fail: `3.0.0`, `5.0.0`.

Handling for pre-release versions:

- Constraints and versions containing pre-release identifiers are supported.
i.e. `>=1.2.3-build.1` or `1.2.3-build.1`.
- A version containing a pre-release identifer follows semantic versioning rules.
i.e. `1.2.3-alpha` < `1.2.3-alpha.1` < `1.2.3-alpha.beta` < `1.2.3-beta` < `1.2.3-beta.2` < `1.2.3-beta.11` < `1.2.3-rc.1` < `1.2.3`.
- A constraint without a pre-release identifer will only match a stable version.
- A constraint without a pre-release identifer will only match a stable version by default.
Set `includePrerelease` to `$True` to include prerelease versions.
- Constraints with a pre-release identifer will only match:
- Matching pre-release versions of the same major.minor.patch version.
- Matching pre-release versions of the same major.minor.patch version by default.
Set `includePrerelease` to `$True` to include prerelease versions of all matching versions.
- Matching stable versions.

By example:
Expand Down
15 changes: 13 additions & 2 deletions schemas/PSRule-options.schema.json
Expand Up @@ -513,8 +513,19 @@
"description": "Specifies the required version of a module to use.",
"markdownDescription": "Specifies the required version of a module to use. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Options.html#requires)",
"additionalProperties": {
"type": "string"
}
"type": "string",
"title": "Version constraint",
"description": "Specifies a module to constrain to a specific version.",
"markdownDescription": "Specifies a module to constrain to a specific version. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Options.html#requires)"
},
"defaultSnippets": [
{
"label": "Version constraint",
"body": {
"${1:Module}": "${2:'>=1.0.0'}"
}
}
]
},
"rule-option": {
"type": "object",
Expand Down
1 change: 0 additions & 1 deletion src/PSRule/Common/EnvironmentHelper.cs
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Security;
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Common/KeyMapDictionary.cs
Expand Up @@ -136,7 +136,7 @@ internal void Load(string prefix, EnvironmentHelper env, Func<string, string> fo
suffix = format(suffix);

_Map[suffix] = (TValue)variable.Value;
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/PSRule/Configuration/BaselineOption.cs
Expand Up @@ -4,7 +4,6 @@
using PSRule.Definitions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace PSRule.Configuration
{
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/PipelineBuilder.cs
Expand Up @@ -235,7 +235,7 @@ private bool GuardModuleVersion(string moduleName, string moduleVersion, string

private static bool TryModuleVersion(string moduleVersion, string requiredVersion)
{
if (!(SemanticVersion.TryParseVersion(moduleVersion, out SemanticVersion.Version version) && SemanticVersion.TryParseConstraint(requiredVersion, out SemanticVersion.Constraint constraint)))
if (!(SemanticVersion.TryParseVersion(moduleVersion, out SemanticVersion.Version version) && SemanticVersion.TryParseConstraint(requiredVersion, out SemanticVersion.IConstraint constraint)))
return false;

return constraint.Equals(version);
Expand Down
2 changes: 0 additions & 2 deletions src/PSRule/Pipeline/PipelineReciever.cs
Expand Up @@ -4,7 +4,6 @@
using Newtonsoft.Json;
using PSRule.Data;
using PSRule.Parser;
using PSRule.Resources;
using PSRule.Runtime;
using System;
using System.Collections;
Expand All @@ -17,7 +16,6 @@
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NodeDeserializers;
using YamlDotNet.Serialization.ValueDeserializers;

namespace PSRule.Pipeline
{
Expand Down
5 changes: 2 additions & 3 deletions src/PSRule/Runtime/Assert.cs
Expand Up @@ -8,7 +8,6 @@
using PSRule.Pipeline;
using PSRule.Resources;
using System;
using System.Collections;
using System.IO;
using System.Management.Automation;
using System.Net;
Expand Down Expand Up @@ -571,7 +570,7 @@ public AssertResult TypeOf(PSObject inputObject, string field, string[] type)
/// <summary>
/// The object field value should match the version constraint. Only applies to strings.
/// </summary>
public AssertResult Version(PSObject inputObject, string field, string constraint = null)
public AssertResult Version(PSObject inputObject, string field, string constraint = null, bool includePrerelease = false)
{
// Guard parameters
if (GuardNullParam(inputObject, nameof(inputObject), out AssertResult result) ||
Expand All @@ -580,7 +579,7 @@ public AssertResult Version(PSObject inputObject, string field, string constrain
GuardSemanticVersion(fieldValue, out SemanticVersion.Version value, out result))
return result;

if (!Runtime.SemanticVersion.TryParseConstraint(constraint, out SemanticVersion.Constraint c))
if (!SemanticVersion.TryParseConstraint(constraint, out SemanticVersion.IConstraint c, includePrerelease))
throw new RuleException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.VersionConstraintInvalid, value));

// Assert
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Runtime/ObjectHelper.cs
Expand Up @@ -385,7 +385,7 @@ private static IEnumerable<PropertyInfo> GetIndexerProperties(Type baseType)
else
{
var properties = baseType.GetProperties();
foreach(PropertyInfo pi in properties)
foreach (PropertyInfo pi in properties)
{
var p = pi.GetIndexParameters();
if (p.Length > 0)
Expand Down

0 comments on commit fd02431

Please sign in to comment.