Project

General

Profile

Feature #14007 ยป ruby-mode-x.diff

add mode 'x' to Ruby, with specs - kernigh (George Koehler), 10/12/2017 12:20 AM

View differences:

include/ruby/io.h
/* #define FMODE_NOREVLOOKUP 0x00000100 */
#define FMODE_TRUNC 0x00000800
#define FMODE_TEXTMODE 0x00001000
#define FMODE_EXCL 0x00002000
/* #define FMODE_PREP 0x00010000 */
#define FMODE_SETENC_BY_BOM 0x00100000
/* #define FMODE_UNIX 0x00200000 */
io.c
case 't':
fmode |= FMODE_TEXTMODE;
break;
case 'x':
if (fmode & FMODE_READABLE)
rb_raise(rb_eArgError, "can't use mode \"x\" with \"r\"");
fmode |= FMODE_EXCL;
break;
case '+':
fmode |= FMODE_READWRITE;
break;
......
if (oflags & O_CREAT) {
fmode |= FMODE_CREATE;
}
if (oflags & O_EXCL) {
fmode |= FMODE_EXCL;
}
#ifdef O_BINARY
if (oflags & O_BINARY) {
fmode |= FMODE_BINMODE;
......
if (fmode & FMODE_CREATE) {
oflags |= O_CREAT;
}
if (fmode & FMODE_EXCL) {
oflags |= O_EXCL;
}
#ifdef O_BINARY
if (fmode & FMODE_BINMODE) {
oflags |= O_BINARY;
......
*
* "t" Text file mode
*
* "x" Exclusive open
* Creates a new file, or raises an error if the file
* exists. Mode "x" is available since Ruby 2.5.
*
* When the open mode of original IO is read only, the mode cannot be
* changed to be writable. Similarly, the open mode cannot be changed from
* write only to readable.
spec/ruby/core/file/open_spec.rb
File.exist?(@file).should == true
end
ruby_version_is "2.5" do
it "opens a file that no exists when use 'ax' mode" do
@fh = File.open(@nonexistent, 'ax') { |f| f }
@fh.should be_kind_of(File)
File.exist?(@file).should == true
end
it "opens a file that no exists when use 'wx' mode" do
@fh = File.open(@nonexistent, 'wx') { |f| f }
@fh.should be_kind_of(File)
File.exist?(@file).should == true
end
it "raises ArgumentError when use 'rx' mode" do
lambda {
File.open(@nonexistent, 'rx') { }
}.should raise_error(ArgumentError)
lambda {
File.open(@file, 'rx') { }
}.should raise_error(ArgumentError)
end
end
# Check the grants associated to the differents open modes combinations.
it "raises an ArgumentError exception when call with an unknown mode" do
lambda { File.open(@file, "q") }.should raise_error(ArgumentError)
......
end
end
ruby_version_is "2.5" do
it "raises an Errno::EEXIST if the file exists when open with 'wx' mode" do
lambda {
File.open(@file, 'wx') { }
}.should raise_error(Errno::EEXIST)
end
it "raises an Errno::EEXIST if the file exists when open with 'ax' mode" do
lambda {
File.open(@file, 'ax') { }
}.should raise_error(Errno::EEXIST)
end
end
platform_is_not :windows do
describe "on a FIFO" do
before :each do
    (1-1/1)