Skip to content

Commit 6f1c50f

Browse files
BurdetteLamarkou
andauthored
Add bare bones console application for filtering CSV (#321)
This is the bare bones (no input/output options yet). Options to be added piecemeal. The help text is a little misleading; it mentions input and output options, but there are none (yet). --------- Co-authored-by: Sutou Kouhei <kou@cozmixng.org> Co-authored-by: Sutou Kouhei <kou@clear-code.com>
1 parent 49129ff commit 6f1c50f

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

bin/csv-filter

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'optparse'
4+
require 'csv'
5+
6+
options = {}
7+
8+
parser = OptionParser.new
9+
10+
parser.version = CSV::VERSION
11+
parser.banner = <<-BANNER
12+
Usage: #{parser.program_name} [options]
13+
14+
Reads and parses the CSV text content of the standard input per the given input options.
15+
From that content, generates CSV text per the given output options
16+
and writes that text to the standard output.
17+
BANNER
18+
19+
begin
20+
parser.parse!
21+
rescue OptionParser::InvalidOption
22+
$stderr.puts($!.message)
23+
$stderr.puts(parser)
24+
exit(false)
25+
end
26+
27+
CSV.filter(**options) do |row|
28+
end

csv.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
end
3939
end
4040
spec.files = files
41+
spec.executables = ["csv-filter"]
4142
spec.rdoc_options.concat(["--main", "README.md"])
4243
rdoc_files = [
4344
"LICENSE.txt",

test/csv/test_csv_filter.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: false
2+
3+
require_relative "helper"
4+
5+
class TestCSVFilter < Test::Unit::TestCase
6+
def setup
7+
@csv = <<-CSV
8+
aaa,bbb,ccc
9+
ddd,eee,fff
10+
CSV
11+
end
12+
13+
# Return stdout and stderr from CLI execution.
14+
def run_csv_filter(csv, *options)
15+
top_dir = File.join(__dir__, "..", "..")
16+
csv_filter = File.join(top_dir, "bin", "csv-filter")
17+
if File.exist?(csv_filter)
18+
# In-place test
19+
command_line = [
20+
Gem.ruby,
21+
"-I",
22+
File.join(top_dir, "lib"),
23+
csv_filter,
24+
*options,
25+
]
26+
else
27+
# Gem test
28+
command_line = [
29+
Gem.ruby,
30+
"-S",
31+
"csv-filter",
32+
*options,
33+
]
34+
end
35+
Tempfile.create("stdout", mode: File::RDWR) do |stdout|
36+
Tempfile.create("stderr", mode: File::RDWR) do |stderr|
37+
Tempfile.create(["csv-filter", ".csv"]) do |input|
38+
input.write(csv)
39+
input.close
40+
system(*command_line, in: input.path, out: stdout, err: stderr)
41+
stdout.rewind
42+
stderr.rewind
43+
[stdout.read, stderr.read]
44+
end
45+
end
46+
end
47+
end
48+
49+
# Test for invalid option.
50+
def test_invalid_option
51+
output, error = run_csv_filter("", "-Z")
52+
assert_equal(["", "invalid option: -Z\n"],
53+
[output, error.lines.first])
54+
end
55+
56+
# Test for no options.
57+
def test_no_options
58+
assert_equal([@csv, ""],
59+
run_csv_filter(@csv))
60+
end
61+
62+
# Tests for general options.
63+
64+
def test_option_h
65+
output, error = run_csv_filter("", "-h")
66+
assert_equal(["Usage: csv-filter [options]\n", ""],
67+
[output.lines.first, error])
68+
end
69+
70+
def test_option_v
71+
assert_equal(["csv-filter #{CSV::VERSION}\n", ""],
72+
run_csv_filter("", "-v"))
73+
end
74+
end

0 commit comments

Comments
 (0)