diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/provider/augeas/augeas_spec.rb | 65 | ||||
-rw-r--r-- | spec/unit/puppet_x/augeas/util/parser_spec.rb | 110 |
2 files changed, 175 insertions, 0 deletions
diff --git a/spec/unit/provider/augeas/augeas_spec.rb b/spec/unit/provider/augeas/augeas_spec.rb index 6166140..180f89c 100644 --- a/spec/unit/provider/augeas/augeas_spec.rb +++ b/spec/unit/provider/augeas/augeas_spec.rb @@ -262,6 +262,49 @@ describe Puppet::Type.type(:augeas).provider(:augeas) do command = ['values', 'fake value', "== ['set', 'of', 'values']"] expect(provider.process_values(command)).to eq(true) end + it 'returns true for an array match with double quotes and spaces' do + command = ['values', 'fake value', '== [ "set" , "of" , "values" ] '] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with internally escaped single quotes' do + provider.aug.stubs(:match).returns(['set', "o'values", 'here']) + provider.aug.stubs(:get).returns('set').then.returns("o'values").then.returns('here') + command = ['values', 'fake value', "== [ 'set', 'o\\'values', 'here']"] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with octal character sequences' do + command = ['values', 'fake value', '== ["\\x73et", "of", "values"]'] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with hex character sequences' do + command = ['values', 'fake value', '== ["\\163et", "of", "values"]'] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with short unicode escape sequences' do + command = ['values', 'fake value', '== ["\\u0073et", "of", "values"]'] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with single character long unicode escape sequences' do + command = ['values', 'fake value', '== ["\\u{0073}et", "of", "values"]'] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with multi-character long unicode escape sequences' do + command = ['values', 'fake value', '== ["\\u{0073 0065 0074}", "of", "values"]'] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an array match with literal backslashes' do + provider.aug.stubs(:match).returns(['set', 'o\\values', 'here']) + provider.aug.stubs(:get).returns('set').then.returns('o\\values').then.returns('here') + command = ['values', 'fake value', '== [ "set", "o\\\\values", "here"]'] + expect(provider.process_values(command)).to eq(true) + end it 'returns false for an array non match' do command = ['values', 'fake value', "== ['this', 'should', 'not', 'match']"] @@ -277,6 +320,18 @@ describe Puppet::Type.type(:augeas).provider(:augeas) do command = ['values', 'fake value', "!= ['this', 'should', 'not', 'match']"] expect(provider.process_values(command)).to eq(true) end + + it 'returns true for an array non match with double quotes and spaces' do + command = ['values', 'fake value', '!= [ "this" , "should" ,"not", "match" ] '] + expect(provider.process_values(command)).to eq(true) + end + + it 'returns true for an empty array match' do + provider.aug.stubs(:match).returns([]) + provider.aug.stubs(:get) + command = ['values', 'fake value', '== []'] + expect(provider.process_values(command)).to eq(true) + end end describe 'match filters' do @@ -322,6 +377,11 @@ describe Puppet::Type.type(:augeas).provider(:augeas) do expect(provider.process_match(command)).to eq(true) end + it 'returns true for an array match with double quotes and spaces' do + command = ['match', 'fake value', '== [ "set" , "of" , "values" ] '] + expect(provider.process_match(command)).to eq(true) + end + it 'returns false for an array non match' do command = ['match', 'fake value', "== ['this', 'should', 'not', 'match']"] expect(provider.process_match(command)).to eq(false) @@ -336,6 +396,11 @@ describe Puppet::Type.type(:augeas).provider(:augeas) do command = ['match', 'fake value', "!= ['this', 'should', 'not', 'match']"] expect(provider.process_match(command)).to eq(true) end + + it 'returns true for an array non match with double quotes and spaces' do + command = ['match', 'fake value', '!= [ "this" , "should" ,"not", "match" ] '] + expect(provider.process_match(command)).to eq(true) + end end describe 'need to run' do diff --git a/spec/unit/puppet_x/augeas/util/parser_spec.rb b/spec/unit/puppet_x/augeas/util/parser_spec.rb new file mode 100644 index 0000000..f8b5b2b --- /dev/null +++ b/spec/unit/puppet_x/augeas/util/parser_spec.rb @@ -0,0 +1,110 @@ +require 'spec_helper' +require 'puppet_x/augeas/util/parser' + +describe PuppetX::Augeas::Util::Parser do + include described_class + + it 'handles an empty array' do + expect(parse_to_array('[]')).to eq([]) + end + + it 'handles an array with a simple single-quoted entry' do + expect(parse_to_array("['entry']")).to eq(['entry']) + end + + it 'handles an array with a simple double-quoted entry' do + expect(parse_to_array('["entry"]')).to eq(['entry']) + end + + it 'handles an array with both single- and double-quoted entries' do + expect(parse_to_array(%q(['first', "second"]))).to eq(['first', 'second']) + end + + context 'inside single-quoted strings' do + it 'allows a literal backslash' do + expect(parse_to_array("['entry\\\\here']")).to eq(['entry\\here']) + end + + it 'allows an internal single-quote' do + expect(parse_to_array("['entry\\'here']")).to eq(['entry\'here']) + end + end + + context 'inside double-quoted strings' do + it 'allows a literal backslash' do + expect(parse_to_array('["entry\\\\here"]')).to eq(['entry\\here']) + end + + it 'allows an internal double-quote' do + expect(parse_to_array('["entry\\"here"]')).to eq(['entry"here']) + end + + it 'does not require escaping a single-quote' do + expect(parse_to_array('["entry\'here"]')).to eq(["entry'here"]) + end + + it 'allows a bell character escape' do + expect(parse_to_array('["entry\\ahere"]')).to eq(["entry\ahere"]) + end + + it 'allows a backspace character escape' do + expect(parse_to_array('["entry\\bhere"]')).to eq(["entry\bhere"]) + end + + it 'allows a horizontal tab character escape' do + expect(parse_to_array('["entry\\there"]')).to eq(["entry\there"]) + end + + it 'allows a line feed character escape' do + expect(parse_to_array('["entry\\nhere"]')).to eq(["entry\nhere"]) + end + + it 'allows a vertical tab character escape' do + expect(parse_to_array('["entry\\vhere"]')).to eq(["entry\vhere"]) + end + + it 'allows a form feed character escape' do + expect(parse_to_array('["entry\\fhere"]')).to eq(["entry\fhere"]) + end + + it 'allows a carriage return character escape' do + expect(parse_to_array('["entry\\rhere"]')).to eq(["entry\rhere"]) + end + + it 'allows an escape character escape' do + expect(parse_to_array('["entry\\ehere"]')).to eq(["entry\ehere"]) + end + + it 'allows a space character escape' do + expect(parse_to_array('["entry\\shere"]')).to eq(['entry here']) + end + + it 'allows octal character escapes' do + expect(parse_to_array('["\7", "\41", "\101", "\1411"]')).to eq(["\a", '!', 'A', 'a1']) + end + + it 'allows hexadecimal character escapes with \\x' do + expect(parse_to_array('["\x7", "\x21", "\x211"]')).to eq(["\a", '!', '!1']) + end + + it 'allows single-character unicode hexadecimal character escapes with \\u' do + expect(parse_to_array('["\u2015", "\u20222"]')).to eq(["\u2015", "\u2022" << '2']) + end + + it 'allows multi-character unicode hexadecimal character escapes with \\u{...}' do + expect(parse_to_array('["\u{7}", "\u{20}", "\u{100}", "\u{2026}", "\u{1F464}", "\u{100000}", "\u{53 74 72 69 6E 67}"]')).to eq(["\a", ' ', "\u{100}", "\u{2026}", "\u{1F464}", "\u{100000}", 'String']) + end + end + + it 'fails with garbage in front of the array' do + expect { parse_to_array("junk ['good', 'array', 'here']") }.to raise_error(RuntimeError, %r{^Unexpected character in array at: junk \['good}) + end + + it 'fails with garbage in the middle of the array' do + expect { parse_to_array("['got', 'some', junk 'here']") }.to raise_error(RuntimeError, %r{^Unexpected character in array at: junk 'here'}) + end + + it 'fails with garbage after the array' do + expect { parse_to_array("['good', 'array', 'here'] junk after") }.to raise_error(RuntimeError, %r{^Unexpected character in array at: junk after}) + end +end |