ptools 1.1.9 → 1.2.1

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/.gemtest ADDED
File without changes
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ == 1.2.1 - 20-May-2011
2
+ * Added an (empty) .gemtest file so that it can be used with test.rubygems.org.
3
+ * Fixed a sparse file test.
4
+
5
+ == 1.2.0 - 8-Jan-2011
6
+ * Added the File.sparse? method for Unix platforms. This method already
7
+ exists on Windows courtesy of the win32-file library.
8
+
1
9
  == 1.1.9 - 25-Mar-2010
2
10
  * Refactored File.which and File.whereis and added additional tests for each.
3
11
  * Removed the block form of File.whereis.
data/README CHANGED
@@ -22,6 +22,7 @@
22
22
  File.touch("newfile") # "newfile" now exists
23
23
  File.null # '/dev/null' on Unix, 'NUL' on Windows
24
24
  File.binary?('some_file') # true or false
25
+ File.sparse?('some_file') # true or false
25
26
 
26
27
  # Creates a copy of 'myfile' called 'newfile', in DOS format
27
28
  File.nl_convert("myfile", "newfile", "dos")
@@ -52,7 +53,7 @@
52
53
  Artistic 2.0
53
54
 
54
55
  == Copyright
55
- (C) 2003-2010 Daniel J. Berger
56
+ (C) 2003-2011 Daniel J. Berger
56
57
  All Rights Reserved.
57
58
 
58
59
  == Warranty
data/Rakefile CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'rake'
2
+ require 'rake/clean'
2
3
  require 'rake/testtask'
3
4
  require 'rbconfig'
4
5
  include Config
5
6
 
7
+ CLEAN.include("**/*.gem", "**/*.rbc", "**/*coverage*")
8
+
6
9
  desc 'Install the ptools package (non-gem)'
7
10
  task :install do
8
11
  sitelibdir = CONFIG["sitelibdir"]
@@ -12,7 +15,7 @@ end
12
15
 
13
16
  namespace 'gem' do
14
17
  desc 'Create the ptools gem'
15
- task :create do
18
+ task :create => [:clean] do
16
19
  Dir["*.gem"].each{ |f| File.delete(f) } # Clean first
17
20
  spec = eval(IO.read('ptools.gemspec'))
18
21
  Gem::Builder.new(spec).build
@@ -26,11 +29,19 @@ namespace 'gem' do
26
29
  end
27
30
 
28
31
  Rake::TestTask.new do |t|
32
+ task :test => :clean
29
33
  t.verbose = true
30
34
  t.warning = true
31
35
  end
32
36
 
33
37
  namespace 'test' do
38
+ desc "Check test coverage using rcov"
39
+ task :coverage => [:clean] do
40
+ require 'rcov'
41
+ rm_rf 'coverage'
42
+ sh "rcov -Ilib test/test*.rb"
43
+ end
44
+
34
45
  Rake::TestTask.new('binary') do |t|
35
46
  t.libs << 'test'
36
47
  t.verbose = true
@@ -73,6 +84,13 @@ namespace 'test' do
73
84
  t.test_files = FileList['test/test_null.rb']
74
85
  end
75
86
 
87
+ Rake::TestTask.new('sparse') do |t|
88
+ t.libs << 'test'
89
+ t.verbose = true
90
+ t.warning = true
91
+ t.test_files = FileList['test/test_is_sparse.rb']
92
+ end
93
+
76
94
  Rake::TestTask.new('tail') do |t|
77
95
  t.libs << 'test'
78
96
  t.verbose = true
@@ -108,3 +126,5 @@ namespace 'test' do
108
126
  t.test_files = FileList['test/test_which.rb']
109
127
  end
110
128
  end
129
+
130
+ task :default => :test
data/lib/ptools.rb CHANGED
@@ -1,17 +1,17 @@
1
1
  require 'rbconfig'
2
2
 
3
- if Config::CONFIG['host_os'] =~ /mswin|dos|win32|cygwin|mingw/i
3
+ if File::ALT_SEPARATOR
4
4
  require 'win32/file'
5
5
  end
6
6
 
7
7
  class File
8
8
  # The version of the ptools library.
9
- PTOOLS_VERSION = '1.1.9'
9
+ PTOOLS_VERSION = '1.2.1'
10
10
 
11
11
  # :stopdoc:
12
12
 
13
13
  # The WIN32EXTS string is used as part of a Dir[] call in certain methods.
14
- if Config::CONFIG['host_os'] =~ /mswin|dos|win32|cygwin|mingw/i
14
+ if File::ALT_SEPARATOR
15
15
  MSWINDOWS = true
16
16
  if ENV['PATHEXT']
17
17
  WIN32EXTS = ('.{' + ENV['PATHEXT'].tr(';', ',').tr('.','') + '}').downcase
@@ -40,7 +40,7 @@ class File
40
40
  # File.image?('somefile.txt') # => true
41
41
  #--
42
42
  # The approach I used here is based on information found at
43
- # http://en.wikipedia.org/wiki/Magic_number_(programming)
43
+ # http://en.wikipedia.org/wiki/Magic_number_(programming)
44
44
  #
