tb 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/README +62 -50
  2. data/bin/tb +22 -18
  3. data/lib/tb.rb +35 -19
  4. data/lib/tb/basic.rb +85 -86
  5. data/lib/tb/catreader.rb +33 -116
  6. data/lib/tb/cmd_cat.rb +31 -27
  7. data/lib/tb/cmd_consecutive.rb +45 -35
  8. data/lib/tb/cmd_crop.rb +86 -52
  9. data/lib/tb/cmd_cross.rb +113 -71
  10. data/lib/tb/cmd_cut.rb +49 -44
  11. data/lib/tb/cmd_git_log.rb +193 -0
  12. data/lib/tb/cmd_grep.rb +43 -32
  13. data/lib/tb/cmd_group.rb +63 -39
  14. data/lib/tb/cmd_gsub.rb +53 -43
  15. data/lib/tb/cmd_help.rb +51 -24
  16. data/lib/tb/cmd_join.rb +32 -35
  17. data/lib/tb/cmd_ls.rb +233 -205
  18. data/lib/tb/cmd_mheader.rb +47 -37
  19. data/lib/tb/cmd_nest.rb +94 -0
  20. data/lib/tb/cmd_newfield.rb +29 -33
  21. data/lib/tb/cmd_rename.rb +40 -32
  22. data/lib/tb/cmd_shape.rb +31 -24
  23. data/lib/tb/cmd_sort.rb +46 -25
  24. data/lib/tb/cmd_svn_log.rb +47 -28
  25. data/lib/tb/cmd_tar_tvf.rb +447 -0
  26. data/lib/tb/cmd_to_csv.rb +60 -0
  27. data/lib/tb/cmd_to_json.rb +60 -0
  28. data/lib/tb/cmd_to_pnm.rb +48 -0
  29. data/lib/tb/cmd_to_pp.rb +71 -0
  30. data/lib/tb/cmd_to_tsv.rb +48 -0
  31. data/lib/tb/cmd_to_yaml.rb +52 -0
  32. data/lib/tb/cmd_unnest.rb +118 -0
  33. data/lib/tb/cmdmain.rb +24 -20
  34. data/lib/tb/cmdtop.rb +33 -25
  35. data/lib/tb/cmdutil.rb +26 -66
  36. data/lib/tb/csv.rb +46 -34
  37. data/lib/tb/enum.rb +294 -0
  38. data/lib/tb/enumerable.rb +198 -7
  39. data/lib/tb/enumerator.rb +73 -0
  40. data/lib/tb/fieldset.rb +27 -19
  41. data/lib/tb/fileenumerator.rb +365 -0
  42. data/lib/tb/json.rb +50 -0
  43. data/lib/tb/pager.rb +6 -6
  44. data/lib/tb/pairs.rb +227 -0
  45. data/lib/tb/pnm.rb +23 -22
  46. data/lib/tb/reader.rb +52 -49
  47. data/lib/tb/record.rb +48 -19
  48. data/lib/tb/revcmp.rb +38 -0
  49. data/lib/tb/ropen.rb +74 -57
  50. data/lib/tb/search.rb +25 -21
  51. data/lib/tb/tsv.rb +31 -34
  52. data/sample/excel2csv +24 -20
  53. data/sample/poi-xls2csv.rb +24 -20
  54. data/sample/poi-xls2csv.sh +22 -18
  55. data/sample/tbplot +185 -127
  56. data/test-all-cov.rb +3 -3
  57. data/test-all.rb +1 -1
  58. data/test/test_basic.rb +26 -10
  59. data/test/test_catreader.rb +7 -6
  60. data/test/test_cmd_cat.rb +32 -0
  61. data/test/test_cmd_consecutive.rb +10 -0
  62. data/test/test_cmd_crop.rb +4 -4
  63. data/test/test_cmd_cross.rb +16 -4
  64. data/test/test_cmd_git_log.rb +46 -0
  65. data/test/test_cmd_help.rb +17 -12
  66. data/test/test_cmd_join.rb +21 -1
  67. data/test/test_cmd_ls.rb +3 -4
  68. data/test/test_cmd_mheader.rb +17 -11
  69. data/test/test_cmd_nest.rb +49 -0
  70. data/test/test_cmd_sort.rb +15 -0
  71. data/test/test_cmd_tar_tvf.rb +281 -0
  72. data/test/{test_cmd_csv.rb → test_cmd_to_csv.rb} +35 -21
  73. data/test/{test_cmd_json.rb → test_cmd_to_json.rb} +31 -3
  74. data/test/{test_cmd_pnm.rb → test_cmd_to_pnm.rb} +2 -2
  75. data/test/{test_cmd_pp.rb → test_cmd_to_pp.rb} +4 -4
  76. data/test/{test_cmd_tsv.rb → test_cmd_to_tsv.rb} +4 -4
  77. data/test/{test_cmd_yaml.rb → test_cmd_to_yaml.rb} +3 -3
  78. data/test/test_cmd_unnest.rb +89 -0
  79. data/test/test_cmdtty.rb +19 -13
  80. data/test/test_enumerable.rb +83 -1
  81. data/test/test_fileenumerator.rb +265 -0
  82. data/test/test_json.rb +15 -0
  83. data/test/test_pager.rb +3 -4
  84. data/test/test_pairs.rb +122 -0
  85. data/test/test_pnm.rb +24 -24
  86. data/test/test_reader.rb +35 -13
  87. data/test/test_revcmp.rb +10 -0
  88. data/test/test_tbenum.rb +173 -0
  89. metadata +51 -23
  90. data/lib/tb/cmd_csv.rb +0 -42
  91. data/lib/tb/cmd_json.rb +0 -60
  92. data/lib/tb/cmd_pnm.rb +0 -43
  93. data/lib/tb/cmd_pp.rb +0 -70
  94. data/lib/tb/cmd_tsv.rb +0 -43
  95. data/lib/tb/cmd_yaml.rb +0 -47
