Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions bin/csv-filter
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/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

begin
parser.parse!
rescue OptionParser::InvalidOption
$stderr.puts($!.message)
$stderr.puts(parser)
exit(false)
end

CSV.filter(**options) do |row|
end
1 change: 1 addition & 0 deletions csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
end
end
spec.files = files
spec.executables = ["csv-filter"]
spec.rdoc_options.concat(["--main", "README.md"])
rdoc_files = [
"LICENSE.txt",
Expand Down
74 changes: 74 additions & 0 deletions test/csv/test_csv_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: false

require_relative "helper"

class TestCSVFilter < Test::Unit::TestCase
def setup
@csv = <<-CSV
aaa,bbb,ccc
ddd,eee,fff
CSV
end

# Return stdout and stderr from CLI execution.
def run_csv_filter(csv, *options)
top_dir = File.join(__dir__, "..", "..")
csv_filter = File.join(top_dir, "bin", "csv-filter")
if File.exist?(csv_filter)
# In-place test
command_line = [
Gem.ruby,
"-I",
File.join(top_dir, "lib"),
csv_filter,
*options,
]
else
# Gem test
command_line = [
Gem.ruby,
"-S",
"csv-filter",
*options,
]
end
Tempfile.create("stdout", mode: File::RDWR) do |stdout|
Tempfile.create("stderr", mode: File::RDWR) do |stderr|
Tempfile.create(["csv-filter", ".csv"]) do |input|
input.write(csv)
input.close
system(*command_line, in: input.path, out: stdout, err: stderr)
stdout.rewind
stderr.rewind
[stdout.read, stderr.read]
end
end
end
end

# Test for invalid option.
def test_invalid_option
output, error = run_csv_filter("", "-Z")
assert_equal(["", "invalid option: -Z\n"],
[output, error.lines.first])
end

# Test for no options.
def test_no_options
assert_equal([@csv, ""],
run_csv_filter(@csv))
end

# Tests for general options.

def test_option_h
output, error = run_csv_filter("", "-h")
assert_equal(["Usage: csv-filter [options]\n", ""],
[output.lines.first, error])
end

def test_option_v
assert_equal(["csv-filter #{CSV::VERSION}\n", ""],
run_csv_filter("", "-v"))
end
end