ptools 1.3.7 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
File without changes
File without changes
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
@@ -0,0 +1,43 @@
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 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :sparse do
11
+ let(:windows) { File::ALT_SEPARATOR }
12
+ let(:osx) { RbConfig::CONFIG['host_os'] =~ /darwin|osx/i }
13
+ let(:non_sparse_file) { File.expand_path(File.basename(__FILE__)) }
14
+ let(:sparse_file) { 'test_sparse_file' }
15
+
16
+ before do
17
+ Dir.chdir("spec") if File.exist?("spec")
18
+ system("dd of=#{sparse_file} bs=1k seek=5120 count=0 2>/dev/null") unless windows
19
+ end
20
+
21
+ example "is_sparse basic functionality" do
22
+ skip "skipped on MS Windows or OSX" if windows || osx
23
+ expect(File).to respond_to(:sparse?)
24
+ expect{ File.sparse?(sparse_file) }.not_to raise_error
25
+ expect(File.sparse?(sparse_file)).to be(true).or be(false)
26
+ end
27
+
28
+ example "is_sparse returns the expected results" do
29
+ skip "skipped on MS Windows or OSX" if windows || osx
30
+ expect(File.sparse?(sparse_file)).to be true
31
+ expect(File.sparse?(non_sparse_file)).to be false
32
+ end
33
+
34
+ example "is_sparse only accepts one argument" do
35
+ skip if windows
36
+ expect{ File.sparse?(sparse_file, sparse_file) }.to raise_error(ArgumentError)
37
+ end
38
+
39
+ after do
40
+ Dir.chdir("spec") if File.exist?("spec")
41
+ File.delete(sparse_file) if File.exist?(sparse_file)
42
+ end
43
+ end
@@ -0,0 +1,107 @@
1
+ #####################################################################
2
+ # tail_spec.rb
3
+ #
4
+ # Tests for the File.tail method. This test should be run via
5
+ # the 'rake spec:tail' task.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :tail do
11
+ let(:dirname) { File.dirname(__FILE__) }
12
+ let(:test_file1) { File.join(dirname, 'test_file1.txt') }
13
+ let(:test_file64) { File.join(dirname, 'test_file64.txt') }
14
+ let(:test_file128) { File.join(dirname, 'test_file128.txt') }
15
+ let(:test_file_trail) { File.join(dirname, 'test_file_trail.txt') }
16
+ let(:test_file_trail_nl) { File.join(dirname, 'test_file_trail_nl.txt') }
17
+
18
+ before do
19
+ File.open(test_file1, 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
20
+
21
+ # Trailing newline test
22
+ File.open(test_file_trail, 'w'){ |fh|
23
+ 2.times{ |n| fh.puts "trail#{n+1}" }
24
+ fh.write "trail3"
25
+ }
26
+
27
+ File.open(test_file_trail_nl, 'w'){ |fh|
28
+ 3.times{ |n| fh.puts "trail#{n+1}" }
29
+ }
30
+
31
+ # Larger files
32
+ test_tail_fmt_str = "line data data data data data data data %5s"
33
+
34
+ File.open(test_file64, 'w'){ |fh|
35
+ 2000.times{ |n|
36
+ fh.puts test_tail_fmt_str % (n+1).to_s
37
+ }
38
+ }
39
+
40
+ File.open(test_file128, 'w'){ |fh|
41
+ 4500.times{ |n|
42
+ fh.puts test_tail_fmt_str % (n+1).to_s
43
+ }
44
+ }
45
+
46
+ @expected_tail1 = %w{
47
+ line16 line17 line18 line19 line20
48
+ line21 line22 line23 line24 line25
49
+ }
50
+
51
+ @expected_tail2 = ["line21","line22","line23","line24","line25"]
52
+
53
+ @expected_tail_more = []
54
+ 25.times{ |n| @expected_tail_more.push "line#{n+1}" }
55
+
56
+ @expected_tail_trail = %w{ trail2 trail3 }
57
+
58
+ @test_tail_fmt_str = "line data data data data data data data %5s"
59
+ end
60
+
61
+ example "tail basic functionality" do
62
+ expect(File).to respond_to(:tail)
63
+ expect{ File.tail(test_file1) }.not_to raise_error
64
+ expect{ File.tail(test_file1, 5) }.not_to raise_error
65
+ expect{ File.tail(test_file1){}.not_to raise_error }
66
+ end
67
+
68
+ example "tail returns the expected values" do
69
+ expect(File.tail(test_file1)).to be_kind_of(Array)
70
+ expect(File.tail(test_file1)).to eq(@expected_tail1)
71
+ expect(File.tail(test_file1, 5)).to eq(@expected_tail2)
72
+ end
73
+
74
+ example "specifying a number greater than the actual number of lines works as expected" do
75
+ expect(File.tail(test_file1, 30)).to eq(@expected_tail_more)
76
+ end
77
+
78
+ example "tail requires two arguments" do
79
+ expect{ File.tail }.to raise_error(ArgumentError)
80
+ expect{ File.tail(test_file1, 5, 5) }.to raise_error(ArgumentError)
81
+ end
82
+
83
+ example "tail works as expected when there is no trailing newline" do
84
+ expect(File.tail(test_file_trail, 2)).to eq(@expected_tail_trail)
85
+ expect(File.tail(test_file_trail_nl, 2)).to eq(@expected_tail_trail)
86
+ end
87
+
88
+ example "tail works as expected on a file larger than 64k" do
89
+ expected_tail_64k = []
90
+ 2000.times{ |n| expected_tail_64k.push(@test_tail_fmt_str % (n+1).to_s) }
91
+ expect(File.tail(test_file64, 2000)).to eq(expected_tail_64k)
92
+ end
93
+
94
+ example "tail works as expected on a file larger than 128k" do
95
+ expected_tail_128k = []
96
+ 4500.times{ |n| expected_tail_128k.push(@test_tail_fmt_str % (n+1).to_s) }
97
+ expect(File.tail(test_file128, 4500)).to eq(expected_tail_128k)
98
+ end
99
+
100
+ after do
101
+ File.delete(test_file1) if File.exist?(test_file1)
102
+ File.delete(test_file64) if File.exist?(test_file64)
103
+ File.delete(test_file128) if File.exist?(test_file128)
104
+ File.delete(test_file_trail_nl) if File.exist?(test_file_trail_nl)
105
+ File.delete(test_file_trail) if File.exist?(test_file_trail)
106
+ end
107
+ end
@@ -0,0 +1,47 @@
1
+ #####################################################################
2
+ # test_touch.rb
3
+ #
4
+ # Test case for the File.touch method. This test should be run
5
+ # via the 'rake test_touch task'.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :touch do
11
+ let(:dirname) { File.dirname(__FILE__) }
12
+ let(:filename) { 'test_file_touch.txt' }
13
+ let(:xfile) { File.join(dirname, filename) }
14
+
15
+ before do
16
+ File.open(xfile, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
17
+ @test_file = File.join(dirname, 'delete.this')
18
+ end
19
+
20
+ example "touch basic functionality" do
21
+ expect(File).to respond_to(:touch)
22
+ expect{ File.touch(@test_file) }.not_to raise_error
23
+ end
24
+
25
+ example "touch a new file returns expected results" do
26
+ expect(File.touch(@test_file)).to eq(File)
27
+ expect(File.exist?(@test_file)).to be true
28
+ expect(File.size(@test_file)).to eq(0)
29
+ end
30
+
31
+ example "touch an existing file returns expected results" do
32
+ stat = File.stat(xfile)
33
+ sleep 0.5
34
+ expect{ File.touch(xfile) }.not_to raise_error
35
+ expect(File.size(xfile) == stat.size).to be true
36
+ expect(File.mtime(xfile) == stat.mtime).to be false
37
+ end
38
+
39
+ example "touch requires an argument" do
40
+ expect{ File.touch }.to raise_error(ArgumentError)
41
+ end
42
+
43
+ after do
44
+ File.delete(@test_file) if File.exist?(@test_file)
45
+ File.delete(xfile) if File.exist?(xfile)
46
+ end
47
+ end
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,65 @@
1
+ #####################################################################
2
+ # wc_spec.rb
3
+ #
4
+ # Specs for the File.wc method. These specs should be run via
5
+ # the 'rake wc' task.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :wc do
11
+ let(:test_file) { 'test_file_wc.txt' }
12
+
13
+ before do
14
+ File.open(test_file, 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
15
+ end
16
+
17
+ example "wc method basic functionality" do
18
+ expect(File).to respond_to(:wc)
19
+ expect{ File.wc(test_file) }.not_to raise_error
20
+ end
21
+
22
+ example "wc accepts specific optional arguments" do
23
+ expect{ File.wc(test_file, 'bytes') }.not_to raise_error
24
+ expect{ File.wc(test_file, 'chars') }.not_to raise_error
25
+ expect{ File.wc(test_file, 'words') }.not_to raise_error
26
+ expect{ File.wc(test_file, 'lines') }.not_to raise_error
27
+ end
28
+
29
+ example "argument to wc ignores the case of the option argument" do
30
+ expect{ File.wc(test_file, 'LINES') }.not_to raise_error
31
+ end
32
+
33
+ example "wc with no option returns expected results" do
34
+ expect(File.wc(test_file)).to be_kind_of(Array)
35
+ expect(File.wc(test_file)).to eq([166, 166, 25, 25])
36
+ end
37
+
38
+ example "wc with bytes option returns the expected result" do
39
+ expect(File.wc(test_file, 'bytes')).to eq(166)
40
+ end
41
+
42
+ example "wc with chars option returns the expected result" do
43
+ expect(File.wc(test_file, 'chars')).to eq(166)
44
+ end
45
+
46
+ example "wc with words option returns the expected result" do
47
+ expect(File.wc(test_file, 'words')).to eq(25)
48
+ end
49
+
50
+ example "wc with lines option returns the expected result" do
51
+ expect(File.wc(test_file, 'lines')).to eq(25)
52
+ end
53
+
54
+ example "wc requires at least on argument" do
55
+ expect{ File.wc }.to raise_error(ArgumentError)
56
+ end
57
+
58
+ example "an invalid option raises an error" do
59
+ expect{ File.wc(test_file, 'bogus') }.to raise_error(ArgumentError)
60
+ end
61
+
62
+ after do
63
+ File.delete(test_file) if File.exists?(test_file)
64
+ end
65
+ end
@@ -0,0 +1,87 @@
1
+ ######################################################################
2
+ # test_whereis.rb
3
+ #
4
+ # Tests for the File.whereis method.
5
+ ######################################################################
6
+ require 'rubygems'
7
+ require 'rspec'
8
+ require 'ptools'
9
+ require 'rbconfig'
10
+
11
+ RSpec.describe File, :whereis do
12
+ let(:windows) { File::ALT_SEPARATOR }
13
+ let(:ruby) { RUBY_ENGINE }
14
+ let(:bin_dir) { RbConfig::CONFIG['bindir'] }
15
+
16
+ before do
17
+ @expected_locs = [File.join(bin_dir, ruby)]
18
+
19
+ if windows
20
+ @expected_locs[0] << '.exe'
21
+ @expected_locs[0].tr!("/", "\\")
22
+ end
23
+
24
+ unless windows
25
+ @expected_locs << "/usr/local/bin/#{ruby}"
26
+ @expected_locs << "/opt/sfw/bin/#{ruby}"
27
+ @expected_locs << "/opt/bin/#{ruby}"
28
+ @expected_locs << "/usr/bin/#{ruby}"
29
+ end
30
+
31
+ @actual_locs = nil
32
+ end
33
+
34
+ example "whereis basic functionality" do
35
+ expect(File).to respond_to(:whereis)
36
+ expect{ File.whereis('ruby') }.not_to raise_error
37
+ expect(File.whereis('ruby')).to be_kind_of(Array).or be_nil
38
+ end
39
+
40
+ example "whereis accepts an optional second argument" do
41
+ expect{ File.whereis('ruby', '/usr/bin:/usr/local/bin') }.not_to raise_error
42
+ end
43
+
44
+ example "whereis returns expected values" do
45
+ expect{ @actual_locs = File.whereis(ruby) }.not_to raise_error
46
+ expect(@actual_locs).to be_kind_of(Array)
47
+ expect((@expected_locs & @actual_locs).size > 0).to be true
48
+ end
49
+
50
+ example "whereis returns nil if program not found" do
51
+ expect(File.whereis('xxxyyy')).to be_nil
52
+ end
53
+
54
+ example "whereis returns nil if program cannot be found in provided path" do
55
+ expect(File.whereis(ruby, '/foo/bar')).to be_nil
56
+ end
57
+
58
+ example "whereis returns single element array or nil if absolute path is provided" do
59
+ absolute = File.join(bin_dir, ruby)
60
+ absolute << '.exe' if windows
61
+
62
+ expect(File.whereis(absolute)).to eq([absolute])
63
+ expect(File.whereis("/foo/bar/baz/#{ruby}")).to be_nil
64
+ end
65
+
66
+ example "whereis works with an explicit extension on ms windows" do
67
+ skip "skipped unless MS Windows" unless windows
68
+ expect(File.whereis(ruby + '.exe')).not_to be_nil
69
+ end
70
+
71
+ example "whereis requires at least one argument" do
72
+ expect{ File.whereis }.to raise_error(ArgumentError)
73
+ end
74
+
75
+ example "whereis returns unique paths only" do
76
+ expect(File.whereis(ruby) == File.whereis(ruby).uniq).to be true
77
+ end
78
+
79
+ example "whereis accepts a maximum of two arguments" do
80
+ expect{ File.whereis(ruby, 'foo', 'bar') }.to raise_error(ArgumentError)
81
+ end
82
+
83
+ example "the second argument to whereis cannot be nil or empty" do
84
+ expect{ File.whereis(ruby, nil) }.to raise_error(ArgumentError)
85
+ expect{ File.whereis(ruby, '') }.to raise_error(ArgumentError)
86
+ end
87
+ end