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
111 111
/* #define FMODE_NOREVLOOKUP        0x00000100 */
112 112
#define FMODE_TRUNC                 0x00000800
113 113
#define FMODE_TEXTMODE              0x00001000
114
#define FMODE_EXCL                  0x00002000
114 115
/* #define FMODE_PREP               0x00010000 */
115 116
#define FMODE_SETENC_BY_BOM         0x00100000
116 117
/* #define FMODE_UNIX                  0x00200000 */
io.c
5163 5163
	  case 't':
5164 5164
            fmode |= FMODE_TEXTMODE;
5165 5165
            break;
5166
	  case 'x':
5167
	    if (fmode & FMODE_READABLE)
5168
		rb_raise(rb_eArgError, "can't use mode \"x\" with \"r\"");
5169
	    fmode |= FMODE_EXCL;
5170
	    break;
5166 5171
	  case '+':
5167 5172
            fmode |= FMODE_READWRITE;
5168 5173
            break;
......
5209 5214
    if (oflags & O_CREAT) {
5210 5215
	fmode |= FMODE_CREATE;
5211 5216
    }
5217
    if (oflags & O_EXCL) {
5218
	fmode |= FMODE_EXCL;
5219
    }
5212 5220
#ifdef O_BINARY
5213 5221
    if (oflags & O_BINARY) {
5214 5222
	fmode |= FMODE_BINMODE;
......
5244 5252
    if (fmode & FMODE_CREATE) {
5245 5253
        oflags |= O_CREAT;
5246 5254
    }
5255
    if (fmode & FMODE_EXCL) {
5256
	oflags |= O_EXCL;
5257
    }
5247 5258
#ifdef O_BINARY
5248 5259
    if (fmode & FMODE_BINMODE) {
5249 5260
        oflags |= O_BINARY;
......
7789 7800
 *
7790 7801
 *  	"t"  Text file mode
7791 7802
 *
7803
 *  	"x"  Exclusive open
7804
 *  	     Creates a new file, or raises an error if the file
7805
 *  	     exists.  Mode "x" is available since Ruby 2.5.
7806
 *
7792 7807
 *  When the open mode of original IO is read only, the mode cannot be
7793 7808
 *  changed to be writable.  Similarly, the open mode cannot be changed from
7794 7809
 *  write only to readable.
spec/ruby/core/file/open_spec.rb
234 234
    File.exist?(@file).should == true
235 235
  end
236 236

  
237
  ruby_version_is "2.5" do
238
    it "opens a file that no exists when use 'ax' mode" do
239
      @fh = File.open(@nonexistent, 'ax') { |f| f }
240
      @fh.should be_kind_of(File)
241
      File.exist?(@file).should == true
242
    end
243

  
244
    it "opens a file that no exists when use 'wx' mode" do
245
      @fh = File.open(@nonexistent, 'wx') { |f| f }
246
      @fh.should be_kind_of(File)
247
      File.exist?(@file).should == true
248
    end
249

  
250
    it "raises ArgumentError when use 'rx' mode" do
251
      lambda {
252
        File.open(@nonexistent, 'rx') { }
253
      }.should raise_error(ArgumentError)
254
      lambda {
255
        File.open(@file, 'rx') { }
256
      }.should raise_error(ArgumentError)
257
    end
258
  end
259

  
237 260
  # Check the grants associated to the differents open modes combinations.
238 261
  it "raises an ArgumentError exception when call with an unknown mode" do
239 262
    lambda { File.open(@file, "q") }.should raise_error(ArgumentError)
......
598 621
    end
599 622
  end
600 623

  
624
  ruby_version_is "2.5" do
625
    it "raises an Errno::EEXIST if the file exists when open with 'wx' mode" do
626
      lambda {
627
        File.open(@file, 'wx') { }
628
      }.should raise_error(Errno::EEXIST)
629
    end
630

  
631
    it "raises an Errno::EEXIST if the file exists when open with 'ax' mode" do
632
      lambda {
633
        File.open(@file, 'ax') { }
634
      }.should raise_error(Errno::EEXIST)
635
    end
636
  end
637

  
601 638
  platform_is_not :windows do
602 639
    describe "on a FIFO" do
603 640
      before :each do