diff options
Diffstat (limited to 'spec/unit/face/parser_spec.rb')
-rw-r--r-- | spec/unit/face/parser_spec.rb | 104 |
1 files changed, 71 insertions, 33 deletions
diff --git a/spec/unit/face/parser_spec.rb b/spec/unit/face/parser_spec.rb index 9b1b07a47..e9ba07a2d 100644 --- a/spec/unit/face/parser_spec.rb +++ b/spec/unit/face/parser_spec.rb @@ -8,58 +8,96 @@ describe Puppet::Face[:parser, :current] do let(:parser) { Puppet::Face[:parser, :current] } - context "from an interactive terminal" do - before :each do - from_an_interactive_terminal + context "validate" do + context "from an interactive terminal" do + before :each do + from_an_interactive_terminal + end + + it "validates the configured site manifest when no files are given" do + manifest = file_containing('site.pp', "{ invalid =>") + + configured_environment = Puppet::Node::Environment.create(:default, [], manifest) + Puppet.override(:current_environment => configured_environment) do + expect { parser.validate() }.to exit_with(1) + end + end + + it "validates the given file" do + manifest = file_containing('site.pp', "{ invalid =>") + + expect { parser.validate(manifest) }.to exit_with(1) + end + + it "runs error free when there are no validation errors" do + manifest = file_containing('site.pp', "notify { valid: }") + + parser.validate(manifest) + end + + it "reports missing files" do + expect do + parser.validate("missing.pp") + end.to raise_error(Puppet::Error, /One or more file\(s\) specified did not exist.*missing\.pp/m) + end + + it "parses supplied manifest files in the context of a directory environment" do + manifest = file_containing('test.pp', "{ invalid =>") + + env = Puppet::Node::Environment.create(:special, []) + env_loader = Puppet::Environments::Static.new(env) + Puppet.override({:environments => env_loader, :current_environment => env}) do + expect { parser.validate(manifest) }.to exit_with(1) + end + + expect(@logs.join).to match(/environment special.*Syntax error at '\{'/) + end + end - it "validates the configured site manifest when no files are given" do - manifest = file_containing('site.pp', "{ invalid =>") + it "validates the contents of STDIN when no files given and STDIN is not a tty" do + from_a_piped_input_of("{ invalid =>") - configured_environment = Puppet::Node::Environment.create(:default, [], manifest) - Puppet.override(:current_environment => configured_environment) do + Puppet.override(:current_environment => Puppet::Node::Environment.create(:special, [])) do expect { parser.validate() }.to exit_with(1) end end + end - it "validates the given file" do - manifest = file_containing('site.pp', "{ invalid =>") - - expect { parser.validate(manifest) }.to exit_with(1) + context "dump" do + it "prints the AST of the passed expression" do + expect(parser.dump({ :e => 'notice hi' })).to eq("(invoke notice hi)\n") end - it "runs error free when there are no validation errors" do - manifest = file_containing('site.pp', "notify { valid: }") + it "prints the AST of the code read from the passed files" do + first_manifest = file_containing('site.pp', "notice hi") + second_manifest = file_containing('site2.pp', "notice bye") - parser.validate(manifest) + output = parser.dump(first_manifest, second_manifest) + + expect(output).to match(/site\.pp.*\(invoke notice hi\)/) + expect(output).to match(/site2\.pp.*\(invoke notice bye\)/) end - it "reports missing files" do - expect do - parser.validate("missing.pp") - end.to raise_error(Puppet::Error, /One or more file\(s\) specified did not exist.*missing\.pp/m) + it "informs the user of files that don't exist" do + expect(parser.dump('does_not_exist_here.pp')).to match(/did not exist:\s*does_not_exist_here\.pp/m) end - it "parses supplied manifest files in the context of a directory environment" do - manifest = file_containing('test.pp', "{ invalid =>") + it "prints the AST of STDIN when no files given and STDIN is not a tty" do + from_a_piped_input_of("notice hi") - env_loader = Puppet::Environments::Static.new( - Puppet::Node::Environment.create(:special, []) - ) - Puppet.override(:environments => env_loader) do - Puppet[:environment] = 'special' - expect { parser.validate(manifest) }.to exit_with(1) + Puppet.override(:current_environment => Puppet::Node::Environment.create(:special, [])) do + expect(parser.dump()).to eq("(invoke notice hi)\n") end - - expect(@logs.join).to match(/environment special.*Syntax error at '\{'/) end - end - - it "validates the contents of STDIN when no files given and STDIN is not a tty" do - from_a_piped_input_of("{ invalid =>") + it "logs an error if the input cannot be parsed" do + output = parser.dump({ :e => '{ invalid =>' }) - expect { parser.validate() }.to exit_with(1) + expect(output).to eq("") + expect(@logs[0].message).to eq("Syntax error at end of file") + expect(@logs[0].level).to eq(:err) + end end def from_an_interactive_terminal |