From 6a574b60042865f37ed418fa52780ddd532b37c6 Mon Sep 17 00:00:00 2001 From: Kevin Paulisse Date: Sat, 14 Jan 2017 10:31:31 -0600 Subject: [PATCH] Construct fact override object in API --- lib/octocatalog-diff/api/v1.rb | 1 + lib/octocatalog-diff/api/v1/fact_override.rb | 22 +++++ lib/octocatalog-diff/cli.rb | 11 +-- lib/octocatalog-diff/cli/fact_override.rb | 93 ++++++++++++++++++ .../cli/helpers/fact_override.rb | 98 ------------------- .../tests/catalog-util/builddir_spec.rb | 4 +- .../cli/{helpers => }/fact_override_spec.rb | 96 +++++++++--------- spec/octocatalog-diff/tests/cli_spec.rb | 7 +- 8 files changed, 175 insertions(+), 157 deletions(-) create mode 100644 lib/octocatalog-diff/api/v1/fact_override.rb create mode 100644 lib/octocatalog-diff/cli/fact_override.rb delete mode 100644 lib/octocatalog-diff/cli/helpers/fact_override.rb rename spec/octocatalog-diff/tests/cli/{helpers => }/fact_override_spec.rb (67%) diff --git a/lib/octocatalog-diff/api/v1.rb b/lib/octocatalog-diff/api/v1.rb index 5c29aba7..7933088d 100644 --- a/lib/octocatalog-diff/api/v1.rb +++ b/lib/octocatalog-diff/api/v1.rb @@ -5,6 +5,7 @@ require_relative 'v1/catalog-diff' require_relative 'v1/config' require_relative 'v1/diff' +require_relative 'v1/fact_override' module OctocatalogDiff module API diff --git a/lib/octocatalog-diff/api/v1/fact_override.rb b/lib/octocatalog-diff/api/v1/fact_override.rb new file mode 100644 index 00000000..4a168269 --- /dev/null +++ b/lib/octocatalog-diff/api/v1/fact_override.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'json' + +module OctocatalogDiff + module API + module V1 + # Sets up the override of a fact during catalog compilation. + class FactOverride + # Accessors + attr_reader :key, :value + + # Constructor: Accepts a key and value. + # @param input [Hash] Must contain :key and :value + def initialize(input) + @key = input.fetch(:key) + @value = input.fetch(:value) + end + end + end + end +end diff --git a/lib/octocatalog-diff/cli.rb b/lib/octocatalog-diff/cli.rb index 459f4538..70603a78 100644 --- a/lib/octocatalog-diff/cli.rb +++ b/lib/octocatalog-diff/cli.rb @@ -2,14 +2,13 @@ require_relative 'api/v1' require_relative 'catalog-util/cached_master_directory' -require_relative 'errors' -require_relative 'util/catalogs' -require_relative 'version' - require_relative 'cli/diffs' +require_relative 'cli/fact_override' require_relative 'cli/options' require_relative 'cli/printer' -require_relative 'cli/helpers/fact_override' +require_relative 'errors' +require_relative 'util/catalogs' +require_relative 'version' require 'logger' require 'socket' @@ -137,7 +136,7 @@ def self.setup_fact_overrides(options) next unless o.is_a?(Array) next unless o.any? options[key] ||= [] - options[key].concat o.map { |x| OctocatalogDiff::Cli::Helpers::FactOverride.new(x) } + options[key].concat o.map { |x| OctocatalogDiff::Cli::FactOverride.fact_override(x) } end end diff --git a/lib/octocatalog-diff/cli/fact_override.rb b/lib/octocatalog-diff/cli/fact_override.rb new file mode 100644 index 00000000..8ee7e3e1 --- /dev/null +++ b/lib/octocatalog-diff/cli/fact_override.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require 'json' + +require_relative '../api/v1' + +module OctocatalogDiff + class Cli + # Helper methods for fact override parsing. + class FactOverride + # Public: Given an input string, construct the fact override object(s). + # + # @param input [String] Input in the format: key=(data type)value + # @return [OctocatalogDiff::API::V1::FactOverride] Constructed override object + def self.fact_override(input, key = nil) + # Normally the input will be a string in the format key=(data type)value where the data + # type is optional and the parentheses are literal. Example: + # foo=1 (auto-determine data type - in this case it would be a fixnum) + # foo=(fixnum)1 (will be a fixnum) + # foo=(string)1 (will be '1' the string) + # If input is not a string, we can still construct the object if the key is given. + # That input would come directly from code and not from the command line, since inputs + # from the command line are always strings. + if key.nil? && input.is_a?(String) + unless input.include?('=') + raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format" + end + key, raw_value = input.strip.split('=', 2) + value = parsed_value(raw_value) + OctocatalogDiff::API::V1::FactOverride.new(key: key, value: value) + elsif key.nil? + message = "Define a key when the input is not a string (#{input.class} => #{input.inspect})" + raise ArgumentError, message + else + OctocatalogDiff::API::V1::FactOverride.new(key: key, value: input) + end + end + + # Guess the datatype from a particular input + # @param input [String] Input in string format + # @return [?] Output in appropriate format + def self.parsed_value(input) + # If data type is explicitly given + if input =~ /^\((\w+)\)(.*)$/m + datatype = Regexp.last_match(1) + value = Regexp.last_match(2) + return convert_to_data_type(datatype.downcase, value) + end + + # Guess data type + return input.to_i if input =~ /^-?\d+$/ + return input.to_f if input =~ /^-?\d*\.\d+$/ + return true if input.casecmp('true').zero? + return false if input.casecmp('false').zero? + input + end + + # Handle data type that's explicitly given + # @param datatype [String] Data type (as a string) + # @param value [String] Value given + # @return [?] Value converted to specified data type + def self.convert_to_data_type(datatype, value) + return value if datatype == 'string' + return parse_json(value) if datatype == 'json' + return nil if datatype == 'nil' + if datatype == 'fixnum' + return Regexp.last_match(1).to_i if value =~ /^(-?\d+)$/ + raise ArgumentError, "Illegal fixnum '#{value}'" + end + if datatype == 'float' + return Regexp.last_match(1).to_f if value =~ /^(-?\d*\.\d+)$/ + return Regexp.last_match(1).to_f if value =~ /^(-?\d+)$/ + raise ArgumentError, "Illegal float '#{value}'" + end + if datatype == 'boolean' + return true if value.casecmp('true').zero? + return false if value.casecmp('false').zero? + raise ArgumentError, "Illegal boolean '#{value}'" + end + raise ArgumentError, "Unknown data type '#{datatype}'" + end + + # Parse JSON value + # @param input [String] Input, hopefully in JSON format + # @return [?] Output data structure + def self.parse_json(input) + JSON.parse(input) + rescue JSON::ParserError => exc + raise JSON::ParserError, "Failed to parse JSON: input=#{input} error=#{exc}" + end + end + end +end diff --git a/lib/octocatalog-diff/cli/helpers/fact_override.rb b/lib/octocatalog-diff/cli/helpers/fact_override.rb deleted file mode 100644 index dccbe568..00000000 --- a/lib/octocatalog-diff/cli/helpers/fact_override.rb +++ /dev/null @@ -1,98 +0,0 @@ -# frozen_string_literal: true - -require 'json' - -module OctocatalogDiff - class Cli - class Helpers - # Since input from the command line is in the format of a string, this class helps to guess - # at the data type. - class FactOverride - # Accessors - attr_reader :key, :value - - # Constructor: Input will be a string (since it comes from command line). - # This code will make a best guess at the data type (or use a supplied data type if any). - # @param input [String] Input in the format: key=(data type)value - def initialize(input, key = nil) - # Normally the input will be a string in the format key=(data type)value where the data - # type is optional and the parentheses are literal. Example: - # foo=1 (auto-determine data type - in this case it would be a fixnum) - # foo=(fixnum)1 (will be a fixnum) - # foo=(string)1 (will be '1' the string) - # If input is not a string, we can still construct the object if the key is given. - # That input would come directly from code and not from the command line, since inputs - # from the command line are always strings. - if input.is_a?(String) - unless input.include?('=') - raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format" - end - @key, raw_value = input.strip.split('=', 2) - @value = parsed_value(raw_value) - elsif key.nil? - message = "Define a key when the input is not a string (#{input.class} => #{input.inspect})" - raise ArgumentError, message - else - @key = key - @value = input - end - end - - private - - # Guess the datatype from a particular input - # @param input [String] Input in string format - # @return [?] Output in appropriate format - def parsed_value(input) - # If data type is explicitly given - if input =~ /^\((\w+)\)(.*)$/m - datatype = Regexp.last_match(1) - value = Regexp.last_match(2) - return convert_to_data_type(datatype.downcase, value) - end - - # Guess data type - return input.to_i if input =~ /^-?\d+$/ - return input.to_f if input =~ /^-?\d*\.\d+$/ - return true if input.casecmp('true').zero? - return false if input.casecmp('false').zero? - input - end - - # Handle data type that's explicitly given - # @param datatype [String] Data type (as a string) - # @param value [String] Value given - # @return [?] Value converted to specified data type - def convert_to_data_type(datatype, value) - return value if datatype == 'string' - return parse_json(value) if datatype == 'json' - return nil if datatype == 'nil' - if datatype == 'fixnum' - return Regexp.last_match(1).to_i if value =~ /^(-?\d+)$/ - raise ArgumentError, "Illegal fixnum '#{value}'" - end - if datatype == 'float' - return Regexp.last_match(1).to_f if value =~ /^(-?\d*\.\d+)$/ - return Regexp.last_match(1).to_f if value =~ /^(-?\d+)$/ - raise ArgumentError, "Illegal float '#{value}'" - end - if datatype == 'boolean' - return true if value.casecmp('true').zero? - return false if value.casecmp('false').zero? - raise ArgumentError, "Illegal boolean '#{value}'" - end - raise ArgumentError, "Unknown data type '#{datatype}'" - end - - # Parse JSON value - # @param input [String] Input, hopefully in JSON format - # @return [?] Output data structure - def parse_json(input) - JSON.parse(input) - rescue JSON::ParserError => exc - raise JSON::ParserError, "Failed to parse JSON: input=#{input} error=#{exc}" - end - end - end - end -end diff --git a/spec/octocatalog-diff/tests/catalog-util/builddir_spec.rb b/spec/octocatalog-diff/tests/catalog-util/builddir_spec.rb index caff7a1c..6ffc469b 100644 --- a/spec/octocatalog-diff/tests/catalog-util/builddir_spec.rb +++ b/spec/octocatalog-diff/tests/catalog-util/builddir_spec.rb @@ -3,7 +3,7 @@ require_relative '../spec_helper' require OctocatalogDiff::Spec.require_path('/facts') require OctocatalogDiff::Spec.require_path('/catalog-util/builddir') -require OctocatalogDiff::Spec.require_path('/cli/helpers/fact_override') +require OctocatalogDiff::Spec.require_path('/cli/fact_override') require 'socket' require 'yaml' @@ -531,7 +531,7 @@ context 'with fact overrides' do it 'should create and populate the fact file' do overrides_raw = %w(ipaddress=10.30.50.70 fizz=buzz jsontest=(json){"foo":"bar"}) - overrides = overrides_raw.map { |x| OctocatalogDiff::Cli::Helpers::FactOverride.new(x) } + overrides = overrides_raw.map { |x| OctocatalogDiff::Cli::FactOverride.fact_override(x) } options = { basedir: OctocatalogDiff::Spec.fixture_path('repos/default'), fact_file: OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'), diff --git a/spec/octocatalog-diff/tests/cli/helpers/fact_override_spec.rb b/spec/octocatalog-diff/tests/cli/fact_override_spec.rb similarity index 67% rename from spec/octocatalog-diff/tests/cli/helpers/fact_override_spec.rb rename to spec/octocatalog-diff/tests/cli/fact_override_spec.rb index 72e463ef..5b41321e 100644 --- a/spec/octocatalog-diff/tests/cli/helpers/fact_override_spec.rb +++ b/spec/octocatalog-diff/tests/cli/fact_override_spec.rb @@ -1,59 +1,59 @@ # frozen_string_literal: true -require_relative '../../spec_helper' +require_relative '../spec_helper' -require OctocatalogDiff::Spec.require_path('/cli/helpers/fact_override') +require OctocatalogDiff::Spec.require_path('/cli/fact_override') require 'json' -describe OctocatalogDiff::Cli::Helpers::FactOverride do +describe OctocatalogDiff::Cli::FactOverride do context 'with faulty input' do - describe '#new' do + describe '#fact_override' do it 'should raise ArgumentError when string lacks an =' do arg = 'dkfjladksfjaldfjdslkfjads' expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(ArgumentError, /Fact override.*is not in 'key=\(data type\)value' format/) end it 'should raise ArgumentError when non-string lacks a key' do arg = ['dkfjladksfjaldfjdslkfjads'] expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(ArgumentError, /Define a key when the input is not a string/) end it 'should raise JSON::ParserError if JSON fails to parse' do arg = 'key=(json){akdsfjalsdkfjasdf}' expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(JSON::ParserError, /unexpected token at '\{akdsfjalsdkfjasdf\}'/) end it 'should raise ArgumentError when unrecognized data type is specified' do arg = 'key=(chicken)blahblah' expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(ArgumentError, /Unknown data type 'chicken'/) end it 'should raise ArgumentError when boolean is specified but not supplied' do arg = 'key=(boolean)blahblah' expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(ArgumentError, /Illegal boolean 'blahblah'/) end it 'should raise ArgumentError when fixnum is specified but not supplied' do arg = 'key=(fixnum)blahblah' expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(ArgumentError, /Illegal fixnum 'blahblah'/) end it 'should raise ArgumentError when float is specified but not supplied' do arg = 'key=(float)blahblah' expect do - _foo = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + _foo = OctocatalogDiff::Cli::FactOverride.fact_override(arg) end.to raise_error(ArgumentError, /Illegal float 'blahblah'/) end end @@ -62,116 +62,116 @@ context 'with proper typed input' do describe '#new' do it 'should pass through a string' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(string)chicken') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(string)chicken') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('chicken') end it 'should pass through an empty string' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(string)') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(string)') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('') end it 'should pass through a multi-line string' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new("foo=(string)foo\nbar") + testobj = OctocatalogDiff::Cli::FactOverride.fact_override("foo=(string)foo\nbar") expect(testobj.key).to eq('foo') expect(testobj.value).to eq("foo\nbar") end it 'should pass through a positive integer' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(fixnum)42') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(fixnum)42') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(42) end it 'should pass through a negative integer' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(fixnum)-42') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(fixnum)-42') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(-42) end it 'should pass through a positive float' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(float)42.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(float)42.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(42.15) end it 'should pass through a negative float' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(float)-42.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(float)-42.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(-42.15) end it 'should pass through a positive float with no leading digit' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(float).15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(float).15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(0.15) end it 'should pass through a negative float with no leading digit' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(float)-.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(float)-.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(-0.15) end it 'should handle true boolean' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(boolean)true') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(boolean)true') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(true) end it 'should handle true boolean case-insensitive' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(boolean)TrUe') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(boolean)TrUe') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(true) end it 'should handle false boolean' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(boolean)false') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(boolean)false') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(false) end it 'should handle false boolean case-insensitive' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(boolean)FaLsE') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(boolean)FaLsE') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(false) end it 'should parse JSON' do arg = 'foo=(json){"bar":"baz"}' - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg) + testobj = OctocatalogDiff::Cli::FactOverride.fact_override(arg) expect(testobj.key).to eq('foo') expect(testobj.value).to eq('bar' => 'baz') end it 'should return string even when input looks like a number' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(string)42') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(string)42') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('42') end it 'should return string even when input looks like a float' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(string)42.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(string)42.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('42.15') end it 'should return string even when input looks like a boolean' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(string)true') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(string)true') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('true') end it 'shouuld pass through a nil with no argument' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(nil)') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(nil)') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(nil) end it 'should pass through a nil with superfluous argument' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=(nil)blahblahblah') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=(nil)blahblahblah') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(nil) end @@ -181,79 +181,79 @@ context 'with proper guessed string input' do describe '#new' do it 'should pass through a string' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=chicken') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=chicken') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('chicken') end it 'should pass through an empty string' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=') expect(testobj.key).to eq('foo') expect(testobj.value).to eq('') end it 'should pass through a multi-line string' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new("foo=foo\nbar") + testobj = OctocatalogDiff::Cli::FactOverride.fact_override("foo=foo\nbar") expect(testobj.key).to eq('foo') expect(testobj.value).to eq("foo\nbar") end it 'should pass through a positive integer' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=42') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=42') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(42) end it 'should pass through a negative integer' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=-42') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=-42') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(-42) end it 'should pass through a positive float' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=42.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=42.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(42.15) end it 'should pass through a negative float' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=-42.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=-42.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(-42.15) end it 'should pass through a positive float with no leading digit' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(0.15) end it 'should pass through a negative float with no leading digit' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=-.15') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=-.15') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(-0.15) end it 'should handle true boolean' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=true') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=true') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(true) end it 'should handle true boolean case-insensitive' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=TrUe') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=TrUe') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(true) end it 'should handle false boolean' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=false') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=false') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(false) end it 'should handle false boolean case-insensitive' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new('foo=FaLsE') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override('foo=FaLsE') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(false) end @@ -263,32 +263,32 @@ context 'with non-string input' do describe '#new' do it 'should pass through a fixnum without manipulation' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new(42, 'foo') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override(42, 'foo') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(42) end it 'should pass through a float without manipulation' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new(42.15, 'foo') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override(42.15, 'foo') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(42.15) end it 'should pass through a boolean without manipulation' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new(false, 'foo') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override(false, 'foo') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(false) end it 'should pass through a nil without manipulation' do - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new(nil, 'foo') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override(nil, 'foo') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(nil) end it 'should pass through a hash without manipulation' do arg = { 'foo' => 'bar', 'baz' => [1, 2, 3, 4, 5] } - testobj = OctocatalogDiff::Cli::Helpers::FactOverride.new(arg, 'foo') + testobj = OctocatalogDiff::Cli::FactOverride.fact_override(arg, 'foo') expect(testobj.key).to eq('foo') expect(testobj.value).to eq(arg) end diff --git a/spec/octocatalog-diff/tests/cli_spec.rb b/spec/octocatalog-diff/tests/cli_spec.rb index c2fc227f..8f3ff8cf 100644 --- a/spec/octocatalog-diff/tests/cli_spec.rb +++ b/spec/octocatalog-diff/tests/cli_spec.rb @@ -2,6 +2,7 @@ require_relative 'spec_helper' +require OctocatalogDiff::Spec.require_path('/api/v1') require OctocatalogDiff::Spec.require_path('/cli') require OctocatalogDiff::Spec.require_path('/errors') @@ -182,7 +183,7 @@ expect(options[:from_fact_override]).to be_a_kind_of(Array) expect(options[:from_fact_override].size).to eq(1) ffo = options[:from_fact_override].first - expect(ffo).to be_a_kind_of(OctocatalogDiff::Cli::Helpers::FactOverride) + expect(ffo).to be_a_kind_of(OctocatalogDiff::API::V1::FactOverride) expect(ffo.key).to eq('foo') expect(ffo.value).to eq('bar') end @@ -194,10 +195,10 @@ expect(options[:to_fact_override].size).to eq(2) tfo = options[:to_fact_override] - expect(tfo[0]).to be_a_kind_of(OctocatalogDiff::Cli::Helpers::FactOverride) + expect(tfo[0]).to be_a_kind_of(OctocatalogDiff::API::V1::FactOverride) expect(tfo[0].key).to eq('foo') expect(tfo[0].value).to eq('bar') - expect(tfo[1]).to be_a_kind_of(OctocatalogDiff::Cli::Helpers::FactOverride) + expect(tfo[1]).to be_a_kind_of(OctocatalogDiff::API::V1::FactOverride) expect(tfo[1].key).to eq('baz') expect(tfo[1].value).to eq('buzz') end