45
45
  def self.image?(file)
46
46
  bool = IMAGE_EXT.include?(File.extname(file).downcase) # Match ext
@@ -63,7 +63,7 @@ class File
63
63
  #
64
64
  def self.null
65
65
  case Config::CONFIG['host_os']
66
- when /mswin|win32|msdos|cygwin|mingw/i
66
+ when /mswin|win32|msdos|cygwin|mingw|windows/i
67
67
  'NUL'
68
68
  when /amiga/i
69
69
  'NIL:'
@@ -94,14 +94,14 @@ class File
94
94
  s = (File.read(file, File.stat(file).blksize) || "").split(//)
95
95
  ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
96
96
  end
97
-
97
+
98
98
  # Looks for the first occurrence of +program+ within +path+.
99
- #
99
+ #
100
100
  # On Windows, it looks for executables ending with the suffixes defined
101
101
  # in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
102
102
  # that isn't defined, which you may optionally include in +program+.
103
103
  #
104
- # Returns nil if not found.
104
+ # Returns nil if not found.
105
105
  #
106
106
  # Examples:
107
107
  #
@@ -202,41 +202,43 @@ class File
202
202
  paths.empty? ? nil : paths.uniq
203
203
  end
204
204
 
205
- # In block form, yields the first +num_lines+ from +filename+. In non-block
206
- # form, returns an Array of +num_lines+
207
- #
208
- # Examples:
209
- #
210
- # # Return an array
211
- # File.head('somefile.txt') # => ['This is line1', 'This is line2', ...]
212
- #
213
- # # Use a block
214
- # File.head('somefile.txt'){ |line| puts line }
215
- #
216
- def self.head(filename, num_lines=10)
217
- a = []
218
- IO.foreach(filename){ |line|
219
- break if num_lines <= 0
220
- num_lines -= 1
221
- if block_given?
222
- yield line
223
- else
224
- a << line
225
- end
226
- }
227
- return a.empty? ? nil : a # Return nil in block form
228
- end
205
+ # In block form, yields the first +num_lines+ from +filename+. In non-block
206
+ # form, returns an Array of +num_lines+
207
+ #
208
+ # Examples:
209
+ #
210
+ # # Return an array
211
+ # File.head('somefile.txt') # => ['This is line1', 'This is line2', ...]
212
+ #
213
+ # # Use a block
214
+ # File.head('somefile.txt'){ |line| puts line }
215
+ #
216
+ def self.head(filename, num_lines=10)
217
+ a = []
229
218
 
230
- # In block form, yields line +from+ up to line +to+. In non-block form
231
- # returns an Array of lines from +from+ to +to+.
232
- #
233
- def self.middle(filename, from=10, to=20)
219
+ IO.foreach(filename){ |line|
220
+ break if num_lines <= 0
221
+ num_lines -= 1
234
222
  if block_given?
235
- IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
223
+ yield line
236
224
  else
237
- IO.readlines(filename)[from-1..to-1]
225
+ a << line
238
226
  end
239
- end
227
+ }
228
+
229
+ return a.empty? ? nil : a # Return nil in block form
230
+ end
231
+
232
+ # In block form, yields line +from+ up to line +to+. In non-block form
233
+ # returns an Array of lines from +from+ to +to+.
234
+ #
235
+ def self.middle(filename, from=10, to=20)
236
+ if block_given?
237
+ IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
238
+ else
239
+ IO.readlines(filename)[from-1..to-1]
240
+ end
241
+ end
240
242
 
241
243
  # In block form, yields the last +num_lines+ of file +filename+.
242
244
  # In non-block form, it returns the lines as an array.
@@ -259,68 +261,69 @@ class File
259
261
  end
260
262
  end
261
263
 
262
- # Converts a text file from one OS platform format to another, ala
263
- # 'dos2unix'. The possible values for +platform+ include:
264
- #
265
- # * MS Windows -> dos, windows, win32, mswin
266
- # * Unix/BSD -> unix, linux, bsd
267
- # * Mac -> mac, macintosh, apple, osx
268
- #
269
- # Note that this method is only valid for an ftype of "file". Otherwise a
270
- # TypeError will be raised. If an invalid format value is received, an
271
- # ArgumentError is raised.
272
- #
273
- def self.nl_convert(old_file, new_file = old_file, platform = 'dos')
274
- unless File.file?(old_file)
275
- raise ArgumentError, 'Only valid for plain text files'
276
- end
264
+ # Converts a text file from one OS platform format to another, ala
265
+ # 'dos2unix'. The possible values for +platform+ include:
266
+ #
267
+ # * MS Windows -> dos, windows, win32, mswin
268
+ # * Unix/BSD -> unix, linux, bsd
269
+ # * Mac -> mac, macintosh, apple, osx
270
+ #
271
+ # Note that this method is only valid for an ftype of "file". Otherwise a
272
+ # TypeError will be raised. If an invalid format value is received, an
273
+ # ArgumentError is raised.
274
+ #
275
+ def self.nl_convert(old_file, new_file = old_file, platform = 'dos')
276
+ unless File.file?(old_file)
277
+ raise ArgumentError, 'Only valid for plain text files'
278
+ end
277
279
 
278
- if platform =~ /dos|windows|win32|mswin|cygwin|mingw/i
279
- format = "\cM\cJ"
280
- elsif platform =~ /unix|linux|bsd/i
281
- format = "\cJ"
282
- elsif platform =~ /mac|apple|macintosh|osx/i
283
- format = "\cM"
284
- else
285
- raise ArgumentError, "Invalid platform string"
280
+ if platform =~ /dos|windows|win32|mswin|cygwin|mingw/i
281
+ format = "\cM\cJ"
282
+ elsif platform =~ /unix|linux|bsd/i
283
+ format = "\cJ"
284
+ elsif platform =~ /mac|apple|macintosh|osx/i
285
+ format = "\cM"
286
+ else
287
+ raise ArgumentError, "Invalid platform string"
288
+ end
289
+
290
+ orig = $\
291
+ $\ = format
292
+
293
+ if old_file == new_file
294
+ require 'fileutils'
295
+ require 'tempfile'
296
+
297
+ begin
298
+ temp_name = Time.new.strftime("%Y%m%d%H%M%S")
299
+ tf = Tempfile.new('ruby_temp_' + temp_name)
300
+ tf.open
301
+
302
+ IO.foreach(old_file){ |line|
303
+ line.chomp!
304
+ tf.print line
305
+ }
306
+ ensure
307
+ tf.close if tf && !tf.closed?
286
308
  end
287
309
 
288
- orig = $\
289
- $\ = format
290
-
291
- if old_file == new_file
292
- require 'fileutils'
293
- require 'tempfile'
294
-
295
- begin
296
- temp_name = Time.new.strftime("%Y%m%d%H%M%S")
297
- tf = Tempfile.new('ruby_temp_' + temp_name)
298
- tf.open
299
-
300
- IO.foreach(old_file){ |line|
301
- line.chomp!
302
- tf.print line
303
- }
304
- ensure
305
- tf.close if tf && !tf.closed?
306
- end
307
- File.delete(old_file)
308
- FileUtils.cp(tf.path, old_file)
309
- else
310
- begin
311
- nf = File.new(new_file, 'w')
312
- IO.foreach(old_file){ |line|
313
- line.chomp!
314
- nf.print line
315
- }
316
- ensure
317
- nf.close if nf && !nf.closed?
318
- end
310
+ File.delete(old_file)
311
+ FileUtils.cp(tf.path, old_file)
312
+ else
313
+ begin
314
+ nf = File.new(new_file, 'w')
315
+ IO.foreach(old_file){ |line|
316
+ line.chomp!
317
+ nf.print line
318
+ }
319
+ ensure
320
+ nf.close if nf && !nf.closed?
319
321
  end
322
+ end
320
323
 
321
- $\ = orig
322
- self
323
- end
324
+ $\ = orig
325
+ self
326
+ end
324
327
 
325
328
  # Changes the access and modification time if present, or creates a 0
326
329
  # byte file +filename+ if it doesn't already exist.
@@ -350,6 +353,7 @@ class File
350
353
  end
351
354
 
352
355
  n = 0
356
+
353
357
  if option == 'lines'
354
358
  IO.foreach(filename){ n += 1 }
355
359
  return n
@@ -386,6 +390,24 @@ class File
386
390
  end
387
391
  end
388
392
 
393
+ class << self
394
+ # Already provided by win32-file on MS Windows
395
+ unless respond_to?(:sparse?)
396
+ # Returns whether or not +file+ is a sparse file.
397
+ #
398
+ # A sparse file is a any file where its size is greater than the number
399
+ # of 512k blocks it consumes, i.e. its apparent and actual file size is
400
+ # not the same.
401
+ #
402
+ # See http://en.wikipedia.org/wiki/Sparse_file for more information.
403
+ #
404
+ def sparse?(file)
405
+ stats = File.stat(file)
406
+ stats.size > stats.blocks * 512
407
+ end
408
+ end
409
+ end
410
+
389
411
  private
390
412
 
391
413
  def self.bmp?(file)
@@ -401,6 +423,6 @@ class File
401
423
  end
402
424
 
403
425
  def self.gif?(file)
404
- ['GIF89a', 'GIF97a'].include?(IO.read(file, 6))
426
+ ['GIF89a', 'GIF97a'].include?(IO.read(file, 6))
405
427
  end
406
428
  end
data/ptools.gemspec CHANGED
@@ -3,7 +3,7 @@ require 'rbconfig'
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = 'ptools'
6
- gem.version = '1.1.9'
6
+ gem.version = '1.2.1'
7
7
  gem.license = 'Artistic 2.0'
8
8
  gem.author = 'Daniel J. Berger'
9
9
  gem.email = 'djberg96@gmail.com'
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.summary = 'Extra methods for the File class'
12
12
  gem.test_files = Dir['test/test*']
13
13
  gem.has_rdoc = true
14
- gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') || f.include?('git') }
14
+ gem.files = Dir['**/*'] << '.gemtest'
15
15
 
16
16
  gem.rubyforge_project = 'shards'
17
17
  gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
@@ -24,7 +24,7 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.add_development_dependency('test-unit', '>= 2.0.7')
26
26
 
27
- if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
27
+ if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw|windows/i
28
28
  gem.platform = Gem::Platform::CURRENT
29
29
  gem.add_dependency('win32-file', '>= 0.5.4')
30
30
  end
@@ -13,7 +13,7 @@ require 'ptools'
13
13
 
14
14
  class TC_Constants < Test::Unit::TestCase
15
15
  def test_version
16
- assert_equal('1.1.9', File::PTOOLS_VERSION)
16
+ assert_equal('1.2.1', File::PTOOLS_VERSION)
17
17
  end
18
18
 
19
19
  def test_image_ext
@@ -0,0 +1,48 @@
1
+ #####################################################################
2
+ # test_is_sparse.rb
3
+ #
4
+ # Test case for the File.sparse? method. You should run this test
5
+ # via the 'rake test:is_sparse' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'ptools'
12
+
13
+ class TC_IsSparse < Test::Unit::TestCase
14
+ def self.startup
15
+ Dir.chdir("test") if File.exists?("test")
16
+ @@win = Config::CONFIG['host_os'] =~ /windows|mswin|dos|cygwin|mingw/i
17
+ @@osx = Config::CONFIG['host_os'] =~ /darwin|osx/i
18
+ end
19
+
20
+ def setup
21
+ @sparse_file = '/var/log/lastlog'
22
+ @non_sparse_file = File.expand_path(File.basename(__FILE__))
23
+ end
24
+
25
+ test "is_sparse basic functionality" do
26
+ omit_if(@@win, "File.sparse? tests skipped on MS Windows")
27
+ assert_respond_to(File, :sparse?)
28
+ assert_nothing_raised{ File.sparse?(@sparse_file) }
29
+ assert_boolean(File.sparse?(@sparse_file))
30
+ end
31
+
32
+ test "is_sparse returns the expected results" do
33
+ omit_if(@@win, "File.sparse? tests skipped on MS Windows")
34
+ omit_if(@@osx, "File.sparse? tests skipped on OS X")
35
+ assert_true(File.sparse?(@sparse_file))
36
+ assert_false(File.sparse?(@non_sparse_file))
37
+ end
38
+
39
+ test "is_sparse only accepts one argument" do
40
+ omit_if(@@win, "File.sparse? tests skipped on MS Windows")
41
+ assert_raise(ArgumentError){ File.sparse?(@sparse_file, @sparse_file) }
42
+ end
43
+
44
+ def teardown
45
+ @sparse_file = nil
46
+ @non_sparse_file = nil
47
+ end
48
+ end
@@ -11,79 +11,91 @@ require 'test/unit'
11
11
  require 'ptools'
12
12
 
13
13
  class TC_FileNLConvert < Test::Unit::TestCase
14
- def self.startup
15
- Dir.chdir('test') if File.exists?('test')
16
- File.open('test_file1.txt', 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
17
- File.open('test_file2.txt', 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
18
- end
19
-
20
- def setup
21
- @test_file1 = 'test_file1.txt'
22
- @test_file2 = 'test_file2.txt'
23
- @dos_file = 'dos_test_file.txt'
24
- @mac_file = 'mac_test_file.txt'
25
- @unix_file = 'nix_test_file.txt'
26
- end
27
-
28
- def test_nl_convert_basic
29
- assert_respond_to(File, :nl_convert)
30
- assert_nothing_raised{ File.nl_convert(@test_file2) }
31
- assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2) }
32
- assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2, "unix") }
33
- end
34
-
35
- def test_nl_convert_to_dos
36
- msg = "dos file should be larger, but isn't"
37
-
38
- assert_nothing_raised{ File.nl_convert(@test_file1, @dos_file, "dos") }
39
- assert_equal(true, File.size(@dos_file) > File.size(@test_file1), msg)
40
- assert_equal(["\cM","\cJ"],
41
- IO.readlines(@dos_file).first.split("")[-2..-1]
42
- )
43
- end
44
-
45
- def test_nl_convert_to_mac
46
- assert_nothing_raised{ File.nl_convert(@test_file1, @mac_file, 'mac') }
47
- assert_equal("\cM", IO.readlines(@mac_file).first.split("").last)
48
-
49
- omit_if(Config::CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw/i)
50
- msg = "=> Mac file should be the same size (or larger), but isn't"
51
- assert_true(File.size(@mac_file) == File.size(@test_file1), msg)
52
- end
53
-
54
- def test_nl_convert_to_unix
55
- msg = "unix file should be the same size (or smaller), but isn't"
56
-
57
- assert_nothing_raised{ File.nl_convert(@test_file1, @unix_file, "unix") }
58
- assert_equal("\n", IO.readlines(@unix_file).first.split("").last)
59
-
60
- if File::ALT_SEPARATOR
61
- assert_equal(true, File.size(@unix_file) >= File.size(@test_file1),msg)
62
- else
63
- assert_equal(true, File.size(@unix_file) <= File.size(@test_file1),msg)
64
- end
65
- end
66
-
67
- def test_nl_convert_expected_errors
68
- assert_raises(ArgumentError){ File.nl_convert }
69
- assert_raises(ArgumentError){
70
- File.nl_convert(@test_file1, "bogus.txt", "blah")
71
- }
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
- @dos_file = nil
79
- @mac_file = nil
80
- @unix_file = nil
81
- @test_file1 = nil
82
- @test_file2 = nil
83
- end
84
-
85
- def self.shutdown
86
- File.delete('test_file1.txt') if File.exists?('test_file1.txt')
87
- File.delete('test_file2.txt') if File.exists?('test_file2.txt')
88
- end
14
+ def self.startup
15
+ Dir.chdir('test') if File.exists?('test')
16
+ @@test_file1 = 'test_nl_convert1.txt'
17
+ @@test_file2 = 'test_nl_convert2.txt'
18
+ File.open(@@test_file1, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
19
+ File.open(@@test_file2, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
20
+ end
21
+
22
+ def setup
23
+ @test_file1 = 'test_nl_convert1.txt'
24
+ @test_file2 = 'test_nl_convert2.txt'
25
+ @dos_file = 'dos_test_file.txt'
26
+ @mac_file = 'mac_test_file.txt'
27
+ @unix_file = 'nix_test_file.txt'
28
+ end
29
+
30
+ test "nl_convert basic functionality" do
31
+ assert_respond_to(File, :nl_convert)
32
+ end
33
+
34
+ test "nl_convert accepts one, two or three arguments" do
35
+ assert_nothing_raised{ File.nl_convert(@test_file2) }
36
+ assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2) }
37
+ assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2, "unix") }
38
+ end
39
+
40
+ test "nl_convert with dos platform argument works as expected" do
41
+ msg = "dos file should be larger, but isn't"
42
+
43
+ assert_nothing_raised{ File.nl_convert(@test_file1, @dos_file, "dos") }
44
+ assert_true(File.size(@dos_file) > File.size(@test_file1), msg)
45
+ assert_equal(["\cM","\cJ"], IO.readlines(@dos_file).first.split("")[-2..-1])
46
+ end
47
+
48
+ test "nl_convert with mac platform argument works as expected" do
49
+ assert_nothing_raised{ File.nl_convert(@test_file1, @mac_file, 'mac') }
50
+ assert_equal("\cM", IO.readlines(@mac_file).first.split("").last)
51
+
52
+ omit_if(File::ALT_SEPARATOR)
53
+ msg = "=> Mac file should be the same size (or larger), but isn't"
54
+ assert_true(File.size(@mac_file) == File.size(@test_file1), msg)
55
+ end
56
+
57
+ test "nl_convert with unix platform argument works as expected" do
58
+ msg = "unix file should be the same size (or smaller), but isn't"
59
+
60
+ assert_nothing_raised{ File.nl_convert(@test_file1, @unix_file, "unix") }
61
+ assert_equal("\n", IO.readlines(@unix_file).first.split("").last)
62
+
63
+ if File::ALT_SEPARATOR
64
+ assert_true(File.size(@unix_file) >= File.size(@test_file1), msg)
65
+ else
66
+ assert_true(File.size(@unix_file) <= File.size(@test_file1), msg)
67
+ end
68
+ end
69
+
70
+ test "nl_convert requires at least one argument" do
71
+ assert_raise(ArgumentError){ File.nl_convert }
72
+ end
73
+
74
+ test "nl_convert requires a valid platform string" do
75
+ assert_raise(ArgumentError){ File.nl_convert(@test_file1, "bogus.txt", "blah") }
76
+ end
77
+
78
+ test "nl_convert accepts a maximum of three arguments" do
79
+ assert_raise(ArgumentError){ File.nl_convert(@test_file1, @test_file2, 'dos', 1) }
80
+ end
81
+
82
+ test "nl_convert will fail on anything but plain files" do
83
+ assert_raise(ArgumentError){ File.nl_convert(File.null_device, @test_file1) }
84
+ end
85
+
86
+ def teardown
87
+ [@dos_file, @mac_file, @unix_file].each{ |file|
88
+ File.delete(file) if File.exists?(file)
89
+ }
90
+ @dos_file = nil
91
+ @mac_file = nil
92
+ @unix_file = nil
93
+ @test_file1 = nil
94
+ @test_file2 = nil
95
+ end
96
+
97
+ def self.shutdown
98
+ File.delete(@@test_file1) if File.exists?(@@test_file1)
99
+ File.delete(@@test_file2) if File.exists?(@@test_file2)
100
+ end
89
101
  end
