Skip to content

Commit 31729cd

Browse files
dafyddcrosbymeta-codesync[bot]
authored andcommitted
Add cookbookrspec key and CookbookRspecExists rule
Summary: X-link: facebook/bookworm#12 Add a new bookworm key `cookbookrspec` to detect RSpec test files in cookbooks, and create a corresponding `CookbookRspecExists` rule. Since normal rspec and chefspec can be side-by-side, I opted for the more generic key. We're ignoring helper-type files, only focusing on _spec since that's where the tests need to reside. The key uses: - glob_pattern: */spec/*_spec.rb - path_name_regex: spec/(.*)_spec\.rb - determine_cookbook_name: true This enables bookworm to track which cookbooks have RSpec test files. Reviewed By: joshuamiller01 Differential Revision: D90535240 fbshipit-source-id: 1d2f958ac61b5afd089bebc62f1435fef4390412
1 parent 8e9046e commit 31729cd

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

cookbooks/fb_bookworm/files/default/bookworm/lib/bookworm/keys.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ module Bookworm
8080
'determine_cookbook_name' => true,
8181
'path_name_regex' => 'providers/(.*)\.rb',
8282
},
83+
'cookbookrspec' => {
84+
'glob_pattern' => '*/spec/*_spec.rb',
85+
'determine_cookbook_name' => true,
86+
'path_name_regex' => 'spec/(.*)_spec\.rb',
87+
},
8388
}.freeze
8489

8590
# Set defaults
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2026-present, Meta Platforms, Inc. and affiliates
2+
# All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
description 'Helper rule to show existence of a cookbook rspec file'
16+
keys %w{
17+
cookbookrspec
18+
}
19+
20+
def output
21+
true
22+
end

cookbooks/fb_bookworm/files/default/bookworm/spec/knowledge_base_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@
111111
} })
112112
end
113113

114+
it 'holds all yer cookbookrspecs' do
115+
kb = Bookworm::KnowledgeBase.new(
116+
{ 'cookbookrspec' => [['mycookbook/spec/default_spec.rb', '(ast)']] },
117+
)
118+
expect(kb.cookbookrspecs).to eq({ 'mycookbook::default' => {
119+
'path' => 'mycookbook/spec/default_spec.rb',
120+
'cookbook' => 'mycookbook',
121+
'ast' => '(ast)',
122+
} })
123+
end
124+
114125
it 'handles empty input' do
115126
kb = Bookworm::KnowledgeBase.new({})
116127
expect(kb.recipes).to eq({})

cookbooks/fb_bookworm/files/default/bookworm/spec/lib/keys_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
it 'contains expected key types' do
2626
expected_keys = %w{
2727
cookbook role metadatarb metadatajson recipe recipejson
28-
attribute library resource provider
28+
attribute library resource provider cookbookrspec
2929
}
3030
expect(keys.keys).to match_array(expected_keys)
3131
end
@@ -77,8 +77,8 @@
7777
end
7878

7979
describe 'determine_cookbook_name' do
80-
it 'is true for recipe, attribute, library, resource, provider' do
81-
%w{recipe attribute library resource provider}.each do |key|
80+
it 'is true for recipe, attribute, library, resource, provider, cookbookrspec' do
81+
%w{recipe attribute library resource provider cookbookrspec}.each do |key|
8282
expect(keys[key]['determine_cookbook_name']).to eq(true)
8383
end
8484
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2026-present, Meta Platforms, Inc. and affiliates
2+
# All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
require_relative './spec_helper'
16+
17+
describe Bookworm::InferRules::CookbookRspecExists do
18+
it 'returns true when cookbookrspec file exists' do
19+
ast = generate_ast(<<~RUBY)
20+
describe 'my_cookbook::default' do
21+
end
22+
RUBY
23+
rule = described_class.new({ 'ast' => ast })
24+
expect(rule.output).to eq(true)
25+
end
26+
end

0 commit comments

Comments
 (0)