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

Console application for filtering CSV #321

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

BurdetteLamar
Copy link
Member

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).

bin/csv-filter Outdated
Comment on lines 28 to 31
parser.on('-v', '--version', 'Prints version.') do
puts CSV::VERSION
exit
end
Copy link
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? csv-filter --version will work because we have parser.version = CSV::VERSION.

Copy link
Member Author

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
Comment on lines 23 to 26
parser.on('-h', '--help', 'Prints this help.') do
puts parser
exit
end
Copy link
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?
--help will work by default.

Copy link
Member Author

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 -*-
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.

Comment on lines 17 to 18
# In case the previous test left this as true.
$TEST_DEBUG = false
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.

Comment on lines 21 to 26
# 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
Copy link
Member

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?

Copy link
Member Author

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|
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Modified.

test/csv/csv-filter/test_csv_filter.rb Outdated Show resolved Hide resolved

def test_option_h
do_test(debugging: false) do
%w[-h --help].each do |option_name|
Copy link
Member

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
end

Copy link
Member Author

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)
Copy link
Member

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)

Copy link
Member Author

Choose a reason for hiding this comment

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

Modified.

Copy link
Member Author

Choose a reason for hiding this comment

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

Modified.

Comment on lines 143 to 177
# 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
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.

BurdetteLamar and others added 5 commits December 16, 2024 15:15
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
@BurdetteLamar
Copy link
Member Author

Thanks, @kou, for your careful review; I learned a lot.

@BurdetteLamar BurdetteLamar requested a review from kou December 16, 2024 22:12
@BurdetteLamar BurdetteLamar marked this pull request as draft December 17, 2024 16:12
@BurdetteLamar
Copy link
Member Author

@kou, okay to add an option or two?

@kou
Copy link
Member

kou commented Dec 17, 2024

Ah, let's add options after this is merged for easy to review this PR.

@BurdetteLamar
Copy link
Member Author

Ah, let's add options after this is merged for easy to review this PR.

Works for me!


require_relative '../helper'

require 'csv'
Copy link
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
Member Author

Choose a reason for hiding this comment

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

Removed.

test/csv/csv-filter/test_csv_filter.rb Outdated Show resolved Hide resolved
test/csv/csv-filter/test_csv_filter.rb Outdated Show resolved Hide resolved
test/csv/csv-filter/test_csv_filter.rb Outdated Show resolved Hide resolved
Comment on lines 53 to 63
# 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
Copy link
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
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.

test/csv/csv-filter/test_csv_filter.rb Outdated Show resolved Hide resolved
Comment on lines 15 to 22
# 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
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
Member Author

Choose a reason for hiding this comment

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

Done.

BurdetteLamar and others added 7 commits December 17, 2024 17:14
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>
@BurdetteLamar BurdetteLamar requested a review from kou December 18, 2024 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants