ptools 1.1.7 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/ptools.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'ptools'
6
+ gem.version = '1.1.9'
7
+ gem.license = 'Artistic 2.0'
8
+ gem.author = 'Daniel J. Berger'
9
+ gem.email = 'djberg96@gmail.com'
10
+ gem.homepage = 'http://www.rubyforge.org/projects/shards'
11
+ gem.summary = 'Extra methods for the File class'
12
+ gem.test_files = Dir['test/test*']
13
+ gem.has_rdoc = true
14
+ gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') || f.include?('git') }
15
+
16
+ gem.rubyforge_project = 'shards'
17
+ gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
18
+
19
+ gem.description = <<-EOF
20
+ The ptools (power tools) library provides several handy methods to
21
+ Ruby's core File class, such as File.which for finding executables,
22
+ File.null to return the null device on your platform, and so on.
23
+ EOF
24
+
25
+ gem.add_development_dependency('test-unit', '>= 2.0.7')
26
+
27
+ if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
28
+ gem.platform = Gem::Platform::CURRENT
29
+ gem.add_dependency('win32-file', '>= 0.5.4')
30
+ end
31
+ end
data/test/test_binary.rb CHANGED
@@ -12,7 +12,8 @@ require 'ptools'
12
12
 
13
13
  class TC_Binary < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
16
17
  end
17
18
 
18
19
  def setup
@@ -29,10 +30,14 @@ class TC_Binary < Test::Unit::TestCase
29
30
  end
30
31
 
31
32
  def test_binary_expected_errors
32
- assert_raises(Errno::ENOENT, ArgumentError){ File.binary?('bogus') }
33
+ assert_raise_kind_of(SystemCallError){ File.binary?('bogus') }
33
34
  end
34
35
 
35
36
  def teardown
36
37
  @text_file = nil
37
38
  end
39
+
40
+ def self.shutdown
41
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
42
+ end
38
43
  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.7', File::PTOOLS_VERSION)
16
+ assert_equal('1.1.9', File::PTOOLS_VERSION)
17
17
  end
18
18
 
19
19
  def test_image_ext
data/test/test_head.rb CHANGED
@@ -12,10 +12,11 @@ require 'ptools'
12
12
 
13
13
  class TC_FileHead < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
16
17
  end
17
18
 
18
- def setup
19
+ def setup
19
20
  @test_file = 'test_file1.txt'
20
21
  @expected_head1 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
21
22
  @expected_head1.push("line6\n","line7\n","line8\n","line9\n","line10\n")
@@ -43,4 +44,8 @@ class TC_FileHead < Test::Unit::TestCase
43
44
  def teardown
44
45
  @test_file = nil
45
46
  end
47
+
48
+ def self.shutdown
49
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
50
+ end
46
51
  end
data/test/test_image.rb CHANGED
@@ -12,7 +12,8 @@ require 'ptools'
12
12
 
13
13
  class TC_Ptools_Image < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
16
17
  end
17
18
 
18
19
  def setup
@@ -35,4 +36,8 @@ class TC_Ptools_Image < Test::Unit::TestCase
35
36
  def teardown
36
37
  @text_file = nil
37
38
  end
39
+
40
+ def self.shutdown
41
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
42
+ end
38
43
  end
data/test/test_middle.rb CHANGED
@@ -12,7 +12,8 @@ require 'ptools'
12
12
 
13
13
  class TC_FileMiddle < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
16
17
  end
17
18
 
18
19
  def setup
@@ -12,7 +12,9 @@ require 'ptools'
12
12
 
13
13
  class TC_FileNLConvert < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
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}" } }
16
18
  end
17
19
 
18
20
  def setup
@@ -41,15 +43,12 @@ class TC_FileNLConvert < Test::Unit::TestCase
41
43
  end
42
44
 
43
45
  def test_nl_convert_to_mac
44
- if Config::CONFIG['host_os'].match("mswin")
45
- msg = "** test may fail on MS Windows **"
46
- else
47
- msg = "** mac file should be the same size (or larger), but isn't **"
48
- end
49
-
50
- assert_nothing_raised{ File.nl_convert(@test_file1, @mac_file, "mac") }
51
- assert_equal(true, File.size(@mac_file) == File.size(@test_file1), msg)
46
+ assert_nothing_raised{ File.nl_convert(@test_file1, @mac_file, 'mac') }
52
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)
53
52
  end
54
53
 
55
54
  def test_nl_convert_to_unix
@@ -82,4 +81,9 @@ class TC_FileNLConvert < Test::Unit::TestCase
82
81
  @test_file1 = nil
83
82
  @test_file2 = nil
84
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
85
89
  end
data/test/test_null.rb CHANGED
@@ -26,6 +26,11 @@ class TC_Null < Test::Unit::TestCase
26
26
  assert_raises(ArgumentError){ File.null(1) }
27
27
  end
28
28
 
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
33
+
29
34
  def teardown
30
35
  @nulls = nil
31
36
  end
data/test/test_tail.rb CHANGED
@@ -12,7 +12,8 @@ require 'ptools'
12
12
 
13
13
  class TC_FileTail < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
16
17
  end
17
18
 
18
19
  def setup
@@ -48,4 +49,8 @@ class TC_FileTail < Test::Unit::TestCase
48
49
  @expected_tail1 = nil
49
50
  @expected_tail2 = nil
50
51
  end
52
+
53
+ def self.shutdown
54
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
55
+ end
51
56
  end
data/test/test_touch.rb CHANGED
@@ -12,7 +12,8 @@ require 'ptools'
12
12
 
13
13
  class TC_FileTouch < Test::Unit::TestCase
14
14
  def self.startup
15
- Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
16
17
  end
17
18
 
18
19
  def setup
@@ -47,4 +48,8 @@ class TC_FileTouch < Test::Unit::TestCase
47
48
  File.delete(@test_file) if File.exists?(@test_file)
48
49
  @test_file = nil
49
50
  end
51
+
52
+ def self.shutdown
53
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
54
+ end
50
55
  end
data/test/test_wc.rb CHANGED
@@ -10,6 +10,7 @@ require 'ptools'
10
10
  class TC_FileWC < Test::Unit::TestCase
11
11
  def self.startup
12
12
  Dir.chdir('test') if File.exists?('test')
13
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
13
14
  end
14
15
 
15
16
  def setup
@@ -42,4 +43,8 @@ class TC_FileWC < Test::Unit::TestCase
42
43
  def teardown
43
44
  @test_file = nil
44
45
  end
46
+
47
+ def self.shutdown
48
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
49
+ end
45
50
  end
data/test/test_whereis.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  ######################################################################
2
2
  # test_whereis.rb
3
3
  #
4
- # Test case for the File.whereis method. This test should be run
5
- # via the 'rake test_whereis' task.
4
+ # Tests for the File.whereis method.
6
5
  ######################################################################
7
6
  require 'rubygems'
8
7
  gem 'test-unit'
@@ -13,40 +12,87 @@ require 'rbconfig'
13
12
  include Config
14
13
 
15
14
  class TC_FileWhereis < Test::Unit::TestCase
16
- def setup
17
- @expected_locs = [CONFIG['bindir']]
15
+ def self.startup
16
+ @@windows = Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
17
+ end
18
+
19
+ def setup
20
+ @expected_locs = [File.join(CONFIG['bindir'], 'ruby')]
21
+
22
+ if @@windows
23
+ @expected_locs[0] << '.exe'
24
+ @expected_locs[0].tr!("/", "\\")
25
+ end
18
26
 
