Skip to content

Loading…

add support for options and not truncating offset for IO.write #764

Merged
merged 1 commit into from

2 participants

@josedonizetti
JRuby Team member

No description provided.

@atambo
JRuby Team member

This change introduces the following rubyspec error:

     [java] IO.write disregards other options if :open_args is given ERROR
     [java] IOError: not opened for writing
     [java] org/jruby/RubyIO.java:1369:in `write'
     [java] org/jruby/RubyIO.java:3771:in `write'
     [java] /home/travis/build/jruby/jruby/spec/ruby/core/io/write_spec.rb:61:in `(root)'
     [java] org/jruby/RubyBasicObject.java:1531:in `instance_eval'
     [java] org/jruby/RubyEnumerable.java:1343:in `all?'
     [java] org/jruby/RubyFixnum.java:280:in `times'
     [java] org/jruby/RubyArray.java:1617:in `each'
     [java] /home/travis/build/jruby/jruby/spec/ruby/core/io/write_spec.rb:48:in `(root)'
     [java] /home/travis/build/jruby/jruby/spec/ruby/core/io/write_spec.rb:47:in `(root)'
     [java] org/jruby/RubyKernel.java:1073:in `load'
     [java] org/jruby/RubyBasicObject.java:1531:in `instance_eval'
     [java] org/jruby/RubyArray.java:1617:in `each'
@josedonizetti
JRuby Team member

@atambo how did you simulate this error? Because my change is not supposed to make it happen, this error should be happening before because IO.write doesn't support "open_args" the specs for "open_args" are tagged. I was planing to support "open_args" after merging this pull request because make reviews easier.

@atambo atambo merged commit e812402 into jruby:master

1 check was pending

Details default The Travis CI build is in progress
@josedonizetti josedonizetti deleted the josedonizetti:io_write branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 8 deletions.
  1. +2 −5 spec/tags/1.9/ruby/core/io/write_tags.txt
  2. +19 −3 src/org/jruby/RubyIO.java
View
7 spec/tags/1.9/ruby/core/io/write_tags.txt
@@ -1,6 +1,3 @@
-fails:IO.write doesn't truncate the file and writes the given string if an offset is given
-fails:IO.write accepts a :mode option
-fails:IO.write raises an error if readonly mode is specified
-fails:IO.write uses encoding from given options, if provided
fails:IO.write uses an :open_args option
-fails:IO.write doesn't truncate and writes at the given offset after passing empty opts
+fails:IO.write disregards other options if :open_args is given
+
View
22 src/org/jruby/RubyIO.java
@@ -3604,12 +3604,28 @@ private static IRubyObject read19(ThreadContext context, IRubyObject recv, IRuby
* open_args: array of string
*/
private static IRubyObject write19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject str, IRubyObject offset, RubyHash options) {
- // FIXME: process options
-
RubyString pathStr = RubyFile.get_path(context, path);
Ruby runtime = context.runtime;
failIfDirectory(runtime, pathStr);
- RubyIO file = newFile(context, recv, pathStr, context.runtime.newString("w"));
+
+ RubyIO file = null;
+
+ long mode = ModeFlags.CREAT;
+
+ if (options == null || (options != null && options.isEmpty())) {
+ if (offset.isNil()) {
+ mode |= ModeFlags.WRONLY;
+ } else {
+ mode |= ModeFlags.RDWR;
+ }
+
+ file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, RubyFixnum.newFixnum(runtime, mode));
+ } else if (!options.containsKey(runtime.newSymbol("mode"))) {
+ mode |= ModeFlags.WRONLY;
+ file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, RubyFixnum.newFixnum(runtime, mode), options);
+ } else {
+ file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, options);
+ }
try {
if (!offset.isNil()) file.seek(context, offset);
Something went wrong with that request. Please try again.