data/test-all-cov.rb CHANGED
@@ -17,10 +17,10 @@ at_exit {
17
17
  %r{lib/tb[/.]} !~ f
18
18
  }
19
19
  if !fs.empty?
20
- if STDOUT.tty?
20
+ if $stdout.tty?
21
21
  out = IO.popen(['less', '-S', '-j20', '+/ 0:'], 'w')
22
22
  else
23
- out = STDOUT
23
+ out = $stdout
24
24
  end
25
25
  pat = nil
26
26
  fs[0].chars.to_a.reverse_each {|ch|
@@ -55,7 +55,7 @@ at_exit {
55
55
  end
56
56
  }
57
57
  }
58
- if out != STDOUT
58
+ if out != $stdout
59
59
  out.close
60
60
  end
61
61
  end
data/test-all.rb CHANGED
@@ -4,7 +4,7 @@ $:.unshift "lib"
4
4
 
5
5
  r, w = IO.pipe
6
6
  w.close
7
- STDIN.reopen(r)
7
+ $stdin.reopen(r)
8
8
  r.close
9
9
 
10
10
  Dir.glob('test/test_*.rb') {|filename|
data/test/test_basic.rb CHANGED
@@ -208,10 +208,10 @@ class TestTbBasic < Test::Unit::TestCase
208
208
  t1 = Tb.new %w[a b], %w[1 2], %w[3 4], %w[0 4]
209
209
  t2 = Tb.new %w[b c], %w[2 3], %w[4 5], %w[5 8]
210
210
  t3 = t1.natjoin2(t2)
211
- assert_equal([{"_recordid"=>0, "a"=>"1", "b"=>"2", "c"=>"3"},
212
- {"_recordid"=>1, "a"=>"3", "b"=>"4", "c"=>"5"},
213
- {"_recordid"=>2, "a"=>"0", "b"=>"4", "c"=>"5"}],
214
- t3.to_a.map {|r| r.to_h_with_reserved })
211
+ assert_equal([{"a"=>"1", "b"=>"2", "c"=>"3"},
212
+ {"a"=>"3", "b"=>"4", "c"=>"5"},
213
+ {"a"=>"0", "b"=>"4", "c"=>"5"}],
214
+ t3.to_a.map {|r| r.to_h })
215
215
  end
216
216
 
217
217
  def test_natjoin2_nocommon
@@ -229,12 +229,12 @@ class TestTbBasic < Test::Unit::TestCase
229
229
  t1 = Tb.new %w[a b], %w[1 2], %w[3 4], %w[0 4], %w[0 1]
230
230
  t2 = Tb.new %w[b c], %w[2 3], %w[4 5], %w[5 8]
231
231
  t3 = t1.natjoin2_outer(t2)
232
- assert_equal([{"_recordid"=>0, "a"=>"1", "b"=>"2", "c"=>"3"},
233
- {"_recordid"=>1, "a"=>"3", "b"=>"4", "c"=>"5"},
234
- {"_recordid"=>2, "a"=>"0", "b"=>"4", "c"=>"5"},
235
- {"_recordid"=>3, "a"=>"0", "b"=>"1"},
236
- {"_recordid"=>4, "b"=>"5", "c"=>"8"}],
237
- t3.to_a.map {|r| r.to_h_with_reserved })
232
+ assert_equal([{"a"=>"0", "b"=>"1"},
233
+ {"a"=>"1", "b"=>"2", "c"=>"3"},
234
+ {"a"=>"3", "b"=>"4", "c"=>"5"},
235
+ {"a"=>"0", "b"=>"4", "c"=>"5"},
236
+ {"b"=>"5", "c"=>"8"}],
237
+ t3.to_a.map {|r| r.to_h })
238
238
  end
239
239
 
240
240
  def test_fmap!
@@ -384,4 +384,20 @@ class TestTbBasic < Test::Unit::TestCase
384
384
  assert_equal([['banana', 'yellow'], ['orange', 'orange']], rs)
385
385
  end
386
386
 
387
+ def test_with_header
388
+ t = Tb.new %w[a b], [1, 2], [3, 4]
389
+ result = []
390
+ t.with_header {|x|
391
+ result << x
392
+ }.each {|x|
393
+ result << x
394
+ }
395
+ assert_equal(3, result.length)
396
+ assert_equal(%w[a b], result[0])
397
+ assert_kind_of(Tb::Record, result[1])
398
+ assert_kind_of(Tb::Record, result[2])
399
+ assert_equal([["a", 1], ["b", 2]], result[1].to_a)
400
+ assert_equal([["a", 3], ["b", 4]], result[2].to_a)
401
+ end
402
+
387
403
  end
@@ -14,12 +14,13 @@ class TestTbCatReader < Test::Unit::TestCase
14
14
  3,4
15
15
  End
16
16
  Tb::CatReader.open([i1, i2]) {|r|
17
- assert_equal(%w[a b], r.header)
18
- assert_equal([%w[1 2], %w[4 3]], r.read_all)
19
- assert_equal(0, r.index_from_field("a"))
20
- assert_equal("b", r.field_from_index(1))
21
- assert_equal(2, r.index_from_field_ex("1"))
22
- assert_equal("2", r.field_from_index_ex(3))
17
+ result = []
18
+ r.with_header {|header|
19
+ result << header
20
+ }.each {|pairs|
21
+ result << pairs.to_a
22
+ }
23
+ assert_equal([%w[a b], [['a','1'], ['b','2']], [['b','3'],['a','4']]], result)
23
24
  }
24
25
  }
