ptools 1.3.5-universal-mingw32 → 1.4.2-universal-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/{CHANGES → CHANGES.md} +59 -31
- data/Gemfile +1 -10
- data/MANIFEST.md +27 -0
- data/{README → README.md} +30 -26
- data/Rakefile +30 -78
- data/certs/djberg96_pub.pem +22 -17
- data/lib/ptools.rb +48 -35
- data/ptools.gemspec +13 -7
- data/spec/binary_spec.rb +59 -0
- data/spec/constants_spec.rb +23 -0
- data/spec/head_spec.rb +41 -0
- data/spec/image_spec.rb +51 -0
- data/{test → spec}/img/test.gif +0 -0
- data/{test → spec}/img/test.ico +0 -0
- data/{test → spec}/img/test.jpg +0 -0
- data/{test → spec}/img/test.png +0 -0
- data/spec/nlconvert_spec.rb +104 -0
- data/spec/sparse_spec.rb +43 -0
- data/spec/tail_spec.rb +107 -0
- data/spec/touch_spec.rb +47 -0
- data/spec/txt/empty.txt +0 -0
- data/{test → spec}/txt/english.txt +0 -0
- data/spec/txt/english.utf16 +0 -0
- data/{test → spec}/txt/korean.txt +0 -0
- data/spec/wc_spec.rb +65 -0
- data/spec/whereis_spec.rb +87 -0
- data/spec/which_spec.rb +112 -0
- metadata +68 -77
- metadata.gz.sig +0 -0
- data/MANIFEST +0 -21
- data/test/test_binary.rb +0 -70
- data/test/test_constants.rb +0 -38
- data/test/test_head.rb +0 -48
- data/test/test_image.rb +0 -62
- data/test/test_is_sparse.rb +0 -53
- data/test/test_nlconvert.rb +0 -110
- data/test/test_null.rb +0 -40
- data/test/test_tail.rb +0 -124
- data/test/test_touch.rb +0 -53
- data/test/test_wc.rb +0 -73
- data/test/test_whereis.rb +0 -98
- data/test/test_which.rb +0 -126
data/certs/djberg96_pub.pem
CHANGED
@@ -1,21 +1,26 @@
|
|
1
1
|
-----BEGIN CERTIFICATE-----
|
2
|
-
|
2
|
+
MIIEcDCCAtigAwIBAgIBATANBgkqhkiG9w0BAQsFADA/MREwDwYDVQQDDAhkamJl
|
3
3
|
cmc5NjEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYDY29t
|
4
|
-
|
4
|
+
MB4XDTE4MDMxODE1MjIwN1oXDTI4MDMxNTE1MjIwN1owPzERMA8GA1UEAwwIZGpi
|
5
5
|
ZXJnOTYxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixkARkWA2Nv
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
6
|
+
bTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBALgfaroVM6CI06cxr0/h
|
7
|
+
A+j+pc8fgpRgBVmHFaFunq28GPC3IvW7Nvc3Y8SnAW7pP1EQIbhlwRIaQzJ93/yj
|
8
|
+
u95KpkP7tA9erypnV7dpzBkzNlX14ACaFD/6pHoXoe2ltBxk3CCyyzx70mTqJpph
|
9
|
+
75IB03ni9a8yqn8pmse+s83bFJOAqddSj009sGPcQO+QOWiNxqYv1n5EHcvj2ebO
|
10
|
+
6hN7YTmhx7aSia4qL/quc4DlIaGMWoAhvML7u1fmo53CYxkKskfN8MOecq2vfEmL
|
11
|
+
iLu+SsVVEAufMDDFMXMJlvDsviolUSGMSNRTujkyCcJoXKYYxZSNtIiyd9etI0X3
|
12
|
+
ctu0uhrFyrMZXCedutvXNjUolD5r9KGBFSWH1R9u2I3n3SAyFF2yzv/7idQHLJJq
|
13
|
+
74BMnx0FIq6fCpu5slAipvxZ3ZkZpEXZFr3cIBtO1gFvQWW7E/Y3ijliWJS1GQFq
|
14
|
+
058qERadHGu1yu1dojmFRo6W2KZvY9al2yIlbkpDrD5MYQIDAQABo3cwdTAJBgNV
|
15
|
+
HRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQUFZsMapgzJimzsbaBG2Tm8j5e
|
16
|
+
AzgwHQYDVR0RBBYwFIESZGpiZXJnOTZAZ21haWwuY29tMB0GA1UdEgQWMBSBEmRq
|
17
|
+
YmVyZzk2QGdtYWlsLmNvbTANBgkqhkiG9w0BAQsFAAOCAYEAW2tnYixXQtKxgGXq
|
18
|
+
/3iSWG2bLwvxS4go3srO+aRXZHrFUMlJ5W0mCxl03aazxxKTsVVpZD8QZxvK91OQ
|
19
|
+
h9zr9JBYqCLcCVbr8SkmYCi/laxIZxsNE5YI8cC8vvlLI7AMgSfPSnn/Epq1GjGY
|
20
|
+
6L1iRcEDtanGCIvjqlCXO9+BmsnCfEVehqZkQHeYczA03tpOWb6pon2wzvMKSsKH
|
21
|
+
ks0ApVdstSLz1kzzAqem/uHdG9FyXdbTAwH1G4ZPv69sQAFAOCgAqYmdnzedsQtE
|
22
|
+
1LQfaQrx0twO+CZJPcRLEESjq8ScQxWRRkfuh2VeR7cEU7L7KqT10mtUwrvw7APf
|
23
|
+
DYoeCY9KyjIBjQXfbj2ke5u1hZj94Fsq9FfbEQg8ygCgwThnmkTrrKEiMSs3alYR
|
24
|
+
ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
|
25
|
+
WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
|
21
26
|
-----END CERTIFICATE-----
|
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.
|
6
|
+
PTOOLS_VERSION = '1.4.2'.freeze
|
7
7
|
|
8
8
|
# :stopdoc:
|
9
9
|
|
@@ -19,17 +19,26 @@ class File
|
|
19
19
|
MSWINDOWS = false
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
if File::ALT_SEPARATOR
|
23
|
+
private_constant :WIN32EXTS
|
24
|
+
private_constant :MSWINDOWS
|
25
|
+
end
|
26
|
+
|
27
|
+
IMAGE_EXT = %w[.bmp .gif .jpg .jpeg .png .ico]
|
23
28
|
|
24
29
|
# :startdoc:
|
25
30
|
|
26
31
|
# Returns whether or not the file is an image. Only JPEG, PNG, BMP,
|
27
32
|
# GIF, and ICO are checked against.
|
28
33
|
#
|
29
|
-
# This
|
34
|
+
# This reads and checks the first few bytes of the file. For a version
|
30
35
|
# that is more robust, but which depends on a 3rd party C library (and is
|
31
36
|
# difficult to build on MS Windows), see the 'filemagic' library.
|
32
37
|
#
|
38
|
+
# By default the filename extension is also checked. You can disable this
|
39
|
+
# by passing false as the second argument, in which case only the contents
|
40
|
+
# are checked.
|
41
|
+
#
|
33
42
|
# Examples:
|
34
43
|
#
|
35
44
|
# File.image?('somefile.jpg') # => true
|
@@ -38,40 +47,14 @@ class File
|
|
38
47
|
# The approach I used here is based on information found at
|
39
48
|
# http://en.wikipedia.org/wiki/Magic_number_(programming)
|
40
49
|
#
|
41
|
-
def self.image?(file)
|
42
|
-
bool = IMAGE_EXT.include?(File.extname(file).downcase)
|
50
|
+
def self.image?(file, check_file_extension = true)
|
43
51
|
bool = bmp?(file) || jpg?(file) || png?(file) || gif?(file) || tiff?(file) || ico?(file)
|
44
|
-
bool
|
45
|
-
end
|
46
52
|
|
47
|
-
|
48
|
-
|
49
|
-
# Examples:
|
50
|
-
#
|
51
|
-
# # On Linux
|
52
|
-
# File.null # => '/dev/null'
|
53
|
-
#
|
54
|
-
# # On MS Windows
|
55
|
-
# File.null # => 'NUL'
|
56
|
-
#--
|
57
|
-
# The values I used here are based on information from
|
58
|
-
# http://en.wikipedia.org/wiki//dev/null
|
59
|
-
#
|
60
|
-
def self.null
|
61
|
-
case RbConfig::CONFIG['host_os']
|
62
|
-
when /mswin|win32|msdos|mingw|windows/i
|
63
|
-
'NUL'
|
64
|
-
when /amiga/i
|
65
|
-
'NIL:'
|
66
|
-
when /openvms/i
|
67
|
-
'NL:'
|
68
|
-
else
|
69
|
-
'/dev/null'
|
53
|
+
if check_file_extension
|
54
|
+
bool = bool && IMAGE_EXT.include?(File.extname(file).downcase)
|
70
55
|
end
|
71
|
-
end
|
72
56
|
|
73
|
-
|
74
|
-
alias null_device null
|
57
|
+
bool
|
75
58
|
end
|
76
59
|
|
77
60
|
# Returns whether or not +file+ is a binary non-image file, i.e. executable,
|
@@ -92,7 +75,9 @@ class File
|
|
92
75
|
# based on Perl's -B switch).
|
93
76
|
#
|
94
77
|
def self.binary?(file, percentage = 0.30)
|
78
|
+
return false if File.stat(file).zero?
|
95
79
|
return false if image?(file)
|
80
|
+
return false if check_bom?(file)
|
96
81
|
bytes = File.stat(file).blksize
|
97
82
|
bytes = 4096 if bytes > 4096
|
98
83
|
s = (File.read(file, bytes) || "")
|
@@ -430,8 +415,24 @@ class File
|
|
430
415
|
end
|
431
416
|
end
|
432
417
|
|
433
|
-
|
418
|
+
# Returns whether or not the given +text+ contains a BOM marker.
|
419
|
+
# If present, we can generally assume it's a text file.
|
420
|
+
#
|
421
|
+
def self.check_bom?(file)
|
422
|
+
text = File.read(file, 4).force_encoding('utf-8')
|
423
|
+
|
424
|
+
bool = false
|
425
|
+
bool = true if text[0,3] == "\xEF\xBB\xBF"
|
426
|
+
bool = true if text[0,4] == "\x00\x00\xFE\xFF" || text[0,4] == "\xFF\xFE\x00\x00"
|
427
|
+
bool = true if text[0,2] == "\xFF\xFE" || text[0,2] == "\xFE\xFF"
|
434
428
|
|
429
|
+
bool
|
430
|
+
end
|
431
|
+
|
432
|
+
private_class_method :check_bom?
|
433
|
+
|
434
|
+
# Returns the newline characters for the given platform.
|
435
|
+
#
|
435
436
|
def self.nl_for_platform(platform)
|
436
437
|
platform = RbConfig::CONFIG["host_os"] if platform == 'local'
|
437
438
|
|
@@ -447,29 +448,39 @@ class File
|
|
447
448
|
end
|
448
449
|
end
|
449
450
|
|
451
|
+
# Is the file a bitmap file?
|
452
|
+
#
|
450
453
|
def self.bmp?(file)
|
451
454
|
IO.read(file, 3) == "BM6"
|
452
455
|
end
|
453
456
|
|
457
|
+
# Is the file a jpeg file?
|
458
|
+
#
|
454
459
|
def self.jpg?(file)
|
455
460
|
IO.read(file, 10, nil, :encoding => 'binary') == "\377\330\377\340\000\020JFIF".force_encoding(Encoding::BINARY)
|
456
461
|
end
|
457
462
|
|
463
|
+
# Is the file a png file?
|
464
|
+
#
|
458
465
|
def self.png?(file)
|
459
466
|
IO.read(file, 4, nil, :encoding => 'binary') == "\211PNG".force_encoding(Encoding::BINARY)
|
460
467
|
end
|
461
468
|
|
469
|
+
# Is the file a gif?
|
470
|
+
#
|
462
471
|
def self.gif?(file)
|
463
472
|
['GIF89a', 'GIF97a'].include?(IO.read(file, 6))
|
464
473
|
end
|
465
474
|
|
475
|
+
# Is the file a tiff?
|
476
|
+
#
|
466
477
|
def self.tiff?(file)
|
467
478
|
return false if File.size(file) < 12
|
468
479
|
|
469
480
|
bytes = IO.read(file, 4)
|
470
481
|
|
471
482
|
# II is Intel, MM is Motorola
|
472
|
-
if bytes[0..1] != 'II'&& bytes[0..1] != 'MM'
|
483
|
+
if bytes[0..1] != 'II' && bytes[0..1] != 'MM'
|
473
484
|
return false
|
474
485
|
end
|
475
486
|
|
@@ -484,6 +495,8 @@ class File
|
|
484
495
|
true
|
485
496
|
end
|
486
497
|
|
498
|
+
# Is the file an ico file?
|
499
|
+
#
|
487
500
|
def self.ico?(file)
|
488
501
|
["\000\000\001\000", "\000\000\002\000"].include?(IO.read(file, 4, nil, :encoding => 'binary'))
|
489
502
|
end
|
data/ptools.gemspec
CHANGED
@@ -1,31 +1,37 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'rbconfig'
|
3
2
|
|
4
3
|
Gem::Specification.new do |spec|
|
5
4
|
spec.name = 'ptools'
|
6
|
-
spec.version = '1.
|
5
|
+
spec.version = '1.4.2'
|
7
6
|
spec.license = 'Artistic-2.0'
|
8
7
|
spec.author = 'Daniel J. Berger'
|
9
8
|
spec.email = 'djberg96@gmail.com'
|
10
9
|
spec.homepage = 'https://github.com/djberg96/ptools'
|
11
10
|
spec.summary = 'Extra methods for the File class'
|
12
|
-
spec.test_files = Dir['
|
11
|
+
spec.test_files = Dir['spec/_spec*']
|
13
12
|
spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
|
14
13
|
spec.cert_chain = ['certs/djberg96_pub.pem']
|
15
14
|
|
16
|
-
spec.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
|
17
|
-
|
18
15
|
spec.description = <<-EOF
|
19
16
|
The ptools (power tools) library provides several handy methods to
|
20
17
|
Ruby's core File class, such as File.which for finding executables,
|
21
18
|
File.null to return the null device on your platform, and so on.
|
22
19
|
EOF
|
23
20
|
|
21
|
+
spec.metadata = {
|
22
|
+
'homepage_uri' => 'https://github.com/djberg96/ptools',
|
23
|
+
'bug_tracker_uri' => 'https://github.com/djberg96/ptools/issues',
|
24
|
+
'changelog_uri' => 'https://github.com/djberg96/ptools/blob/master/CHANGES.md',
|
25
|
+
'documentation_uri' => 'https://github.com/djberg96/ptools/wiki',
|
26
|
+
'source_code_uri' => 'https://github.com/djberg96/ptools',
|
27
|
+
'wiki_uri' => 'https://github.com/djberg96/ptools/wiki'
|
28
|
+
}
|
29
|
+
|
24
30
|
spec.add_development_dependency('rake')
|
25
|
-
spec.add_development_dependency('
|
31
|
+
spec.add_development_dependency('rspec', '~> 3.9')
|
26
32
|
|
27
33
|
if File::ALT_SEPARATOR
|
28
34
|
spec.platform = Gem::Platform.new(['universal', 'mingw32'])
|
29
|
-
spec.add_dependency('win32-file'
|
35
|
+
spec.add_dependency('win32-file')
|
30
36
|
end
|
31
37
|
end
|
data/spec/binary_spec.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#####################################################################
|
2
|
+
# test_binary.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.binary? method. You should run this test
|
5
|
+
# via the 'rake test_binary' task.
|
6
|
+
#####################################################################
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rspec'
|
9
|
+
require 'ptools'
|
10
|
+
|
11
|
+
RSpec.describe File, :binary do
|
12
|
+
let(:dirname) { File.dirname(__FILE__) }
|
13
|
+
let(:bin_file) { File::ALT_SEPARATOR ? File.join(ENV['windir'], 'notepad.exe') : '/bin/ls' }
|
14
|
+
|
15
|
+
before do
|
16
|
+
@txt_file = File.join(dirname, 'txt', 'english.txt')
|
17
|
+
@emp_file = File.join(dirname, 'txt', 'empty.txt')
|
18
|
+
@uni_file = File.join(dirname, 'txt', 'korean.txt')
|
19
|
+
@utf_file = File.join(dirname, 'txt', 'english.utf16')
|
20
|
+
@png_file = File.join(dirname, 'img', 'test.png')
|
21
|
+
@jpg_file = File.join(dirname, 'img', 'test.jpg')
|
22
|
+
@gif_file = File.join(dirname, 'img', 'test.gif')
|
23
|
+
end
|
24
|
+
|
25
|
+
example "File.binary? basic functionality" do
|
26
|
+
expect(File).to respond_to(:binary?)
|
27
|
+
expect{ File.binary?(@txt_file) }.not_to raise_error
|
28
|
+
end
|
29
|
+
|
30
|
+
example "File.binary? returns true for binary files" do
|
31
|
+
expect(File.binary?(bin_file)).to be true
|
32
|
+
end
|
33
|
+
|
34
|
+
example "File.binary? returns false for text files" do
|
35
|
+
expect(File.binary?(@emp_file)).to be false
|
36
|
+
expect(File.binary?(@txt_file)).to be false
|
37
|
+
expect(File.binary?(@uni_file)).to be false
|
38
|
+
expect(File.binary?(@utf_file)).to be false
|
39
|
+
end
|
40
|
+
|
41
|
+
example "File.binary? returns false for image files" do
|
42
|
+
expect(File.binary?(@png_file)).to be false
|
43
|
+
expect(File.binary?(@jpg_file)).to be false
|
44
|
+
expect(File.binary?(@gif_file)).to be false
|
45
|
+
end
|
46
|
+
|
47
|
+
example "File.binary? accepts an optional percentage argument" do
|
48
|
+
expect(File.binary?(@txt_file, 0.50)).to be false
|
49
|
+
expect(File.binary?(@txt_file, 0.05)).to be true
|
50
|
+
end
|
51
|
+
|
52
|
+
example "File.binary? raises an error if the file cannot be found" do
|
53
|
+
expect{ File.binary?('bogus') }.to raise_error(SystemCallError)
|
54
|
+
end
|
55
|
+
|
56
|
+
example "File.binary? only accepts one argument" do
|
57
|
+
expect{ File.binary?(@txt_file, bin_file) }.to raise_error(ArgumentError)
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
##############################################################################
|
2
|
+
# constants_spec.rb
|
3
|
+
#
|
4
|
+
# Specs for the constants that have been defined in the ptools library.
|
5
|
+
# This test case should be run via the 'rake spec:constants' task.
|
6
|
+
##############################################################################
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rspec'
|
9
|
+
require 'rbconfig'
|
10
|
+
require 'ptools'
|
11
|
+
|
12
|
+
RSpec.describe File, :constants do
|
13
|
+
let(:windows) { File::ALT_SEPARATOR }
|
14
|
+
|
15
|
+
example "PTOOLS_VERSION constant is set to expected value" do
|
16
|
+
expect(File::PTOOLS_VERSION).to eq('1.4.2')
|
17
|
+
expect(File::PTOOLS_VERSION.frozen?).to be true
|
18
|
+
end
|
19
|
+
|
20
|
+
example "IMAGE_EXT constant is set to array of values" do
|
21
|
+
expect(File::IMAGE_EXT).to match_array(%w[.bmp .gif .ico .jpeg .jpg .png])
|
22
|
+
end
|
23
|
+
end
|
data/spec/head_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
######################################################################
|
2
|
+
# head_spec.rb
|
3
|
+
#
|
4
|
+
# Specs for the File.head method. These specs should be run via
|
5
|
+
# the 'rake spec:head' task.
|
6
|
+
######################################################################
|
7
|
+
require 'rspec'
|
8
|
+
require 'ptools'
|
9
|
+
|
10
|
+
RSpec.describe File, :head do
|
11
|
+
let(:test_file) { 'test_file_head.txt' }
|
12
|
+
|
13
|
+
before do
|
14
|
+
File.open(test_file, 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
|
15
|
+
@expected_head1 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
|
16
|
+
@expected_head1.push("line6\n","line7\n","line8\n","line9\n","line10\n")
|
17
|
+
@expected_head2 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
|
18
|
+
end
|
19
|
+
|
20
|
+
example "head method basic functionality" do
|
21
|
+
expect(File).to respond_to(:head)
|
22
|
+
expect{ File.head(test_file) }.not_to raise_error
|
23
|
+
expect{ File.head(test_file, 5) }.not_to raise_error
|
24
|
+
expect{ File.head(test_file){} }.not_to raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
example "head method returns the expected results" do
|
28
|
+
expect(File.head(test_file)).to be_kind_of(Array)
|
29
|
+
expect(File.head(test_file)).to eq(@expected_head1)
|
30
|
+
expect(File.head(test_file, 5)).to eq(@expected_head2)
|
31
|
+
end
|
32
|
+
|
33
|
+
example "head method requires two arguments" do
|
34
|
+
expect{ File.head(test_file, 5, "foo") }.to raise_error(ArgumentError)
|
35
|
+
expect{ File.head("bogus") }.to raise_error(Errno::ENOENT)
|
36
|
+
end
|
37
|
+
|
38
|
+
after do
|
39
|
+
File.delete(test_file) if File.exists?(test_file)
|
40
|
+
end
|
41
|
+
end
|
data/spec/image_spec.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
#####################################################################
|
2
|
+
# image_spec.rb
|
3
|
+
#
|
4
|
+
# Specs for the File.image? method. You should run these specs via
|
5
|
+
# the 'rake spec:image' task.
|
6
|
+
#####################################################################
|
7
|
+
require 'rspec'
|
8
|
+
require 'ptools'
|
9
|
+
|
10
|
+
RSpec.describe File, :image do
|
11
|
+
before do
|
12
|
+
Dir.chdir('spec') if File.exist?('spec')
|
13
|
+
@txt_file = File.join(Dir.pwd, 'txt', 'english.txt')
|
14
|
+
@uni_file = File.join(Dir.pwd, 'txt', 'korean.txt')
|
15
|
+
@jpg_file = File.join(Dir.pwd, 'img', 'test.jpg')
|
16
|
+
@png_file = File.join(Dir.pwd, 'img', 'test.png')
|
17
|
+
@gif_file = File.join(Dir.pwd, 'img', 'test.gif')
|
18
|
+
@ico_file = File.join(Dir.pwd, 'img', 'test.ico')
|
19
|
+
end
|
20
|
+
|
21
|
+
example "image? method basic functionality" do
|
22
|
+
expect(File).to respond_to(:image?)
|
23
|
+
expect{ File.image?(@txt_file) }.not_to raise_error
|
24
|
+
expect(File.image?(@txt_file)).to be(true).or be(false)
|
25
|
+
end
|
26
|
+
|
27
|
+
example "image? method returns false for a text file" do
|
28
|
+
expect(File.image?(@txt_file)).to be false
|
29
|
+
expect(File.image?(@uni_file)).to be false
|
30
|
+
end
|
31
|
+
|
32
|
+
example "image? method returns true for a gif" do
|
33
|
+
expect(File.image?(@gif_file)).to be true
|
34
|
+
end
|
35
|
+
|
36
|
+
example "image? method returns true for a jpeg" do
|
37
|
+
expect(File.image?(@jpg_file)).to be true
|
38
|
+
end
|
39
|
+
|
40
|
+
example "image? method returns true for a png" do
|
41
|
+
expect(File.image?(@png_file)).to be true
|
42
|
+
end
|
43
|
+
|
44
|
+
example "image? method returns true for an ico" do
|
45
|
+
expect(File.image?(@ico_file)).to be true
|
46
|
+
end
|
47
|
+
|
48
|
+
example "image? method raises an error if the file does not exist" do
|
49
|
+
expect{ File.image?('bogus') }.to raise_error(Exception) # Errno::ENOENT or ArgumentError
|
50
|
+
end
|
51
|
+
end
|
data/{test → spec}/img/test.gif
RENAMED
File without changes
|
data/{test → spec}/img/test.ico
RENAMED
File without changes
|
data/{test → spec}/img/test.jpg
RENAMED
File without changes
|
data/{test → spec}/img/test.png
RENAMED
File without changes
|
@@ -0,0 +1,104 @@
|
|
1
|
+
#####################################################################
|
2
|
+
# test_nlconvert.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.nl_convert method. You should run this
|
5
|
+
# test via the 'rake test_nlconvert' task.
|
6
|
+
#####################################################################
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rspec'
|
9
|
+
require 'ptools'
|
10
|
+
|
11
|
+
RSpec.describe File, :nlconvert do
|
12
|
+
let(:windows) { File::ALT_SEPARATOR }
|
13
|
+
let(:dirname) { File.dirname(__FILE__) }
|
14
|
+
let(:test_file1) { File.join(dirname, 'test_nl_convert1.txt') }
|
15
|
+
let(:test_file2) { File.join(dirname, 'test_nl_convert2.txt') }
|
16
|
+
|
17
|
+
before do
|
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
|
+
@test_file1 = File.join(dirname, 'test_nl_convert1.txt')
|
21
|
+
@test_file2 = File.join(dirname, 'test_nl_convert2.txt')
|
22
|
+
@dos_file = File.join(dirname, 'dos_test_file.txt')
|
23
|
+
@mac_file = File.join(dirname, 'mac_test_file.txt')
|
24
|
+
@unix_file = 'unix_test_file.txt'
|
25
|
+
end
|
26
|
+
|
27
|
+
example "nl_for_platform basic functionality" do
|
28
|
+
expect(File).to respond_to(:nl_for_platform)
|
29
|
+
end
|
30
|
+
|
31
|
+
example "nl_for_platform returns expected results" do
|
32
|
+
expect(File.nl_for_platform('dos') ).to eq( "\cM\cJ")
|
33
|
+
expect(File.nl_for_platform('unix') ).to eq( "\cJ")
|
34
|
+
expect(File.nl_for_platform('mac') ).to eq( "\cM")
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
example "nl_for_platform with 'local' platform does not raise an error" do
|
39
|
+
expect{ File.nl_for_platform('local') }.not_to raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
example "nl_for_platform with unsupported platform raises an error" do
|
43
|
+
expect{ File.nl_for_platform('bogus') }.to raise_error(ArgumentError)
|
44
|
+
end
|
45
|
+
|
46
|
+
example "nl_convert basic functionality" do
|
47
|
+
expect(File).to respond_to(:nl_convert)
|
48
|
+
end
|
49
|
+
|
50
|
+
example "nl_convert accepts one, two or three arguments" do
|
51
|
+
expect{ File.nl_convert(@test_file2) }.not_to raise_error
|
52
|
+
expect{ File.nl_convert(@test_file2, @test_file2) }.not_to raise_error
|
53
|
+
expect{ File.nl_convert(@test_file2, @test_file2, "unix") }.not_to raise_error
|
54
|
+
end
|
55
|
+
|
56
|
+
example "nl_convert with dos platform argument works as expected" do
|
57
|
+
expect{ File.nl_convert(@test_file1, @dos_file, "dos") }.not_to raise_error
|
58
|
+
expect{ File.nl_convert(@test_file1, @dos_file, "dos") }.not_to raise_error
|
59
|
+
expect(File.size(@dos_file)).to be > File.size(@test_file1)
|
60
|
+
expect(IO.readlines(@dos_file).first.split("")[-2..-1]).to eq(["\cM","\cJ"])
|
61
|
+
end
|
62
|
+
|
63
|
+
example "nl_convert with mac platform argument works as expected" do
|
64
|
+
expect{ File.nl_convert(@test_file1, @mac_file, 'mac') }.not_to raise_error
|
65
|
+
expect(IO.readlines(@mac_file).first.split("").last).to eq("\cM")
|
66
|
+
|
67
|
+
skip if windows
|
68
|
+
expect(File.size(@mac_file)).to eq(File.size(@test_file1))
|
69
|
+
end
|
70
|
+
|
71
|
+
example "nl_convert with unix platform argument works as expected" do
|
72
|
+
expect{ File.nl_convert(@test_file1, @unix_file, "unix") }.not_to raise_error
|
73
|
+
expect(IO.readlines(@unix_file).first.split("").last).to eq("\n")
|
74
|
+
|
75
|
+
if windows
|
76
|
+
expect(File.size(@unix_file) >= File.size(@test_file1)).to be true
|
77
|
+
else
|
78
|
+
expect(File.size(@unix_file) <= File.size(@test_file1)).to be true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
example "nl_convert requires at least one argument" do
|
83
|
+
expect{ File.nl_convert }.to raise_error(ArgumentError)
|
84
|
+
end
|
85
|
+
|
86
|
+
example "nl_convert requires a valid platform string" do
|
87
|
+
expect{ File.nl_convert(@test_file1, "bogus.txt", "blah") }.to raise_error(ArgumentError)
|
88
|
+
end
|
89
|
+
|
90
|
+
example "nl_convert accepts a maximum of three arguments" do
|
91
|
+
expect{ File.nl_convert(@example_file1, @test_file2, 'dos', 1) }.to raise_error(ArgumentError)
|
92
|
+
expect{ File.nl_convert(@test_file1, @test_file2, 'dos', 1) }.to raise_error(ArgumentError)
|
93
|
+
end
|
94
|
+
|
95
|
+
example "nl_convert will fail on anything but plain files" do
|
96
|
+
expect{ File.nl_convert(IO::NULL, @test_file1) }.to raise_error(ArgumentError)
|
97
|
+
end
|
98
|
+
|
99
|
+
after do
|
100
|
+
[@dos_file, @mac_file, @unix_file].each{ |file| File.delete(file) if File.exist?(file) }
|
101
|
+
File.delete(test_file1) if File.exist?(test_file1)
|
102
|
+
File.delete(test_file2) if File.exist?(test_file2)
|
103
|
+
end
|
104
|
+
end
|