ptools 1.2.3 → 1.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16f06f25547577f77bf9f56f263bd4bd29bd2c2c
4
- data.tar.gz: 6d86459c2c74ea62fe50b674ba1a866aef4059b6
3
+ metadata.gz: 8b1ad5b7756616fecd9369472860f83dd3294f5e
4
+ data.tar.gz: 44943151f729df7c9c2f5a00fbfd9491ac3ea694
5
5
  SHA512:
6
- metadata.gz: c2ed19fd3d1e598a5599b2f3893f922c2fd132702dc5af72153a1e87bcdd4dfb9e5dc8c8f5ffdd34d19eaf8c55429186e87527633ab2bb2027f3fb329864a6f9
7
- data.tar.gz: 2ce7328135836e1b821664167099de0d434c451e4269ab838141638be4c9f67e21096904ff69244fb973370629a3289a95056779801abaea9fba7d67b6d6a834
6
+ metadata.gz: 3f05fdc52605e22f394c85b575aa9dccb475b707665da115615ba2fafd807b9649a860446ca2088f27ee7973f83b3f3bb1a129b464e1a094c5845ef990949e74
7
+ data.tar.gz: 6447938245ae44d917b9013a3d0a128e80b26218db680f4b81a01e5d9734b91b591ab8f673afab7d36eb4fe23638cde85fe7c98e6c564a90aa34e51529e730b3
data/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.2.4 - 25-Feb-2014
2
+ * The File.binary method now always returns false for images. It is meant to
3
+ detect executables, shared objects, etc. Use File.image? to detect images.
4
+ * Encoding fixes for the File.image? method.
5
+
1
6
  == 1.2.3 - 19-Feb-2014
2
7
  * Fixed a bug where File.binary? would return true for unicode text. Thanks go
3
8
  to Ben Hollis for the spot.
data/Rakefile CHANGED
@@ -68,6 +68,13 @@ namespace 'test' do
68
68
  t.test_files = FileList['test/test_head.rb']
69
69
  end
70
70
 
71
+ Rake::TestTask.new('image') do |t|
72
+ t.libs << 'test'
73
+ t.verbose = true
74
+ t.warning = true
75
+ t.test_files = FileList['test/test_image.rb']
76
+ end
77
+
71
78
  Rake::TestTask.new('middle') do |t|
72
79
  t.libs << 'test'
73
80
  t.verbose = true
@@ -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.2.3'
6
+ PTOOLS_VERSION = '1.2.4'
7
7
 
8
8
  # :stopdoc:
9
9
 
@@ -28,13 +28,12 @@ class File
28
28
  #
29
29
  # This method does some simple read and extension checks. For a version
30
30
  # that is more robust, but which depends on a 3rd party C library (and is
31
- # difficult to build on MS Windows), see the 'filemagic' library, available
32
- # on the RAA.
31
+ # difficult to build on MS Windows), see the 'filemagic' library.
33
32
  #
34
33
  # Examples:
35
34
  #
36
35
  # File.image?('somefile.jpg') # => true
37
- # File.image?('somefile.txt') # => true
36
+ # File.image?('somefile.txt') # => false
38
37
  #--
39
38
  # The approach I used here is based on information found at
40
39
  # http://en.wikipedia.org/wiki/Magic_number_(programming)
@@ -75,9 +74,10 @@ class File
75
74
  alias null_device null
76
75
  end
77
76
 
78
- # Returns whether or not +file+ is a binary file. Note that this is
79
- # not guaranteed to be 100% accurate. It performs a "best guess" based
80
- # on a simple test of the first +File.blksize+ characters.
77
+ # Returns whether or not +file+ is a binary non-image file, i.e. executable,
78
+ # shared object, ect. Note that this is NOT guaranteed to be 100% accurate.
79
+ # It performs a "best guess" based on a simple test of the first
80
+ # +File.blksize+ characters.
81
81
  #
82
82
  # Example:
83
83
  #
@@ -88,7 +88,9 @@ class File
88
88
  # based on Perl's -B switch).
89
89
  #
90
90
  def self.binary?(file)