data/test/test_null.rb CHANGED
@@ -4,34 +4,37 @@
4
4
  # Test case for the File.null method. You should run this test via
5
5
  # the 'rake test_null' task.
6
6
  #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
7
10
  require 'test/unit'
8
11
  require 'ptools'
9
12
 
10
- class TC_Null < Test::Unit::TestCase
11
- def setup
12
- @nulls = ['/dev/null', 'NUL', 'NIL:', 'NL:']
13
- end
13
+ class TC_FileNull < Test::Unit::TestCase
14
+ def setup
15
+ @nulls = ['/dev/null', 'NUL', 'NIL:', 'NL:']
16
+ end
14
17
 
15
- def test_null_basic
16
- assert_respond_to(File, :null)
17
- assert_nothing_raised{ File.null }
18
- end
18
+ test "null method basic functionality" do
19
+ assert_respond_to(File, :null)
20
+ assert_nothing_raised{ File.null }
21
+ end
19
22
 
20
- def test_null_expected_results
21
- assert_kind_of(String, File.null)
22
- assert(@nulls.include?(File.null))
23
- end
23
+ test "null method returns expected results" do
24
+ assert_kind_of(String, File.null)
25
+ assert(@nulls.include?(File.null))
26
+ end
24
27
 
25
- def test_null_expected_errors
26
- assert_raises(ArgumentError){ File.null(1) }
27
- end
28
+ test "null method does not accept any arguments" do
29
+ assert_raises(ArgumentError){ File.null(1) }
30
+ end
28
31
 
