summaryrefslogtreecommitdiff
path: root/spec/ruby/core/file
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/file')
-rw-r--r--spec/ruby/core/file/delete_spec.rb61
-rw-r--r--spec/ruby/core/file/fnmatch_spec.rb298
-rw-r--r--spec/ruby/core/file/path_spec.rb5
-rw-r--r--spec/ruby/core/file/shared/fnmatch.rb294
-rw-r--r--spec/ruby/core/file/shared/path.rb82
-rw-r--r--spec/ruby/core/file/shared/unlink.rb61
-rw-r--r--spec/ruby/core/file/sticky_spec.rb2
-rw-r--r--spec/ruby/core/file/to_path_spec.rb82
-rw-r--r--spec/ruby/core/file/unlink_spec.rb5
-rw-r--r--spec/ruby/core/file/zero_spec.rb6
10 files changed, 444 insertions, 452 deletions
diff --git a/spec/ruby/core/file/delete_spec.rb b/spec/ruby/core/file/delete_spec.rb
index 4098499942..7149b8a37d 100644
--- a/spec/ruby/core/file/delete_spec.rb
+++ b/spec/ruby/core/file/delete_spec.rb
@@ -1,6 +1,63 @@
require_relative '../../spec_helper'
-require_relative 'shared/unlink'
describe "File.delete" do
- it_behaves_like :file_unlink, :delete
+ before :each do
+ @file1 = tmp('test.txt')
+ @file2 = tmp('test2.txt')
+
+ touch @file1
+ touch @file2
+ end
+
+ after :each do
+ File.delete(@file1) if File.exist?(@file1)
+ File.delete(@file2) if File.exist?(@file2)
+
+ @file1 = nil
+ @file2 = nil
+ end
+
+ it "returns 0 when called without arguments" do
+ File.delete.should == 0
+ end
+
+ it "deletes a single file" do
+ File.delete(@file1).should == 1
+ File.should_not.exist?(@file1)
+ end
+
+ it "deletes multiple files" do
+ File.delete(@file1, @file2).should == 2
+ File.should_not.exist?(@file1)
+ File.should_not.exist?(@file2)
+ end
+
+ it "raises a TypeError if not passed a String type" do
+ -> { File.delete(1) }.should.raise(TypeError)
+ end
+
+ it "raises an Errno::ENOENT when the given file doesn't exist" do
+ -> { File.delete('bogus') }.should.raise(Errno::ENOENT)
+ end
+
+ it "coerces a given parameter into a string if possible" do
+ mock = mock("to_str")
+ mock.should_receive(:to_str).and_return(@file1)
+ File.delete(mock).should == 1
+ end
+
+ it "accepts an object that has a #to_path method" do
+ File.delete(mock_to_path(@file1)).should == 1
+ end
+
+ platform_is :windows do
+ it "allows deleting an open file with File::SHARE_DELETE" do
+ path = tmp("share_delete.txt")
+ File.open(path, mode: File::CREAT | File::WRONLY | File::BINARY | File::SHARE_DELETE) do |f|
+ File.should.exist?(path)
+ File.delete(path)
+ end
+ File.should_not.exist?(path)
+ end
+ end
end
diff --git a/spec/ruby/core/file/fnmatch_spec.rb b/spec/ruby/core/file/fnmatch_spec.rb
index a1b7fa12b3..44a143bddc 100644
--- a/spec/ruby/core/file/fnmatch_spec.rb
+++ b/spec/ruby/core/file/fnmatch_spec.rb
@@ -1,10 +1,302 @@
require_relative '../../spec_helper'
-require_relative 'shared/fnmatch'
describe "File.fnmatch" do
- it_behaves_like :file_fnmatch, :fnmatch
+ it "matches entire strings" do
+ File.fnmatch('cat', 'cat').should == true
+ end
+
+ it "does not match partial strings" do
+ File.fnmatch('cat', 'category').should == false
+ end
+
+ it "does not support { } patterns by default" do
+ File.fnmatch('c{at,ub}s', 'cats').should == false
+ File.fnmatch('c{at,ub}s', 'c{at,ub}s').should == true
+ end
+
+ it "supports some { } patterns when File::FNM_EXTGLOB is passed" do
+ File.fnmatch("{a,b}", "a", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{a,b}", "b", File::FNM_EXTGLOB).should == true
+ File.fnmatch("c{at,ub}s", "cats", File::FNM_EXTGLOB).should == true
+ File.fnmatch("c{at,ub}s", "cubs", File::FNM_EXTGLOB).should == true
+ File.fnmatch("-c{at,ub}s-", "-cats-", File::FNM_EXTGLOB).should == true
+ File.fnmatch("-c{at,ub}s-", "-cubs-", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{a,b,c}{d,e,f}{g,h}", "adg", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{a,b,c}{d,e,f}{g,h}", "bdg", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{a,b,c}{d,e,f}{g,h}", "ceh", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{aa,bb,cc,dd}", "aa", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{aa,bb,cc,dd}", "bb", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{aa,bb,cc,dd}", "cc", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{aa,bb,cc,dd}", "dd", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{1,5{a,b{c,d}}}", "1", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{1,5{a,b{c,d}}}", "5a", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{1,5{a,b{c,d}}}", "5bc", File::FNM_EXTGLOB).should == true
+ File.fnmatch("{1,5{a,b{c,d}}}", "5bd", File::FNM_EXTGLOB).should == true
+ File.fnmatch("\\\\{a\\,b,b\\}c}", "\\a,b", File::FNM_EXTGLOB).should == true
+ File.fnmatch("\\\\{a\\,b,b\\}c}", "\\b}c", File::FNM_EXTGLOB).should == true
+ end
+
+ it "doesn't support some { } patterns even when File::FNM_EXTGLOB is passed" do
+ File.fnmatch("a{0..3}b", "a0b", File::FNM_EXTGLOB).should == false
+ File.fnmatch("a{0..3}b", "a1b", File::FNM_EXTGLOB).should == false
+ File.fnmatch("a{0..3}b", "a2b", File::FNM_EXTGLOB).should == false
+ File.fnmatch("a{0..3}b", "a3b", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{0..12}", "0", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{0..12}", "6", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{0..12}", "12", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{3..-2}", "3", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{3..-2}", "0", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{3..-2}", "-2", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{a..g}", "a", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{a..g}", "d", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{a..g}", "g", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{g..a}", "a", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{g..a}", "d", File::FNM_EXTGLOB).should == false
+ File.fnmatch("{g..a}", "g", File::FNM_EXTGLOB).should == false
+ File.fnmatch("escaping: {{,\\,,\\},\\{}", "escaping: {", File::FNM_EXTGLOB).should == false
+ File.fnmatch("escaping: {{,\\,,\\},\\{}", "escaping: ,", File::FNM_EXTGLOB).should == false
+ File.fnmatch("escaping: {{,\\,,\\},\\{}", "escaping: }", File::FNM_EXTGLOB).should == false
+ File.fnmatch("escaping: {{,\\,,\\},\\{}", "escaping: {", File::FNM_EXTGLOB).should == false
+ end
+
+ it "doesn't match an extra } when File::FNM_EXTGLOB is passed" do
+ File.fnmatch('c{at,ub}}s', 'cats', File::FNM_EXTGLOB).should == false
+ end
+
+ it "matches when both FNM_EXTGLOB and FNM_PATHNAME are passed" do
+ File.fnmatch("?.md", "a.md", File::FNM_EXTGLOB | File::FNM_PATHNAME).should == true
+ end
+
+ it "matches a single character for each ? character" do
+ File.fnmatch('c?t', 'cat').should == true
+ File.fnmatch('c??t', 'cat').should == false
+ end
+
+ it "matches zero or more characters for each * character" do
+ File.fnmatch('c*', 'cats').should == true
+ File.fnmatch('c*t', 'c/a/b/t').should == true
+ end
+
+ it "does not match unterminated range of characters" do
+ File.fnmatch('abc[de', 'abcd').should == false
+ end
+
+ it "does not match unterminated range of characters as a literal" do
+ File.fnmatch('abc[de', 'abc[de').should == false
+ end
+
+ it "matches ranges of characters using bracket expression (e.g. [a-z])" do
+ File.fnmatch('ca[a-z]', 'cat').should == true
+ end
+
+ it "matches ranges of characters using bracket expression, taking case into account" do
+ File.fnmatch('[a-z]', 'D').should == false
+ File.fnmatch('[^a-z]', 'D').should == true
+ File.fnmatch('[A-Z]', 'd').should == false
+ File.fnmatch('[^A-Z]', 'd').should == true
+ File.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD).should == true
+ end
+
+ it "does not match characters outside of the range of the bracket expression" do
+ File.fnmatch('ca[x-z]', 'cat').should == false
+ File.fnmatch('/ca[s][s-t]/rul[a-b]/[z]he/[x-Z]orld', '/cats/rule/the/World').should == false
+ end
+
+ it "matches ranges of characters using exclusive bracket expression (e.g. [^t] or [!t])" do
+ File.fnmatch('ca[^t]', 'cat').should == false
+ File.fnmatch('ca[^t]', 'cas').should == true
+ File.fnmatch('ca[!t]', 'cat').should == false
+ end
+
+ it "matches characters with a case sensitive comparison" do
+ File.fnmatch('cat', 'CAT').should == false
+ end
+
+ it "matches characters with case insensitive comparison when flags includes FNM_CASEFOLD" do
+ File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD).should == true
+ end
+
+ platform_is_not :windows do
+ it "doesn't match case sensitive characters on platforms with case sensitive paths, when flags include FNM_SYSCASE" do
+ File.fnmatch('cat', 'CAT', File::FNM_SYSCASE).should == false
+ end
+ end
+
+ platform_is :windows do
+ it "matches case sensitive characters on platforms with case insensitive paths, when flags include FNM_SYSCASE" do
+ File.fnmatch('cat', 'CAT', File::FNM_SYSCASE).should == true
+ end
+ end
+
+ it "matches wildcard with characters when flags includes FNM_PATHNAME" do
+ File.fnmatch('*a', 'aa', File::FNM_PATHNAME).should == true
+ File.fnmatch('a*', 'aa', File::FNM_PATHNAME).should == true
+ File.fnmatch('a*', 'aaa', File::FNM_PATHNAME).should == true
+ File.fnmatch('*a', 'aaa', File::FNM_PATHNAME).should == true
+ end
+
+ it "does not match '/' characters with ? or * when flags includes FNM_PATHNAME" do
+ File.fnmatch('?', '/', File::FNM_PATHNAME).should == false
+ File.fnmatch('*', '/', File::FNM_PATHNAME).should == false
+ end
+
+ it "does not match '/' characters inside bracket expressions when flags includes FNM_PATHNAME" do
+ File.fnmatch('[/]', '/', File::FNM_PATHNAME).should == false
+ end
+
+ it "matches literal ? or * in path when pattern includes \\? or \\*" do
+ File.fnmatch('\?', '?').should == true
+ File.fnmatch('\?', 'a').should == false
+
+ File.fnmatch('\*', '*').should == true
+ File.fnmatch('\*', 'a').should == false
+ end
+
+ it "matches literal character (e.g. 'a') in path when pattern includes escaped character (e.g. \\a)" do
+ File.fnmatch('\a', 'a').should == true
+ File.fnmatch('this\b', 'thisb').should == true
+ end
+
+ it "matches '\\' characters in path when flags includes FNM_NOESCAPE" do
+ File.fnmatch('\a', '\a', File::FNM_NOESCAPE).should == true
+ File.fnmatch('\a', 'a', File::FNM_NOESCAPE).should == false
+ File.fnmatch('\[foo\]\[bar\]', '[foo][bar]', File::FNM_NOESCAPE).should == false
+ end
+
+ it "escapes special characters inside bracket expression" do
+ File.fnmatch('[\?]', '?').should == true
+ File.fnmatch('[\*]', '*').should == true
+ end
+
+ it "does not match leading periods in filenames with wildcards by default" do
+ File.should_not.fnmatch('*', '.profile')
+ File.should.fnmatch('*', 'home/.profile')
+ File.should.fnmatch('*/*', 'home/.profile')
+ File.should_not.fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME)
+ end
+
+ it "matches patterns with leading periods to dotfiles" do
+ File.fnmatch('.*', '.profile').should == true
+ File.fnmatch('.*', '.profile', File::FNM_PATHNAME).should == true
+ File.fnmatch(".*file", "nondotfile").should == false
+ File.fnmatch(".*file", "nondotfile", File::FNM_PATHNAME).should == false
+ end
+
+ it "does not match directories with leading periods by default with FNM_PATHNAME" do
+ File.fnmatch('.*', '.directory/nondotfile', File::FNM_PATHNAME).should == false
+ File.fnmatch('.*', '.directory/.profile', File::FNM_PATHNAME).should == false
+ File.fnmatch('.*', 'foo/.directory/nondotfile', File::FNM_PATHNAME).should == false
+ File.fnmatch('.*', 'foo/.directory/.profile', File::FNM_PATHNAME).should == false
+ File.fnmatch('**/.dotfile', '.dotsubdir/.dotfile', File::FNM_PATHNAME).should == false
+ end
+
+ it "matches leading periods in filenames when flags includes FNM_DOTMATCH" do
+ File.fnmatch('*', '.profile', File::FNM_DOTMATCH).should == true
+ File.fnmatch('*', 'home/.profile', File::FNM_DOTMATCH).should == true
+ end
+
+ it "matches multiple directories with ** and *" do
+ files = '**/*.rb'
+ File.fnmatch(files, 'main.rb').should == false
+ File.fnmatch(files, './main.rb').should == false
+ File.fnmatch(files, 'lib/song.rb').should == true
+ File.fnmatch('**.rb', 'main.rb').should == true
+ File.fnmatch('**.rb', './main.rb').should == false
+ File.fnmatch('**.rb', 'lib/song.rb').should == true
+ File.fnmatch('*', 'dave/.profile').should == true
+ end
+
+ it "matches multiple directories with ** when flags includes File::FNM_PATHNAME" do
+ files = '**/*.rb'
+ flags = File::FNM_PATHNAME
+
+ File.fnmatch(files, 'main.rb', flags).should == true
+ File.fnmatch(files, 'one/two/three/main.rb', flags).should == true
+ File.fnmatch(files, './main.rb', flags).should == false
+
+ flags = File::FNM_PATHNAME | File::FNM_DOTMATCH
+
+ File.fnmatch(files, './main.rb', flags).should == true
+ File.fnmatch(files, 'one/two/.main.rb', flags).should == true
+
+ File.fnmatch("**/best/*", 'lib/my/best/song.rb').should == true
+ end
+
+ it "returns false if '/' in pattern do not match '/' in path when flags includes FNM_PATHNAME" do
+ pattern = '*/*'
+ File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME).should == false
+
+ pattern = '**/foo'
+ File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME).should == false
+ end
+
+ it "returns true if '/' in pattern match '/' in path when flags includes FNM_PATHNAME" do
+ pattern = '*/*'
+ File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+
+ pattern = '**/foo'
+ File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME).should == true
+ File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME).should == true
+ File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME).should == true
+ File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ end
+
+ it "has special handling for ./ when using * and FNM_PATHNAME" do
+ File.fnmatch('./*', '.', File::FNM_PATHNAME).should == false
+ File.fnmatch('./*', './', File::FNM_PATHNAME).should == true
+ File.fnmatch('./*/', './', File::FNM_PATHNAME).should == false
+ File.fnmatch('./**', './', File::FNM_PATHNAME).should == true
+ File.fnmatch('./**/', './', File::FNM_PATHNAME).should == true
+ File.fnmatch('./*', '.', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == false
+ File.fnmatch('./*', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ File.fnmatch('./*/', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == false
+ File.fnmatch('./**', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ File.fnmatch('./**/', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ end
+
+ it "matches **/* with FNM_PATHNAME to recurse directories" do
+ File.fnmatch('nested/**/*', 'nested/subdir', File::FNM_PATHNAME).should == true
+ File.fnmatch('nested/**/*', 'nested/subdir/file', File::FNM_PATHNAME).should == true
+ File.fnmatch('nested/**/*', 'nested/.dotsubdir', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ File.fnmatch('nested/**/*', 'nested/.dotsubir/.dotfile', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ end
+
+ it "matches ** with FNM_PATHNAME only in current directory" do
+ File.fnmatch('nested/**', 'nested/subdir', File::FNM_PATHNAME).should == true
+ File.fnmatch('nested/**', 'nested/subdir/file', File::FNM_PATHNAME).should == false
+ File.fnmatch('nested/**', 'nested/.dotsubdir', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
+ File.fnmatch('nested/**', 'nested/.dotsubir/.dotfile', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == false
+ end
+
+ it "accepts an object that has a #to_path method" do
+ File.fnmatch('\*', mock_to_path('a')).should == false
+ end
+
+ it "raises a TypeError if the first and second arguments are not string-like" do
+ -> { File.fnmatch(nil, nil, 0, 0) }.should.raise(ArgumentError)
+ -> { File.fnmatch(1, 'some/thing') }.should.raise(TypeError)
+ -> { File.fnmatch('some/thing', 1) }.should.raise(TypeError)
+ -> { File.fnmatch(1, 1) }.should.raise(TypeError)
+ end
+
+ it "raises a TypeError if the third argument is not an Integer" do
+ -> { File.fnmatch("*/place", "path/to/file", "flags") }.should.raise(TypeError)
+ -> { File.fnmatch("*/place", "path/to/file", nil) }.should.raise(TypeError)
+ end
+
+ it "does not raise a TypeError if the third argument can be coerced to an Integer" do
+ flags = mock("flags")
+ flags.should_receive(:to_int).and_return(10)
+ -> { File.fnmatch("*/place", "path/to/file", flags) }.should_not.raise
+ end
+
+ it "matches multibyte characters" do
+ File.fnmatch("*/ä/ø/ñ", "a/ä/ø/ñ").should == true
+ end
end
describe "File.fnmatch?" do
- it_behaves_like :file_fnmatch, :fnmatch?
+ it "is an alias of File.fnmatch" do
+ File.method(:fnmatch?).should == File.method(:fnmatch)
+ end
end
diff --git a/spec/ruby/core/file/path_spec.rb b/spec/ruby/core/file/path_spec.rb
index ce78dbeede..f3b9b56dbe 100644
--- a/spec/ruby/core/file/path_spec.rb
+++ b/spec/ruby/core/file/path_spec.rb
@@ -1,8 +1,9 @@
require_relative '../../spec_helper'
-require_relative 'shared/path'
describe "File#path" do
- it_behaves_like :file_path, :path
+ it "is an alias of File#to_path" do
+ File.instance_method(:path).should == File.instance_method(:to_path)
+ end
end
describe "File.path" do
diff --git a/spec/ruby/core/file/shared/fnmatch.rb b/spec/ruby/core/file/shared/fnmatch.rb
deleted file mode 100644
index b9140d027d..0000000000
--- a/spec/ruby/core/file/shared/fnmatch.rb
+++ /dev/null
@@ -1,294 +0,0 @@
-describe :file_fnmatch, shared: true do
- it "matches entire strings" do
- File.send(@method, 'cat', 'cat').should == true
- end
-
- it "does not match partial strings" do
- File.send(@method, 'cat', 'category').should == false
- end
-
- it "does not support { } patterns by default" do
- File.send(@method, 'c{at,ub}s', 'cats').should == false
- File.send(@method, 'c{at,ub}s', 'c{at,ub}s').should == true
- end
-
- it "supports some { } patterns when File::FNM_EXTGLOB is passed" do
- File.send(@method, "{a,b}", "a", File::FNM_EXTGLOB).should == true
- File.send(@method, "{a,b}", "b", File::FNM_EXTGLOB).should == true
- File.send(@method, "c{at,ub}s", "cats", File::FNM_EXTGLOB).should == true
- File.send(@method, "c{at,ub}s", "cubs", File::FNM_EXTGLOB).should == true
- File.send(@method, "-c{at,ub}s-", "-cats-", File::FNM_EXTGLOB).should == true
- File.send(@method, "-c{at,ub}s-", "-cubs-", File::FNM_EXTGLOB).should == true
- File.send(@method, "{a,b,c}{d,e,f}{g,h}", "adg", File::FNM_EXTGLOB).should == true
- File.send(@method, "{a,b,c}{d,e,f}{g,h}", "bdg", File::FNM_EXTGLOB).should == true
- File.send(@method, "{a,b,c}{d,e,f}{g,h}", "ceh", File::FNM_EXTGLOB).should == true
- File.send(@method, "{aa,bb,cc,dd}", "aa", File::FNM_EXTGLOB).should == true
- File.send(@method, "{aa,bb,cc,dd}", "bb", File::FNM_EXTGLOB).should == true
- File.send(@method, "{aa,bb,cc,dd}", "cc", File::FNM_EXTGLOB).should == true
- File.send(@method, "{aa,bb,cc,dd}", "dd", File::FNM_EXTGLOB).should == true
- File.send(@method, "{1,5{a,b{c,d}}}", "1", File::FNM_EXTGLOB).should == true
- File.send(@method, "{1,5{a,b{c,d}}}", "5a", File::FNM_EXTGLOB).should == true
- File.send(@method, "{1,5{a,b{c,d}}}", "5bc", File::FNM_EXTGLOB).should == true
- File.send(@method, "{1,5{a,b{c,d}}}", "5bd", File::FNM_EXTGLOB).should == true
- File.send(@method, "\\\\{a\\,b,b\\}c}", "\\a,b", File::FNM_EXTGLOB).should == true
- File.send(@method, "\\\\{a\\,b,b\\}c}", "\\b}c", File::FNM_EXTGLOB).should == true
- end
-
- it "doesn't support some { } patterns even when File::FNM_EXTGLOB is passed" do
- File.send(@method, "a{0..3}b", "a0b", File::FNM_EXTGLOB).should == false
- File.send(@method, "a{0..3}b", "a1b", File::FNM_EXTGLOB).should == false
- File.send(@method, "a{0..3}b", "a2b", File::FNM_EXTGLOB).should == false
- File.send(@method, "a{0..3}b", "a3b", File::FNM_EXTGLOB).should == false
- File.send(@method, "{0..12}", "0", File::FNM_EXTGLOB).should == false
- File.send(@method, "{0..12}", "6", File::FNM_EXTGLOB).should == false
- File.send(@method, "{0..12}", "12", File::FNM_EXTGLOB).should == false
- File.send(@method, "{3..-2}", "3", File::FNM_EXTGLOB).should == false
- File.send(@method, "{3..-2}", "0", File::FNM_EXTGLOB).should == false
- File.send(@method, "{3..-2}", "-2", File::FNM_EXTGLOB).should == false
- File.send(@method, "{a..g}", "a", File::FNM_EXTGLOB).should == false
- File.send(@method, "{a..g}", "d", File::FNM_EXTGLOB).should == false
- File.send(@method, "{a..g}", "g", File::FNM_EXTGLOB).should == false
- File.send(@method, "{g..a}", "a", File::FNM_EXTGLOB).should == false
- File.send(@method, "{g..a}", "d", File::FNM_EXTGLOB).should == false
- File.send(@method, "{g..a}", "g", File::FNM_EXTGLOB).should == false
- File.send(@method, "escaping: {{,\\,,\\},\\{}", "escaping: {", File::FNM_EXTGLOB).should == false
- File.send(@method, "escaping: {{,\\,,\\},\\{}", "escaping: ,", File::FNM_EXTGLOB).should == false
- File.send(@method, "escaping: {{,\\,,\\},\\{}", "escaping: }", File::FNM_EXTGLOB).should == false
- File.send(@method, "escaping: {{,\\,,\\},\\{}", "escaping: {", File::FNM_EXTGLOB).should == false
- end
-
- it "doesn't match an extra } when File::FNM_EXTGLOB is passed" do
- File.send(@method, 'c{at,ub}}s', 'cats', File::FNM_EXTGLOB).should == false
- end
-
- it "matches when both FNM_EXTGLOB and FNM_PATHNAME are passed" do
- File.send(@method, "?.md", "a.md", File::FNM_EXTGLOB | File::FNM_PATHNAME).should == true
- end
-
- it "matches a single character for each ? character" do
- File.send(@method, 'c?t', 'cat').should == true
- File.send(@method, 'c??t', 'cat').should == false
- end
-
- it "matches zero or more characters for each * character" do
- File.send(@method, 'c*', 'cats').should == true
- File.send(@method, 'c*t', 'c/a/b/t').should == true
- end
-
- it "does not match unterminated range of characters" do
- File.send(@method, 'abc[de', 'abcd').should == false
- end
-
- it "does not match unterminated range of characters as a literal" do
- File.send(@method, 'abc[de', 'abc[de').should == false
- end
-
- it "matches ranges of characters using bracket expression (e.g. [a-z])" do
- File.send(@method, 'ca[a-z]', 'cat').should == true
- end
-
- it "matches ranges of characters using bracket expression, taking case into account" do
- File.send(@method, '[a-z]', 'D').should == false
- File.send(@method, '[^a-z]', 'D').should == true
- File.send(@method, '[A-Z]', 'd').should == false
- File.send(@method, '[^A-Z]', 'd').should == true
- File.send(@method, '[a-z]', 'D', File::FNM_CASEFOLD).should == true
- end
-
- it "does not match characters outside of the range of the bracket expression" do
- File.send(@method, 'ca[x-z]', 'cat').should == false
- File.send(@method, '/ca[s][s-t]/rul[a-b]/[z]he/[x-Z]orld', '/cats/rule/the/World').should == false
- end
-
- it "matches ranges of characters using exclusive bracket expression (e.g. [^t] or [!t])" do
- File.send(@method, 'ca[^t]', 'cat').should == false
- File.send(@method, 'ca[^t]', 'cas').should == true
- File.send(@method, 'ca[!t]', 'cat').should == false
- end
-
- it "matches characters with a case sensitive comparison" do
- File.send(@method, 'cat', 'CAT').should == false
- end
-
- it "matches characters with case insensitive comparison when flags includes FNM_CASEFOLD" do
- File.send(@method, 'cat', 'CAT', File::FNM_CASEFOLD).should == true
- end
-
- platform_is_not :windows do
- it "doesn't match case sensitive characters on platforms with case sensitive paths, when flags include FNM_SYSCASE" do
- File.send(@method, 'cat', 'CAT', File::FNM_SYSCASE).should == false
- end
- end
-
- platform_is :windows do
- it "matches case sensitive characters on platforms with case insensitive paths, when flags include FNM_SYSCASE" do
- File.send(@method, 'cat', 'CAT', File::FNM_SYSCASE).should == true
- end
- end
-
- it "matches wildcard with characters when flags includes FNM_PATHNAME" do
- File.send(@method, '*a', 'aa', File::FNM_PATHNAME).should == true
- File.send(@method, 'a*', 'aa', File::FNM_PATHNAME).should == true
- File.send(@method, 'a*', 'aaa', File::FNM_PATHNAME).should == true
- File.send(@method, '*a', 'aaa', File::FNM_PATHNAME).should == true
- end
-
- it "does not match '/' characters with ? or * when flags includes FNM_PATHNAME" do
- File.send(@method, '?', '/', File::FNM_PATHNAME).should == false
- File.send(@method, '*', '/', File::FNM_PATHNAME).should == false
- end
-
- it "does not match '/' characters inside bracket expressions when flags includes FNM_PATHNAME" do
- File.send(@method, '[/]', '/', File::FNM_PATHNAME).should == false
- end
-
- it "matches literal ? or * in path when pattern includes \\? or \\*" do
- File.send(@method, '\?', '?').should == true
- File.send(@method, '\?', 'a').should == false
-
- File.send(@method, '\*', '*').should == true
- File.send(@method, '\*', 'a').should == false
- end
-
- it "matches literal character (e.g. 'a') in path when pattern includes escaped character (e.g. \\a)" do
- File.send(@method, '\a', 'a').should == true
- File.send(@method, 'this\b', 'thisb').should == true
- end
-
- it "matches '\\' characters in path when flags includes FNM_NOESACPE" do
- File.send(@method, '\a', '\a', File::FNM_NOESCAPE).should == true
- File.send(@method, '\a', 'a', File::FNM_NOESCAPE).should == false
- File.send(@method, '\[foo\]\[bar\]', '[foo][bar]', File::FNM_NOESCAPE).should == false
- end
-
- it "escapes special characters inside bracket expression" do
- File.send(@method, '[\?]', '?').should == true
- File.send(@method, '[\*]', '*').should == true
- end
-
- it "does not match leading periods in filenames with wildcards by default" do
- File.should_not.send(@method, '*', '.profile')
- File.should.send(@method, '*', 'home/.profile')
- File.should.send(@method, '*/*', 'home/.profile')
- File.should_not.send(@method, '*/*', 'dave/.profile', File::FNM_PATHNAME)
- end
-
- it "matches patterns with leading periods to dotfiles" do
- File.send(@method, '.*', '.profile').should == true
- File.send(@method, '.*', '.profile', File::FNM_PATHNAME).should == true
- File.send(@method, ".*file", "nondotfile").should == false
- File.send(@method, ".*file", "nondotfile", File::FNM_PATHNAME).should == false
- end
-
- it "does not match directories with leading periods by default with FNM_PATHNAME" do
- File.send(@method, '.*', '.directory/nondotfile', File::FNM_PATHNAME).should == false
- File.send(@method, '.*', '.directory/.profile', File::FNM_PATHNAME).should == false
- File.send(@method, '.*', 'foo/.directory/nondotfile', File::FNM_PATHNAME).should == false
- File.send(@method, '.*', 'foo/.directory/.profile', File::FNM_PATHNAME).should == false
- File.send(@method, '**/.dotfile', '.dotsubdir/.dotfile', File::FNM_PATHNAME).should == false
- end
-
- it "matches leading periods in filenames when flags includes FNM_DOTMATCH" do
- File.send(@method, '*', '.profile', File::FNM_DOTMATCH).should == true
- File.send(@method, '*', 'home/.profile', File::FNM_DOTMATCH).should == true
- end
-
- it "matches multiple directories with ** and *" do
- files = '**/*.rb'
- File.send(@method, files, 'main.rb').should == false
- File.send(@method, files, './main.rb').should == false
- File.send(@method, files, 'lib/song.rb').should == true
- File.send(@method, '**.rb', 'main.rb').should == true
- File.send(@method, '**.rb', './main.rb').should == false
- File.send(@method, '**.rb', 'lib/song.rb').should == true
- File.send(@method, '*', 'dave/.profile').should == true
- end
-
- it "matches multiple directories with ** when flags includes File::FNM_PATHNAME" do
- files = '**/*.rb'
- flags = File::FNM_PATHNAME
-
- File.send(@method, files, 'main.rb', flags).should == true
- File.send(@method, files, 'one/two/three/main.rb', flags).should == true
- File.send(@method, files, './main.rb', flags).should == false
-
- flags = File::FNM_PATHNAME | File::FNM_DOTMATCH
-
- File.send(@method, files, './main.rb', flags).should == true
- File.send(@method, files, 'one/two/.main.rb', flags).should == true
-
- File.send(@method, "**/best/*", 'lib/my/best/song.rb').should == true
- end
-
- it "returns false if '/' in pattern do not match '/' in path when flags includes FNM_PATHNAME" do
- pattern = '*/*'
- File.send(@method, pattern, 'dave/.profile', File::FNM_PATHNAME).should == false
-
- pattern = '**/foo'
- File.send(@method, pattern, 'a/.b/c/foo', File::FNM_PATHNAME).should == false
- end
-
- it "returns true if '/' in pattern match '/' in path when flags includes FNM_PATHNAME" do
- pattern = '*/*'
- File.send(@method, pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
-
- pattern = '**/foo'
- File.send(@method, pattern, 'a/b/c/foo', File::FNM_PATHNAME).should == true
- File.send(@method, pattern, '/a/b/c/foo', File::FNM_PATHNAME).should == true
- File.send(@method, pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME).should == true
- File.send(@method, pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- end
-
- it "has special handling for ./ when using * and FNM_PATHNAME" do
- File.send(@method, './*', '.', File::FNM_PATHNAME).should == false
- File.send(@method, './*', './', File::FNM_PATHNAME).should == true
- File.send(@method, './*/', './', File::FNM_PATHNAME).should == false
- File.send(@method, './**', './', File::FNM_PATHNAME).should == true
- File.send(@method, './**/', './', File::FNM_PATHNAME).should == true
- File.send(@method, './*', '.', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == false
- File.send(@method, './*', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- File.send(@method, './*/', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == false
- File.send(@method, './**', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- File.send(@method, './**/', './', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- end
-
- it "matches **/* with FNM_PATHNAME to recurse directories" do
- File.send(@method, 'nested/**/*', 'nested/subdir', File::FNM_PATHNAME).should == true
- File.send(@method, 'nested/**/*', 'nested/subdir/file', File::FNM_PATHNAME).should == true
- File.send(@method, 'nested/**/*', 'nested/.dotsubdir', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- File.send(@method, 'nested/**/*', 'nested/.dotsubir/.dotfile', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- end
-
- it "matches ** with FNM_PATHNAME only in current directory" do
- File.send(@method, 'nested/**', 'nested/subdir', File::FNM_PATHNAME).should == true
- File.send(@method, 'nested/**', 'nested/subdir/file', File::FNM_PATHNAME).should == false
- File.send(@method, 'nested/**', 'nested/.dotsubdir', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == true
- File.send(@method, 'nested/**', 'nested/.dotsubir/.dotfile', File::FNM_PATHNAME | File::FNM_DOTMATCH).should == false
- end
-
- it "accepts an object that has a #to_path method" do
- File.send(@method, '\*', mock_to_path('a')).should == false
- end
-
- it "raises a TypeError if the first and second arguments are not string-like" do
- -> { File.send(@method, nil, nil, 0, 0) }.should.raise(ArgumentError)
- -> { File.send(@method, 1, 'some/thing') }.should.raise(TypeError)
- -> { File.send(@method, 'some/thing', 1) }.should.raise(TypeError)
- -> { File.send(@method, 1, 1) }.should.raise(TypeError)
- end
-
- it "raises a TypeError if the third argument is not an Integer" do
- -> { File.send(@method, "*/place", "path/to/file", "flags") }.should.raise(TypeError)
- -> { File.send(@method, "*/place", "path/to/file", nil) }.should.raise(TypeError)
- end
-
- it "does not raise a TypeError if the third argument can be coerced to an Integer" do
- flags = mock("flags")
- flags.should_receive(:to_int).and_return(10)
- -> { File.send(@method, "*/place", "path/to/file", flags) }.should_not.raise
- end
-
- it "matches multibyte characters" do
- File.fnmatch("*/ä/ø/ñ", "a/ä/ø/ñ").should == true
- end
-end
diff --git a/spec/ruby/core/file/shared/path.rb b/spec/ruby/core/file/shared/path.rb
deleted file mode 100644
index 6c6f7d4234..0000000000
--- a/spec/ruby/core/file/shared/path.rb
+++ /dev/null
@@ -1,82 +0,0 @@
-describe :file_path, shared: true do
- before :each do
- @path = tmp("file_to_path")
- @name = File.basename(@path)
- touch @path
- end
-
- after :each do
- @file.close if @file and !@file.closed?
- rm_r @path
- end
-
- it "returns a String" do
- @file = File.new @path
- @file.send(@method).should.instance_of?(String)
- end
-
- it "returns a different String on every call" do
- @file = File.new @path
- path1 = @file.send(@method)
- path2 = @file.send(@method)
- path1.should == path2
- path1.should_not.equal?(path2)
- end
-
- it "returns a mutable String" do
- @file = File.new @path.dup.freeze
- path = @file.send(@method)
- path.should == @path
- path.should_not.frozen?
- path << "test"
- @file.send(@method).should == @path
- end
-
- it "calls to_str on argument and returns exact value" do
- path = mock('path')
- path.should_receive(:to_str).and_return(@path)
- @file = File.new path
- @file.send(@method).should == @path
- end
-
- it "does not normalise the path it returns" do
- Dir.chdir(tmp("")) do
- unorm = "./#{@name}"
- @file = File.new unorm
- @file.send(@method).should == unorm
- end
- end
-
- it "does not canonicalize the path it returns" do
- dir = File.basename tmp("")
- path = "#{tmp("")}../#{dir}/#{@name}"
- @file = File.new path
- @file.send(@method).should == path
- end
-
- it "does not absolute-ise the path it returns" do
- Dir.chdir(tmp("")) do
- @file = File.new @name
- @file.send(@method).should == @name
- end
- end
-
- it "preserves the encoding of the path" do
- path = @path.force_encoding("euc-jp")
- @file = File.new path
- @file.send(@method).encoding.should == Encoding.find("euc-jp")
- end
-
- platform_is :linux do
- guard -> { defined?(File::TMPFILE) } do
- before :each do
- @dir = tmp("tmpfilespec")
- mkdir_p @dir
- end
-
- after :each do
- rm_r @dir
- end
- end
- end
-end
diff --git a/spec/ruby/core/file/shared/unlink.rb b/spec/ruby/core/file/shared/unlink.rb
deleted file mode 100644
index 0032907ba2..0000000000
--- a/spec/ruby/core/file/shared/unlink.rb
+++ /dev/null
@@ -1,61 +0,0 @@
-describe :file_unlink, shared: true do
- before :each do
- @file1 = tmp('test.txt')
- @file2 = tmp('test2.txt')
-
- touch @file1
- touch @file2
- end
-
- after :each do
- File.send(@method, @file1) if File.exist?(@file1)
- File.send(@method, @file2) if File.exist?(@file2)
-
- @file1 = nil
- @file2 = nil
- end
-
- it "returns 0 when called without arguments" do
- File.send(@method).should == 0
- end
-
- it "deletes a single file" do
- File.send(@method, @file1).should == 1
- File.should_not.exist?(@file1)
- end
-
- it "deletes multiple files" do
- File.send(@method, @file1, @file2).should == 2
- File.should_not.exist?(@file1)
- File.should_not.exist?(@file2)
- end
-
- it "raises a TypeError if not passed a String type" do
- -> { File.send(@method, 1) }.should.raise(TypeError)
- end
-
- it "raises an Errno::ENOENT when the given file doesn't exist" do
- -> { File.send(@method, 'bogus') }.should.raise(Errno::ENOENT)
- end
-
- it "coerces a given parameter into a string if possible" do
- mock = mock("to_str")
- mock.should_receive(:to_str).and_return(@file1)
- File.send(@method, mock).should == 1
- end
-
- it "accepts an object that has a #to_path method" do
- File.send(@method, mock_to_path(@file1)).should == 1
- end
-
- platform_is :windows do
- it "allows deleting an open file with File::SHARE_DELETE" do
- path = tmp("share_delete.txt")
- File.open(path, mode: File::CREAT | File::WRONLY | File::BINARY | File::SHARE_DELETE) do |f|
- File.should.exist?(path)
- File.send(@method, path)
- end
- File.should_not.exist?(path)
- end
- end
-end
diff --git a/spec/ruby/core/file/sticky_spec.rb b/spec/ruby/core/file/sticky_spec.rb
index 5f7b2d93eb..782541abf3 100644
--- a/spec/ruby/core/file/sticky_spec.rb
+++ b/spec/ruby/core/file/sticky_spec.rb
@@ -41,7 +41,7 @@ describe "File.sticky?" do
touch(filename)
stat = File.stat(filename)
mode = stat.mode
- raise_error(Errno::EFTYPE){File.chmod(mode|01000, filename)}
+ -> { File.chmod(mode|01000, filename) }.should.raise(Errno::EFTYPE)
File.sticky?(filename).should == false
rm_r filename
diff --git a/spec/ruby/core/file/to_path_spec.rb b/spec/ruby/core/file/to_path_spec.rb
index 6d168a065c..b968d3b2b9 100644
--- a/spec/ruby/core/file/to_path_spec.rb
+++ b/spec/ruby/core/file/to_path_spec.rb
@@ -1,6 +1,84 @@
require_relative '../../spec_helper'
-require_relative 'shared/path'
describe "File#to_path" do
- it_behaves_like :file_path, :to_path
+ before :each do
+ @path = tmp("file_to_path")
+ @name = File.basename(@path)
+ touch @path
+ end
+
+ after :each do
+ @file.close if @file and !@file.closed?
+ rm_r @path
+ end
+
+ it "returns a String" do
+ @file = File.new @path
+ @file.to_path.should.instance_of?(String)
+ end
+
+ it "returns a different String on every call" do
+ @file = File.new @path
+ path1 = @file.to_path
+ path2 = @file.to_path
+ path1.should == path2
+ path1.should_not.equal?(path2)
+ end
+
+ it "returns a mutable String" do
+ @file = File.new @path.dup.freeze
+ path = @file.to_path
+ path.should == @path
+ path.should_not.frozen?
+ path << "test"
+ @file.to_path.should == @path
+ end
+
+ it "calls to_str on argument and returns exact value" do
+ path = mock('path')
+ path.should_receive(:to_str).and_return(@path)
+ @file = File.new path
+ @file.to_path.should == @path
+ end
+
+ it "does not normalise the path it returns" do
+ Dir.chdir(tmp("")) do
+ unorm = "./#{@name}"
+ @file = File.new unorm
+ @file.to_path.should == unorm
+ end
+ end
+
+ it "does not canonicalize the path it returns" do
+ dir = File.basename tmp("")
+ path = "#{tmp("")}../#{dir}/#{@name}"
+ @file = File.new path
+ @file.to_path.should == path
+ end
+
+ it "does not absolute-ise the path it returns" do
+ Dir.chdir(tmp("")) do
+ @file = File.new @name
+ @file.to_path.should == @name
+ end
+ end
+
+ it "preserves the encoding of the path" do
+ path = @path.force_encoding("euc-jp")
+ @file = File.new path
+ @file.to_path.encoding.should == Encoding.find("euc-jp")
+ end
+
+ platform_is :linux do
+ guard -> { defined?(File::TMPFILE) } do
+ before :each do
+ @dir = tmp("tmpfilespec")
+ mkdir_p @dir
+ end
+
+ after :each do
+ rm_r @dir
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/file/unlink_spec.rb b/spec/ruby/core/file/unlink_spec.rb
index 28872d55ed..db0c08f3ae 100644
--- a/spec/ruby/core/file/unlink_spec.rb
+++ b/spec/ruby/core/file/unlink_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/unlink'
describe "File.unlink" do
- it_behaves_like :file_unlink, :unlink
+ it "is an alias of File.delete" do
+ File.method(:unlink).should == File.method(:delete)
+ end
end
diff --git a/spec/ruby/core/file/zero_spec.rb b/spec/ruby/core/file/zero_spec.rb
index 01c7505ef2..09b0decf48 100644
--- a/spec/ruby/core/file/zero_spec.rb
+++ b/spec/ruby/core/file/zero_spec.rb
@@ -1,7 +1,7 @@
require_relative '../../spec_helper'
-require_relative '../../shared/file/zero'
describe "File.zero?" do
- it_behaves_like :file_zero, :zero?, File
- it_behaves_like :file_zero_missing, :zero?, File
+ it "is an alias of File.empty?" do
+ File.method(:zero?).should == File.method(:empty?)
+ end
end