Skip to content

Commit db4908a

Browse files
committed
[GR-18163] Fix repeated calling of methods Dir#each, Dir#each_child and Dir#children
PullRequest: truffleruby/4203
2 parents aefb5a0 + 2e31df0 commit db4908a

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ New features:
55
Bug fixes:
66

77
* Add missing thread-safe objects write barriers for `TruffleRuby::ConcurrentMap` (#3179, @eregon).
8+
* Fix repeated calling of methods `Dir#{each,each_child,children}` (#3464, @andrykonchin).
89

910
Compatibility:
1011

spec/ruby/core/dir/children_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,17 @@
131131
children = @dir.children.sort
132132
children.first.encoding.should equal(Encoding::EUC_KR)
133133
end
134+
135+
it "returns the same result when called repeatedly" do
136+
@dir = Dir.open DirSpecs.mock_dir
137+
138+
a = []
139+
@dir.each {|dir| a << dir}
140+
141+
b = []
142+
@dir.each {|dir| b << dir}
143+
144+
a.sort.should == b.sort
145+
a.sort.should == DirSpecs.expected_paths
146+
end
134147
end

spec/ruby/core/dir/each_child_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@
8686
@dir.each_child { |f| f }.should == @dir
8787
end
8888

89+
it "returns the same result when called repeatedly" do
90+
@dir = Dir.open DirSpecs.mock_dir
91+
92+
a = []
93+
@dir.each {|dir| a << dir}
94+
95+
b = []
96+
@dir.each {|dir| b << dir}
97+
98+
a.sort.should == b.sort
99+
a.sort.should == DirSpecs.expected_paths
100+
end
101+
89102
describe "when no block is given" do
90103
it "returns an Enumerator" do
91104
@dir = Dir.new(DirSpecs.mock_dir)

spec/ruby/core/dir/each_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
ls.should include(@dir.read)
3636
end
3737

38+
it "returns the same result when called repeatedly" do
39+
a = []
40+
@dir.each {|dir| a << dir}
41+
42+
b = []
43+
@dir.each {|dir| b << dir}
44+
45+
a.sort.should == b.sort
46+
a.sort.should == DirSpecs.expected_paths
47+
end
48+
3849
describe "when no block is given" do
3950
it "returns an Enumerator" do
4051
@dir.each.should be_an_instance_of(Enumerator)

src/main/ruby/truffleruby/core/dir.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ def closed?
116116
def each
117117
return to_enum unless block_given?
118118

119+
rewind
120+
119121
while s = read
120122
yield s
121123
end
@@ -124,6 +126,8 @@ def each
124126
end
125127

126128
def children
129+
rewind
130+
127131
ret = []
128132
while s = read
129133
ret << s if s != '.' and s != '..'
@@ -134,6 +138,8 @@ def children
134138
def each_child
135139
return to_enum(:each_child) unless block_given?
136140

141+
rewind
142+
137143
while s = read
138144
yield s unless s == '.' or s == '..'
139145
end

0 commit comments

Comments
 (0)