29
- def test_null_device_alias
30
- assert_respond_to(File, :null_device)
31
- assert_equal(true, File.method(:null) == File.method(:null_device))
32
- end
32
+ test "null_device is an alias for null" do
33
+ assert_respond_to(File, :null_device)
34
+ assert_alias_method(File, :null_device, :null)
35
+ end
33
36
 
34
- def teardown
35
- @nulls = nil
36
- end
37
+ def teardown
38
+ @nulls = nil
39
+ end
37
40
  end
data/test/test_wc.rb CHANGED
@@ -4,47 +4,73 @@
4
4
  # Test case for the File.wc method. This test should be run via
5
5
  # the 'rake test_wc' task.
6
6
  #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
7
10
  require 'test/unit'
8
11
  require 'ptools'
9
12
 
10
13
  class TC_FileWC < Test::Unit::TestCase
11
- def self.startup
12
- Dir.chdir('test') if File.exists?('test')
13
- File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
14
- end
14
+ def self.startup
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
17
+ @@test_file = 'test_file1.txt'
18
+ end
15
19
 
16
- def setup
17
- @test_file = 'test_file1.txt'
18
- end
19
-
20
- def test_wc_basic
21
- assert_respond_to(File, :wc)
22
- assert_nothing_raised{ File.wc(@test_file) }
23
- assert_nothing_raised{ File.wc(@test_file, 'bytes') }
24
- assert_nothing_raised{ File.wc(@test_file, 'chars') }
25
- assert_nothing_raised{ File.wc(@test_file, 'words') }
26
- assert_nothing_raised{ File.wc(@test_file, 'LINES') }
27
- end
28
-
29
- def test_wc_results
30
- assert_kind_of(Array, File.wc(@test_file))
31
- assert_equal([166,166,25,25], File.wc(@test_file))
32
- assert_equal(166, File.wc(@test_file,'bytes'), "Wrong number of bytes")
33
- assert_equal(166, File.wc(@test_file,'chars'), "Wrong number of chars")
34
- assert_equal(25, File.wc(@test_file,'words'), "Wrong number of words")
35
- assert_equal(25, File.wc(@test_file,'lines'), "Wrong number of lines")
36
- end
37
-
38
- def test_wc_expected_errors
39
- assert_raises(ArgumentError){ File.wc }
40
- assert_raises(ArgumentError){ File.wc(@test_file, "bogus") }
41
- end
42
-
43
- def teardown
44
- @test_file = nil
45
- end
46
-
47
- def self.shutdown
48
- File.delete('test_file1.txt') if File.exists?('test_file1.txt')
49
- end
20
+ def setup
21
+ @test_file = 'test_file1.txt'
22
+ end
23
+
24
+ test "wc method basic functionality" do
25
+ assert_respond_to(File, :wc)
26
+ assert_nothing_raised{ File.wc(@test_file) }
27
+ end
28
+
29
+ test "wc accepts specific optional arguments" do
30
+ assert_nothing_raised{ File.wc(@test_file, 'bytes') }
31
+ assert_nothing_raised{ File.wc(@test_file, 'chars') }
32
+ assert_nothing_raised{ File.wc(@test_file, 'words') }
33
+ assert_nothing_raised{ File.wc(@test_file, 'lines') }
34
+ end
35
+
36
+ test "argument to wc ignores the case of the option argument" do
37
+ assert_nothing_raised{ File.wc(@test_file, 'LINES') }
38
+ end
39
+
40
+ test "wc with no option returns expected results" do
41
+ assert_kind_of(Array, File.wc(@test_file))
42
+ assert_equal([166,166,25,25], File.wc(@test_file))
43
+ end
44
+
45
+ test "wc with bytes option returns the expected result" do
46
+ assert_equal(166, File.wc(@test_file, 'bytes'), "Wrong number of bytes")
47
+ end
48
+
49
+ test "wc with chars option returns the expected result" do
50
+ assert_equal(166, File.wc(@test_file, 'chars'), "Wrong number of chars")
51
+ end
52
+
53
+ test "wc with words option returns the expected result" do
54
+ assert_equal(25, File.wc(@test_file, 'words'), "Wrong number of words")
55
+ end
56
+
57
+ test "wc with lines option returns the expected result" do
58
+ assert_equal(25, File.wc(@test_file, 'lines'), "Wrong number of lines")
59
+ end
60
+
61
+ test "wc requires at least on argument" do
62
+ assert_raises(ArgumentError){ File.wc }
63
+ end
64
+
65
+ test "an invalid option raises an error" do
66
+ assert_raises(ArgumentError){ File.wc(@test_file, 'bogus') }
67
+ end
68
+
69
+ def teardown
70
+ @test_file = nil
71
+ end
72
+
73
+ def self.shutdown
74
+ File.delete(@@test_file) if File.exists?(@@test_file)
75
+ end
50
76
  end
