Skip to content

Commit 8e9046e

Browse files
dafyddcrosbymeta-codesync[bot]
authored andcommitted
Add JSON recipe literals (rule)
Summary: X-link: facebook/bookworm#13 Now that JSON recipes support including recipes, we need to take those literals into account in downstream reports. Differential Revision: D90605017 fbshipit-source-id: 38cb6b59576d14a7f88fece3655b75ca99eafddb
1 parent 8a7a9ac commit 8e9046e

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

cookbooks/fb_bookworm/files/default/bookworm/lib/bookworm/rules/IncludeRecipeLiterals.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,32 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
description 'Extracts recipes that are used by include_recipe with string literals'
16-
keys ['recipe']
16+
keys ['recipe', 'recipejson']
1717

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

2020
def to_a
2121
arr = []
22-
include_recipe_string_literals(@metadata['ast']).each do |x|
23-
arr << x
24-
end
25-
return [] if arr.empty?
26-
arr.map! do |x|
27-
if x.start_with?('::')
28-
"#{@metadata['cookbook']}#{x}"
29-
elsif !x.include?('::')
30-
"#{x}::default"
31-
else
32-
x
22+
if @metadata['ast'] # Ruby recipe
23+
include_recipe_string_literals(@metadata['ast']).each do |x|
24+
arr << x
25+
end
26+
return [] if arr.empty?
27+
arr.map! do |x|
28+
if x.start_with?('::')
29+
"#{@metadata['cookbook']}#{x}"
30+
elsif !x.include?('::')
31+
"#{x}::default"
32+
else
33+
x
34+
end
35+
end
36+
elsif @metadata['object'] # JSON recipe
37+
if @metadata['object'].key?('include_recipes')
38+
arr = @metadata['object']['include_recipes']
3339
end
3440
end
41+
3542
arr.uniq!
3643
arr.sort!
3744
end

cookbooks/fb_bookworm/files/default/bookworm/spec/rules/IncludeRecipeLiterals_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,36 @@
5656
# Note - output is sorted
5757
expect(rule.output).to eq(['fake_cookbook::bar', 'fake_cookbook::foo'])
5858
end
59+
60+
context 'with JSON recipes' do
61+
it 'extracts include_recipes from JSON object' do
62+
rule = described_class.new({
63+
'object' => { 'include_recipes' => ['foo::bar', 'baz::qux'] },
64+
})
65+
expect(rule.output).to eq(['baz::qux', 'foo::bar'])
66+
end
67+
68+
it 'returns empty array when no include_recipes key' do
69+
rule = described_class.new({
70+
'object' => { 'some_other_key' => 'value' },
71+
})
72+
expect(rule.output).to eq([])
73+
end
74+
75+
it 'returns empty array when include_recipes is empty' do
76+
rule = described_class.new({
77+
'object' => { 'include_recipes' => [] },
78+
})
79+
expect(rule.output).to eq([])
80+
end
81+
82+
it 'deduplicates include_recipes' do
83+
rule = described_class.new({
84+
'object' => {
85+
'include_recipes' => ['foo::bar', 'foo::bar', 'baz::qux'],
86+
},
87+
})
88+
expect(rule.output).to eq(['baz::qux', 'foo::bar'])
89+
end
90+
end
5991
end

0 commit comments

Comments
 (0)