19
- if Config::CONFIG['host_os'].match('mswin')
20
- @expected_locs = ["c:\\ruby\\bin\\ruby.exe"]
21
- else
22
- @expected_locs << '/usr/local/bin/ruby'
23
- @expected_locs << '/opt/sfw/bin/ruby'
24
- @expected_locs << '/opt/bin/ruby'
25
- @expected_locs << '/usr/bin/ruby'
26
- end
27
- end
28
-
29
- def test_whereis_basic
30
- assert_respond_to(File, :whereis)
31
- assert_nothing_raised{ File.whereis("ruby") }
32
- assert_nothing_raised{ File.whereis("ruby","/usr/bin:/usr/local/bin") }
33
- assert_nothing_raised{ File.whereis("ruby"){} }
34
- end
35
-
36
- def test_whereis_expected_return_values
37
- msg = "You may need to adjust the setup method if this test failed"
38
- locs = File.whereis('ruby').map{ |e| e.downcase }
39
- assert_kind_of(Array, locs)
40
- assert(@expected_locs.include?(locs.first), msg)
41
- assert_equal(nil, File.whereis("blahblah"))
42
- end
43
-
44
- def test_whereis_expected_errors
45
- assert_raises(ArgumentError){ File.whereis }
46
- assert_raises(ArgumentError){ File.whereis("ruby", "foo", "bar") }
47
- end
48
-
49
- def teardown
50
- @expected_locs = nil
51
- end
27
+ 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'
32
+ end
33
+
34
+ @actual_locs = nil
35
+ end
36
+
37
+ test "whereis basic functionality" do
38
+ assert_respond_to(File, :whereis)
39
+ assert_nothing_raised{ File.whereis('ruby') }
40
+ assert_kind_of([Array, NilClass], File.whereis('ruby'))
41
+ end
42
+
43
+ test "whereis accepts an optional second argument" do
44
+ assert_nothing_raised{ File.whereis('ruby', '/usr/bin:/usr/local/bin') }
45
+ end
46
+
47
+ test "whereis returns expected values" do
48
+ assert_nothing_raised{ @actual_locs = File.whereis('ruby') }
49
+ assert_kind_of(Array, @actual_locs)
50
+ assert_true((@expected_locs & @actual_locs).size > 0)
51
+ end
52
+
53
+ test "whereis returns nil if program not found" do
54
+ assert_nil(File.whereis('xxxyyy'))
55
+ end
56
+
57
+ test "whereis returns nil if program cannot be found in provided path" do
58
+ assert_nil(File.whereis('ruby', '/foo/bar'))
59
+ end
60
+
61
+ test "whereis returns single element array or nil if absolute path is provided" do
62
+ absolute = File.join(CONFIG['bindir'], 'ruby')
63
+ absolute << '.exe' if @@windows
64
+ assert_equal([absolute], File.whereis(absolute))
65
+ assert_nil(File.whereis('/foo/bar/baz/ruby'))
66
+ end
67
+
68
+ test "whereis works with an explicit extension on ms windows" do
69
+ omit_unless(@@windows, 'test skipped except on MS Windows')
70
+ assert_not_nil(File.whereis('ruby.exe'))
71
+ end
72
+
73
+ test "whereis requires at least one argument" do
74
+ assert_raise(ArgumentError){ File.whereis }
75
+ end
76
+
77
+ test "whereis returns unique paths only" do
78
+ assert_true(File.whereis('ruby') == File.whereis('ruby').uniq)
79
+ end
80
+
81
+ test "whereis accepts a maximum of two arguments" do
82
+ assert_raise(ArgumentError){ File.whereis('ruby', 'foo', 'bar') }
83
+ end
84
+
85
+ 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', '') }
88
+ end
89
+
90
+ def teardown
91
+ @expected_locs = nil
92
+ @actual_locs = nil
93
+ end
94
+
95
+ def self.shutdown
96
+ @@windows = nil
97
+ end
52
98
  end
data/test/test_which.rb CHANGED
@@ -12,40 +12,98 @@ gem 'test-unit'
12
12
 
13
13
  require 'test/unit'
14
14
  require 'rbconfig'
15
+ require 'fileutils'
15
16
  require 'ptools'
16
17
  include Config
17
18
 
18
19
  class TC_FileWhich < Test::Unit::TestCase
