require "dev-cmd/audit" require "formulary" module Count def self.increment @count ||= 0 @count += 1 end end module Homebrew describe FormulaText do alias_matcher :have_data, :be_data alias_matcher :have_end, :be_end alias_matcher :have_trailing_newline, :be_trailing_newline let(:dir) { mktmpdir } def formula_text(name, body = nil, options = {}) path = dir/"#{name}.rb" path.write <<~RUBY class #{Formulary.class_s(name)} < Formula #{body} end #{options[:patch]} RUBY described_class.new(path) end specify "simple valid Formula" do ft = formula_text "valid", <<~RUBY url "https://www.example.com/valid-1.0.tar.gz" RUBY expect(ft).not_to have_data expect(ft).not_to have_end expect(ft).to have_trailing_newline expect(ft =~ /\burl\b/).to be_truthy expect(ft.line_number(/desc/)).to be nil expect(ft.line_number(/\burl\b/)).to eq(2) expect(ft).to include("Valid") end specify "#trailing_newline?" do ft = formula_text "newline" expect(ft).to have_trailing_newline end specify "#data?" do ft = formula_text "data", <<~RUBY patch :DATA RUBY expect(ft).to have_data end specify "#end?" do ft = formula_text "end", "", patch: "__END__\na patch here" expect(ft).to have_end expect(ft.without_patch).to eq("class End < Formula\n \nend") end end describe FormulaAuditor do def formula_auditor(name, text, options = {}) path = Pathname.new "#{dir}/#{name}.rb" path.open("w") do |f| f.write text end described_class.new(Formulary.factory(path), options) end let(:dir) { mktmpdir } describe "#problems" do it "is empty by default" do fa = formula_auditor "foo", <<~RUBY class Foo < Formula url "https://example.com/foo-1.0.tgz" end RUBY expect(fa.problems).to be_empty end end describe "#audit_file" do specify "file permissions" do allow(File).to receive(:umask).and_return(022) fa = formula_auditor "foo", <<~RUBY class Foo < Formula url "https://example.com/foo-1.0.tgz" end RUBY path = fa.formula.path path.chmod 0400 fa.audit_file expect(fa.problems) .to eq(["Incorrect file permissions (400): chmod 644 #{path}"]) end specify "DATA but no __END__" do fa = formula_auditor "foo", <<~RUBY class Foo < Formula url "https://example.com/foo-1.0.tgz" patch :DATA end RUBY fa.audit_file expect(fa.problems).to eq(["'DATA' was found, but no '__END__'"]) end specify "__END__ but no DATA" do fa = formula_auditor "foo", <<~RUBY class Foo < Formula url "https://example.com/foo-1.0.tgz" end __END__ a patch goes here RUBY fa.audit_file expect(fa.problems).to eq(["'__END__' was found, but 'DATA' is not used"]) end specify "no trailing newline" do fa = formula_auditor "foo", 'class Foo