Skip to content

Cookbook libraries in spec/chef_helper.rb might be loaded in incorrect order

The file spechef_helper.rb loads the required cookbook libraries through the code block below:

cookbooks.each do |cookbook|
  Dir["#{cookbook}/libraries/**/*.rb"].each { |library| require library }
end

But Dir does not guarantee the order of the files. Though the RSpec tests go well currently in com, there are chances of failures because some files might be loaded before their dependencies.

In our pipelines, provider_runit_service.rb is somehow always loaded before the required helper.rb.

Screenshots image

A temporary solution could be making the files under lib directories in alphabetic order, by just simply adding sort

cookbooks.each do |cookbook|
  Dir["#{cookbook}/libraries/**/*.rb"].sort.each { |library| require library }
end

It makes sure that the files are loaded in stable alphabetic order and the pipelines are green again. But still, it cannot guarantee the load requirements.

Edited by vincent stchu