From e272772c11e353e6ce485757cee773478c58e841 Mon Sep 17 00:00:00 2001 From: takkanm Date: Fri, 14 Oct 2016 18:50:13 +0900 Subject: [PATCH] configurable csv separator add following configuration for csv. - csv column separator :: Adhoq.config.csv_column_separator - csv row separator :: Adhoq.config.csv_row_separator --- lib/adhoq/configuration.rb | 3 ++ lib/adhoq/reporter/csv.rb | 7 ++++- spec/adhoq/reporter/csv_spec.rb | 55 ++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/adhoq/configuration.rb b/lib/adhoq/configuration.rb index ba1435f..b5feba9 100644 --- a/lib/adhoq/configuration.rb +++ b/lib/adhoq/configuration.rb @@ -18,6 +18,9 @@ class Configuration config_accessor :async_execution config_accessor :job_queue_name + config_accessor :csv_row_separator + config_accessor :csv_column_separator + def callablize(name) if (c = config[name]).respond_to?(:call) c diff --git a/lib/adhoq/reporter/csv.rb b/lib/adhoq/reporter/csv.rb index a1772ac..379b62d 100644 --- a/lib/adhoq/reporter/csv.rb +++ b/lib/adhoq/reporter/csv.rb @@ -14,7 +14,12 @@ def initialize(result) def build_report file = Tempfile.new(['adhoq-reporter', '.csv'], Dir.tmpdir, encoding: 'UTF-8') - csv = CSV.new(file) + + csv_option = {} + csv_option[:row_sep] = Adhoq.config.csv_row_separator if Adhoq.config.csv_row_separator + csv_option[:col_sep] = Adhoq.config.csv_column_separator if Adhoq.config.csv_column_separator + + csv = CSV.new(file, csv_option) csv << @result.header @result.rows.each {|row| csv << row } diff --git a/spec/adhoq/reporter/csv_spec.rb b/spec/adhoq/reporter/csv_spec.rb index 4c900ff..cd4ecf3 100644 --- a/spec/adhoq/reporter/csv_spec.rb +++ b/spec/adhoq/reporter/csv_spec.rb @@ -20,6 +20,59 @@ module Adhoq ] end end + + describe 'CSV configuration' do + let(:report_body) { + [ + %w(ab cd ef), + %w(gh ij kl) + ] + } + let(:report_header) { + %w(1 2 3) + } + let(:adhoq_result) { + Adhoq::Result.new(report_header, report_body) + } + let(:reporter) { + Adhoq::Reporter::Csv.new(adhoq_result) + } + + describe 'row separator' do + let(:row_sep) { "\r\n" } + + before do + @before_row_sep = Adhoq.config.csv_row_separator + Adhoq.config.csv_row_separator = row_sep + end + + after do + Adhoq.config.csv_row_separator = @before_row_sep + end + + specify do + report_result = reporter.build_report.read + expect(CSV.parse(report_result, row_sep: row_sep)).to eq([report_header, *report_body]) + end + end + + describe 'column separator' do + let(:col_sep) { "\t" } + + before do + @before_col_sep = Adhoq.config.csv_column_separator + Adhoq.config.csv_column_separator = col_sep + end + + after do + Adhoq.config.csv_column_separator = @before_col_sep + end + + specify do + report_result = reporter.build_report.read + expect(CSV.parse(report_result, col_sep: col_sep)).to eq([report_header, *report_body]) + end + end + end end end -