Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/bookworm/rules/IncludeRecipeLiterals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.
description 'Extracts recipes that are used by include_recipe with string literals'
keys ['recipe']
keys ['recipe', 'recipejson']

def_node_search :include_recipe_string_literals, '`(send nil? :include_recipe (str $_))'

def to_a
arr = []
include_recipe_string_literals(@metadata['ast']).each do |x|
arr << x
end
return [] if arr.empty?
arr.map! do |x|
if x.start_with?('::')
"#{@metadata['cookbook']}#{x}"
elsif !x.include?('::')
"#{x}::default"
else
x
if @metadata['ast'] # Ruby recipe
include_recipe_string_literals(@metadata['ast']).each do |x|
arr << x
end
return [] if arr.empty?
arr.map! do |x|
if x.start_with?('::')
"#{@metadata['cookbook']}#{x}"
elsif !x.include?('::')
"#{x}::default"
else
x
end
end
elsif @metadata['object'] # JSON recipe
if @metadata['object'].key?('include_recipes')
arr = @metadata['object']['include_recipes']
end
end

arr.uniq!
arr.sort!
end
32 changes: 32 additions & 0 deletions spec/rules/IncludeRecipeLiterals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,36 @@
# Note - output is sorted
expect(rule.output).to eq(['fake_cookbook::bar', 'fake_cookbook::foo'])
end

context 'with JSON recipes' do
it 'extracts include_recipes from JSON object' do
rule = described_class.new({
'object' => { 'include_recipes' => ['foo::bar', 'baz::qux'] },
})
expect(rule.output).to eq(['baz::qux', 'foo::bar'])
end

it 'returns empty array when no include_recipes key' do
rule = described_class.new({
'object' => { 'some_other_key' => 'value' },
})
expect(rule.output).to eq([])
end

it 'returns empty array when include_recipes is empty' do
rule = described_class.new({
'object' => { 'include_recipes' => [] },
})
expect(rule.output).to eq([])
end

it 'deduplicates include_recipes' do
rule = described_class.new({
'object' => {
'include_recipes' => ['foo::bar', 'foo::bar', 'baz::qux'],
},
})
expect(rule.output).to eq(['baz::qux', 'foo::bar'])
end
end
end
Loading