25
26
  end
data/test/test_cmd_cat.rb CHANGED
@@ -115,4 +115,36 @@ class TestTbCmdCat < Test::Unit::TestCase
115
115
  End
116
116
  end
117
117
 
118
+ def test_with_filename
119
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
120
+ a,b,c
121
+ 1,2,3
122
+ End
123
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
124
+ a,b,c
125
+ 4,5,6
126
+ End
127
+ Tb::Cmd.main_cat(['-o', o="o.csv", '--with-filename', i1, i2])
128
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
129
+ filename,a,b,c
130
+ i1.csv,1,2,3
131
+ i2.csv,4,5,6
132
+ End
133
+ end
134
+
135
+ def test_empty
136
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
137
+ a,b,c
138
+ 1,2,3
139
+ End
140
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
141
+ a,b,c
142
+ End
143
+ Tb::Cmd.main_cat(['-o', o="o.csv", '--with-filename', i1, i2])
144
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
145
+ filename,a,b,c
146
+ i1.csv,1,2,3
147
+ End
148
+ end
149
+
118
150
  end
@@ -87,4 +87,14 @@ class TestTbCmdConsecutive < Test::Unit::TestCase
87
87
  End
88
88
  end
89
89
 
90
+ def test_header_only
91
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
92
+ a,b,c
93
+ End
94
+ Tb::Cmd.main_consecutive(['-o', o="o.csv", i])
95
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
96
+ a_1,a_2,b_1,b_2,c_1,c_2
97
+ End
98
+ end
99
+
90
100
  end
