Skip to content

Loading…

Fixing write specs #730

Closed
wants to merge 2 commits into from

1 participant

View
3 spec/tags/1.9/ruby/core/io/binwrite_tags.txt
@@ -1,3 +0,0 @@
-fails:IO.binwrite accepts a :mode option
-fails:IO.binwrite raises an error if readonly mode is specified
-fails:IO.binwrite truncates if empty :opts provided and offset skipped
View
4 spec/tags/1.9/ruby/core/io/write_tags.txt
@@ -1,6 +1,2 @@
-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
View
101 src/org/jruby/RubyIO.java
@@ -3603,13 +3603,58 @@ private static IRubyObject read19(ThreadContext context, IRubyObject recv, IRuby
* mode: string
* open_args: array of string
*/
- private static IRubyObject write19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject str, IRubyObject offset, RubyHash options) {
- // FIXME: process options
+ private static IRubyObject write19(ThreadContext context, IRubyObject recv, IRubyObject [] args) {
+ Ruby runtime = context.runtime;
+
+ IRubyObject nil = runtime.getNil();
+ IRubyObject path = args[0];
+ IRubyObject str = args[1];
+ IRubyObject offset = nil;
+ RubyHash options = null;
+ RubyString mode = runtime.newString("");
+ int oflags = ModeFlags.WRONLY;
RubyString pathStr = RubyFile.get_path(context, path);
- Ruby runtime = context.runtime;
failIfDirectory(runtime, pathStr);
- RubyIO file = newFile(context, recv, pathStr, context.runtime.newString("w"));
+
+ if (args.length > 2 && args[2] instanceof RubyHash) {
+ options = (RubyHash) args[2];
+ RubySymbol key = runtime.newSymbol("mode");
+
+ if(options.containsKey(key)) {
+ mode = (RubyString) options.fastARef(key);
+ }
+
+ } else if(args.length > 2) {
+ offset = args[2];
+ }
+
+ if (args.length > 3) {
+ if (!(args[3] instanceof RubyHash)) throw runtime.newTypeError("Must be a hash");
+ options = (RubyHash) args[3];
+ RubySymbol key = runtime.newSymbol("mode");
+
+ if(options.containsKey(key)) {
+ mode = (RubyString) options.fastARef(key);
+ }
+ }
+
+ long fmode = ModeFlags.CREAT | ModeFlags.BINARY;
+
+ if (!offset.isNil() && mode.isEmpty()) {
+ fmode |= ModeFlags.RDWR;
+ } else {
+ if(!mode.isEmpty()) {
+ try {
+ oflags = OpenFile.getModeFlagsAsIntFrom(OpenFile.getFModeFromString(mode.asJavaString()));
+ } catch (InvalidValueException e) {
+ throw runtime.newArgumentError("invalid access mode " + mode.asJavaString());
+ }
+ }
+ fmode |= oflags;
+ }
+
+ RubyIO file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, RubyFixnum.newFixnum(runtime, fmode));
try {
if (!offset.isNil()) file.seek(context, offset);
@@ -3689,56 +3734,12 @@ public static IRubyObject read19(ThreadContext context, IRubyObject recv, IRubyO
@JRubyMethod(meta = true, required = 2, optional = 2, compat = RUBY1_9)
public static IRubyObject binwrite(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
- IRubyObject nil = context.runtime.getNil();
- IRubyObject path = args[0];
- IRubyObject str = args[1];
- IRubyObject offset = nil;
- Ruby runtime = context.runtime;
-
- if (args.length > 2) {
- offset = args[2];
- }
-
- long mode = ModeFlags.CREAT | ModeFlags.BINARY;
-
- if (offset.isNil()) {
- mode |= ModeFlags.WRONLY;
- } else {
- mode |= ModeFlags.RDWR;
- }
-
- RubyIO file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, RubyFixnum.newFixnum(runtime, mode));
-
- try {
- if (!offset.isNil()) file.seek(context, offset);
- return file.write(context, str);
- } finally {
- file.close();
- }
+ return write19(context, recv, args);
}
@JRubyMethod(name = "write", meta = true, required = 2, optional = 2, compat = RUBY1_9)
public static IRubyObject writeStatic(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
- IRubyObject nil = context.nil;
- IRubyObject path = args[0];
- IRubyObject str = args[1];
- IRubyObject offset = nil;
- RubyHash options = null;
- if (args.length > 3) {
- if (!(args[3] instanceof RubyHash)) {
- throw context.runtime.newTypeError("Must be a hash");
- }
- options = (RubyHash) args[3];
- offset = args[2];
- } else if (args.length > 2) {
- if (args[2] instanceof RubyHash) {
- options = (RubyHash) args[2];
- } else {
- offset = args[2];
- }
- }
-
- return write19(context, recv, path, str, offset, (RubyHash) options);
+ return write19(context, recv, args);
}
@JRubyMethod(name = "readlines", required = 1, optional = 1, meta = true, compat = RUBY1_8)
View
1 test/externals/ruby1.9/excludes/TestIO.rb
@@ -47,7 +47,6 @@
exclude :test_reinitialize, "needs investigation"
exclude :test_reopen, "needs investigation"
exclude :test_reopen_inherit, "needs investigation"
-exclude :test_s_binwrite, "needs investigation"
exclude :test_s_readlines, "needs investigation"
exclude :test_s_write, "needs investigation"
exclude :test_set_lineno, "needs investigation"
Something went wrong with that request. Please try again.