Skip to content

Commit

Permalink
Merge pull request #9124 from mhashizume/maint/7.x/jruby-dir-glob
Browse files Browse the repository at this point in the history
(PUP-11788) Account for JRuby behavior in Dir.glob
  • Loading branch information
AriaXLi committed Oct 13, 2023
2 parents d835ef6 + 10d219a commit 1a68257
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/puppet/node/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,12 @@ def perform_initial_import
if file == NO_MANIFEST
empty_parse_result
elsif File.directory?(file)
parse_results = Puppet::FileSystem::PathPattern.absolute(File.join(file, '**/*.pp')).glob.sort.map do | file_to_parse |
parser.file = file_to_parse
parser.parse
end
# JRuby does not properly perform Dir.glob operations with wildcards, (see PUP-11788 and https://github.com/jruby/jruby/issues/7836).
# We sort the results because Dir.glob order is inconsistent in Ruby < 3 (see PUP-10115).
parse_results = Puppet::FileSystem::PathPattern.absolute(File.join(file, '**/*')).glob.select {|globbed_file| globbed_file.end_with?('.pp')}.sort.map do | file_to_parse |
parser.file = file_to_parse
parser.parse
end
# Use a parser type specific merger to concatenate the results
Puppet::Parser::AST::Hostclass.new('', :code => Puppet::Parser::ParserFactory.code_merger.concatenate(parse_results))
else
Expand Down
15 changes: 15 additions & 0 deletions spec/unit/file_system/path_pattern_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'puppet_spec/files'
require 'puppet/file_system'
require 'puppet/util'

describe Puppet::FileSystem::PathPattern do
include PuppetSpec::Files
Expand Down Expand Up @@ -132,6 +133,20 @@
File.join(dir, "found_two")])
end

it 'globs wildcard patterns properly' do
# See PUP-11788 and https://github.com/jruby/jruby/issues/7836.
pending 'JRuby does not properly handle Dir.glob' if Puppet::Util::Platform.jruby?

dir = tmpdir('globtest')
create_file_in(dir, 'foo.pp')
create_file_in(dir, 'foo.pp.pp')

pattern = Puppet::FileSystem::PathPattern.absolute(File.join(dir, '**/*.pp'))

expect(pattern.glob).to match_array([File.join(dir, 'foo.pp'),
File.join(dir, 'foo.pp.pp')])
end

def create_file_in(dir, name)
File.open(File.join(dir, name), "w") { |f| f.puts "data" }
end
Expand Down

0 comments on commit 1a68257

Please sign in to comment.