From 8b8b4db2b2d4cdd32c948f3811fe3866c7eacdb2 Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Wed, 23 Feb 2022 16:28:11 +0530 Subject: [PATCH] Resource id attribute added for the resources in the base class Signed-off-by: Nikita Mathur --- lib/inspec/resource.rb | 6 ++++++ test/unit/resource_test.rb | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/inspec/resource.rb b/lib/inspec/resource.rb index 4a9364b7a..0fe49745e 100644 --- a/lib/inspec/resource.rb +++ b/lib/inspec/resource.rb @@ -34,6 +34,12 @@ module Inspec Inspec::Resource.support_registry[key].push(criteria) end + def self.resource_id(value = nil) + return (find_class_instance_variable(:@resource_id) || "") unless value + + @resource_id = value + end + # TODO: this is pretty terrible and is only here to work around # the idea that we've trained resource authors to make initialize # methods w/o calling super. diff --git a/test/unit/resource_test.rb b/test/unit/resource_test.rb index 359cff492..52d068913 100644 --- a/test/unit/resource_test.rb +++ b/test/unit/resource_test.rb @@ -44,12 +44,12 @@ describe Inspec::Resource do end describe "#example" do - it "will register a description" do + it "will register a example" do expected = rand.to_s _(create { example expected }.example).must_equal expected end - it "can change the description" do + it "can change the example" do c = create { example rand.to_s } c.example(x = rand.to_s) _(c.example).must_equal x @@ -94,4 +94,17 @@ describe Inspec::Resource do Inspec::Resource.support_registry["os"] = nil end end + + describe "resource_id" do + it "will register a resource_id" do + expected = rand.to_s + _(create { resource_id expected }.resource_id).must_equal expected + end + + it "can change the resource_id" do + c = create { resource_id rand.to_s } + c.resource_id(x = rand.to_s) + _(c.resource_id).must_equal x + end + end end