From 096294880e44a21c9a6bc467979a24868ea06efd Mon Sep 17 00:00:00 2001 From: Bernie White Date: Fri, 25 Nov 2022 22:24:47 +1000 Subject: [PATCH] Fixes same key for ref and name #1354 (#1359) --- docs/CHANGELOG-v2.md | 2 + .../Definitions/DependencyTargetCollection.cs | 2 +- tests/PSRule.Tests/FromFile.Json.schema.json | 52 +-- tests/PSRule.Tests/FromFile.Rule.jsonc | 370 +++++++++--------- tests/PSRule.Tests/FromFile.Rule.yaml | 1 + tests/PSRule.Tests/FromFileAlias.Rule.jsonc | 34 +- tests/PSRule.Tests/RulesTests.cs | 2 +- 7 files changed, 233 insertions(+), 230 deletions(-) diff --git a/docs/CHANGELOG-v2.md b/docs/CHANGELOG-v2.md index afabff6d84..2f2487da9c 100644 --- a/docs/CHANGELOG-v2.md +++ b/docs/CHANGELOG-v2.md @@ -35,6 +35,8 @@ What's changed since pre-release v2.7.0-B0006: - Bug fixes: - Fixed job summary directory creation by @BernieWhite. [#1353](https://github.com/microsoft/PSRule/issues/1353) + - Fixed same key for ref and name by @BernieWhite + [#1354](https://github.com/microsoft/PSRule/issues/1354) ## v2.7.0-B0006 (pre-release) diff --git a/src/PSRule/Definitions/DependencyTargetCollection.cs b/src/PSRule/Definitions/DependencyTargetCollection.cs index c8b95d856c..38df977ad3 100644 --- a/src/PSRule/Definitions/DependencyTargetCollection.cs +++ b/src/PSRule/Definitions/DependencyTargetCollection.cs @@ -56,7 +56,7 @@ public bool TryAdd(T target) // Add Id, Ref, and aliases to the index. _Index.Add(target.Id, new TargetLink(target, ResourceIdKind.Id)); - if (target.Ref.HasValue) + if (target.Ref.HasValue && target.Id != target.Ref.Value) _Index.Add(target.Ref.Value, new TargetLink(target, ResourceIdKind.Ref)); for (var i = 0; target.Alias != null && i < target.Alias.Length; i++) diff --git a/tests/PSRule.Tests/FromFile.Json.schema.json b/tests/PSRule.Tests/FromFile.Json.schema.json index 24b2ccd1b6..ca547b5a72 100644 --- a/tests/PSRule.Tests/FromFile.Json.schema.json +++ b/tests/PSRule.Tests/FromFile.Json.schema.json @@ -1,30 +1,30 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "Unit test schema", - "description": "A schema for PSRule YAML options files.", - "oneOf": [ - { - "$ref": "#/definitions/structure" - } - ], - "definitions": { - "structure": { - "type": "object", - "properties": { - "Name": { - "type": "string", - "enum": [ - "TestObject1" - ] - }, - "Type": { - "type": "string" - } - }, - "required": [ - "Type", - "Name" - ] + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Unit test schema", + "description": "A schema for PSRule YAML options files.", + "oneOf": [ + { + "$ref": "#/definitions/structure" + } + ], + "definitions": { + "structure": { + "type": "object", + "properties": { + "Name": { + "type": "string", + "enum": [ + "TestObject1" + ] + }, + "Type": { + "type": "string" } + }, + "required": [ + "Type", + "Name" + ] } + } } diff --git a/tests/PSRule.Tests/FromFile.Rule.jsonc b/tests/PSRule.Tests/FromFile.Rule.jsonc index b6c15bda82..838439ff13 100644 --- a/tests/PSRule.Tests/FromFile.Rule.jsonc +++ b/tests/PSRule.Tests/FromFile.Rule.jsonc @@ -1,202 +1,202 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. - // // JSON-based rules for unit testing // [ - { - // Synopsis: A YAML rule for testing. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "JsonBasicRule", - "tags": { - "feature": "tag" - }, - "labels": { - "single": "Value", - "multi": [ - "Value1", - "Value2" - ] - } - }, - "spec": { - "condition": { - "allOf": [ - { - "field": "Name", - "equals": "TargetObject1" - }, - { - "field": "Value", - "in": [ - "Value1", - "Value2" - ] - } - ] - } - } + { + // Synopsis: A YAML rule for testing. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "JsonBasicRule", + "ref": "JsonBasicRule", + "tags": { + "feature": "tag" + }, + "labels": { + "single": "Value", + "multi": [ + "Value1", + "Value2" + ] + } }, - { - // Synopsis: A YAML rule for testing. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "RuleJsonTrue", - "tags": { - "release": "GA" - } - }, - "spec": { - "condition": { - "field": "Value", - "equals": 3 - } - } + "spec": { + "condition": { + "allOf": [ + { + "field": "Name", + "equals": "TargetObject1" + }, + { + "field": "Value", + "in": [ + "Value1", + "Value2" + ] + } + ] + } + } + }, + { + // Synopsis: A YAML rule for testing. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "RuleJsonTrue", + "tags": { + "release": "GA" + } }, - { - // Synopsis: A YAML rule for testing. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "RuleJsonFalse", - "tags": { - "release": "GA" - } - }, - "spec": { - "condition": { - "field": "Value", - "greater": 3 - } - } + "spec": { + "condition": { + "field": "Value", + "equals": 3 + } + } + }, + { + // Synopsis: A YAML rule for testing. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "RuleJsonFalse", + "tags": { + "release": "GA" + } }, - { - // Synopsis: A YAML rule for testing. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "RuleJsonWithCustomType", - "tags": { - "release": "GA" - } - }, - "spec": { - "type": [ - "CustomType" - ], - "condition": { - "field": "Value", - "greater": 3 - } - } + "spec": { + "condition": { + "field": "Value", + "greater": 3 + } + } + }, + { + // Synopsis: A YAML rule for testing. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "RuleJsonWithCustomType", + "tags": { + "release": "GA" + } + }, + "spec": { + "type": [ + "CustomType" + ], + "condition": { + "field": "Value", + "greater": 3 + } + } + }, + { + // Synopsis: A YAML rule for testing. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "RuleJsonWithSelector", + "tags": { + "release": "GA" + } }, - { - // Synopsis: A YAML rule for testing. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "RuleJsonWithSelector", - "tags": { - "release": "GA" - } - }, - "spec": { - "with": [ - "Test.Rule.Selector.1" - ], - "condition": { - "field": "notValue", - "greaterOrEquals": 3 - } - } + "spec": { + "with": [ + "Test.Rule.Selector.1" + ], + "condition": { + "field": "notValue", + "greaterOrEquals": 3 + } + } + }, + { + // Synopsis: A selector for YAML rule tests + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Selector", + "metadata": { + "name": "Test.Rule.Selector.1" }, - { - // Synopsis: A selector for YAML rule tests - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Selector", - "metadata": { - "name": "Test.Rule.Selector.1" - }, - "spec": { - "if": { - "field": "notValue", - "exists": true - } - } + "spec": { + "if": { + "field": "notValue", + "exists": true + } + } + }, + { + // Synopsis: Test reason from rule. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "JsonReasonTest", + "tags": { + "test": "Reason" + } }, - { - // Synopsis: Test reason from rule. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "JsonReasonTest", - "tags": { - "test": "Reason" - } - }, - "spec": { - "condition": { - "field": "Name", - "equals": "TestValue" - } - } + "spec": { + "condition": { + "field": "Name", + "equals": "TestValue" + } + } + }, + { + // Synopsis: Rule for unit testing of rule error level. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "JsonRuleErrorLevel", + "tags": { + "test": "Level" + } }, - { - // Synopsis: Rule for unit testing of rule error level. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "JsonRuleErrorLevel", - "tags": { - "test": "Level" - } - }, - "spec": { - "level": "Error", - "condition": { - "field": "name", - "equals": "TestObject1" - } - } + "spec": { + "level": "Error", + "condition": { + "field": "name", + "equals": "TestObject1" + } + } + }, + { + // Synopsis: Rule for unit testing of rule warning level. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "JsonRuleWarningLevel", + "tags": { + "test": "Level" + } }, - { - // Synopsis: Rule for unit testing of rule warning level. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "JsonRuleWarningLevel", - "tags": { - "test": "Level" - } - }, - "spec": { - "level": "Warning", - "condition": { - "field": "name", - "equals": "TestObject1" - } - } + "spec": { + "level": "Warning", + "condition": { + "field": "name", + "equals": "TestObject1" + } + } + }, + { + // Synopsis: Rule for unit testing of rule information level. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "JsonRuleInfoLevel", + "tags": { + "test": "Level" + } }, - { - // Synopsis: Rule for unit testing of rule information level. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "JsonRuleInfoLevel", - "tags": { - "test": "Level" - } - }, - "spec": { - "level": "Information", - "condition": { - "field": "name", - "equals": "TestObject1" - } - } + "spec": { + "level": "Information", + "condition": { + "field": "name", + "equals": "TestObject1" + } } + } ] diff --git a/tests/PSRule.Tests/FromFile.Rule.yaml b/tests/PSRule.Tests/FromFile.Rule.yaml index 7680f131e7..2b729f8f0b 100644 --- a/tests/PSRule.Tests/FromFile.Rule.yaml +++ b/tests/PSRule.Tests/FromFile.Rule.yaml @@ -11,6 +11,7 @@ apiVersion: github.com/microsoft/PSRule/v1 kind: Rule metadata: name: YamlBasicRule + ref: YamlBasicRule tags: feature: tag labels: diff --git a/tests/PSRule.Tests/FromFileAlias.Rule.jsonc b/tests/PSRule.Tests/FromFileAlias.Rule.jsonc index 537008695b..f182738c51 100644 --- a/tests/PSRule.Tests/FromFileAlias.Rule.jsonc +++ b/tests/PSRule.Tests/FromFileAlias.Rule.jsonc @@ -1,20 +1,20 @@ [ - { - // Synopsis: A rule with an alias. - "apiVersion": "github.com/microsoft/PSRule/v1", - "kind": "Rule", - "metadata": { - "name": "JSON.RuleWithAlias1", - "alias": [ - "JSON.AlternativeName" - ], - "ref": "PSRZZ.0003" - }, - "spec": { - "condition": { - "field": "name", - "exists": true - } - } + { + // Synopsis: A rule with an alias. + "apiVersion": "github.com/microsoft/PSRule/v1", + "kind": "Rule", + "metadata": { + "name": "JSON.RuleWithAlias1", + "alias": [ + "JSON.AlternativeName" + ], + "ref": "PSRZZ.0003" + }, + "spec": { + "condition": { + "field": "name", + "exists": true + } } + } ] diff --git a/tests/PSRule.Tests/RulesTests.cs b/tests/PSRule.Tests/RulesTests.cs index abe32802c7..4cc4c48dc1 100644 --- a/tests/PSRule.Tests/RulesTests.cs +++ b/tests/PSRule.Tests/RulesTests.cs @@ -209,7 +209,7 @@ public void ReadJsonRule() Assert.NotNull(rule); Assert.Equal("JsonBasicRule", rule[0].Name); Assert.Equal(PSRuleOption.GetRootedPath(""), rule[0].Source.HelpPath); - Assert.Equal(8, rule[0].Extent.Line); + Assert.Equal(7, rule[0].Extent.Line); // From relative path rule = HostHelper.GetRule(GetSource("../../../FromFile.Rule.jsonc"), context, includeDependencies: false);