Skip to content

Commit

Permalink
brilliant test by @leomao10
Browse files Browse the repository at this point in the history
  • Loading branch information
seamusabshere committed Mar 21, 2012
1 parent 0e22c9c commit 093a237
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/unix_utils.rb
Expand Up @@ -192,12 +192,13 @@ def self.dos2unix(infile)
end
end

# POSIX sed, whether it's provided by sed or gsed
def self.sed(infile, *expr)
infile = ::File.expand_path infile
outfile = tmp_path infile
bin = available?('gsed') ? 'gsed' : 'sed'
argv = [ bin, expr.map { |e| ['-e', e] } ].flatten
spawn argv, :read_from => infile, :write_to => outfile
bin = available?('sed') ? 'sed' : ['gsed', '--posix']
argv = [ bin, expr.map { |e| ['-e', e] }, infile ].flatten
spawn argv, :write_to => outfile
outfile
end

Expand Down
32 changes: 32 additions & 0 deletions test/regression/test_sed_large_file.rb
@@ -0,0 +1,32 @@
# encoding: UTF-8
require 'helper'
require 'tempfile'

describe "handle large file with sed" do
let(:normal_line_no) { 124000 }
let(:invalid_line_no) { 10 }
let(:input_file) { Tempfile.open('test', '/tmp') }

before do
begin
(1..normal_line_no).each do |i|
input_file.puts "\"psmith01\",\"CLASS2B\",\"Peter Smith 1\",\"YEAR2\",\"1\",\"N\",\"ADVANCED\",\"STAFF\",\"1\",\"Y\",\"Y\",\"psmith01\",\"CLASS2B\",\"Peter Smith 1\",\"YEAR2\",\"1\",\"N\",\"ADVANCED\",\"STAFF\",\"1\",\"Y\",\"Y\",\"psmith01\",\"CLASS2B\",\"Peter Smith 1\",\"YEAR2\",\"1\",\"N\",\"ADVANCED\",\"STAFF\",\"1\",\"Y\",\"Y\",\"psmith01\",\"CLASS2B\",\"Peter Smith 1\",\"YEAR2\",\"1\",\"N\",\"ADVANCED\",\"STAFF\",\"1\",\"Y\",\"Y\""
end
(1..invalid_line_no).each do |i|
input_file.puts "@!!@"
end
input_file.flush
ensure
input_file.close
end
end

it "should have 124000 lines in output file" do
output_path = UnixUtils.sed(input_file.path, ':a', "1,#{invalid_line_no}!{P;N;D;};N;ba")
UnixUtils.wc(output_path).first.must_equal normal_line_no
end

after do
input_file.unlink
end
end

0 comments on commit 093a237

Please sign in to comment.