Skip to content

Commit

Permalink
Control Header Object (#5)
Browse files Browse the repository at this point in the history
- inital support for adding headers to controls
- simplifed logic for adding the header per peer review
- implementation is a basic string block
- Setup unit tests and linting for project
- added Bundle and added cookstyle
- ran the rake unit tests

Signed-off-by: Aaron Lippold <[email protected]>
  • Loading branch information
aaronlippold authored Feb 8, 2022
1 parent e6d1075 commit 59144fa
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Gemfile.lock

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.vscode/*
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "inspec-objects", path: "."

group :test do
gem "chef-utils", "~> 16.6.14"
gem "chefstyle", "0.13.0"
gem "minitest", "~> 5.5"
gem "rake", ">= 10"
gem "chef-utils", "~> 16.6.14"
end
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler"
require "rake/testtask"

Expand All @@ -6,8 +8,8 @@ Bundler::GemHelper.install_tasks name: "inspec-objects"
Rake::TestTask.new(:unit) do |t|
t.libs << "test"
t.test_files = Dir.glob([
"test/unit/**/*_test.rb",
])
"test/unit/**/*_test.rb",
])
end

begin
Expand Down
2 changes: 2 additions & 0 deletions inspec-objects.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Expand Down
2 changes: 2 additions & 0 deletions lib/inspec-objects.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

require "inspec-objects/version"
require_relative "inspec/objects"
4 changes: 3 additions & 1 deletion lib/inspec-objects/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module InspecObjects
VERSION = "0.1.0".freeze
VERSION = "0.1.0"
end
3 changes: 3 additions & 0 deletions lib/inspec/objects.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec
module Object
require_relative "objects/tag"
Expand All @@ -10,5 +12,6 @@ module Object
require_relative "objects/or_test"
require_relative "objects/test"
require_relative "objects/input"
require_relative "objects/header"
end
end
16 changes: 13 additions & 3 deletions lib/inspec/objects/control.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# frozen_string_literal: true

module Inspec::Object
class Control
attr_accessor :id, :title, :descriptions, :impact, :tests, :tags, :refs, :only_if
attr_accessor :header, :id, :title, :descriptions, :impact, :tests, :tags, :refs, :only_if
def initialize
@header = ""
@tests = []
@tags = []
@refs = []
@descriptions = {}
end

def add_header(header)
@header = header
end

def add_test(t)
@tests.push(t)
end
Expand All @@ -18,6 +25,7 @@ def add_tag(t)

def to_hash
{
header: header,
id: id,
title: title,
descriptions: descriptions,
Expand All @@ -27,8 +35,10 @@ def to_hash
}
end

def to_ruby # rubocop:disable Metrics/AbcSize
res = ["control #{id.inspect} do"]
def to_ruby
res = []
res.push header unless header.nil? || header.empty?
res.push "control #{id.inspect} do"
res.push " title #{title.inspect}" unless title.to_s.empty?
descriptions.each do |label, text|
if label == :default
Expand Down
14 changes: 8 additions & 6 deletions lib/inspec/objects/describe.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class Describe
# Internal helper to structure test objects.
Expand All @@ -11,11 +13,11 @@ def negate!
def to_ruby
itsy = "it"
unless its.nil?
if its.is_a? Array
itsy = "its(" + its.inspect + ")"
else
itsy = "its(" + its.to_s.inspect + ")"
end
itsy = if its.is_a? Array
"its(" + its.inspect + ")"
else
"its(" + its.to_s.inspect + ")"
end
end
naughty = negated ? "_not" : ""
xpect = if expectation.nil?
Expand Down Expand Up @@ -72,7 +74,7 @@ def to_hash
end

def resource
return nil if qualifier.empty? || qualifier[0].empty? || qualifier[0][0].empty?
return if qualifier.empty? || qualifier[0].empty? || qualifier[0][0].empty?

