ptools 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +40 -0
- data/MANIFEST +20 -0
- data/README +62 -0
- data/lib/ptools.rb +230 -0
- data/test/tc_constants.rb +22 -0
- data/test/tc_head.rb +49 -0
- data/test/tc_middle.rb +58 -0
- data/test/tc_nlconvert.rb +79 -0
- data/test/tc_tail.rb +50 -0
- data/test/tc_touch.rb +41 -0
- data/test/tc_wc.rb +49 -0
- data/test/tc_whereis.rb +52 -0
- data/test/tc_which.rb +54 -0
- data/test/test_file1.txt +25 -0
- data/test/test_file2.txt +25 -0
- data/test/ts_all.rb +17 -0
- metadata +55 -0
data/CHANGES
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
== 1.0.0 - 2-Jun-2005
|
2
|
+
* Modified the File.middle method to accept an optional block.
|
3
|
+
* File.whereis is now limited to unique values so that redundant PATH entries
|
4
|
+
do not cause redundant entries in the returned array (or block).
|
5
|
+
* File.nl_convert and File.touch now return self.
|
6
|
+
* File.nl_convert now raises ArgumentError (instead of StandardError) if
|
7
|
+
an unknown platform is provided.
|
8
|
+
* File.wc now raises ArgumentError (instead of RuntimeError) if a bad option
|
9
|
+
is provided.
|
10
|
+
* Made documentation rdoc friendly.
|
11
|
+
* Test suite updates, corrections and additions.
|
12
|
+
* Removed the INSTALL file. See the README instead.
|
13
|
+
* Moved project to RubyForge.
|
14
|
+
|
15
|
+
== 0.1.3 - 5-Dec-2003
|
16
|
+
* Modified File#which and File#whereis for Win32 to handle extensions better,
|
17
|
+
i.e. you can send "ruby" or "ruby.exe" and get back the same result.
|
18
|
+
* Minor test changes to tc_which.rb and tc_whereis.rb.
|
19
|
+
|
20
|
+
== 0.1.2 - 14-May-2003
|
21
|
+
* Modified nl_convert() to allow the destination filename to be the same as the
|
22
|
+
source file. In that case, a tempfile is used and copied back over the
|
23
|
+
original file.
|
24
|
+
* The target file name for nl_convert() now defaults to the source file name and
|
25
|
+
the default format is now "dos".
|
26
|
+
* Added a tc_constants.rb test suite.
|
27
|
+
* Test suite additions/fixes.
|
28
|
+
* Updates to MANIFEST.
|
29
|
+
|
30
|
+
== 0.1.1 - 21-Mar-2003
|
31
|
+
* Modified File.tail to return data in the same order that 'tail' does.
|
32
|
+
* Modified File.which to return nil if the program is not found.
|
33
|
+
* Modified File.whereis now uses '\' instead of '/' on MS Windows.
|
34
|
+
* Added File.middle class method.
|
35
|
+
* Test suite modification & additions.
|
36
|
+
* Thanks go to Shanko for both the spot and patch for tail, which, whereis
|
37
|
+
and middle.
|
38
|
+
|
39
|
+
== 0.1.0 - 18-Mar-2003
|
40
|
+
* Initial release
|
data/MANIFEST
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
CHANGES
|
2
|
+
README
|
3
|
+
MANIFEST
|
4
|
+
install.rb
|
5
|
+
ptools.gemspec
|
6
|
+
|
7
|
+
lib/ptools.rb
|
8
|
+
|
9
|
+
test/ts_all.rb
|
10
|
+
test/tc_constants.rb
|
11
|
+
test/tc_head.rb
|
12
|
+
test/tc_middle.rb
|
13
|
+
test/tc_nlconvert.rb
|
14
|
+
test/tc_tail.rb
|
15
|
+
test/tc_touch.rb
|
16
|
+
test/tc_wc.rb
|
17
|
+
test/tc_which.rb
|
18
|
+
test/tc_whereis.rb
|
19
|
+
test/test_file.txt
|
20
|
+
test/test_file2.txt
|
data/README
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
== Description
|
2
|
+
The ptools (power tools) package is an additional set of commands for the
|
3
|
+
File class based on Unix command line tools.
|
4
|
+
|
5
|
+
== Prerequisites
|
6
|
+
Ruby 1.8.0 or later is recommended but not required.
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
ruby test/ts_all.rb (optional)
|
10
|
+
ruby install.rb
|
11
|
+
|
12
|
+
== Synopsis
|
13
|
+
require "ptools"
|
14
|
+
|
15
|
+
File.which("ruby") # '/usr/local/bin/ruby'
|
16
|
+
File.whereis("ruby") # ['/usr/local/bin/ruby','/opt/bin/ruby']
|
17
|
+
|
18
|
+
File.head("myfile") # Returns first 10 lines of 'myfile'
|
19
|
+
File.middle("myfile",8,12) # Returns lines 8-12 of 'myfile'
|
20
|
+
File.tail("myfile",3) # Returns last 3 lines of 'myfile'
|
21
|
+
File.wc("myfile",'words') # Returns the number of words in 'myfile'
|
22
|
+
|
23
|
+
File.touch("newfile") # "newfile" now exists
|
24
|
+
|
25
|
+
# Creates a copy of 'myfile' called 'newfile', in DOS format
|
26
|
+
File.nl_convert("myfile", "newfile", "dos")
|
27
|
+
|
28
|
+
== Known Bugs
|
29
|
+
There is a bug in 1.6.x that can cause $\ characters to accumulate when
|
30
|
+
converting to DOS or MAC format if nl_convert is run multiple times on
|
31
|
+
the same file. This appears to be fixed in 1.8.x.
|
32
|
+
|
33
|
+
== Acknowledgements
|
34
|
+
The which() method was adopted from the FileWhich code posted by Michael
|
35
|
+
Granger on http://www.rubygarden.org. The 'whereis' command is a minor
|
36
|
+
modification of that code as well.
|
37
|
+
|
38
|
+
The nl_convert() method was adopted (somewhat) from the nlcvt program
|
39
|
+
found at http://www.perl.com/language/ppt/src/nlcvt/nlcvt, written by
|
40
|
+
Tom Christiansen.
|
41
|
+
|
42
|
+
The middle() method was provided by Shashank Date.
|
43
|
+
|
44
|
+
== Future Plans
|
45
|
+
Add whatever other tools people think might be useful.
|
46
|
+
|
47
|
+
== License
|
48
|
+
Ruby's
|
49
|
+
|
50
|
+
== Copyright
|
51
|
+
(C) 2003-2005 Daniel J. Berger
|
52
|
+
All Rights Reserved.
|
53
|
+
|
54
|
+
== Warranty
|
55
|
+
This package is provided "as is" and without any express or
|
56
|
+
implied warranties, including, without limitation, the implied
|
57
|
+
warranties of merchantability and fitness for a particular purpose.
|
58
|
+
|
59
|
+
== Author
|
60
|
+
Daniel J. Berger
|
61
|
+
djberg96 at gmail dot com
|
62
|
+
imperator on IRC (irc.freenode.net)
|
data/lib/ptools.rb
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
class File
|
2
|
+
|
3
|
+
PTOOLS_VERSION = "1.0.0"
|
4
|
+
WIN32EXTS = %w/.exe .com .bat/
|
5
|
+
|
6
|
+
# Looks for the first occurrence of +program+ within +path+.
|
7
|
+
#
|
8
|
+
# On Win32, it looks for executables ending with .exe, .bat and .com
|
9
|
+
# which you may optionally include in the program name.
|
10
|
+
#
|
11
|
+
# Returns nil if not found.
|
12
|
+
def self.which(program, path=ENV['PATH'])
|
13
|
+
programs = program.to_a
|
14
|
+
|
15
|
+
# If no file extension is provided on Windows, try .bat, .com
|
16
|
+
# and .exe in turn.
|
17
|
+
if File::ALT_SEPARATOR && File.extname(program).empty?
|
18
|
+
unless WIN32EXTS.include?(File.extname(program))
|
19
|
+
WIN32EXTS.each{ |ext|
|
20
|
+
programs.push(program + ext)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Catch the first path found, or nil
|
26
|
+
location = catch(:done){
|
27
|
+
path.split(File::PATH_SEPARATOR).each{ |dir|
|
28
|
+
programs.each{ |prog|
|
29
|
+
f = File.join(dir, prog)
|
30
|
+
if File.executable?(f) && !File.directory?(f)
|
31
|
+
location = File.join(dir, prog)
|
32
|
+
location.tr!('/',File::ALT_SEPARATOR) if File::ALT_SEPARATOR
|
33
|
+
throw(:done, location)
|
34
|
+
end
|
35
|
+
}
|
36
|
+
}
|
37
|
+
nil # Evaluate to nil if not found
|
38
|
+
}
|
39
|
+
|
40
|
+
location
|
41
|
+
end
|
42
|
+
|
43
|
+
# In block form, yields each +program+ within +path+. In non-block
|
44
|
+
# form, returns an array of each +program+ within +path+.
|
45
|
+
#
|
46
|
+
# On Win32, it looks for executables ending with .exe, .bat and .com
|
47
|
+
# which you may optionally include in the program name.
|
48
|
+
def self.whereis(program, path=ENV['PATH'])
|
49
|
+
dirs = []
|
50
|
+
programs = program.to_a
|
51
|
+
|
52
|
+
# If no file extension is provided on Windows, try .bat, .com
|
53
|
+
# and .exe in turn.
|
54
|
+
if File::ALT_SEPARATOR && File.extname(program).empty?
|
55
|
+
unless WIN32EXTS.include?(File.extname(program))
|
56
|
+
WIN32EXTS.each{ |ext|
|
57
|
+
programs.push(program + ext)
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
path.split(File::PATH_SEPARATOR).each{ |dir|
|
63
|
+
programs.each{ |prog|
|
64
|
+
file = File.join(dir,prog)
|
65
|
+
file.tr!('/',File::ALT_SEPARATOR) if File::ALT_SEPARATOR
|
66
|
+
if File.executable?(file) && !File.directory?(file)
|
67
|
+
if block_given?
|
68
|
+
yield file
|
69
|
+
else
|
70
|
+
dirs << file
|
71
|
+
end
|
72
|
+
end
|
73
|
+
}
|
74
|
+
}
|
75
|
+
dirs.empty? ? nil : dirs.uniq
|
76
|
+
end
|
77
|
+
|
78
|
+
# In block form, yields the first +num_lines+ from +filename+. In non-block
|
79
|
+
# form, returns an Array of +num_lines+
|
80
|
+
def self.head(filename, num_lines=10)
|
81
|
+
a = []
|
82
|
+
IO.foreach(filename){ |line|
|
83
|
+
break if num_lines <= 0
|
84
|
+
num_lines -= 1
|
85
|
+
if block_given?
|
86
|
+
yield line
|
87
|
+
else
|
88
|
+
a << line
|
89
|
+
end
|
90
|
+
}
|
91
|
+
return a.empty? ? nil : a # Return nil in block form
|
92
|
+
end
|
93
|
+
|
94
|
+
# In block form, yields line +from+ up to line +to+. In non-block form
|
95
|
+
# returns an Array of lines from +from+ to +to+.
|
96
|
+
def self.middle(filename, from=10, to=20)
|
97
|
+
if block_given?
|
98
|
+
IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
|
99
|
+
else
|
100
|
+
IO.readlines(filename)[from-1..to-1]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# In block form, yields the last +num_lines+ of file +filename+.
|
105
|
+
# In non-block form, it returns the lines as an array.
|
106
|
+
#
|
107
|
+
# Note that this method slurps the entire file, so I don't recommend it
|
108
|
+
# for very large files. Also note that 'tail -f' functionality is not
|
109
|
+
# present.
|
110
|
+
def self.tail(filename, num_lines=10)
|
111
|
+
if block_given?
|
112
|
+
IO.readlines(filename).reverse[0..num_lines-1].reverse.each{ |line|
|
113
|
+
yield line
|
114
|
+
}
|
115
|
+
else
|
116
|
+
IO.readlines(filename).reverse[0..num_lines-1].reverse
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Converts a text file from one OS platform format to another, ala
|
121
|
+
# 'dos2unix'. Valid values for 'format', which are case insensitve,
|
122
|
+
# include:
|
123
|
+
|
124
|
+
# * MS Windows -> dos, windows, win32
|
125
|
+
# * Unix/BSD -> unix, linux, bsd
|
126
|
+
# * Mac -> mac, macintosh, apple, osx
|
127
|
+
|
128
|
+
# Note that this method is only valid for an ftype of "file". Otherwise a
|
129
|
+
# TypeError will be raised. If an invalid format value is received, an
|
130
|
+
# ArgumentError is raised.
|
131
|
+
def self.nl_convert(filename, newfilename=filename, platform="dos")
|
132
|
+
|
133
|
+
unless File.ftype(filename) == "file"
|
134
|
+
raise TypeError, "Only valid for plain text files"
|
135
|
+
end
|
136
|
+
|
137
|
+
if platform =~ /dos|windows|win32/i
|
138
|
+
format = "\cM\cJ"
|
139
|
+
elsif platform =~ /unix|linux|bsd/i
|
140
|
+
format = "\cJ"
|
141
|
+
elsif platform =~ /mac|apple|macintosh|osx/i
|
142
|
+
format = "\cM"
|
143
|
+
else
|
144
|
+
raise ArgumentError, "Invalid platform string"
|
145
|
+
end
|
146
|
+
|
147
|
+
orig = $\
|
148
|
+
$\ = format
|
149
|
+
|
150
|
+
if filename == newfilename
|
151
|
+
require "ftools"
|
152
|
+
require "tempfile"
|
153
|
+
tf = Tempfile.new("temp")
|
154
|
+
tf.open
|
155
|
+
IO.foreach(filename){ |line|
|
156
|
+
line.chomp!
|
157
|
+
tf.print line
|
158
|
+
}
|
159
|
+
tf.close
|
160
|
+
File.delete(filename)
|
161
|
+
File.copy(tf.path,filename)
|
162
|
+
else
|
163
|
+
nf = File.new(newfilename,"w+")
|
164
|
+
IO.foreach(filename){ |line|
|
165
|
+
line.chomp!
|
166
|
+
nf.print line
|
167
|
+
}
|
168
|
+
nf.close
|
169
|
+
end
|
170
|
+
|
171
|
+
$\ = orig
|
172
|
+
self
|
173
|
+
end
|
174
|
+
|
175
|
+
# Creates the 0 byte file +filename+.
|
176
|
+
def self.touch(filename)
|
177
|
+
File.open(filename,"w+"){}
|
178
|
+
self
|
179
|
+
end
|
180
|
+
|
181
|
+
# With no arguments, returns a four element array consisting of the number
|
182
|
+
# of bytes, characters, words and lines in filename, respectively.
|
183
|
+
|
184
|
+
# Valid options are 'bytes', 'characters' (or just 'chars'), 'words' and
|
185
|
+
# 'lines'.
|
186
|
+
def self.wc(filename,option='all')
|
187
|
+
option.downcase!
|
188
|
+
valid = %w/all bytes characters chars lines words/
|
189
|
+
|
190
|
+
unless valid.include?(option)
|
191
|
+
raise ArgumentError, "Invalid option: '#{option}'"
|
192
|
+
end
|
193
|
+
|
194
|
+
n = 0
|
195
|
+
if option == 'lines'
|
196
|
+
IO.foreach(filename){ n += 1 }
|
197
|
+
return n
|
198
|
+
elsif option == 'bytes'
|
199
|
+
File.open(filename){ |f|
|
200
|
+
f.each_byte{ n += 1 }
|
201
|
+
}
|
202
|
+
return n
|
203
|
+
elsif option == 'characters' || option == 'chars'
|
204
|
+
File.open(filename){ |f|
|
205
|
+
while f.getc
|
206
|
+
n += 1
|
207
|
+
end
|
208
|
+
}
|
209
|
+
return n
|
210
|
+
elsif option == 'words'
|
211
|
+
IO.foreach(filename){ |line|
|
212
|
+
n += line.split.length
|
213
|
+
}
|
214
|
+
return n
|
215
|
+
else
|
216
|
+
bytes,chars,lines,words = 0,0,0,0
|
217
|
+
IO.foreach(filename){ |line|
|
218
|
+
lines += 1
|
219
|
+
words += line.split.length
|
220
|
+
chars += line.split('').length
|
221
|
+
}
|
222
|
+
File.open(filename){ |f|
|
223
|
+
while f.getc
|
224
|
+
bytes += 1
|
225
|
+
end
|
226
|
+
}
|
227
|
+
return [bytes,chars,words,lines]
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
##################################################
|
2
|
+
# tc_constants.rb
|
3
|
+
#
|
4
|
+
# Tests whatever constant that have been defined
|
5
|
+
##################################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_Constants < Test::Unit::TestCase
|
19
|
+
def test_version
|
20
|
+
assert_equal("1.0.0", File::PTOOLS_VERSION, "Bad VERSION")
|
21
|
+
end
|
22
|
+
end
|
data/test/tc_head.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
##########################################
|
2
|
+
# tc_head.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.head method.
|
5
|
+
##########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileHead < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@test_file = 'test_file1.txt'
|
21
|
+
|
22
|
+
@expected_head1 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
|
23
|
+
@expected_head1.push("line6\n","line7\n","line8\n","line9\n","line10\n")
|
24
|
+
|
25
|
+
@expected_head2 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_head_basic
|
29
|
+
assert_respond_to(File, :head)
|
30
|
+
assert_nothing_raised{ File.head(@test_file) }
|
31
|
+
assert_nothing_raised{ File.head(@test_file, 5) }
|
32
|
+
assert_nothing_raised{ File.head(@test_file){} }
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_head_expected_results
|
36
|
+
assert_kind_of(Array, File.head(@test_file))
|
37
|
+
assert_equal(@expected_head1, File.head(@test_file))
|
38
|
+
assert_equal(@expected_head2, File.head(@test_file, 5))
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_head_expected_errors
|
42
|
+
assert_raises(ArgumentError){ File.head(@test_file, 5, "foo") }
|
43
|
+
assert_raises(Errno::ENOENT){ File.head("bogus") }
|
44
|
+
end
|
45
|
+
|
46
|
+
def teardown
|
47
|
+
@test_file = nil
|
48
|
+
end
|
49
|
+
end
|
data/test/tc_middle.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
##########################################
|
2
|
+
# tc_middle.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.middle method
|
5
|
+
##########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileMiddle < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@test_file = 'test_file1.txt'
|
21
|
+
|
22
|
+
@expected_middle1 = ["line10\n", "line11\n", "line12\n", "line13\n", "line14\n"]
|
23
|
+
@expected_middle1.push("line15\n","line16\n", "line17\n", "line18\n")
|
24
|
+
@expected_middle1.push("line19\n","line20\n")
|
25
|
+
|
26
|
+
@expected_middle2 = ["line14\n","line15\n","line16\n","line17\n"]
|
27
|
+
@expected_middle2.push("line18\n","line19\n","line20\n")
|
28
|
+
|
29
|
+
@expected_middle3 = ["line5\n","line6\n","line7\n"]
|
30
|
+
@expected_middle3.push("line8\n","line9\n","line10\n")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_method_basic
|
34
|
+
assert_respond_to(File, :middle)
|
35
|
+
assert_nothing_raised{ File.middle(@test_file) }
|
36
|
+
assert_nothing_raised{ File.middle(@test_file, 14) }
|
37
|
+
assert_nothing_raised{ File.middle(@test_file, 5, 10) }
|
38
|
+
assert_nothing_raised{ File.middle(@test_file){} }
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_middle_expected_results
|
42
|
+
assert_kind_of(Array, File.middle(@test_file))
|
43
|
+
assert_equal(@expected_middle1, File.middle(@test_file))
|
44
|
+
assert_equal(@expected_middle2, File.middle(@test_file, 14))
|
45
|
+
assert_equal(@expected_middle3, File.middle(@test_file, 5, 10))
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_middle_expected_errors
|
49
|
+
assert_raises(ArgumentError){ File.middle }
|
50
|
+
assert_raises(ArgumentError){ File.middle(@test_file, 5, 10, 15) }
|
51
|
+
assert_raises(NoMethodError){ File.middle(@test_file, "foo") }
|
52
|
+
assert_raises(Errno::ENOENT){ File.middle("bogus") }
|
53
|
+
end
|
54
|
+
|
55
|
+
def teardown
|
56
|
+
@test_file = nil
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#############################################
|
2
|
+
# tc_nlconvert.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.nl_convert method.
|
5
|
+
#############################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileNLConvert < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@test_file1 = 'test_file1.txt'
|
21
|
+
@test_file2 = 'test_file2.txt'
|
22
|
+
@dos_file = 'dos_test_file.txt'
|
23
|
+
@mac_file = 'mac_test_file.txt'
|
24
|
+
@unix_file = 'nix_test_file.txt'
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_nl_convert_basic
|
28
|
+
assert_respond_to(File, :nl_convert)
|
29
|
+
assert_nothing_raised{ File.nl_convert(@test_file2) }
|
30
|
+
assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2) }
|
31
|
+
assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2, "unix") }
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_nl_convert_to_dos
|
35
|
+
msg = "dos file should be larger, but isn't"
|
36
|
+
|
37
|
+
assert_nothing_raised{ File.nl_convert(@test_file1, @dos_file, "dos") }
|
38
|
+
assert_equal(true, File.size(@dos_file) > File.size(@test_file1), msg)
|
39
|
+
assert_equal(["\cM","\cJ"],
|
40
|
+
IO.readlines(@dos_file).first.split("")[-2..-1]
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_nl_convert_to_mac
|
45
|
+
if File::ALT_SEPARATOR
|
46
|
+
msg = "test may fail on Win32"
|
47
|
+
else
|
48
|
+
msg = "mac file should be the same size (or larger), but isn't"
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_nothing_raised{ File.nl_convert(@test_file1, @mac_file, "mac") }
|
52
|
+
assert_equal(true, File.size(@mac_file) == File.size(@test_file1), msg)
|
53
|
+
assert_equal("\cM", IO.readlines(@mac_file).first.split("").last)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_nl_convert_to_unix
|
57
|
+
msg = "unix file should be the same size (or smaller), but isn't"
|
58
|
+
|
59
|
+
assert_nothing_raised{ File.nl_convert(@test_file1, @unix_file, "unix") }
|
60
|
+
assert_equal("\n", IO.readlines(@unix_file).first.split("").last)
|
61
|
+
|
62
|
+
if File::ALT_SEPARATOR
|
63
|
+
assert_equal(true, File.size(@unix_file) >= File.size(@test_file1),msg)
|
64
|
+
else
|
65
|
+
assert_equal(true, File.size(@unix_file) <= File.size(@test_file1),msg)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_nl_convert_expected_errors
|
70
|
+
assert_raises(ArgumentError){ File.nl_convert }
|
71
|
+
assert_raises(ArgumentError){ File.nl_convert(@test_file1, "bogus.txt", "blah") }
|
72
|
+
end
|
73
|
+
|
74
|
+
def teardown
|
75
|
+
[@dos_file, @mac_file, @unix_file].each{ |file|
|
76
|
+
File.delete(file) if File.exists?(file)
|
77
|
+
}
|
78
|
+
end
|
79
|
+
end
|
data/test/tc_tail.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
########################################
|
2
|
+
# tc_tail.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.tail method.
|
5
|
+
########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileTail < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@test_file = 'test_file1.txt'
|
21
|
+
|
22
|
+
@expected_tail1 = ["line16\n","line17\n","line18\n","line19\n"]
|
23
|
+
@expected_tail1.push("line20\n","line21\n","line22\n", "line23\n")
|
24
|
+
@expected_tail1.push("line24\n","line25\n")
|
25
|
+
|
26
|
+
@expected_tail2 = ["line21\n","line22\n","line23\n","line24\n","line25\n"]
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_tail_basic
|
30
|
+
assert_respond_to(File, :tail)
|
31
|
+
assert_nothing_raised{ File.tail(@test_file) }
|
32
|
+
assert_nothing_raised{ File.tail(@test_file, 5) }
|
33
|
+
assert_nothing_raised{ File.tail(@test_file){} }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_tail_expected_return_values
|
37
|
+
assert_kind_of(Array, File.tail(@test_file))
|
38
|
+
assert_equal(@expected_tail1, File.tail(@test_file))
|
39
|
+
assert_equal(@expected_tail2, File.tail(@test_file, 5))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_tail_expected_errors
|
43
|
+
assert_raises(ArgumentError){ File.tail }
|
44
|
+
assert_raises(ArgumentError){ File.tail(@test_file, 5, 5) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def teardown
|
48
|
+
@test_file = nil
|
49
|
+
end
|
50
|
+
end
|
data/test/tc_touch.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
###########################################
|
2
|
+
# tc_touch.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.touch method.
|
5
|
+
###########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileTouch < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@test_file = "delete.this"
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_touch_basic
|
24
|
+
assert_respond_to(File, :touch)
|
25
|
+
assert_nothing_raised{ File.touch(@test_file) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_touch_expected_results
|
29
|
+
assert_equal(File, File.touch(@test_file))
|
30
|
+
assert_equal(true, File.exists?(@test_file))
|
31
|
+
assert_equal(0, File.size(@test_file))
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_touch_expected_errors
|
35
|
+
assert_raises(ArgumentError){ File.touch }
|
36
|
+
end
|
37
|
+
|
38
|
+
def teardown
|
39
|
+
File.delete(@test_file) if File.exists?(@test_file)
|
40
|
+
end
|
41
|
+
end
|
data/test/tc_wc.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
####################################
|
2
|
+
# tc_wc.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.wc method.
|
5
|
+
####################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileWC < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@test_file = 'test_file1.txt'
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_wc_basic
|
24
|
+
assert_respond_to(File, :wc)
|
25
|
+
assert_nothing_raised{ File.wc(@test_file) }
|
26
|
+
assert_nothing_raised{ File.wc(@test_file, 'bytes') }
|
27
|
+
assert_nothing_raised{ File.wc(@test_file, 'chars') }
|
28
|
+
assert_nothing_raised{ File.wc(@test_file, 'words') }
|
29
|
+
assert_nothing_raised{ File.wc(@test_file, 'LINES') }
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_wc_results
|
33
|
+
assert_kind_of(Array, File.wc(@test_file))
|
34
|
+
assert_equal([166,166,25,25], File.wc(@test_file))
|
35
|
+
assert_equal(166, File.wc(@test_file,'bytes'), "Wrong number of bytes")
|
36
|
+
assert_equal(166, File.wc(@test_file,'chars'), "Wrong number of chars")
|
37
|
+
assert_equal(25, File.wc(@test_file,'words'), "Wrong number of words")
|
38
|
+
assert_equal(25, File.wc(@test_file,'lines'), "Wrong number of lines")
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_wc_expected_errors
|
42
|
+
assert_raises(ArgumentError){ File.wc }
|
43
|
+
assert_raises(ArgumentError){ File.wc(@test_file, "bogus") }
|
44
|
+
end
|
45
|
+
|
46
|
+
def teardown
|
47
|
+
@test_file = nil
|
48
|
+
end
|
49
|
+
end
|
data/test/tc_whereis.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
##########################################
|
2
|
+
# tc_whereis.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.whereis method.
|
5
|
+
##########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_FileWhereis < Test::Unit::TestCase
|
19
|
+
|
20
|
+
def setup
|
21
|
+
# Change this to suit your system
|
22
|
+
@expected_locs = ['/usr/local/bin/ruby','/opt/sfw/bin/ruby']
|
23
|
+
@expected_locs.push('/opt/bin/ruby', '/usr/bin/ruby')
|
24
|
+
|
25
|
+
# MS Windows - change as needed
|
26
|
+
if File::ALT_SEPARATOR
|
27
|
+
@expected_locs = ["c:\\ruby\\bin\\ruby.exe"]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_whereis_basic
|
32
|
+
assert_respond_to(File, :whereis)
|
33
|
+
assert_nothing_raised{ File.whereis("ruby") }
|
34
|
+
assert_nothing_raised{ File.whereis("ruby","/usr/bin:/usr/local/bin") }
|
35
|
+
assert_nothing_raised{ File.whereis("ruby"){} }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_whereis_expected_return_values
|
39
|
+
assert_kind_of(Array, File.whereis("ruby"))
|
40
|
+
assert_equal(true, @expected_locs.include?(File.whereis("ruby").first))
|
41
|
+
assert_equal(nil, File.whereis("blahblah"))
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_whereis_expected_errors
|
45
|
+
assert_raises(ArgumentError){ File.whereis }
|
46
|
+
assert_raises(ArgumentError){ File.whereis("ruby","foo","bar") }
|
47
|
+
end
|
48
|
+
|
49
|
+
def teardown
|
50
|
+
@expected_locs = nil
|
51
|
+
end
|
52
|
+
end
|
data/test/tc_which.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
########################################
|
2
|
+
# tc_which.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.which method.
|
5
|
+
########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'rbconfig'
|
16
|
+
require 'test/unit'
|
17
|
+
require 'ptools'
|
18
|
+
|
19
|
+
# I make the assumption that Ruby is in your PATH for these tests
|
20
|
+
|
21
|
+
class TC_FileWhich < Test::Unit::TestCase
|
22
|
+
include Config
|
23
|
+
|
24
|
+
def setup
|
25
|
+
@exe = File.join(CONFIG["bindir"], CONFIG["ruby_install_name"])
|
26
|
+
|
27
|
+
if File::ALT_SEPARATOR
|
28
|
+
@exe.tr!('/','\\')
|
29
|
+
@exe << ".exe"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_which_basic
|
34
|
+
assert_respond_to(File, :which)
|
35
|
+
assert_nothing_raised{ File.which("ruby") }
|
36
|
+
assert_nothing_raised{ File.which("ruby", "/usr/bin:/usr/local/bin") }
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_which_expected_return_values
|
40
|
+
assert_kind_of(String, File.which("ruby"))
|
41
|
+
assert_equal(@exe, File.which("ruby"))
|
42
|
+
assert_equal(nil, File.which("ruby", "/bogus/path"))
|
43
|
+
assert_equal(nil, File.which("blahblah"))
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_which_expected_errors
|
47
|
+
assert_raises(ArgumentError){ File.which }
|
48
|
+
assert_raises(ArgumentError){ File.which("ruby", "foo", "bar") }
|
49
|
+
end
|
50
|
+
|
51
|
+
def teardown
|
52
|
+
@exe = nil
|
53
|
+
end
|
54
|
+
end
|
data/test/test_file1.txt
ADDED
data/test/test_file2.txt
ADDED
data/test/ts_all.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
########################################
|
2
|
+
# ts_all.rb
|
3
|
+
#
|
4
|
+
# All tests for the ptools package.
|
5
|
+
########################################
|
6
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
7
|
+
$LOAD_PATH.unshift(Dir.pwd + "/test")
|
8
|
+
|
9
|
+
require 'tc_constants'
|
10
|
+
require 'tc_head'
|
11
|
+
require 'tc_middle'
|
12
|
+
require 'tc_nlconvert'
|
13
|
+
require 'tc_tail'
|
14
|
+
require 'tc_touch'
|
15
|
+
require 'tc_wc'
|
16
|
+
require 'tc_which'
|
17
|
+
require 'tc_whereis'
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.10
|
3
|
+
specification_version: 1
|
4
|
+
name: ptools
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2005-06-02
|
8
|
+
summary: Extra methods for the File class
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: djberg96@gmail.com
|
12
|
+
homepage: http://www.rubyforge.org/projects/shards
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Daniel J. Berger
|
29
|
+
files:
|
30
|
+
- lib/ptools.rb
|
31
|
+
- README
|
32
|
+
- CHANGES
|
33
|
+
- MANIFEST
|
34
|
+
- test/tc_head.rb
|
35
|
+
- test/ts_all.rb
|
36
|
+
- test/tc_whereis.rb
|
37
|
+
- test/tc_middle.rb
|
38
|
+
- test/tc_which.rb
|
39
|
+
- test/tc_constants.rb
|
40
|
+
- test/tc_tail.rb
|
41
|
+
- test/tc_wc.rb
|
42
|
+
- test/tc_nlconvert.rb
|
43
|
+
- test/test_file1.txt
|
44
|
+
- test/test_file2.txt
|
45
|
+
- test/tc_touch.rb
|
46
|
+
test_files:
|
47
|
+
- test/ts_all.rb
|
48
|
+
rdoc_options: []
|
49
|
+
extra_rdoc_files:
|
50
|
+
- README
|
51
|
+
- CHANGES
|
52
|
+
executables: []
|
53
|
+
extensions: []
|
54
|
+
requirements: []
|
55
|
+
dependencies: []
|