test/test_file_find.rb in file-find-0.3.7 vs test/test_file_find.rb in file-find-0.3.8

- old
+ new

@@ -5,15 +5,15 @@ # 'rake test' task. ###################################################################### require 'test-unit' require 'fileutils' require 'file/find' +require 'sys/admin' -begin - require 'sys/admin' -rescue LoadError - require 'etc' +if File::ALT_SEPARATOR + require 'win32/file' + require 'win32/security' end include FileUtils class TC_File_Find < Test::Unit::TestCase @@ -21,18 +21,20 @@ Dir.chdir(File.dirname(File.expand_path(__FILE__))) @@windows = File::ALT_SEPARATOR @@jruby = RUBY_PLATFORM.match('java') - unless @@windows - if @@jruby || !defined? Sys::Admin - @@loguser = Etc.getpwnam(Etc.getlogin) - @@logroup = Etc.getgrgid(@@loguser.gid) + if @@windows + @@elevated = Win32::Security.elevated_security? + if @@elevated + @@loguser = Sys::Admin.get_group("Administrators", :LocalAccount => true) else - @@loguser = Sys::Admin.get_user(Sys::Admin.get_login) - @@logroup = Sys::Admin.get_group(@@loguser.gid) + @@loguser = Sys::Admin.get_user(Sys::Admin.get_login, :LocalAccount => true) end + else + @@loguser = Sys::Admin.get_user(Sys::Admin.get_login) + @@logroup = Sys::Admin.get_group(@@loguser.gid) end end def setup @file_rb = 'test1.rb' @@ -45,179 +47,214 @@ File.open(@file_rb, 'w'){} File.open(@file_txt1, 'w'){} File.open(@file_txt2, 'w'){} File.open(@file_doc, 'w'){} - unless @@windows - @link1 = 'link1' + @link1 = 'link1' + + if @@windows + File.symlink(@file_rb, @link1) if @@elevated + else File.symlink(@file_rb, @link1) end - Dir.mkdir(@directory1) unless File.exists?(@directory1) - Dir.mkdir(@directory2) unless File.exists?(@directory2) + Dir.mkdir(@directory1) unless File.exist?(@directory1) + Dir.mkdir(@directory2) unless File.exist?(@directory2) File.open(File.join(@directory1, 'bar.txt'), 'w'){} File.open(File.join(@directory2, 'baz.txt'), 'w'){} @rule1 = File::Find.new(:name => '*.txt') @rule2 = File::Find.new end - def test_version - assert_equal('0.3.7', File::Find::VERSION) + test "version constant is set to expected value" do + assert_equal('0.3.8', File::Find::VERSION) end - def test_path + test "path accessor basic functionality" do assert_respond_to(@rule1, :path) assert_respond_to(@rule1, :path=) + end + + test "path method returns expected value" do assert_equal(Dir.pwd, @rule1.path) end - def test_options + test "options accessor basic functionality" do assert_respond_to(@rule1, :options) assert_respond_to(@rule1, :options=) + end + + test "options method returns expected value" do assert_equal({:name => '*.txt'}, @rule1.options) end - def test_atime_basic + test "atime accessor basic functionality" do assert_respond_to(@rule1, :atime) assert_respond_to(@rule1, :atime=) + end + + test "atime method returns expected default value" do assert_nil(@rule1.atime) end - def test_atime + test "find with atime option works as expected" do rule1 = File::Find.new(:name => "*.rb", :atime => 0) rule2 = File::Find.new(:name => "*.rb", :atime => 1) assert_false(rule1.find.empty?) assert_true(rule2.find.empty?) end - def test_ctime_basic + test "ctime accessor basic functionality" do assert_respond_to(@rule1, :ctime) assert_respond_to(@rule1, :ctime=) + end + + test "ctime method returns expected default value" do assert_nil(@rule1.ctime) end - def test_ctime + test "find with ctime option works as expected" do rule1 = File::Find.new(:name => "*.rb", :ctime => 0) rule2 = File::Find.new(:name => "*.rb", :ctime => 1) assert_false(rule1.find.empty?) assert_true(rule2.find.empty?) end - def test_find_basic + test "find method basic functionality" do assert_respond_to(@rule1, :find) assert_nothing_raised{ @rule1.find } end - def test_find + test "find method returns expected value" do assert_kind_of(Array, @rule1.find) assert_nil(@rule1.find{}) end - def test_filetest_basic + test "filetest accessor basic functionality" do assert_respond_to(@rule1, :filetest) assert_respond_to(@rule1, :filetest=) assert_nothing_raised{ @rule1.filetest } + end + + test "filetest method returns expected value" do assert_kind_of(Array, @rule1.filetest) end - def test_filetest_valid_options + test "valid filetest options work as expected" do assert_nothing_raised{ File::Find.new(:readable? => true) } assert_nothing_raised{ File::Find.new(:writable? => true) } end - def test_filetest - omit_if(@@windows && @@jruby, "Skipping file test on JRuby/Windows") - + test "find method works with filetest option" do rule = File::Find.new(:name => "*.doc", :writable? => true) File.chmod(0644, @file_doc) assert_equal([@file_doc], rule.find.map{ |f| File.basename(f) }) File.chmod(0444, @file_doc) assert_equal([], rule.find) end - def test_mtime + test "mtime accessor basic functionality" do + assert_respond_to(@rule1, :mtime) + assert_respond_to(@rule1, :mtime=) + end + + test "mtime method returns expected default value" do + assert_nil(@rule1.mtime) + end + + test "find with mtime option works as expected" do rule1 = File::Find.new(:name => "*.rb", :mtime => 0) rule2 = File::Find.new(:name => "*.rb", :mtime => 1) assert_false(rule1.find.empty?) assert_true(rule2.find.empty?) end - def test_mtime_basic - assert_respond_to(@rule1, :mtime) - assert_respond_to(@rule1, :mtime=) - assert_nil(@rule1.mtime) - end - - def test_ftype_basic + test "ftype accessor basic functionality" do assert_respond_to(@rule1, :ftype) assert_respond_to(@rule1, :ftype=) + end + + test "ftype method returns expected default value" do assert_nil(@rule1.ftype) end - def test_ftype + test "ftype method works as expected" do rule1 = File::Find.new(:name => "*.rb", :ftype => "file") rule2 = File::Find.new(:name => "*.rb", :ftype => "characterSpecial") assert_false(rule1.find.empty?) assert_true(rule2.find.empty?) end - def test_group_basic + test "group accessor basic functionality" do assert_respond_to(@rule1, :group) assert_respond_to(@rule1, :group=) + end + + test "group method returns expected default value" do assert_nil(@rule1.group) end - def test_group_with_numeric_id + # TODO: Update test for Windows + test "find with numeric group id works as expected" do omit_if(@@windows, 'group test skipped on MS Windows') @rule1 = File::Find.new(:name => '*.doc', :group => @@loguser.gid) assert_equal([File.expand_path(@file_doc)], @rule1.find) end - def test_group_with_string + # TODO: Update test for Windows + test "find with string group id works as expected" do omit_if(@@windows, 'group test skipped on MS Windows') @rule1 = File::Find.new(:name => '*.doc', :group => @@logroup.name) assert_equal([File.expand_path(@file_doc)], @rule1.find) end - def test_group_with_bad_id + test "find with bogus group returns empty results" do omit_if(@@windows, 'group test skipped on MS Windows') @rule1 = File::Find.new(:name => '*.doc', :group => 'totallybogus') @rule2 = File::Find.new(:name => '*.doc', :group => 99999999) assert_equal([], @rule1.find) assert_equal([], @rule2.find) end - def test_inum_basic + test "inum accessor basic functionality" do assert_respond_to(@rule1, :inum) assert_respond_to(@rule1, :inum=) + end + + test "inum method returns expected default value" do assert_nil(@rule1.inum) end - def test_follow_basic + test "follow accessor basic functionality" do assert_respond_to(@rule1, :follow) assert_respond_to(@rule1, :follow=) + end + + test "follow method returns expected default value" do assert_true(@rule1.follow) end - def test_links_basic + test "links accessor basic functionality" do assert_respond_to(@rule1, :links) assert_respond_to(@rule1, :links=) + end + + test "links method returns expected default value" do assert_nil(@rule1.links) end - def test_links - omit_if(@@windows, 'links test skipped on MS Windows') - + test "links method returns expected result" do + omit_if(@@windows && !@@elevated) @rule1 = File::Find.new(:name => '*.rb', :links => 2) @rule2 = File::Find.new(:name => '*.doc', :links => 1) assert_equal([], @rule1.find) assert_equal([File.expand_path(@file_doc)], @rule2.find) @@ -232,11 +269,11 @@ # This test is a little uglier because we actually check to make sure # we're looking at the right subdir, not just a filename shows up. # I did this because I'm a little paranoid about the directory # not getting mangled. - jlawler. # - def test_dirs_with_brackets + test "find method works on dirs that contain brackets" do omit_if(@@windows, 'dirs with brackets test skipped on MS Windows') bracket_files = [ 'bracket/a[1]/a.foo', 'bracket/a [2] /b.foo', 'bracket/[a] b [c]/d.foo' ].sort bracket_paths = [ 'bracket/a[1]', 'bracket/a [2] ', 'bracket/[a] b [c]', 'bracket/[z] x' ].sort @@ -262,11 +299,11 @@ assert_not_equal(path, file_results.first) assert_equal(bracket_files, file_results.map{ |e| e.sub(path,'') }.sort ) assert_equal(bracket_paths, @dir_rule.find.sort ) end - def test_maxdepth_file + test "find with maxdepth option returns expected results" do mkpath('a1/a2/a3') touch('a1/a.foo') touch('a1/a2/b.foo') touch('a1/a2/c.foo') touch('a1/a2/a3/d.foo') @@ -288,11 +325,11 @@ ['a.foo', 'b.foo', 'c.foo', 'd.foo', 'e.foo', 'f.foo'], @rule2.find.map{ |e| File.basename(e) }.sort ) end - def test_maxdepth_directory + test "find with maxdepth option returns expected results for directories" do mkpath('a/b/c') @rule2.pattern = "c" @rule2.maxdepth = 1 assert_equal([], @rule2.find) @@ -302,17 +339,20 @@ @rule2.maxdepth = 3 assert_equal(['c'], @rule2.find.map{ |e| File.basename(e) }) end - def test_mindepth_basic + test "mindepth accessor basic functionality" do assert_respond_to(@rule1, :mindepth) assert_respond_to(@rule1, :mindepth=) + end + + test "mindepth method returns expected default value" do assert_nil(@rule1.mindepth) end - def test_mindepth_file + test "find with mindepth option returns expected results" do mkpath('a1/a2/a3') touch('z.min') touch('a1/a.min') touch('a1/a2/b.min') touch('a1/a2/c.min') @@ -351,11 +391,11 @@ @rule2.mindepth = 5 assert_equal([], @rule2.find.map{ |e| File.basename(e) }) end - def test_mindepth_directory + test "find with mindepth option returns expected results for directories" do mkpath('a/b/c') @rule2.pattern = "a" @rule2.mindepth = 1 assert_equal(['a'], @rule2.find.map{ |e| File.basename(e) }) @@ -365,46 +405,58 @@ @rule2.mindepth = 3 assert_equal([], @rule2.find) end - def test_mount_basic + test "mount accessor basic functionality" do assert_respond_to(@rule1, :mount) assert_respond_to(@rule1, :mount=) + end + + test "mount method returns expected default value" do assert_nil(@rule1.mount) end - def test_name_basic + test "name accessor basic functionality" do assert_respond_to(@rule1, :name) assert_respond_to(@rule1, :name=) + end + + test "name method returns expected default value" do assert_equal('*.txt', @rule1.name) end - def test_pattern_alias + test "pattern accessor basic functionality" do assert_respond_to(@rule1, :pattern) assert_respond_to(@rule1, :pattern=) - assert_true(@rule1.method(:name) == @rule1.method(:pattern)) - assert_true(@rule1.method(:name=) == @rule1.method(:pattern=)) end - def test_perm_basic + test "pattern is an alias for name" do + assert_alias_method(@rule1, :name, :pattern) + assert_alias_method(@rule1, :name=, :pattern=) + end + + test "perm accessor basic functionality" do assert_respond_to(@rule1, :perm) assert_respond_to(@rule1, :perm=) + end + + test "perm method returns expected default value" do assert_nil(@rule1.perm) end - def test_perm - omit_if(@@windows, 'perm test skipped on MS Windows') - File.chmod(0664, @file_rb) + test "perm method returns expected results" do + File.chmod(0444, @file_rb) File.chmod(0644, @file_txt1) - results = File::Find.new(:name => "test1*", :perm => 664).find + results = File::Find.new(:name => "test1*", :perm => 0644).find + assert_equal(1, results.length) - assert_equal('test1.rb', File.basename(results.first)) + assert_equal('test1.txt', File.basename(results.first)) end - def test_perm_with_symbolic_permissions + test "perm method works with symbolic permissions" do omit_if(@@windows, 'symbolic perm test skipped on MS Windows') File.chmod(0664, @file_rb) # test1.rb File.chmod(0644, @file_txt1) # test1.txt results1 = File::Find.new(:name => "test1*", :perm => "g=rw").find @@ -414,59 +466,75 @@ assert_equal(2, results2.length) assert_equal('test1.rb', File.basename(results1.first)) assert_equal(['test1.rb', 'test1.txt'], results2.map{ |e| File.basename(e) }.sort) end - def test_prune_basic + test "prune accessor basic functionality" do assert_respond_to(@rule1, :prune) assert_respond_to(@rule1, :prune=) + end + + test "prune method returns expected default value" do assert_nil(@rule1.prune) end - def test_prune + test "find method with prune option works as expected" do rule = File::Find.new(:name => "*.txt", :prune => 'foo') assert_equal('test1.txt', File.basename(rule.find.first)) end - def test_size_basic + test "size accessor basic functionality" do assert_respond_to(@rule1, :size) assert_respond_to(@rule1, :size=) + end + + test "size method returns expected default value" do assert_nil(@rule1.size) end - def test_user_basic + test "user accessor basic functionality" do assert_respond_to(@rule1, :user) assert_respond_to(@rule1, :user=) + end + + test "user method returns expected default value" do assert_nil(@rule1.user) end - def test_user_with_numeric_id - omit_if(@@windows, 'user test skipped on MS Windows') - @rule1 = File::Find.new(:name => '*.doc', :user => @@loguser.uid) + test "user method works with numeric id as expected" do + if @@windows && @@elevated + uid = @@loguser.gid # Windows assigns the group if any member is an admin + else + uid = @@loguser.uid + end + + @rule1 = File::Find.new(:name => '*.doc', :user => uid) assert_equal([File.expand_path(@file_doc)], @rule1.find) end - def test_user_with_string - omit_if(@@windows, 'user test skipped on MS Windows') + test "user method works with string as expected" do + omit_if(@@windows && @@elevated) @rule1 = File::Find.new(:name => '*.doc', :user => @@loguser.name) assert_equal([File.expand_path(@file_doc)], @rule1.find) end - def test_user_with_bad_id - omit_if(@@windows, 'user test skipped on MS Windows') + test "find method with user option using invalid user returns expected results" do @rule1 = File::Find.new(:name => '*.doc', :user => 'totallybogus') @rule2 = File::Find.new(:name => '*.doc', :user => 99999999) assert_equal([], @rule1.find) assert_equal([], @rule2.find) end - def test_previous_basic + test "previous method basic functionality" do assert_respond_to(@rule1, :previous) end - def test_expected_errors + test "an error is raised if the path does not exist" do assert_raise(Errno::ENOENT){ File::Find.new(:path => '/bogus/dir').find } + end + + test "an error is raised if an invalid option is passed" do assert_raise(ArgumentError){ File::Find.new(:bogus => 1) } assert_raise(ArgumentError){ File::Find.new(:bogus? => true) } end def teardown @@ -474,15 +542,15 @@ rm_rf(@file_txt1) rm_rf(@file_txt2) rm_rf(@file_doc) rm_rf(@directory1) rm_rf(@directory2) - rm_rf(@link1) unless @@windows + rm_rf(@link1) #unless @@windows rm_rf('a') rm_rf('a1') rm_rf('bracket') - rm_rf('z.min') if File.exists?('z.min') + rm_rf('z.min') if File.exist?('z.min') @rule1 = nil @rule2 = nil @file_rb = nil @file_txt1 = nil @@ -491,9 +559,9 @@ end def self.shutdown @@windows = nil @@jruby = nil - @@loguser = nil unless @@windows + @@elevated = nil if @@windows @@logroup = nil unless @@windows end end