Module: PigSpec
- Defined in:
- lib/pig-spec.rb,
lib/pig-spec/version.rb,
lib/pig-spec/pig_unit_core.rb
Constant Summary collapse
- VERSION =
"0.0.2"
- INPUT_DIR_PREFIX =
"pig_test_"
- PIG_CMD_PREFIX =
"java -jar "
- @@test_number =
0
Instance Attribute Summary collapse
-
#error_stream ⇒ Object
readonly
Returns the value of attribute error_stream.
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#input_dir ⇒ Object
readonly
Returns the value of attribute input_dir.
-
#output_files ⇒ Object
readonly
Returns the value of attribute output_files.
-
#stdout_stream ⇒ Object
readonly
Returns the value of attribute stdout_stream.
Class Method Summary collapse
Instance Method Summary collapse
- #build_cmd_line(pig_binary, pig_script, params) ⇒ Object
- #build_pig_script_params(params) ⇒ Object
- #compare_pairs(file, pairs, mapping) ⇒ Object
- #initialize ⇒ Object
- #print_mismatch_error(file) ⇒ Object
- #run_script(pig_binary, pig_script, params) ⇒ Object
- #test_pig_script(pig_binary, pig_script, input_files_hash, output_files_hash, params) ⇒ Object
- #verify_output(order_matters) ⇒ Object
- #write_input_files(input_files_hash) ⇒ Object
Instance Attribute Details
#error_stream ⇒ Object (readonly)
Returns the value of attribute error_stream.
6 7 8 |
# File 'lib/pig-spec/pig_unit_core.rb', line 6 def error_stream @error_stream end |
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
6 7 8 |
# File 'lib/pig-spec/pig_unit_core.rb', line 6 def exit_code @exit_code end |
#input_dir ⇒ Object (readonly)
Returns the value of attribute input_dir.
6 7 8 |
# File 'lib/pig-spec/pig_unit_core.rb', line 6 def input_dir @input_dir end |
#output_files ⇒ Object (readonly)
Returns the value of attribute output_files.
6 7 8 |
# File 'lib/pig-spec/pig_unit_core.rb', line 6 def output_files @output_files end |
#stdout_stream ⇒ Object (readonly)
Returns the value of attribute stdout_stream.
6 7 8 |
# File 'lib/pig-spec/pig_unit_core.rb', line 6 def stdout_stream @stdout_stream end |
Class Method Details
.test_number ⇒ Object
17 18 19 |
# File 'lib/pig-spec/pig_unit_core.rb', line 17 def self.test_number @@test_number end |
Instance Method Details
#build_cmd_line(pig_binary, pig_script, params) ⇒ Object
42 43 44 45 46 |
# File 'lib/pig-spec/pig_unit_core.rb', line 42 def build_cmd_line(pig_binary, pig_script, params) pig_params = build_pig_script_params(params) pig_params = pig_params.empty? ? "" : "#{pig_params} " "#{PIG_CMD_PREFIX}#{pig_binary} -x local #{pig_params}#{pig_script}" end |
#build_pig_script_params(params) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/pig-spec/pig_unit_core.rb', line 31 def build_pig_script_params(params) if params.class != Hash error_stream.puts "Params had unexpected class: #{params.class}" return "" end params.reduce("") do |param_str, param| param_str + (param_str.empty? ? "" : " ") + "-p " + param[0].to_s + "=" + param[1].to_s end end |
#compare_pairs(file, pairs, mapping) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pig-spec/pig_unit_core.rb', line 81 def compare_pairs(file, pairs, mapping) pairs.each do |pair| if pair[0] != pair[1] print_mismatch_error(file) error_stream.puts "\tExpected line: '#{pair[mapping[:expected]]}'" error_stream.puts "\tActual line: '#{pair[mapping[:actual]]}'" return false end end end |
#initialize ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/pig-spec/pig_unit_core.rb', line 9 def initialize @output_files = {} @input_dir = "" @error_stream = $stderr @stdout_stream = $stdout @exit_code = 0 end |
#print_mismatch_error(file) ⇒ Object
77 78 79 |
# File 'lib/pig-spec/pig_unit_core.rb', line 77 def print_mismatch_error(file) error_stream.puts "Mismatch detected in '#{file}':" end |
#run_script(pig_binary, pig_script, params) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pig-spec/pig_unit_core.rb', line 48 def run_script(pig_binary, pig_script, params) cmd_to_run = build_cmd_line(pig_binary, pig_script, params) stdout_stream.puts("Running the following command: #{cmd_to_run}") original_cwd = Dir.pwd Dir.chdir(input_dir) system cmd_to_run @exit_code = $? if exit_code != 0 error_stream.puts "Pig script exited with non-zero exit code: #{exit_code}." end Dir.chdir(original_cwd) end |
#test_pig_script(pig_binary, pig_script, input_files_hash, output_files_hash, params) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/pig-spec/pig_unit_core.rb', line 21 def test_pig_script(pig_binary, pig_script, input_files_hash, output_files_hash, params) @error_stream = error_stream @stdout_stream = stdout_stream @@test_number = @@test_number + 1 write_input_files(input_files_hash) @output_files = output_files_hash run_script(pig_binary, pig_script, params) end |
#verify_output(order_matters) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pig-spec/pig_unit_core.rb', line 92 def verify_output(order_matters) stdout_stream.puts "----------------------------------" stdout_stream.puts "| Verifying Pig script output... |" stdout_stream.puts "----------------------------------" if output_files.class != Hash error_stream.puts "Expected hash of expected output with (filename, file content) pairs. Unexpected class: #{output_files.class}" return false elsif output_files.size == 0 error_stream.puts "No output files to verify." return false end if exit_code != 0 error_stream.puts "Pig script exited with non-zero exit code: #{exit_code}." return false end all_output_matched = true original_cwd = Dir.pwd Dir.chdir(input_dir) output_files.keys.each do |file| file_lines_array = IO.read(file).split("\n") file_lines_array = file_lines_array.sort if !order_matters expected_output_array = output_files[file].split("\n") expected_output_array = expected_output_array.sort if !order_matters if (!compare_pairs(file, expected_output_array.zip(file_lines_array), { :expected => 0, :actual => 1 }) || !compare_pairs(file, file_lines_array.zip(expected_output_array), { :expected => 1, :actual => 0 })) all_output_matched = false next end end Dir.chdir(original_cwd) all_output_matched end |
#write_input_files(input_files_hash) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pig-spec/pig_unit_core.rb', line 61 def write_input_files(input_files_hash) @input_dir = File.(INPUT_DIR_PREFIX + PigSpec.test_number.to_i.to_s) FileUtils.rm_rf(input_dir) FileUtils.mkdir_p(input_dir) if input_files_hash.class != Hash error_stream.puts "Input files had unexpected class: #{input_files_hash.class}" return end input_files_hash.keys.each do |file_name| File.open(File.join(input_dir, file_name.to_s), "w") { |f| f.print(input_files_hash[file_name]) } end end |