tb 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/README +156 -5
  2. data/bin/tb +2 -1110
  3. data/lib/tb.rb +4 -2
  4. data/lib/tb/catreader.rb +131 -0
  5. data/lib/tb/cmd_cat.rb +65 -0
  6. data/lib/tb/cmd_consecutive.rb +79 -0
  7. data/lib/tb/cmd_crop.rb +105 -0
  8. data/lib/tb/cmd_cross.rb +119 -0
  9. data/lib/tb/cmd_csv.rb +42 -0
  10. data/lib/tb/cmd_cut.rb +77 -0
  11. data/lib/tb/cmd_grep.rb +76 -0
  12. data/lib/tb/cmd_group.rb +82 -0
  13. data/lib/tb/cmd_gsub.rb +77 -0
  14. data/lib/tb/cmd_help.rb +98 -0
  15. data/lib/tb/cmd_join.rb +81 -0
  16. data/lib/tb/cmd_json.rb +60 -0
  17. data/lib/tb/cmd_ls.rb +273 -0
  18. data/lib/tb/cmd_mheader.rb +77 -0
  19. data/lib/tb/cmd_newfield.rb +59 -0
  20. data/lib/tb/cmd_pnm.rb +43 -0
  21. data/lib/tb/cmd_pp.rb +70 -0
  22. data/lib/tb/cmd_rename.rb +58 -0
  23. data/lib/tb/cmd_shape.rb +67 -0
  24. data/lib/tb/cmd_sort.rb +58 -0
  25. data/lib/tb/cmd_svn_log.rb +158 -0
  26. data/lib/tb/cmd_tsv.rb +43 -0
  27. data/lib/tb/cmd_yaml.rb +47 -0
  28. data/lib/tb/cmdmain.rb +45 -0
  29. data/lib/tb/cmdtop.rb +58 -0
  30. data/lib/tb/cmdutil.rb +327 -0
  31. data/lib/tb/csv.rb +30 -6
  32. data/lib/tb/fieldset.rb +39 -41
  33. data/lib/tb/pager.rb +132 -0
  34. data/lib/tb/pnm.rb +357 -0
  35. data/lib/tb/reader.rb +18 -128
  36. data/lib/tb/record.rb +3 -3
  37. data/lib/tb/ropen.rb +70 -0
  38. data/lib/tb/{pathfinder.rb → search.rb} +69 -34
  39. data/lib/tb/tsv.rb +29 -1
  40. data/sample/colors.ppm +0 -0
  41. data/sample/gradation.pgm +0 -0
  42. data/sample/langs.csv +46 -0
  43. data/sample/tbplot +293 -0
  44. data/test-all-cov.rb +65 -0
  45. data/test-all.rb +5 -0
  46. data/test/test_basic.rb +99 -2
  47. data/test/test_catreader.rb +27 -0
  48. data/test/test_cmd_cat.rb +118 -0
  49. data/test/test_cmd_consecutive.rb +90 -0
  50. data/test/test_cmd_crop.rb +101 -0
  51. data/test/test_cmd_cross.rb +113 -0
  52. data/test/test_cmd_csv.rb +129 -0
  53. data/test/test_cmd_cut.rb +100 -0
  54. data/test/test_cmd_grep.rb +89 -0
  55. data/test/test_cmd_group.rb +181 -0
  56. data/test/test_cmd_gsub.rb +103 -0
  57. data/test/test_cmd_help.rb +190 -0
  58. data/test/test_cmd_join.rb +197 -0
  59. data/test/test_cmd_json.rb +75 -0
  60. data/test/test_cmd_ls.rb +203 -0
  61. data/test/test_cmd_mheader.rb +86 -0
  62. data/test/test_cmd_newfield.rb +63 -0
  63. data/test/test_cmd_pnm.rb +35 -0
  64. data/test/test_cmd_pp.rb +62 -0
  65. data/test/test_cmd_rename.rb +91 -0
  66. data/test/test_cmd_shape.rb +50 -0
  67. data/test/test_cmd_sort.rb +105 -0
  68. data/test/test_cmd_tsv.rb +67 -0
  69. data/test/test_cmd_yaml.rb +55 -0
  70. data/test/test_cmdtty.rb +154 -0
  71. data/test/test_cmdutil.rb +43 -0
  72. data/test/test_csv.rb +10 -0
  73. data/test/test_fieldset.rb +42 -0
  74. data/test/test_pager.rb +142 -0
  75. data/test/test_pnm.rb +374 -0
  76. data/test/test_reader.rb +147 -0
  77. data/test/test_record.rb +49 -0
  78. data/test/test_search.rb +575 -0
  79. data/test/test_tsv.rb +7 -0
  80. metadata +108 -5
  81. data/lib/tb/qtsv.rb +0 -93
