diff --git a/spec/acceptance/emit_json_options.rb b/spec/acceptance/emit_json_options.rb new file mode 100644 index 0000000..d1e99f3 --- /dev/null +++ b/spec/acceptance/emit_json_options.rb @@ -0,0 +1,41 @@ +require 'spec_helper_acceptance' +require 'util' +require 'json' + +include PuppetStrings::Acceptance::Util + +describe 'Emitting JSON' do +expected = { + "puppet_classes" => [], + "defined_types" => [], + "resource_types" => [], + "providers" => [], + "puppet_functions" => [ + "name" => "function3x", + "file" => "/etc/puppet/modules/test/lib/puppet/parser/functions/function3x.rb", + "line" => 1, + "type" => "ruby3x", + "signature" => "function3x()", + "docstring" => { + "text" => "This is the function documentation for `function3x`", + "tags" => ["tag_name" => "return", "text" => "", "types" => ["Any"]]}, + "source" => "Puppet::Parser::Functions.newfunction(:function3x, :doc => \"This is the function documentation for `function3x`\") do |args|\nend" + ] +} + + it 'should emit JSON to stdout when using the --emit-json-stdout option' do + test_module_path = get_test_module_path(master, /Module test/) + on master, puppet('strings', 'generate', '--emit-json-stdout', "#{test_module_path}/lib/puppet/parser/functions/function3x.rb") do + output = stdout.chomp + expect(JSON.parse(output)).to eq(expected) + end + end + + it 'should write JSON to a file when using the --emit-json option' do + test_module_path = get_test_module_path(master, /Module test/) + tmpfile = master.tmpfile('json_output.json') + on master, puppet('strings', 'generate', "--emit-json #{tmpfile}", "#{test_module_path}/lib/puppet/parser/functions/function3x.rb") + output = read_file_on(master, tmpfile) + expect(JSON.parse(output)).to eq(expected) + end +end diff --git a/spec/acceptance/lib/util.rb b/spec/acceptance/lib/util.rb new file mode 100644 index 0000000..e5a5793 --- /dev/null +++ b/spec/acceptance/lib/util.rb @@ -0,0 +1,15 @@ +module PuppetStrings + module Acceptance + module Util + def read_file_on(host, filename) + on(host, "cat #{filename}").stdout + end + + def get_test_module_path(host, module_regex) + modules = JSON.parse(on(host, puppet('module', 'list', '--render-as', 'json')).stdout) + test_module_info = modules['modules_by_path'].values.flatten.find { |mod_info| mod_info =~ module_regex } + test_module_info.match(/\(([^)]*)\)/)[1] + end + end + end +end diff --git a/spec/acceptance/running_strings_generate.rb b/spec/acceptance/running_strings_generate.rb index a8c382e..b02256e 100644 --- a/spec/acceptance/running_strings_generate.rb +++ b/spec/acceptance/running_strings_generate.rb @@ -1,16 +1,12 @@ require 'spec_helper_acceptance' +require 'util' require 'json' +include PuppetStrings::Acceptance::Util + describe 'Generating module documentation using generate action' do - def read_file_on(host, filename) - on(host, "cat #{filename}").stdout - end - before :all do - modules = JSON.parse(on(master, puppet('module', 'list', '--render-as', 'json')).stdout) - test_module_info = modules['modules_by_path'].values.flatten.find { |mod_info| mod_info =~ /Module test/ } - test_module_path = test_module_info.match(/\(([^)]*)\)/)[1] - + test_module_path = get_test_module_path(master, /Module test/) on master, puppet('strings', 'generate', "#{test_module_path}/**/*.{rb,pp}") end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 578fa3c..2eba14c 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -1,6 +1,8 @@ require 'beaker-rspec/spec_helper' require 'beaker-rspec/helpers/serverspec' +$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), 'acceptance/lib')) + unless ENV['RS_PROVISION'] == 'no' install_puppet end