-
Notifications
You must be signed in to change notification settings - Fork 123
Add bare bones console application for filtering CSV #321
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
Conversation
bin/csv-filter
Outdated
| parser.on('-v', '--version', 'Prints version.') do | ||
| puts CSV::VERSION | ||
| exit | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? csv-filter --version will work because we have parser.version = CSV::VERSION.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
bin/csv-filter
Outdated
| parser.on('-h', '--help', 'Prints this help.') do | ||
| puts parser | ||
| exit | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?
--help will work by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
| @@ -0,0 +1,179 @@ | |||
| # -*- coding: utf-8 -*- | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this for newly created file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
| # In case the previous test left this as true. | ||
| $TEST_DEBUG = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
| # Print debugging information if indicated. | ||
| def debug(label, value, newline: false) | ||
| return unless $TEST_DEBUG | ||
| print("\n") if newline | ||
| printf("%15s: %s\n", label, value.inspect) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
|
|
||
| def test_invalid_option | ||
| do_test(debugging: false) do | ||
| %w[-Z --ZZZ].each do |option_name| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to test both of short option and long option?
This is not a OptionParser test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified.
|
|
||
| def test_option_h | ||
| do_test(debugging: false) do | ||
| %w[-h --help].each do |option_name| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you avoid using each to run multiple tests?
Could you define separated tests instead of checking multiple items in one test? It's for easy to debug on error.
def test_option_h
end
def test_option_help
endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified.
| do_test(debugging: false) do | ||
| %w[-h --help].each do |option_name| | ||
| cli_out_s, cli_err_s = results_for_cli_option(option_name) | ||
| assert_match(/Usage/, cli_out_s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_equal is better than assert_match on error because assert_equal shows diff too.
assert_equal("Usage: csv-filter [options]\n", cli_out_s.lines.first)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified.
| # Two methods copied from module Minitest::Assertions. | ||
| # because we need access to the subprocess io. | ||
|
|
||
| def _synchronize # :nodoc: | ||
| yield | ||
| end | ||
|
|
||
| def capture_subprocess_io | ||
| _synchronize do | ||
| begin | ||
| require "tempfile" | ||
|
|
||
| captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err") | ||
|
|
||
| orig_stdout, orig_stderr = $stdout.dup, $stderr.dup | ||
| $stdout.reopen captured_stdout | ||
| $stderr.reopen captured_stderr | ||
|
|
||
| yield | ||
|
|
||
| $stdout.rewind | ||
| $stderr.rewind | ||
|
|
||
| return captured_stdout.read, captured_stderr.read | ||
| ensure | ||
| $stdout.reopen orig_stdout | ||
| $stderr.reopen orig_stderr | ||
|
|
||
| orig_stdout.close | ||
| orig_stderr.close | ||
| captured_stdout.close! | ||
| captured_stderr.close! | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
|
Thanks, @kou, for your careful review; I learned a lot. |
|
@kou, okay to add an option or two? |
|
Ah, let's add options after this is merged for easy to review this PR. |
Works for me! |
|
|
||
| require_relative '../helper' | ||
|
|
||
| require 'csv' |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
| # Return results for CLI-only option (or invalid option). | ||
| def results_for_cli_option(option_name) | ||
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| # 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
|
@kou, no longer interested in this? |
kou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry. I forgot this...
| def test_invalid_option | ||
| output, error = results_for_cli_option("-Z") | ||
| assert_equal("", output) | ||
| assert_match(/OptionParser::InvalidOption/, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we show --help for an invalid option?
| def test_no_options | ||
| output = api_output(@input) | ||
| assert_equal(@input, output) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this meaningful test?
I think that we need to use results_for_cli_option not api_output here to test csv-filter.
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
|
@kou, I have merged the suggested changes (but have not yet worked on the other comments). I've restored Also, I could not make the test for option |
|
Can I push some commits to this branch? |
Yes |
|
I've simplified the current implementation. Could you review this? |
BurdetteLamar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
|
Thanks. |
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).