qualifier[0][0]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/each_loop.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class EachLoop < List
attr_reader :variables
Expand Down
25 changes: 25 additions & 0 deletions lib/inspec/objects/header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Inspec::Object
class Header
attr_accessor :header

def initialize(header)
@header = header
end

def to_hash
{
header: header,
}
end

def to_ruby
header.inspect.to_s
end

def to_s
"Header #{header}"
end
end
end
3 changes: 2 additions & 1 deletion lib/inspec/objects/input.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require "inspec/input"

module Inspec::Object
class Input < ::Inspec::Input

# NOTE: No initialize method or accessors for the reasons listed above

#--------------------------------------------------------------------------#
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class List < Value
def map
Expand Down
5 changes: 3 additions & 2 deletions lib/inspec/objects/or_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class OrTest
attr_reader :tests
Expand All @@ -18,12 +20,11 @@ def to_ruby
if @negated
# We don't use the describe.one wrapper when negated because:
# !(test1 || test2) same as (!test1 && !test2) where && is implicit in inspec
all_tests = @tests.map do |test|
@tests.map do |test|
test.negate!
test
end.map(&:to_ruby).join("\n")

all_tests
else
all_tests = @tests.map(&:to_ruby).join("\n").gsub("\n", "\n ")

Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/ruby_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
module RubyHelper
def ruby_qualifier(q)
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class Tag
attr_accessor :key, :value
Expand Down
6 changes: 4 additions & 2 deletions lib/inspec/objects/test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class Test
attr_accessor :qualifier, :matcher, :expectation, :skip, :negated, :variables, :only_if
Expand Down Expand Up @@ -39,7 +41,7 @@ def remove_expectation
private

def describe_chain
return nil if @qualifier.empty?
return if @qualifier.empty?

resource = @qualifier.length > 1 ? @qualifier[0..-2] : [@qualifier[0]]
res = resource.map { |q| ruby_qualifier(q) }.join(".")
Expand Down Expand Up @@ -75,7 +77,7 @@ def rb_describe
" " + expectation.inspect
end
format("%s%sdescribe %s do\n %s { should%s %s%s }\nend",
only_if_clause, vars, res, itsy, naughty, matcher, xpect)
only_if_clause, vars, res, itsy, naughty, matcher, xpect)
end

def rb_skip
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/value.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class Value
include ::Inspec::Object::RubyHelper
Expand Down
2 changes: 2 additions & 0 deletions test/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require "minitest/autorun"
37 changes: 36 additions & 1 deletion test/unit/objects_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "spec_helper"
require "inspec-objects"

Expand Down Expand Up @@ -468,6 +470,7 @@
'.strip

control_hash = {
header: "",
id: "tag.control.id",
title: nil,
descriptions: {},
Expand All @@ -485,6 +488,38 @@
end
end

describe "Inspec::Object::Tag" do
it "constructs a control with a company header" do
control = Inspec::Object::Control.new
res1 = { name: "key", value: "value" }
tag1 = Inspec::Object::Tag.new(res1[:name], res1[:value])
_(tag1.to_hash).must_equal res1
control.add_header("# my company header")
control.id = "tag.control.id"
control.add_tag(tag1)
_(control.to_ruby).must_equal '
# my company header
control "tag.control.id" do
tag key: "value"
end
'.strip

control_hash = {
header: "# my company header",
id: "tag.control.id",
title: nil,
descriptions: {},
impact: nil,
tests: [],
tags: [{
name: "key",
value: "value",
}],
}
_(control.to_hash).must_equal control_hash
end
end

describe "Inspec::Object::Input" do
describe "to_ruby method" do
it "generates the code for the input" do
Expand All @@ -510,7 +545,7 @@
end
end

# TODO - deprecate this, not sure it is used
# TODO: - deprecate this, not sure it is used
describe "to_hash method" do
it "generates a similar hash" do
ipt = Inspec::Object::Input.new(
Expand Down

0 comments on commit 59144fa

Please sign in to comment.