@@ -19,16 +19,16 @@ class TestTbCmdCrop < Test::Unit::TestCase
19
19
  File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
20
  ,,
21
21
  ,a,b,,
22
- ,0,1,,,
23
- ,4,,,
22
+ ,0,1,,2,
23
+ ,,4,,,
24
24
  ,,,
25
25
 
26
26
  End
27
27
  Tb::Cmd.main_crop(['-o', o="o.csv", i])
28
28
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
29
29
  a,b
30
- 0,1
31
- 4
30
+ 0,1,,2
31
+ ,4
32
32
  End
33
33
  end
34
34
 
@@ -23,7 +23,7 @@ class TestTbCmdCross < Test::Unit::TestCase
23
23
  bbbb,2000,4
24
24
  cccc,2002,5
25
25
  End
26
- Tb::Cmd.main_cross(['-o', o="o.csv", 'name', 'year', i])
26
+ Tb::Cmd.main_cross(['-o', o="o.csv", 'year', 'name', i])
27
27
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
28
28
  name,aaaa,bbbb,cccc
29
29
  year,count,count,count
@@ -43,6 +43,18 @@ class TestTbCmdCross < Test::Unit::TestCase
43
43
  assert(!exc.success?)
44
44
  end
45
45
 
46
+ def test_field_not_found
47
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
48
+ name,year,observ
49
+ aaaa,2000,1
50
+ bbbb,2001,3
51
+ End
52
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_cross(['-o', "o.csv", 'foo', 'year', i]) }
53
+ assert(!exc.success?)
54
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_cross(['-o', "o.csv", 'name', 'bar', i]) }
55
+ assert(!exc.success?)
56
+ end
57
+
46
58
  def test_compact
47
59
  File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
48
60
  name,year,observ
@@ -51,7 +63,7 @@ class TestTbCmdCross < Test::Unit::TestCase
51
63
  bbbb,2000,4
52
64
  cccc,2002,5
53
65
  End
54
- Tb::Cmd.main_cross(['-o', o="o.csv", 'name', 'year', '-c', i])
66
+ Tb::Cmd.main_cross(['-o', o="o.csv", 'year', 'name', '-c', i])
55
67
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
56
68
  year,aaaa,bbbb,cccc
57
69
  2000,1,1,
@@ -69,7 +81,7 @@ class TestTbCmdCross < Test::Unit::TestCase
69
81
  cccc,2002,5
70
82
  aaaa,2000,2
71
83
  End
72
- Tb::Cmd.main_cross(['-o', o="o.csv", 'name', 'year', '-a', 'sum(observ)', i])
84
+ Tb::Cmd.main_cross(['-o', o="o.csv", 'year', 'name', '-a', 'sum(observ)', i])
73
85
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
74
86
  name,aaaa,bbbb,cccc
75
87
  year,sum(observ),sum(observ),sum(observ)
@@ -90,7 +102,7 @@ class TestTbCmdCross < Test::Unit::TestCase
90
102
  5,6
91
103
  7,8
92
104
  End
93
- Tb::Cmd.main_cross(['-o', o="o.csv", 'a', 'b', i1, i2])
105
+ Tb::Cmd.main_cross(['-o', o="o.csv", 'b', 'a', i1, i2])
94
106
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
95
107
  a,1,3,6,8
