Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configurable csv separator #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/adhoq/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/adhoq/reporter/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
55 changes: 54 additions & 1 deletion spec/adhoq/reporter/csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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