19
- def setup
20
- @ruby = RUBY_PLATFORM.match('java') ? 'jruby' : 'ruby'
21
- @exe = File.join(CONFIG["bindir"], CONFIG["ruby_install_name"])
22
-
23
- if Config::CONFIG['host_os'].match('mswin')
24
- @exe.tr!('/','\\')
25
- @exe << ".exe"
26
- end
27
- end
28
-
29
- def test_which_basic
30
- assert_respond_to(File, :which)
31
- assert_nothing_raised{ File.which(@ruby) }
32
- assert_nothing_raised{ File.which(@ruby, "/usr/bin:/usr/local/bin") }
33
- end
34
-
35
- def test_which_expected_return_values
36
- assert_kind_of(String, File.which(@ruby))
37
- assert_equal(@exe, File.which(@ruby))
38
- assert_equal(nil, File.which(@ruby, "/bogus/path"))
39
- assert_equal(nil, File.which("blahblah"))
40
- end
41
-
42
- def test_which_expected_errors
43
- assert_raises(ArgumentError){ File.which }
44
- assert_raises(ArgumentError){ File.which(@ruby, "foo", "bar") }
45
- end
46
-
47
- def teardown
48
- @exe = nil
49
- @ruby = nil
50
- end
20
+ def self.startup
21
+ @@windows = Config::CONFIG['host_os'] =~ /mswin|msdos|win32|cygwin|mingw/i
22
+ @@dir = File.join(Dir.pwd, 'tempdir')
23
+ @@non_exe = File.join(Dir.pwd, 'tempfile')
24
+
25
+ Dir.mkdir(@@dir) unless File.exists?(@@dir)
26
+ FileUtils.touch(@@non_exe)
27
+ File.chmod(775, @@dir)
28
+ File.chmod(644, @@non_exe)
29
+ end
30
+
31
+ def setup
32
+ @ruby = RUBY_PLATFORM.match('java') ? 'jruby' : 'ruby'
33
+ @exe = File.join(CONFIG['bindir'], CONFIG['ruby_install_name'])
34
+
35
+ if @@windows
36
+ @exe.tr!('/','\\')
37
+ @exe << ".exe"
38
+ end
39
+ end
40
+
41
+ test "which basic functionality" do
42
+ assert_respond_to(File, :which)
43
+ assert_nothing_raised{ File.which(@ruby) }
44
+ assert_kind_of(String, File.which(@ruby))
45
+ end
46
+
47
+ test "which accepts an optional path to search" do
48
+ assert_nothing_raised{ File.which(@ruby, "/usr/bin:/usr/local/bin") }
49
+ end
50
+
51
+ test "which returns nil if not found" do
52
+ assert_equal(nil, File.which(@ruby, '/bogus/path'))
53
+ assert_equal(nil, File.which('blahblahblah'))
54
+ end
55
+
56
+ test "which handles executables without extensions on windows" do
57
+ omit_unless(@@windows, "test skipped unless MS Windows")
58
+ assert_not_nil(File.which('ruby'))
59
+ assert_not_nil(File.which('notepad'))
60
+ end
61
+
62
+ test "which handles executables that already contain extensions on windows" do
63
+ omit_unless(@@windows, "test skipped unless MS Windows")
64
+ assert_not_nil(File.which('ruby.exe'))
65
+ assert_not_nil(File.which('notepad.exe'))
66
+ end
67
+
68
+ test "which returns argument if an existent absolute path is provided" do
69
+ assert_equal(@exe, File.which(@ruby))
70
+ end
71
+
72
+ test "which returns nil if a non-existent absolute path is provided" do
73
+ assert_nil(File.which('/foo/bar/baz/ruby'))
74
+ end
75
+
76
+ test "which does not pickup files that are not executable" do
77
+ assert_nil(File.which(@@non_exe))
78
+ end
79
+
80
+ test "which does not pickup executable directories" do
81
+ assert_nil(File.which(@@dir))
82
+ end
83
+
84
+ test "which accepts a minimum of one argument" do
85
+ assert_raises(ArgumentError){ File.which }
86
+ end
87
+
88
+ test "which accepts a maximum of two arguments" do
89
+ assert_raises(ArgumentError){ File.which(@ruby, "foo", "bar") }
90
+ end
91
+
92
+ test "the second argument cannot be nil or empty" do
93
+ assert_raises(ArgumentError){ File.which(@ruby, nil) }
94
+ assert_raises(ArgumentError){ File.which(@ruby, '') }
95
+ end
96
+
97
+ def teardown
98
+ @exe = nil
99
+ @ruby = nil
100
+ end
101
+
102
+ def self.shutdown
103
+ FileUtils.rm(@@non_exe)
104
+ FileUtils.rm_rf(@@dir)
105
+ @@windows = nil
106
+ @@dir = nil
107
+ @@non_exe = nil
108
+ end
51
109
  end