ptools 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +4 -0
- data/lib/ptools.rb +17 -16
- data/ptools.gemspec +1 -1
- data/test/test_constants.rb +1 -1
- data/test/test_tail.rb +75 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0041b6df231b056f194bffcee580248797c341c5
|
4
|
+
data.tar.gz: 196e358a536fa6b2d7bc6243c3fe4d320c65860a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a611871af4c407b0ca637d1aa1a9e76854813a9ba52219a4abc97ac806d1af1e78e26fabc877abb97d099d7fa3a5d4fcfa6e083ec3e54a8eeb012410fb75fe17
|
7
|
+
data.tar.gz: 11d52f61ad1fc09fabb0c0c5a2006db4c917df704dc9c7252d3d41567762697fcb48bae93842c0f9c90a855286eed80f7224cdafd39e658923a7d1828b8f1859
|
data/CHANGES
CHANGED
data/lib/ptools.rb
CHANGED
@@ -3,7 +3,7 @@ require 'win32/file' if File::ALT_SEPARATOR
|
|
3
3
|
|
4
4
|
class File
|
5
5
|
# The version of the ptools library.
|
6
|
-
PTOOLS_VERSION = '1.3.
|
6
|
+
PTOOLS_VERSION = '1.3.1'
|
7
7
|
|
8
8
|
# :stopdoc:
|
9
9
|
|
@@ -252,30 +252,31 @@ class File
|
|
252
252
|
|
253
253
|
# MS Windows gets unhappy if you try to seek backwards past the
|
254
254
|
# end of the file, so we have some extra checks here and later.
|
255
|
-
file_size
|
256
|
-
|
257
|
-
|
255
|
+
file_size = File.size(filename)
|
256
|
+
read_bytes = file_size % tail_size
|
257
|
+
read_bytes = tail_size if read_bytes == 0
|
258
|
+
line_sep = File::ALT_SEPARATOR ? "\r\n" : "\n"
|
258
259
|
|
259
260
|
buf = ''
|
260
261
|
|
261
262
|
File.open(filename){ |fh|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
end
|
263
|
+
# Set the starting read position
|
264
|
+
position = file_size - read_bytes
|
265
|
+
|
266
|
+
# Loop until we have the lines or run out of file
|
267
|
+
while buf.scan(line_sep).size <= num_lines and position >= 0
|
268
|
+
fh.seek(position, IO::SEEK_SET)
|
269
|
+
buf = fh.read(read_bytes) + buf
|
270
|
+
read_bytes = tail_size
|
271
|
+
position -= read_bytes
|
272
272
|
end
|
273
273
|
}
|
274
274
|
|
275
|
+
lines = buf.split(line_sep).pop(num_lines)
|
275
276
|
if block_given?
|
276
|
-
|
277
|
+
lines.each{ |line| yield line }
|
277
278
|
else
|
278
|
-
|
279
|
+
lines
|
279
280
|
end
|
280
281
|
end
|
281
282
|
|
data/ptools.gemspec
CHANGED
data/test/test_constants.rb
CHANGED
@@ -15,7 +15,7 @@ class TC_Ptools_Constants < Test::Unit::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
test "PTOOLS_VERSION constant is set to expected value" do
|
18
|
-
assert_equal('1.3.
|
18
|
+
assert_equal('1.3.1', File::PTOOLS_VERSION)
|
19
19
|
end
|
20
20
|
|
21
21
|
test "IMAGE_EXT constant is set to array of values" do
|
data/test/test_tail.rb
CHANGED
@@ -9,18 +9,61 @@ require 'ptools'
|
|
9
9
|
|
10
10
|
class TC_FileTail < Test::Unit::TestCase
|
11
11
|
def self.startup
|
12
|
+
|
12
13
|
Dir.chdir('test') if File.exist?('test')
|
13
|
-
|
14
|
+
|
15
|
+
File.open('test_file1.txt', 'w'){ |fh|
|
16
|
+
25.times{ |n| fh.puts "line#{n+1}" }
|
17
|
+
}
|
18
|
+
|
19
|
+
# Trailing newline test
|
20
|
+
File.open('test_file_trail.txt', 'w'){ |fh|
|
21
|
+
2.times{ |n| fh.puts "trail#{n+1}" }
|
22
|
+
fh.write "trail3"
|
23
|
+
}
|
24
|
+
File.open('test_file_trail_nl.txt', 'w'){ |fh|
|
25
|
+
3.times{ |n| fh.puts "trail#{n+1}" }
|
26
|
+
}
|
27
|
+
|
28
|
+
# Larger files
|
29
|
+
test_tail_fmt_str = "line data data data data data data data %5s"
|
30
|
+
|
31
|
+
File.open('test_file64.txt', 'w'){ |fh|
|
32
|
+
2000.times{ |n|
|
33
|
+
fh.puts test_tail_fmt_str % (n+1).to_s
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
File.open('test_file128.txt', 'w'){ |fh|
|
38
|
+
4500.times{ |n|
|
39
|
+
fh.puts test_tail_fmt_str % (n+1).to_s
|
40
|
+
}
|
41
|
+
|
42
|
+
}
|
14
43
|
end
|
15
44
|
|
16
45
|
def setup
|
17
|
-
@test_file
|
46
|
+
@test_file = 'test_file1.txt'
|
47
|
+
@test_trail = 'test_file_trail.txt'
|
48
|
+
@test_trail_nl = 'test_file_trail_nl.txt'
|
49
|
+
@test_file_64 = 'test_file64.txt'
|
50
|
+
@test_file_128 = 'test_file128.txt'
|
18
51
|
|
19
|
-
@expected_tail1 =
|
20
|
-
|
21
|
-
|
52
|
+
@expected_tail1 = %w{
|
53
|
+
line16 line17 line18 line19 line20
|
54
|
+
line21 line22 line23 line24 line25
|
55
|
+
}
|
22
56
|
|
23
57
|
@expected_tail2 = ["line21","line22","line23","line24","line25"]
|
58
|
+
|
59
|
+
@expected_tail_more = []
|
60
|
+
25.times{ |n| @expected_tail_more.push "line#{n+1}" }
|
61
|
+
|
62
|
+
@expected_tail_trail = %w{ trail2 trail3 }
|
63
|
+
|
64
|
+
@test_tail_fmt_str = "line data data data data data data data %5s"
|
65
|
+
|
66
|
+
|
24
67
|
end
|
25
68
|
|
26
69
|
def test_tail_basic
|
@@ -36,11 +79,34 @@ class TC_FileTail < Test::Unit::TestCase
|
|
36
79
|
assert_equal(@expected_tail2, File.tail(@test_file, 5))
|
37
80
|
end
|
38
81
|
|
82
|
+
def test_more_lines_than_file
|
83
|
+
assert_equal( @expected_tail_more, File.tail(@test_file, 30) )
|
84
|
+
end
|
85
|
+
|
39
86
|
def test_tail_expected_errors
|
40
87
|
assert_raises(ArgumentError){ File.tail }
|
41
88
|
assert_raises(ArgumentError){ File.tail(@test_file, 5, 5) }
|
42
89
|
end
|
43
90
|
|
91
|
+
def test_no_trailing_newline
|
92
|
+
assert_equal( @expected_tail_trail, File.tail(@test_trail, 2) )
|
93
|
+
assert_equal( @expected_tail_trail, File.tail(@test_trail_nl, 2) )
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_tail_larger_than_64k
|
97
|
+
expected_tail_64k=[]
|
98
|
+
2000.times{ |n| expected_tail_64k.push( @test_tail_fmt_str % (n+1).to_s ) }
|
99
|
+
assert_equal( expected_tail_64k, File.tail(@test_file_64, 2000) )
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_tail_larger_than_128k
|
103
|
+
expected_tail_128k = []
|
104
|
+
4500.times{ |n| expected_tail_128k.push( @test_tail_fmt_str % (n+1).to_s ) }
|
105
|
+
assert_equal( expected_tail_128k, File.tail(@test_file_128, 4500) )
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
|
44
110
|
def teardown
|
45
111
|
@test_file = nil
|
46
112
|
@expected_tail1 = nil
|
@@ -49,5 +115,9 @@ class TC_FileTail < Test::Unit::TestCase
|
|
49
115
|
|
50
116
|
def self.shutdown
|
51
117
|
File.delete('test_file1.txt') if File.exist?('test_file1.txt')
|
118
|
+
File.delete('test_file64.txt') if File.exist?('test_file64.txt')
|
119
|
+
File.delete('test_file128.txt') if File.exist?('test_file128.txt')
|
120
|
+
File.delete('test_file_trail_nl.txt') if File.exist?('test_file_trail_nl.txt')
|
121
|
+
File.delete('test_file_trail.txt') if File.exist?('test_file_trail.txt')
|
52
122
|
end
|
53
123
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ptools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel J. Berger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|