96
108
  b,count,count,count,count
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdGitLog < Test::Unit::TestCase
6
+ def setup
7
+ Tb::Cmd.reset_option
8
+ @curdir = Dir.pwd
9
+ @tmpdir = Dir.mktmpdir
10
+ Dir.chdir @tmpdir
11
+ end
12
+ def teardown
13
+ Tb::Cmd.reset_option
14
+ Dir.chdir @curdir
15
+ FileUtils.rmtree @tmpdir
16
+ end
17
+
18
+ def test_basic
19
+ system("git init -q")
20
+ File.open("foo", "w") {|f| f.puts "bar" }
21
+ system("git add foo")
22
+ system("git commit -q -m msg foo")
23
+ Tb::Cmd.main_git_log(['-o', o="o.csv"])
24
+ result = File.read(o)
25
+ tb = Tb.parse_csv(result)
26
+ assert_equal(1, tb.size)
27
+ assert_match(/,A,foo\n/, tb.get_record(0)["files"])
28
+ end
29
+
30
+ def test_escaped_filename
31
+ filename = (1..32).to_a.pack("C*") + "\"\\"
32
+ system("git init -q")
33
+ File.open(filename, "w") {|f| f.puts "bar" }
34
+ system("git", "add", filename)
35
+ system("git", "commit", "-q", "-m", "msg", filename)
36
+ Tb::Cmd.main_git_log(['-o', o="o.csv"])
37
+ result = File.read(o)
38
+ tb = Tb.parse_csv(result)
39
+ assert_equal(1, tb.size)
40
+ assert_match(/,A,/, tb.get_record(0)["files"])
41
+ ftb = Tb.parse_csv(tb.get_record(0)["files"])
42
+ assert_equal(1, ftb.size)
43
+ assert_equal(filename, ftb.get_record(0)["filename"])
44
+ end
45
+
46
+ end
@@ -16,24 +16,22 @@ class TestTbCmdHelp < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  def with_stdout(io)
19
- save = STDOUT.dup
20
- STDOUT.reopen(io)
19
+ save = $stdout
20
+ $stdout = io
21
21
  begin
22
22
  yield
23
23
  ensure
24
- STDOUT.reopen(save)
25
- save.close
24
+ $stdout = save
26
25
  end
27
26
  end
28
27
 
29
28
  def with_stderr(io)
30
- save = STDERR.dup
31
- STDERR.reopen(io)
29
+ save = $stderr
30
+ $stderr = io
32
31
  begin
33
32
  yield
34
33
  ensure
35
- STDERR.reopen(save)
36
- save.close
34
+ $stderr = save
37
35
  end
38
36
  end
39
37
 
@@ -81,7 +79,7 @@ class TestTbCmdHelp < Test::Unit::TestCase
81
79
  msg = th.value
82
80
  r.close
83
81
  assert_match(/Usage:/, msg)
84
- assert_match(/ tb csv /, msg)
82
+ assert_match(/ tb to-csv /, msg)
85
83
  assert_match(/ tb cut /, msg)
86
84
  }
87
85
  end
@@ -90,7 +88,7 @@ class TestTbCmdHelp < Test::Unit::TestCase
90
88
  assert_equal(true, Tb::Cmd.main(['-h', '-o', o="msg"]))
91
89
  msg = File.read(o)
92
90
  assert_match(/Usage:/, msg)
93
- assert_match(/ tb csv /, msg)
91
+ assert_match(/ tb to-csv /, msg)
94
92
  assert_match(/ tb cut /, msg)
95
93
  end
96
94
 
@@ -98,7 +96,7 @@ class TestTbCmdHelp < Test::Unit::TestCase
98
96
  assert_equal(true, Tb::Cmd.main(['--help', '-o', o="msg"]))
99
97
  msg = File.read(o)
100
98
  assert_match(/Usage:/, msg)
101
- assert_match(/ tb csv /, msg)
99
+ assert_match(/ tb to-csv /, msg)
102
100
  assert_match(/ tb cut /, msg)