data/test/test_whereis.rb CHANGED
@@ -14,10 +14,11 @@ include Config
14
14
  class TC_FileWhereis < Test::Unit::TestCase
15
15
  def self.startup
16
16
  @@windows = Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
17
+ @@ruby = RUBY_PLATFORM == 'java' ? 'jruby' : 'ruby'
17
18
  end
18
19
 
19
20
  def setup
20
- @expected_locs = [File.join(CONFIG['bindir'], 'ruby')]
21
+ @expected_locs = [File.join(CONFIG['bindir'], @@ruby)]
21
22
 
22
23
  if @@windows
23
24
  @expected_locs[0] << '.exe'
@@ -25,10 +26,10 @@ class TC_FileWhereis < Test::Unit::TestCase
25
26
  end
26
27
 
27
28
  unless @@windows
28
- @expected_locs << '/usr/local/bin/ruby'
29
- @expected_locs << '/opt/sfw/bin/ruby'
30
- @expected_locs << '/opt/bin/ruby'
31
- @expected_locs << '/usr/bin/ruby'
29
+ @expected_locs << "/usr/local/bin/#{@@ruby}"
30
+ @expected_locs << "/opt/sfw/bin/#{@@ruby}"
31
+ @expected_locs << "/opt/bin/#{@@ruby}"
32
+ @expected_locs << "/usr/bin/#{@@ruby}"
32
33
  end
33
34
 
34
35
  @actual_locs = nil
@@ -45,7 +46,7 @@ class TC_FileWhereis < Test::Unit::TestCase
45
46
  end
46
47
 
47
48
  test "whereis returns expected values" do
48
- assert_nothing_raised{ @actual_locs = File.whereis('ruby') }
49
+ assert_nothing_raised{ @actual_locs = File.whereis(@@ruby) }
49
50
  assert_kind_of(Array, @actual_locs)
50
51
  assert_true((@expected_locs & @actual_locs).size > 0)
51
52
  end
@@ -55,19 +56,19 @@ class TC_FileWhereis < Test::Unit::TestCase
55
56
  end
56
57
 
57
58
  test "whereis returns nil if program cannot be found in provided path" do
58
- assert_nil(File.whereis('ruby', '/foo/bar'))
59
+ assert_nil(File.whereis(@@ruby, '/foo/bar'))
59
60
  end
60
61
 
61
62
  test "whereis returns single element array or nil if absolute path is provided" do
62
- absolute = File.join(CONFIG['bindir'], 'ruby')
63
+ absolute = File.join(CONFIG['bindir'], @@ruby)
63
64
  absolute << '.exe' if @@windows
64
65
  assert_equal([absolute], File.whereis(absolute))
65
- assert_nil(File.whereis('/foo/bar/baz/ruby'))
66
+ assert_nil(File.whereis("/foo/bar/baz/#{@@ruby}"))
66
67
  end
67
68
 
68
69
  test "whereis works with an explicit extension on ms windows" do
69
70
  omit_unless(@@windows, 'test skipped except on MS Windows')
70
- assert_not_nil(File.whereis('ruby.exe'))
71
+ assert_not_nil(File.whereis(@@ruby + '.exe'))
71
72
  end
72
73
 
73
74
  test "whereis requires at least one argument" do
@@ -75,16 +76,16 @@ class TC_FileWhereis < Test::Unit::TestCase
75
76
  end
76
77
 
77
78
  test "whereis returns unique paths only" do
78
- assert_true(File.whereis('ruby') == File.whereis('ruby').uniq)
79
+ assert_true(File.whereis(@@ruby) == File.whereis(@@ruby).uniq)
79
80
  end
80
81
 
81
82
  test "whereis accepts a maximum of two arguments" do
82
- assert_raise(ArgumentError){ File.whereis('ruby', 'foo', 'bar') }
83
+ assert_raise(ArgumentError){ File.whereis(@@ruby, 'foo', 'bar') }
83
84
  end
84
85
 
85
86
  test "the second argument to whereis cannot be nil or empty" do
86
- assert_raise(ArgumentError){ File.whereis('ruby', nil) }
87
- assert_raise(ArgumentError){ File.whereis('ruby', '') }
87
+ assert_raise(ArgumentError){ File.whereis(@@ruby, nil) }
88
+ assert_raise(ArgumentError){ File.whereis(@@ruby, '') }
88
89
  end
89
90
 
90
91
  def teardown
data/test/test_which.rb CHANGED
@@ -14,7 +14,6 @@ require 'test/unit'
14
14
  require 'rbconfig'
15
15
  require 'fileutils'
16
16
  require 'ptools'
17
- include Config
18
17
 
19
18
  class TC_FileWhich < Test::Unit::TestCase
20
19
  def self.startup
@@ -30,7 +29,9 @@ class TC_FileWhich < Test::Unit::TestCase
30
29
 
31
30
  def setup
32
31
  @ruby = RUBY_PLATFORM.match('java') ? 'jruby' : 'ruby'
33
- @exe = File.join(CONFIG['bindir'], CONFIG['ruby_install_name'])
32
+ @ruby = 'rbx' if defined?(Rubinius)
33
+
34
+ @exe = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
34
35
 
35
36
  if @@windows
36
37
  @exe.tr!('/','\\')
@@ -38,7 +39,7 @@ class TC_FileWhich < Test::Unit::TestCase
38
39
  end
39
40
  end
40
41
 
41
- test "which basic functionality" do
42
+ test "which method basic functionality" do
42
43
  assert_respond_to(File, :which)
43
44
  assert_nothing_raised{ File.which(@ruby) }
44
45
  assert_kind_of(String, File.which(@ruby))
@@ -66,7 +67,7 @@ class TC_FileWhich < Test::Unit::TestCase
66
67
  end
67
68
 
68
69
  test "which returns argument if an existent absolute path is provided" do
69
- assert_equal(@exe, File.which(@ruby))
70
+ assert_equal(@exe, File.which(@ruby), "=> May fail on a symlink")
70
71
  end
71
72
 
72
73
  test "which returns nil if a non-existent absolute path is provided" do
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ptools
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 29
5
+ prerelease:
5
6
  segments:
6
7
  - 1
8
+ - 2
7
9
  - 1
8
- - 9
9
- version: 1.1.9
10
+ version: 1.2.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Daniel J. Berger
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-03-25 00:00:00 -06:00
18
+ date: 2011-05-20 00:00:00 -06:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: test-unit
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 1
27
30
  segments:
28
31
  - 2
29
32
  - 0
@@ -42,24 +45,26 @@ extra_rdoc_files:
42
45
  - CHANGES
43
46
  - MANIFEST
44
47
  files:
45
- - CHANGES
46
- - lib/ptools.rb
47
- - MANIFEST
48
- - ptools.gemspec
49
48
  - Rakefile
50
49
  - README
51
- - test/test_binary.rb
52
- - test/test_constants.rb
53
- - test/test_head.rb
54
- - test/test_image.rb
50
+ - ptools.gemspec
51
+ - lib/ptools.rb
52
+ - CHANGES
53
+ - test/test_whereis.rb
55
54
  - test/test_middle.rb
55
+ - test/test_binary.rb
56
56
  - test/test_nlconvert.rb
57
- - test/test_null.rb
58
- - test/test_tail.rb
59
57
  - test/test_touch.rb
60
- - test/test_wc.rb
61
- - test/test_whereis.rb
62
58
  - test/test_which.rb
59
+ - test/test_null.rb
60
+ - test/test_head.rb
61
+ - test/test_is_sparse.rb
62
+ - test/test_wc.rb
63
+ - test/test_image.rb
64
+ - test/test_constants.rb
65
+ - test/test_tail.rb
66
+ - MANIFEST
67
+ - .gemtest
63
68
  has_rdoc: true
64
69
  homepage: http://www.rubyforge.org/projects/shards
65
70
  licenses:
@@ -70,36 +75,41 @@ rdoc_options: []
70
75
  require_paths:
71
76
  - lib
72
77
  required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
73
79
  requirements:
74
80
  - - ">="
75
81
  - !ruby/object:Gem::Version
82
+ hash: 3
76
83
  segments:
77
84
  - 0
78
85
  version: "0"
79
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
80
88
  requirements:
81
89
  - - ">="
82
90
  - !ruby/object:Gem::Version
91
+ hash: 3
83
92
  segments:
84
93
  - 0
85
94
  version: "0"
86
95
  requirements: []
87
96
 
88
97
  rubyforge_project: shards
89
- rubygems_version: 1.3.6
98
+ rubygems_version: 1.6.2
90
99
  signing_key:
91
100
  specification_version: 3
92
101
  summary: Extra methods for the File class
93
102
  test_files:
94
- - test/test_binary.rb
95
- - test/test_constants.rb
96
- - test/test_head.rb
97
- - test/test_image.rb
103
+ - test/test_whereis.rb
98
104
  - test/test_middle.rb
105
+ - test/test_binary.rb
99
106
  - test/test_nlconvert.rb
100
- - test/test_null.rb
101
- - test/test_tail.rb
102
107
  - test/test_touch.rb
103
- - test/test_wc.rb
104
- - test/test_whereis.rb
105
108
  - test/test_which.rb
109
+ - test/test_null.rb
110
+ - test/test_head.rb
111
+ - test/test_is_sparse.rb
112
+ - test/test_wc.rb
113
+ - test/test_image.rb
114
+ - test/test_constants.rb
115
+ - test/test_tail.rb