@@ -0,0 +1,75 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+ begin
5
+ require 'json'
6
+ rescue LoadError
7
+ end
8
+
9
+ class TestTbCmdJSON < Test::Unit::TestCase
10
+ def setup
11
+ Tb::Cmd.reset_option
12
+ @curdir = Dir.pwd
13
+ @tmpdir = Dir.mktmpdir
14
+ Dir.chdir @tmpdir
15
+ end
16
+ def teardown
17
+ Tb::Cmd.reset_option
18
+ Dir.chdir @curdir
19
+ FileUtils.rmtree @tmpdir
20
+ end
21
+
22
+ def test_basic
23
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
24
+ a,b,c
25
+ 0,1,2
26
+ 4,5,6
27
+ End
28
+ Tb::Cmd.main_json(['-o', o="o.json", i])
29
+ assert_equal(<<-"End".gsub(/\s/, ''), File.read(o).gsub(/\s/, ''))
30
+ [{
31
+ "a": "0",
32
+ "b": "1",
33
+ "c": "2"
34
+ },
35
+ {
36
+ "a": "4",
37
+ "b": "5",
38
+ "c": "6"
39
+ }]
40
+ End
41
+ end
42
+
43
+ def test_twofile
44
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
45
+ a,b
46
+ 1,2
47
+ 3,4
48
+ End
49
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
50
+ b,a
51
+ 5,6
52
+ 7,8
53
+ End
54
+ Tb::Cmd.main_json(['-o', o="o.csv", i1, i2])
55
+ assert_equal(<<-"End".gsub(/\s/, ''), File.read(o).gsub(/\s/, ''))
56
+ [{
57
+ "a": "1",
58
+ "b": "2"
59
+ },
60
+ {
61
+ "a": "3",
62
+ "b": "4"
63
+ },
64
+ {
65
+ "b": "5",
66
+ "a": "6"
67
+ },
68
+ {
69
+ "b": "7",
70
+ "a": "8"
71
+ }]
72
+ End
73
+ end
74
+
75
+ end if defined?(JSON)
@@ -0,0 +1,203 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdLs < 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 with_stderr(io)
19
+ save = STDERR.dup
20
+ STDERR.reopen(io)
21
+ begin
22
+ yield
23
+ ensure
24
+ STDERR.reopen(save)
25
+ save.close
26
+ end
27
+ end
28
+
29
+ def with_pipe
30
+ r, w = IO.pipe
31
+ begin
32
+ yield r, w
33
+ ensure
34
+ r.close if !r.closed?
35
+ w.close if !w.closed?
36
+ end
37
+ end
38
+
39
+ def reader_thread(io)
40
+ Thread.new {
41
+ r = ''
42
+ loop {
43
+ begin
44
+ r << io.readpartial(4096)
45
+ rescue EOFError, Errno::EIO
46
+ break
47
+ end
48
+ }
49
+ r
50
+ }
51
+ end
52
+
53
+ def test_basic
54
+ Dir.mkdir("d")
55
+ File.open("d/a", "w") {}
56
+ Tb::Cmd.main_ls(['-o', o="o.csv", "d"])
57
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
58
+ filename
59
+ d/a
60
+ End
61
+ end
62
+
63
+ def test_opt_l_single
64
+ File.open("a", "w") {}
65
+ File.chmod(0754, "a")
66
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-l', 'a'])
67
+ tb = Tb.load_csv(o)
68
+ assert_equal(1, tb.size)
69
+ assert_equal(%w[filemode nlink user group size mtime filename symlink], tb.list_fields)
70
+ rec = tb.get_record(0)
71
+ assert_equal("-rwxr-xr--", rec["filemode"])
72
+ assert_equal("1", rec["nlink"])
73
+ assert_match(/\A\d+\z/, rec["size"])
74
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["mtime"])
75
+ assert_equal("a", rec["filename"])
76
+ assert_equal(nil, rec["symlink"])
77
+ end
78
+
79
+ def test_opt_l
80
+ Dir.mkdir("d")
81
+ File.open("d/a", "w") {}
82
+ Dir.mkdir("d/d2")
83
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-l', 'd'])
84
+ tb = Tb.load_csv(o)
85
+ assert_equal(2, tb.size)
86
+ assert_equal("d/a", tb.get_record(0)["filename"])
87
+ assert_equal("d/d2", tb.get_record(1)["filename"])
88
+ end
89
+
90
+ def test_opt_ll_single
91
+ File.open("a", "w") {}
92
+ File.chmod(0754, "a")
93
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-ll', 'a'])
94
+ tb = Tb.load_csv(o)
95
+ assert_equal(1, tb.size)
96
+ assert_equal(%w[dev ino mode filemode nlink uid user gid group rdev size blksize blocks atime mtime ctime filename symlink], tb.list_fields)
97
+ rec = tb.get_record(0)
98
+ assert_match(/\A0x[0-9a-f]+\z/, rec["dev"])
99
+ assert_match(/\A\d+\z/, rec["ino"])
100
+ assert_match(/\A0[0-7]+\z/, rec["mode"])
101
+ assert_equal("-rwxr-xr--", rec["filemode"])
102
+ assert_equal("1", rec["nlink"])
103
+ assert_match(/\A\d+\z/, rec["uid"])
104
+ assert_match(/\A\d+\z/, rec["gid"])
105
+ assert_match(/\A0x[0-9a-f]+\z/, rec["rdev"])
106
+ assert_match(/\A\d+\z/, rec["size"])
107
+ assert_match(/\A\d+\z/, rec["blksize"])
108
+ assert_match(/\A\d+\z/, rec["blocks"])
109
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["atime"])
110
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["mtime"])
111
+ assert_match(/\A\d+-\d\d-\d\dT\d\d:\d\d:\d\d/, rec["ctime"])
112
+ assert_equal("a", rec["filename"])
113
+ assert_equal(nil, rec["symlink"])
114
+ end
115
+
116
+ def test_opt_a
117
+ Dir.mkdir("d")
118
+ File.open("d/.foo", "w") {}
119
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-a', 'd'])
120
+ tb = Tb.load_csv(o)
121
+ assert_equal(3, tb.size)
122
+ assert_equal("d", tb.get_record(0)["filename"]) # d/.
123
+ assert_equal(".", tb.get_record(1)["filename"]) # d/..
124
+ assert_equal("d/.foo", tb.get_record(2)["filename"])
125
+ end
126
+
127
+ def test_opt_A
128
+ Dir.mkdir("d")
129
+ File.open("d/.foo", "w") {}
130
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-A', 'd'])
131
+ tb = Tb.load_csv(o)
132
+ assert_equal(1, tb.size)
133
+ assert_equal("d/.foo", tb.get_record(0)["filename"])
134
+ end
135
+
136
+ def test_opt_R
137
+ Dir.mkdir("d")
138
+ File.open("d/a", "w") {}
139
+ Dir.mkdir("d/d2")
140
+ File.open("d/d2/b", "w") {}
141
+ File.open("d/d2/c", "w") {}
142
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-R', 'd'])
143
+ tb = Tb.load_csv(o)
144
+ assert_equal(4, tb.size)
145
+ assert_equal("d/a", tb.get_record(0)["filename"])
146
+ assert_equal("d/d2", tb.get_record(1)["filename"])
147
+ assert_equal("d/d2/b", tb.get_record(2)["filename"])
148
+ assert_equal("d/d2/c", tb.get_record(3)["filename"])
149
+ end
150
+
151
+ def test_opt_Ra
152
+ Dir.mkdir("d")
153
+ File.open("d/a", "w") {}
154
+ Dir.mkdir("d/d2")
155
+ File.open("d/d2/b", "w") {}
156
+ File.open("d/d2/c", "w") {}
157
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-Ra', 'd'])
158
+ tb = Tb.load_csv(o)
159
+ assert_equal(8, tb.size)
160
+ assert_equal("d/.", tb.get_record(0)["filename"])
161
+ assert_equal("d/..", tb.get_record(1)["filename"])
162
+ assert_equal("d/a", tb.get_record(2)["filename"])
163
+ assert_equal("d/d2", tb.get_record(3)["filename"])
164
+ assert_equal("d/d2/.", tb.get_record(4)["filename"])
165
+ assert_equal("d/d2/..", tb.get_record(5)["filename"])
166
+ assert_equal("d/d2/b", tb.get_record(6)["filename"])
167
+ assert_equal("d/d2/c", tb.get_record(7)["filename"])
168
+ end
169
+
170
+ def test_symlink
171
+ File.symlink("a", "b")
172
+ Tb::Cmd.main_ls(['-o', o="o.csv", '-l', 'b'])
173
+ tb = Tb.load_csv(o)
174
+ assert_equal(1, tb.size)
175
+ assert_equal("b", tb.get_record(0)["filename"])
176
+ assert_equal("a", tb.get_record(0)["symlink"])
177
+ end
178
+
179
+ def test_not_found
180
+ File.open("log", "w") {|log|
181
+ with_stderr(log) {
182
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_ls(['-o', "o.csv", '-l', 'a']) }
183
+ assert(!exc.success?)
184
+ }
185
+ }
186
+ end
187
+
188
+ def test_dir_entries_fail
189
+ Dir.mkdir("d")
190
+ begin
191
+ File.chmod(0, "d")
192
+ File.open("log", "w") {|log|
193
+ with_stderr(log) {
194
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_ls(['-o', "o.csv", '-l', 'd']) }
195
+ assert(!exc.success?)
196
+ }
197
+ }
198
+ ensure
199
+ File.chmod(0700, "d")
200
+ end
201
+ end
202
+
203
+ end
@@ -0,0 +1,86 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdMheader < 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
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
+ ,2000,2000,2001,2001
21
+ name,aaaa,bbbb,aaaa,bbbb
22
+ x,1,2,3,4
23
+ y,5,6,7,8
24
+ End
25
+ Tb::Cmd.main_mheader(['-o', o="o.csv", i])
26
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
27
+ name,2000 aaaa,2000 bbbb,2001 aaaa,2001 bbbb
28
+ x,1,2,3,4
29
+ y,5,6,7,8
30
+ End
31
+ end
32
+
33
+ def test_opt_c
34
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
35
+ a,b,c
36
+ 1,2,3
37
+ 4,5,6
38
+ End
39
+ Tb::Cmd.main_mheader(['-o', o="o.csv", '-c', '2', i])
40
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
41
+ a 1,b 2,c 3
42
+ 4,5,6
43
+ End
44
+ end
45
+
46
+ def test_no_unique_header
47
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
48
+ a,a
49
+ 1,1
50
+ 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
58
+ 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?
63
+ end
64
+
65
+ def test_twofile
66
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
67
+ a,b
68
+ 1,2
69
+ 3,4
70
+ End
71
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
72
+ b,a
73
+ 5,6
74
+ 7,8
75
+ End
76
+ Tb::Cmd.main_mheader(['-o', o="o.csv", '-c', '2', i1, i2])
77
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
78
+ a 1,b 2
79
+ 3,4
80
+ b,a
81
+ 5,6
82
+ 7,8
83
+ End
84
+ end
85
+
86
+ end
@@ -0,0 +1,63 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdNewfield < 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
+ File.open(i="i.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
+ a,b
21
+ 1,2
22
+ 3,4
23
+ End
24
+ Tb::Cmd.main_newfield(['-o', o="o.csv", 'c', '_["a"].to_i + _["b"].to_i', i])
25
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
26
+ c,a,b
27
+ 3,1,2
28
+ 7,3,4
29
+ End
30
+ end
31
+
32
+ def test_no_new_field_name
33
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_newfield([]) }
34
+ assert(!exc.success?)
35
+ end
36
+
37
+ def test_no_newfield_ruby_exp
38
+ exc = assert_raise(SystemExit) { Tb::Cmd.main_newfield(['foo']) }
39
+ assert(!exc.success?)
40
+ end
41
+
42
+ def test_twofile
43
+ File.open(i1="i1.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
44
+ a,b
45
+ 1,2
46
+ 3,4
47
+ End
48
+ File.open(i2="i2.csv", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
49
+ b,a
50
+ 5,6
51
+ 7,8
52
+ End
53
+ Tb::Cmd.main_newfield(['-o', o="o.csv", 'c', '_["a"].to_i - _["b"].to_i', i1, i2])
54
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
55
+ c,a,b
56
+ -1,1,2
57
+ -1,3,4
58
+ 1,6,5
59
+ 1,8,7
60
+ End
61
+ end
62
+
63
+ end
@@ -0,0 +1,35 @@
1
+ require 'test/unit'
2
+ require 'tb/cmdtop'
3
+ require 'tmpdir'
4
+
5
+ class TestTbCmdPNM < 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
+ File.open(i="i.ppm", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
20
+ P1
21
+ 2 3
22
+ 10
23
+ 11
24
+ 01
25
+ End
26
+ Tb::Cmd.main_pnm(['-o', o="o.ppm", i])
27
+ assert_equal(<<-"End".gsub(/^[ \t]+/, ''), File.read(o))
28
+ P1
29
+ 2 3
30
+ 10
31
+ 11
32
+ 01
33
+ End
34
+ end
35
+ end