103
101
  end
104
102
 
@@ -106,7 +104,7 @@ class TestTbCmdHelp < Test::Unit::TestCase
106
104
  assert_equal(true, Tb::Cmd.main(['help', '-o', o="msg"]))
107
105
  msg = File.read(o)
108
106
  assert_match(/Usage:/, msg)
109
- assert_match(/ tb csv /, msg)
107
+ assert_match(/ tb to-csv /, msg)
110
108
  assert_match(/ tb cut /, msg)
111
109
  end
112
110
 
@@ -187,4 +185,11 @@ class TestTbCmdHelp < Test::Unit::TestCase
187
185
  assert_match(/unexpected subcommand/, exc.message)
188
186
  end
189
187
 
188
+ def test_help_s
189
+ assert_exit_success(['help', '-o', o="msg", '-s'])
190
+ msg = File.read(o)
191
+ assert_match(/Show help message of tb command/, msg)
192
+ assert_no_match(/Usage:/, msg)
193
+ end
194
+
190
195
  end
@@ -64,6 +64,26 @@ class TestTbCmdJoin < Test::Unit::TestCase
64
64
  End
65
65
  end
66
66
 
67
+ def test_outer2
68
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
69
+ b,c
70
+ 4,5
71
+ 6,7
72
+ End
73
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
74
+ a,b
75
+ 1,2
76
+ 3,4
77
+ End
78
+ Tb::Cmd.main_join(['-o', o="o.csv", '--outer', i1, i2])
79
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
80
+ b,c,a
81
+ 2,,1
82
+ 4,5,3
83
+ 6,7,
84
+ End
85
+ end
86
+
67
87
  def test_outer_missing
68
88
  File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
69
89
  a,b
@@ -189,8 +209,8 @@ class TestTbCmdJoin < Test::Unit::TestCase
189
209
  Tb::Cmd.main_join(['-o', o="o.csv", '--right', i1, i2, i3])
190
210
  assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
191
211
  a,b,c,d
192
- 3,,D,F
193
212
  2,,,E
213
+ 3,,D,F
194
214
  End
195
215
  end
196
216
 
data/test/test_cmd_ls.rb CHANGED
@@ -16,13 +16,12 @@ class TestTbCmdLs < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  def with_stderr(io)
19
- save = STDERR.dup
20
- STDERR.reopen(io)
19
+ save = $stderr
20
+ $stderr = io
21
21
  begin
22
22
  yield
23
23
  ensure
24
- STDERR.reopen(save)
25
- save.close
24
+ $stderr = save
26
25
  end
27
26
  end
28
27
 
@@ -15,6 +15,16 @@ class TestTbCmdMheader < Test::Unit::TestCase
15
15
  FileUtils.rmtree @tmpdir
16
16
  end
17
17
 
18
+ def with_stderr(io)
19
+ save = $stderr
20
+ $stderr = io
21
+ begin
22
+ yield
23
+ ensure
24
+ $stderr = save
25
+ end
26
+ end
27
+
18
28
  def test_basic
19
29
  File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
30
  ,2000,2000,2001,2001
@@ -48,18 +58,14 @@ class TestTbCmdMheader < Test::Unit::TestCase
48
58
  a,a
49
59
  1,1
50
60
  End
51
- save = STDERR.dup
52
- logf = File.open(log="log", "w")
53
- STDERR.reopen(logf)
54
- logf.close
55
- Tb::Cmd.main_mheader(['-o', o="o.csv", i])
56
- STDERR.reopen(save)
57
- save.close
61
+ o = nil
62
+ File.open(log="log", "w") {|logf|
63
+ with_stderr(logf) {
64
+ Tb::Cmd.main_mheader(['-o', o="o.csv", i])
65
+ }
66
+ }
58
67
  assert_equal('', File.read(o))
59
- assert_match(/no header found/, File.read(log))
60
- ensure
61
- save.close if save && !save.closed?
62
- logf.close if logf && !logf.closed?
68
+ assert_match(/unique header fields not recognized/, File.read(log))
63
69
  end
64
70
 
65
71
  def test_twofile