[前][次][番号順一覧][スレッド一覧]

ruby-changes:69806

From: Nobuyoshi <ko1@a...>
Date: Thu, 18 Nov 2021 21:47:32 +0900 (JST)
Subject: [ruby-changes:69806] 89b440bf72 (master): Expect bool as `sort:` option at glob [Feature #18287]

https://git.ruby-lang.org/ruby.git/commit/?id=89b440bf72

From 89b440bf724b5e670da0fa31c36a7945a7ddc80f Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 18 Nov 2021 21:47:18 +0900
Subject: Expect bool as `sort:` option at glob [Feature #18287]

---
 common.mk                         | 3 +++
 dir.c                             | 3 ++-
 spec/ruby/core/dir/shared/glob.rb | 9 +++++++++
 test/ruby/test_dir.rb             | 6 ++++++
 4 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/common.mk b/common.mk
index 8a916315ff7..c840ec654de 100644
--- a/common.mk
+++ b/common.mk
@@ -3858,6 +3858,7 @@ debug_counter.$(OBJEXT): {$(VPATH)}thread_native.h https://github.com/ruby/ruby/blob/trunk/common.mk#L3858
 dir.$(OBJEXT): $(hdrdir)/ruby.h
 dir.$(OBJEXT): $(hdrdir)/ruby/ruby.h
 dir.$(OBJEXT): $(top_srcdir)/internal/array.h
+dir.$(OBJEXT): $(top_srcdir)/internal/class.h
 dir.$(OBJEXT): $(top_srcdir)/internal/compilers.h
 dir.$(OBJEXT): $(top_srcdir)/internal/dir.h
 dir.$(OBJEXT): $(top_srcdir)/internal/encoding.h
@@ -3865,6 +3866,7 @@ dir.$(OBJEXT): $(top_srcdir)/internal/error.h https://github.com/ruby/ruby/blob/trunk/common.mk#L3866
 dir.$(OBJEXT): $(top_srcdir)/internal/file.h
 dir.$(OBJEXT): $(top_srcdir)/internal/gc.h
 dir.$(OBJEXT): $(top_srcdir)/internal/io.h
+dir.$(OBJEXT): $(top_srcdir)/internal/object.h
 dir.$(OBJEXT): $(top_srcdir)/internal/serial.h
 dir.$(OBJEXT): $(top_srcdir)/internal/static_assert.h
 dir.$(OBJEXT): $(top_srcdir)/internal/string.h
@@ -3888,6 +3890,7 @@ dir.$(OBJEXT): {$(VPATH)}dir.rbinc https://github.com/ruby/ruby/blob/trunk/common.mk#L3890
 dir.$(OBJEXT): {$(VPATH)}encindex.h
 dir.$(OBJEXT): {$(VPATH)}encoding.h
 dir.$(OBJEXT): {$(VPATH)}id.h
+dir.$(OBJEXT): {$(VPATH)}id_table.h
 dir.$(OBJEXT): {$(VPATH)}intern.h
 dir.$(OBJEXT): {$(VPATH)}internal.h
 dir.$(OBJEXT): {$(VPATH)}internal/anyargs.h
diff --git a/dir.c b/dir.c
index 4e552d772a8..184dbf007ed 100644
--- a/dir.c
+++ b/dir.c
@@ -112,6 +112,7 @@ char *strchr(char*,char); https://github.com/ruby/ruby/blob/trunk/dir.c#L112
 #include "internal/file.h"
 #include "internal/gc.h"
 #include "internal/io.h"
+#include "internal/object.h"
 #include "internal/vm.h"
 #include "ruby/encoding.h"
 #include "ruby/ruby.h"
@@ -2937,7 +2938,7 @@ dir_glob_option_base(VALUE base) https://github.com/ruby/ruby/blob/trunk/dir.c#L2938
 static int
 dir_glob_option_sort(VALUE sort)
 {
-    return (sort ? 0 : FNM_GLOB_NOSORT);
+    return (rb_bool_expected(sort, "sort") ? 0 : FNM_GLOB_NOSORT);
 }
 
 static VALUE
diff --git a/spec/ruby/core/dir/shared/glob.rb b/spec/ruby/core/dir/shared/glob.rb
index 173038ee24f..69fb0f0a012 100644
--- a/spec/ruby/core/dir/shared/glob.rb
+++ b/spec/ruby/core/dir/shared/glob.rb
@@ -55,6 +55,15 @@ describe :dir_glob, shared: true do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/dir/shared/glob.rb#L55
     end
   end
 
+  ruby_version_is "3.1" do
+    it "true or false is expected as sort:" do
+      -> {Dir.send(@method, '*', sort: nil)}.should raise_error ArgumentError, /true or false/
+      -> {Dir.send(@method, '*', sort: 0)}.should raise_error ArgumentError, /true or false/
+      -> {Dir.send(@method, '*', sort: "")}.should raise_error ArgumentError, /true or false/
+      -> {Dir.send(@method, '*', sort: Object.new)}.should raise_error ArgumentError, /true or false/
+    end
+  end
+
   it "matches non-dotfiles with '*'" do
     expected = %w[
       brace
diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb
index 3e03d7c7bcf..9474c6e927f 100644
--- a/test/ruby/test_dir.rb
+++ b/test/ruby/test_dir.rb
@@ -176,6 +176,12 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L176
     assert_raise_with_message(ArgumentError, /nul-separated/) do
       Dir.glob(@root + "\0\0\0" + File.join(@root, "*"))
     end
+    assert_raise_with_message(ArgumentError, /expected true or false/) do
+      Dir.glob(@root, sort: 1)
+    end
+    assert_raise_with_message(ArgumentError, /expected true or false/) do
+      Dir.glob(@root, sort: nil)
+    end
 
     assert_equal(("a".."z").step(2).map {|f| File.join(File.join(@root, f), "") },
                  Dir.glob(File.join(@root, "*/")))
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]