Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
02a733d
Console application for filtering CSV
BurdetteLamar Nov 18, 2024
c23e371
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 16, 2024
f6a7b29
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 16, 2024
e929d19
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 16, 2024
c6f9dd6
Respond to review
BurdetteLamar Dec 16, 2024
6a01bbf
Respond to review
BurdetteLamar Dec 16, 2024
8fee505
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 17, 2024
c354176
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 17, 2024
97444e7
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 17, 2024
7102e61
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Dec 17, 2024
cdc2b47
Respond to review
BurdetteLamar Dec 17, 2024
119e98c
Respond to review
BurdetteLamar Dec 18, 2024
ebb8f48
Respond to review
BurdetteLamar Dec 18, 2024
1776b21
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
c3e960e
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
0658a05
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
b5cdca6
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
ae72bb6
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
a63628d
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
b176c63
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
60ff670
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
31c2f2f
Update test/csv/csv-filter/test_csv_filter.rb
BurdetteLamar Feb 8, 2025
2d67ff1
Respond to review
BurdetteLamar Feb 8, 2025
ecd77d6
Simplify
kou Feb 24, 2025
4befc44
Add to executables
kou Feb 24, 2025
cf86328
Use "csv-filter" for gem test
kou Feb 24, 2025
0dc6777
Improve comment
kou Feb 24, 2025
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
26 changes: 26 additions & 0 deletions bin/csv-filter
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

require 'optparse'
require 'csv'

options = {}

parser = OptionParser.new

parser.version = CSV::VERSION
parser.banner = <<-BANNER
Usage: #{parser.program_name} [options]

Reads and parses the CSV text content of the standard input per the given input options.
From that content, generates CSV text per the given output options
and writes that text to the standard output.

BANNER

parser.separator('Generic Options')
parser.separator(nil)

parser.parse!

CSV.filter(**options) do |row|
end
102 changes: 102 additions & 0 deletions test/csv/csv-filter/test_csv_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: false

require_relative '../helper'

require 'csv'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this because ../helper has this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


class TestFilter < Test::Unit::TestCase

Comment thread
BurdetteLamar marked this conversation as resolved.
Outdated
# Some rows data (useful as default).
Rows = [
%w[aaa bbb ccc],
%w[ddd eee fff],
]
Comment thread
BurdetteLamar marked this conversation as resolved.
Outdated

# Return CSV string generated from rows array and options.
def make_csv_s(rows: Rows, **options)
CSV.generate(**options) do|csv|
rows.each do |row|
csv << row
end
end
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

How about keeping all CSV data as String not [[...], [...], ...] in this file?
I think that interface of csv-filter is CSV string not a CSV Ruby object.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


# Return filepath of file containing CSV data.
def csv_filepath(csv_in_s, dirpath, option_sym)
filename = "#{option_sym}.csv"
filepath = File.join(dirpath, filename)
File.write(filepath, csv_in_s)
filepath
end

# Return stdout and stderr from CLI execution.
def run_csv_filter(filepath, cli_option_names = [])
Comment thread
BurdetteLamar marked this conversation as resolved.
Outdated
top_dir = File.join(__dir__, "..", "..", "..")
command_line_s = [
Gem.ruby,
"-I",
File.join(top_dir, "lib"),
File.join(top_dir, "bin", "csv-filter"),
* cli_option_names,
Comment thread
BurdetteLamar marked this conversation as resolved.
Outdated
filepath,
].join(' ')
Tempfile.create("stdout", mode: File::RDWR) do |stdout|
Tempfile.create("stderr", mode: File::RDWR) do |stderr|
status = system(command_line_s, {1 => stdout, 2 => stderr})
stdout.rewind
stderr.rewind
[stdout.read, stderr.read]
end
end
end

# Return results for CLI-only option (or invalid option).
def results_for_cli_option(option_name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use better name? This executes csv-filter not a simple getter. So we should use a verb not a noun (results) for method name. (I think that we can unify this and run_csv_filter and use run_csv_filter for it...)

cli_out_s = ''
cli_err_s = ''
Dir.mktmpdir do |dirpath|
sym = option_name.to_sym
filepath = csv_filepath('', dirpath, sym)
cli_out_s, cli_err_s = run_csv_filter(filepath, [option_name])
end
[cli_out_s, cli_err_s]
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this?

Can run_csv_filter accept CSV in string as the first argument?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests that use this have CLI-only options (-v -h etc.) that can't be passed to the API. I think we need to keep this.


# Get and return the actual output from the API.
def get_via_api(csv_in_s, **api_options)
cli_out_s = ''
CSV.filter(csv_in_s, cli_out_s, **api_options) {|row| }
cli_out_s
end

# Test for invalid option.

def test_invalid_option
cli_out_s, cli_err_s = results_for_cli_option('-Z')
assert_equal("", cli_out_s)
assert_match(/OptionParser::InvalidOption/, cli_err_s)
end

# Test for no options.

def test_no_options
csv_in_s = make_csv_s
cli_out_s = get_via_api(csv_in_s)
assert_equal(csv_in_s, cli_out_s)
end

# Tests for general options.

def test_option_h
cli_out_s, cli_err_s = results_for_cli_option('-h')
Comment thread
BurdetteLamar marked this conversation as resolved.
Outdated
assert_equal("Usage: csv-filter [options]\n", cli_out_s.lines.first)
assert_equal('', cli_err_s)
end

def test_option_v
cli_out_s, cli_err_s = results_for_cli_option('-v')
assert_match(/\d+\.\d+\.\d+/, cli_out_s)
assert_equal('', cli_err_s)
end

Comment thread
BurdetteLamar marked this conversation as resolved.
Outdated
end