From 4d7a98341556614818223060ca94413b17e3d1e6 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Thu, 1 Jun 2017 00:57:24 +0530 Subject: audit: Port audit_checksum method to rubocop and add tests --- .../Homebrew/test/rubocops/checksum_cop_spec.rb | 179 +++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 Library/Homebrew/test/rubocops/checksum_cop_spec.rb (limited to 'Library/Homebrew/test') diff --git a/Library/Homebrew/test/rubocops/checksum_cop_spec.rb b/Library/Homebrew/test/rubocops/checksum_cop_spec.rb new file mode 100644 index 000000000..c2caab73a --- /dev/null +++ b/Library/Homebrew/test/rubocops/checksum_cop_spec.rb @@ -0,0 +1,179 @@ +require "rubocop" +require "rubocop/rspec/support" +require_relative "../../extend/string" +require_relative "../../rubocops/checksum_cop" + +describe RuboCop::Cop::FormulaAudit::Checksum do + subject(:cop) { described_class.new } + + context "When auditing spec checksums" do + it "When the checksum is empty" do + source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "" + end + end + end + EOS + + expected_offenses = [{ message: "stable: sha256 is empty", + severity: :convention, + line: 5, + column: 4, + source: source }, + { message: "stable resource \"foo-package\": sha256 is empty", + severity: :convention, + line: 9, + column: 6, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "When the checksum is not 64 characters" do + source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426c0474cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9ad" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426c047aaa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9" + end + end + end + EOS + + expected_offenses = [{ message: "stable: sha256 should be 64 characters", + severity: :convention, + line: 5, + column: 12, + source: source }, + { message: "stable resource \"foo-package\": sha256 should be 64 characters", + severity: :convention, + line: 9, + column: 14, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "When the checksum has invalid chars" do + source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426c0k7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426x047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9" + end + end + end + EOS + + expected_offenses = [{ message: "stable: sha256 contains invalid characters", + severity: :convention, + line: 5, + column: 4, + source: source }, + { message: "stable resource \"foo-package\": sha256 contains invalid characters", + severity: :convention, + line: 9, + column: 6, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "When the checksum has upper case characters" do + source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0A645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "5cf6e1Ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9" + end + end + end + EOS + + expected_offenses = [{ message: "stable: sha256 should be lowercase", + severity: :convention, + line: 5, + column: 21, + source: source }, + { message: "stable resource \"foo-package\": sha256 should be lowercase", + severity: :convention, + line: 9, + column: 20, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "When auditing stable blocks outside spec blocks" do + source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + resource "foo-outside" do + url "https://github.com/foo-lang/foo-outside/archive/0.18.0.tar.gz" + sha256 "A4cc7cd3f7d1605ffa1ac5755cf6e1ae0a645b426b047a6a39a8b2268ddc7ea9" + end + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9" + end + end + end + EOS + + expected_offenses = [{ message: "stable resource \"foo-outside\": sha256 should be lowercase", + severity: :convention, + line: 5, + column: 12, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + end +end -- cgit v1.2.3 From 77da75e7d6ecc2ca749eb939d8e24ad9e3dfc5e1 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Wed, 14 Jun 2017 15:37:37 +0530 Subject: Simplify Checksum cop by auditing all checksums --- .../Homebrew/test/rubocops/checksum_cop_spec.rb | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'Library/Homebrew/test') diff --git a/Library/Homebrew/test/rubocops/checksum_cop_spec.rb b/Library/Homebrew/test/rubocops/checksum_cop_spec.rb index c2caab73a..633f3117a 100644 --- a/Library/Homebrew/test/rubocops/checksum_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/checksum_cop_spec.rb @@ -23,15 +23,15 @@ describe RuboCop::Cop::FormulaAudit::Checksum do end EOS - expected_offenses = [{ message: "stable: sha256 is empty", + expected_offenses = [{ message: "sha256 is empty", severity: :convention, line: 5, - column: 4, + column: 12, source: source }, - { message: "stable resource \"foo-package\": sha256 is empty", + { message: "sha256 is empty", severity: :convention, line: 9, - column: 6, + column: 14, source: source }] inspect_source(cop, source) @@ -57,12 +57,12 @@ describe RuboCop::Cop::FormulaAudit::Checksum do end EOS - expected_offenses = [{ message: "stable: sha256 should be 64 characters", + expected_offenses = [{ message: "sha256 should be 64 characters", severity: :convention, line: 5, column: 12, source: source }, - { message: "stable resource \"foo-package\": sha256 should be 64 characters", + { message: "sha256 should be 64 characters", severity: :convention, line: 9, column: 14, @@ -91,15 +91,15 @@ describe RuboCop::Cop::FormulaAudit::Checksum do end EOS - expected_offenses = [{ message: "stable: sha256 contains invalid characters", + expected_offenses = [{ message: "sha256 contains invalid characters", severity: :convention, line: 5, - column: 4, + column: 31, source: source }, - { message: "stable resource \"foo-package\": sha256 contains invalid characters", + { message: "sha256 contains invalid characters", severity: :convention, line: 9, - column: 6, + column: 31, source: source }] inspect_source(cop, source) @@ -125,12 +125,12 @@ describe RuboCop::Cop::FormulaAudit::Checksum do end EOS - expected_offenses = [{ message: "stable: sha256 should be lowercase", + expected_offenses = [{ message: "sha256 should be lowercase", severity: :convention, line: 5, column: 21, source: source }, - { message: "stable resource \"foo-package\": sha256 should be lowercase", + { message: "sha256 should be lowercase", severity: :convention, line: 9, column: 20, @@ -163,7 +163,7 @@ describe RuboCop::Cop::FormulaAudit::Checksum do end EOS - expected_offenses = [{ message: "stable resource \"foo-outside\": sha256 should be lowercase", + expected_offenses = [{ message: "sha256 should be lowercase", severity: :convention, line: 5, column: 12, -- cgit v1.2.3 From 0e1c88e7aefb93a8e2cc927fa9a4e903ac015c57 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Fri, 16 Jun 2017 19:44:14 +0530 Subject: Refactor Checksum cop to add autocorrect method --- .../Homebrew/test/rubocops/checksum_cop_spec.rb | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'Library/Homebrew/test') diff --git a/Library/Homebrew/test/rubocops/checksum_cop_spec.rb b/Library/Homebrew/test/rubocops/checksum_cop_spec.rb index 633f3117a..644152c32 100644 --- a/Library/Homebrew/test/rubocops/checksum_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/checksum_cop_spec.rb @@ -108,7 +108,13 @@ describe RuboCop::Cop::FormulaAudit::Checksum do expect_offense(expected, actual) end end + end +end +describe RuboCop::Cop::FormulaAudit::ChecksumCase do + subject(:cop) { described_class.new } + + context "When auditing spec checksums" do it "When the checksum has upper case characters" do source = <<-EOS.undent class Foo < Formula @@ -176,4 +182,41 @@ describe RuboCop::Cop::FormulaAudit::Checksum do end end end + + context "When auditing checksum with autocorrect" do + it "When there is uppercase sha256" do + source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0A645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "5cf6e1Ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9" + end + end + end + EOS + + corrected_source = <<-EOS.undent + class Foo < Formula + url 'http://example.com/foo-1.0.tgz' + stable do + url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a" + + resource "foo-package" do + url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz" + sha256 "5cf6e1ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9" + end + end + end + EOS + + new_source = autocorrect_source(cop, source) + expect(new_source).to eq(corrected_source) + end + end end -- cgit v1.2.3