Skip to content

Conversation

gty929
Copy link

@gty929 gty929 commented Dec 5, 2021

When working on #88 , I noticed some defects in the current library, so I'd like to submit this separate patch, which does the following:

Fix the defect that Column() crashes on zero or a negative input
The library panics when the following code is run

script.Echo("1").Column(-1)

After the change, the program will return an empty pipe.

Fix the defect that Last() crashes on a negative input
The library panics when the following code is run

script.Echo("1").Last(-1)

After the change, the program will return an empty pipe.

Fix the defect that Regex filters crash on a nil regex expression
The library panics when the following code is run

script.Echo("1").MatchRegexp(nil)

After the change, the program will return a pipe with the error "nil regular expression"

Add unit test for WithError(); close the pipe when a non-nil error is set
When fixing the regex defect, I just wrote

if re == nil { // to prevent SIGSEGV
	return p.WithError(errors.New("nil regular expression"))
}

Then, I realized that p will not be automatically closed, since the subsequent filters and sinks will catch the error and return immediately. To fix this problem and prevent contributors from making the same mistake in the future, I think the best approach is to just close the pipe reader when a non-nil error is set.

Add ExecForEach, Reject(), and RejectRegexp to doMethodsOnPipe tests
Just some jobs that previous contributors forgot.

Improve coverage by adding equal counts in the test case of Freq
To test that lines are sorted in alphabetical order when there is a tie.

Use io.Copy() in Stdout() to improve efficiency
It seems unnecessary to write the pipe's reader into a string and then print it.

By the way, after all the changes, the total line coverage has risen from 92.7% to 93.8%.

@gty929
Copy link
Author

gty929 commented Dec 12, 2021

@bitfield Hi, would you please take a look at this pull request? Many thanks.

gty929 and others added 4 commits January 1, 2022 14:16
Fix the defect that Column crashes on zero or negative input.
Fix the defect that Last() crashes on negative input;
Fix the defect that Regex filters crashes on nil regex expression;
Add unit test for WithError(); close the pipe when a non-nil error is set;
Add ExecForEach, Reject(), and RejectRegexp to doMethodsOnPipe tests;
Improve coverage by adding equal counts in the testcase of Freq;
Use io.Copy() in Stdout() to improve efficiency.
Previously, pipes always wrote to os.Stdout on calling Stdout().
This made it awkward to test. Now you can set a 'fake stdout'
by calling WithStdout(io.Writer) on the pipe.
@bitfield
Copy link
Owner

bitfield commented Jan 1, 2022

Excellent work @gty929, thanks a lot!

@bitfield bitfield merged commit b95a706 into bitfield:master Jan 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants