ptools 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,6 @@
1
+ == 1.1.1 - 24-Aug-2006
2
+ * Added the File.binary? method, based on code from Ryan Davis.
3
+
1
4
  == 1.1.0 - 23-Aug-2006
2
5
  * Added the File.null method which returns the bit bucket on your platform.
3
6
  * The suffixes on MS Windows are now based on the PATHEXT environment variable,
data/README CHANGED
@@ -26,6 +26,7 @@
26
26
 
27
27
  File.touch("newfile") # "newfile" now exists
28
28
  File.null # '/dev/null' on Unix, 'NUL' on Windows
29
+ File.binary?('some_file') # true or false
29
30
 
30
31
  # Creates a copy of 'myfile' called 'newfile', in DOS format
31
32
  File.nl_convert("myfile", "newfile", "dos")
@@ -35,6 +36,9 @@
35
36
  converting to DOS or MAC format if nl_convert is run multiple times on
36
37
  the same file. This appears to be fixed in 1.8.x.
37
38
 
39
+ One or two of The File.nl_convert tests may fail on Windows. You can
40
+ ignore these.
41
+
38
42
  == Acknowledgements
39
43
  The which() method was adopted from the FileWhich code posted by Michael
40
44
  Granger on http://www.rubygarden.org. The 'whereis' command is a minor
@@ -46,6 +50,9 @@
46
50
 
47
51
  The middle() method was provided by Shashank Date.
48
52
 
53
+ The binary?() method was based almost entirely on a blog post by Ryan
54
+ Davis (who, in turn, based his code on Perl's -B switch).
55
+
49
56
  == Future Plans
50
57
  Add whatever other tools people think might be useful.
51
58
 
data/lib/ptools.rb CHANGED
@@ -1,5 +1,7 @@
1
+ require 'win32/file/stat' if PLATFORM.match('mswin')
2
+
1
3
  class File
2
- PTOOLS_VERSION = '1.1.0'
4
+ PTOOLS_VERSION = '1.1.1'
3
5
  IS_WINDOWS = RUBY_PLATFORM.match('mswin') ? true : false
4
6
 
5
7
  WIN32EXTS = ENV['PATHEXT'].split(';').map{ |e| e.downcase } rescue %w/.exe .com .bat/
@@ -21,6 +23,18 @@ class File
21
23
  '/dev/null'
22
24
  end
23
25
  end
26
+
27
+ # Returns whether or not +file+ is a binary file. Note that this is
28
+ # not guaranteed to be 100% accurate. It performs a "best guess" based
29
+ # on a simple test of the first +File.blksize+ characters.
30
+ #--
31
+ # Based on code originally provided by Ryan Davis (which, in turn, is
32
+ # based on Perl's -B switch).
33
+ #
34
+ def self.binary?(file)
35
+ s = (File.read(file, File.stat(file).blksize) || "").split(//)
36
+ ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
37
+ end
24
38
 
25
39
  # Looks for the first occurrence of +program+ within +path+.
26
40
  #
data/test/tc_binary.rb ADDED
@@ -0,0 +1,38 @@
1
+ ##########################################
2
+ # tc_binary.rb
3
+ #
4
+ # Test case for the File.binary? 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_Binary < Test::Unit::TestCase
19
+ def setup
20
+ @text_file = 'test_file1.txt'
21
+ end
22
+
23
+ def test_binary_basic
24
+ assert_respond_to(File, :binary?)
25
+ assert_nothing_raised{ File.binary?(@text_file) }
26
+ end
27
+
28
+ def test_binary_expected_results
29
+ assert_equal(false, File.binary?(@text_file))
30
+ end
31
+
32
+ def test_binary_expected_errors
33
+ assert_raises(Errno::ENOENT){ File.binary?('bogus') }
34
+ end
35
+
36
+ def teardown
37
+ end
38
+ end
data/test/tc_constants.rb CHANGED
@@ -17,7 +17,7 @@ require "ptools"
17
17
 
18
18
  class TC_Constants < Test::Unit::TestCase
19
19
  def test_version
20
- assert_equal("1.1.0", File::PTOOLS_VERSION, "Bad VERSION")
20
+ assert_equal("1.1.1", File::PTOOLS_VERSION, "Bad VERSION")
21
21
  end
22
22
 
23
23
  def test_windows
data/test/ts_all.rb CHANGED
@@ -6,6 +6,7 @@
6
6
  $LOAD_PATH.unshift(Dir.pwd)
7
7
  $LOAD_PATH.unshift(Dir.pwd + "/test")
8
8
 
9
+ require 'tc_binary'
9
10
  require 'tc_constants'
10
11
  require 'tc_head'
11
12
  require 'tc_middle'
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ptools
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
6
+ version: 1.1.1
7
7
  date: 2006-08-23 00:00:00 -06:00
8
8
  summary: Extra methods for the File class
9
9
  require_paths:
@@ -33,6 +33,7 @@ files:
33
33
  - CHANGES
34
34
  - MANIFEST
35
35
  - README
36
+ - test/tc_binary.rb
36
37
  - test/tc_constants.rb
37
38
  - test/tc_head.rb
38
39
  - test/tc_middle.rb