91
- s = (File.read(file, File.stat(file).blksize) || "").encode('US-ASCII', :undef => :replace).split(//)
91
+ return false if image?(file)
92
+ s = (File.read(file, File.stat(file).blksize) || "")
93
+ s = s.encode('US-ASCII', :undef => :replace).split(//)
92
94
  ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
93
95
  end
94
96
 
@@ -416,11 +418,11 @@ class File
416
418
  end
417
419
 
418
420
  def self.jpg?(file)
419
- IO.read(file, 10) == "\377\330\377\340\000\020JFIF"
421
+ IO.read(file, 10, encoding: 'binary') == "\377\330\377\340\000\020JFIF".force_encoding(Encoding::BINARY)
420
422
  end
421
423
 
422
424
  def self.png?(file)
423
- IO.read(file, 4) == "\211PNG"
425
+ IO.read(file, 4, encoding: 'binary') == "\211PNG".force_encoding(Encoding::BINARY)
424
426
  end
425
427
 
426
428
  def self.gif?(file)
@@ -3,7 +3,7 @@ require 'rbconfig'
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = 'ptools'
6
- gem.version = '1.2.3'
6
+ gem.version = '1.2.4'
7
7
  gem.license = 'Artistic 2.0'
8
8
  gem.author = 'Daniel J. Berger'
9
9
  gem.email = 'djberg96@gmail.com'
Binary file
Binary file
Binary file
@@ -10,8 +10,6 @@ require 'ptools'
10
10
 
11
11
  class TC_Ptools_Binary < Test::Unit::TestCase
12
12
  def self.startup
13
- @@txt_file = 'test_binary.txt'
14
-
15
13
  if File::ALT_SEPARATOR
16
14
  @@bin_file = File.join(ENV['windir'], 'notepad.exe')
17
15
  else
@@ -19,29 +17,49 @@ class TC_Ptools_Binary < Test::Unit::TestCase
19
17
  end
20
18
 
21
19
  Dir.chdir('test') if File.exist?('test')
20
+ end
22
21
 
23
- File.open(@@txt_file, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
22
+ def setup
23
+ @txt_file = File.join('txt', 'english.txt')
24
+ @uni_file = File.join('txt', 'korean.txt')
25
+ @png_file = File.join('img', 'test.png')
26
+ @jpg_file = File.join('img', 'test.jpg')
27
+ @gif_file = File.join('img', 'test.gif')
24
28
  end
25
29
 
26
30
  test "File.binary? basic functionality" do
27
31
  assert_respond_to(File, :binary?)
28
- assert_nothing_raised{ File.binary?(@@txt_file) }
32
+ assert_nothing_raised{ File.binary?(@txt_file) }
29
33
  end
30
34
 
31
- test "File.binary? returns expected results" do
32
- assert_false(File.binary?(@@txt_file))
35
+ test "File.binary? returns true for binary files" do
33
36
  assert_true(File.binary?(@@bin_file))
34
37
  end
35
38
 
39
+ test "File.binary? returns false for text files" do
40
+ assert_false(File.binary?(@txt_file))
41
+ assert_false(File.binary?(@uni_file))
42
+ end
43
+
44
+ test "File.binary? returns false for image files" do
45
+ assert_false(File.binary?(@png_file))
46
+ assert_false(File.binary?(@jpg_file))
47
+ assert_false(File.binary?(@gif_file))
48
+ end
49
+
36
50
  test "File.binary? raises an error if the file cannot be found" do
37
51
  assert_raise_kind_of(SystemCallError){ File.binary?('bogus') }
38
52
  end
39
53
 
40
54
  test "File.binary? only accepts one argument" do
41
- assert_raise_kind_of(ArgumentError){ File.binary?(@@txt_file, @@bin_file) }
55
+ assert_raise_kind_of(ArgumentError){ File.binary?(@txt_file, @@bin_file) }
42
56
  end
43
57
 
44
- def self.shutdown
45
- File.delete(@@txt_file) if File.exist?(@@txt_file)
58
+ def teardown
59
+ @txt_file = nil
60
+ @uni_file = nil
61
+ @png_file = nil
62
+ @jpg_file = nil
63
+ @gif_file = nil
46
64
  end
47
65
  end
@@ -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.2.3', File::PTOOLS_VERSION)
18
+ assert_equal('1.2.4', File::PTOOLS_VERSION)
19
19
  end
20
20
 
21
21
  test "IMAGE_EXT constant is set to array of values" do
@@ -2,7 +2,7 @@
2
2
  # test_image.rb
3
3
  #
4
4
  # Test case for the File.image? method. You should run this test
5
- # via the 'rake test_image' task.
5
+ # via the 'rake test:image' task.
6
6
  #####################################################################
7
7
  require 'test-unit'
8
8
  require 'ptools'
@@ -10,31 +10,48 @@ require 'ptools'
10
10
  class TC_Ptools_Image < Test::Unit::TestCase
11
11
  def self.startup
12
12
  Dir.chdir('test') if File.exist?('test')
13
- File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
14
13
  end
15
14
 
16
15
  def setup
17
- @text_file = 'test_file1.txt'
16
+ @txt_file = File.join(Dir.pwd, 'txt', 'english.txt')
17
+ @uni_file = File.join(Dir.pwd, 'txt', 'korean.txt')
18
+ @jpg_file = File.join(Dir.pwd, 'img', 'test.jpg')
19
+ @png_file = File.join(Dir.pwd, 'img', 'test.png')
20
+ @gif_file = File.join(Dir.pwd, 'img', 'test.gif')
18
21
  end
19
22
 
20
- def test_image_basic
23
+ test "image? method basic functionality" do
21
24
  assert_respond_to(File, :image?)
22
- assert_nothing_raised{ File.image?(@text_file) }
25
+ assert_nothing_raised{ File.image?(@txt_file) }
26
+ assert_boolean(File.image?(@txt_file))
23
27
  end
24
28
 
25
- def test_image_expected_results
26
- assert_equal(false, File.image?(@text_file))
29
+ test "image? method returns false for a text file" do
30
+ assert_false(File.image?(@txt_file))
31
+ assert_false(File.image?(@uni_file))
27
32
  end
28
33
 
29
- def test_image_expected_errors
30
- assert_raises(Errno::ENOENT, ArgumentError){ File.image?('bogus') }
34
+ test "image? method returns true for a gif" do
35
+ assert_true(File.image?(@gif_file))
31
36
  end
32
37
 
33
- def teardown
34
- @text_file = nil
38
+ test "image? method returns true for a jpeg" do
39
+ assert_true(File.image?(@jpg_file))
40
+ end
41
+
42
+ test "image? method returns true for a png" do
43
+ assert_true(File.image?(@png_file))
35
44
  end
36
45
 
37
- def self.shutdown
38
- File.delete('test_file1.txt') if File.exist?('test_file1.txt')
46
+ test "image? method raises an error if the file does not exist" do
47
+ assert_raises(Errno::ENOENT, ArgumentError){ File.image?('bogus') }
48
+ end
49
+
50
+ def teardown
51
+ @txt_file = nil
52
+ @uni_file = nil
53
+ @jpg_file = nil
54
+ @png_file = nil
55
+ @gif_file = nil
39
56
  end
40
57
  end
@@ -0,0 +1 @@
1
+ Hello World
@@ -0,0 +1 @@
1
+ 한국어의 내용분석을 위한
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.2.3
4
+ version: 1.2.4
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-02-19 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -43,6 +43,9 @@ files:
43
43
  - Rakefile
44
44
  - lib/ptools.rb
45
45
  - ptools.gemspec
46
+ - test/img/test.gif
47
+ - test/img/test.jpg
48
+ - test/img/test.png
46
49
  - test/test_binary.rb
47
50
  - test/test_constants.rb
48
51
  - test/test_head.rb
@@ -56,6 +59,8 @@ files:
56
59
  - test/test_wc.rb
57
60
  - test/test_whereis.rb
58
61
  - test/test_which.rb
62
+ - test/txt/english.txt
63
+ - test/txt/korean.txt
59
64
  homepage: https://github.com/djberg96/ptools
60
65
  licenses:
61
66
  - Artistic 2.0