Skip to content
This repository has been archived by the owner on Sep 19, 2020. It is now read-only.

Commit

Permalink
Basic test of the helper
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 committed Mar 21, 2017
1 parent 4ab99c3 commit 27904bb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions spec/foodcritic/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,40 @@ def parse_ast(str)
end
end

describe "#cookbook_base_path" do
# TODO we should do this with a double instead
# but this is the accepted pattern so far so...
def mock_cookbook(metadata_path)
dir = File.dirname(metadata_path)
unless File.directory?(dir)
FileUtils.mkdir_p(dir)
end
File.open(metadata_path, "w") { |file| file.write('name "YOUR_COOKBOOK_NAME"') }
FileUtils.mkdir_p(File.join(dir, 'templates/defaults'))
File.open(File.join(dir, 'templates', 'defaults', 'test.erb'), "w") { |file| file.write('Some erb') }
end

it "returns the cookbook dir when passed the path itself" do
mock_cookbook("/tmp/fc/mock/cb/metadata.rb")
api.cookbook_base_path('/tmp/fc/mock/cb/').must_equal "/tmp/fc/mock/cb"
end

it "returns the cookbook dir when passed a nested directory" do
mock_cookbook("/tmp/fc/mock/cb/metadata.rb")
api.cookbook_base_path('/tmp/fc/mock/cb/templates/defaults/test.erb').must_equal "/tmp/fc/mock/cb"
end

it "returns the cookbook dir when path contains cookbook like names" do
mock_cookbook('/tmp/fc/mock/resources/cb/metadata.rb')
api.cookbook_base_path('/tmp/fc/mock/resources/cb/templates/defaults/test.erb').must_equal "/tmp/fc/mock/resources/cb"
end

it "returns the cookbook dir when path has a metadata.json not metadata.rb" do
mock_cookbook('/tmp/fc/mock/cb/metadata.json')
api.cookbook_base_path('/tmp/fc/mock/cb/templates/defaults/test.erb').must_equal "/tmp/fc/mock/cb"
end
end

describe "#cookbook_name" do
def mock_cookbook_metadata(f)
dir = File.dirname(f)
Expand Down

0 comments on commit 27904bb

Please sign in to comment.