aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/rubocops/homepage_cop_spec.rb
blob: d394f94a16836b4a14f6f455944cdc5181b02afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require_relative "../../rubocops/homepage_cop"

describe RuboCop::Cop::FormulaAudit::Homepage do
  subject(:cop) { described_class.new }

  context "When auditing homepage" do
    it "When there is no homepage" do
      source = <<~EOS
        class Foo < Formula
          url 'http://example.com/foo-1.0.tgz'
        end
      EOS

      expected_offenses = [{  message: "Formula should have a homepage.",
                              severity: :convention,
                              line: 1,
                              column: 0,
                              source: source }]

      inspect_source(source)

      expected_offenses.zip(cop.offenses).each do |expected, actual|
        expect_offense(expected, actual)
      end
    end

    it "Homepage with ftp" do
      source = <<~EOS
        class Foo < Formula
          homepage "ftp://example.com/foo"
          url "http://example.com/foo-1.0.tgz"
        end
      EOS

      expected_offenses = [{  message: "The homepage should start with http or https (URL is ftp://example.com/foo).",
                              severity: :convention,
                              line: 2,
                              column: 2,
                              source: source }]

      inspect_source(source)

      expected_offenses.zip(cop.offenses).each do |expected, actual|
        expect_offense(expected, actual)
      end
    end

    it "Homepage URLs" do
      formula_homepages = {
        "bar" => "http://www.freedesktop.org/wiki/bar",
        "baz" => "http://www.freedesktop.org/wiki/Software/baz",
        "qux" => "https://code.google.com/p/qux",
        "quux" => "http://github.com/quux",
        "corge" => "http://savannah.nongnu.org/corge",
        "grault" => "http://grault.github.io/",
        "garply" => "http://www.gnome.org/garply",
        "sf1" => "http://foo.sourceforge.net/",
        "sf2" => "http://foo.sourceforge.net",
        "sf3" => "http://foo.sf.net/",
        "sf4" => "http://foo.sourceforge.io/",
        "waldo" => "http://www.gnu.org/waldo",
      }

      formula_homepages.each do |name, homepage|
        source = <<~EOS
          class #{name.capitalize} < Formula
            homepage "#{homepage}"
            url "http://example.com/#{name}-1.0.tgz"
          end
        EOS

        inspect_source(source)
        if homepage =~ %r{http:\/\/www\.freedesktop\.org}
          if homepage =~ /Software/
            expected_offenses = [{  message: "#{homepage} should be styled " \
                                             "`https://wiki.freedesktop.org/www/Software/project_name`",
                                    severity: :convention,
                                    line: 2,
                                    column: 2,
                                    source: source }]
          else
            expected_offenses = [{  message:  "#{homepage} should be styled " \
                                              "`https://wiki.freedesktop.org/project_name`",
                                    severity: :convention,
                                    line: 2,
                                    column: 2,
                                    source: source }]
          end
        elsif homepage =~ %r{https:\/\/code\.google\.com}
          expected_offenses = [{  message:  "#{homepage} should end with a slash",
                                  severity: :convention,
                                  line: 2,
                                  column: 2,
                                  source: source }]
        elsif homepage =~ /foo\.(sf|sourceforge)\.net/
          expected_offenses = [{  message:  "#{homepage} should be `https://foo.sourceforge.io/`",
                                  severity: :convention,
                                  line: 2,
                                  column: 2,
                                  source: source }]
        else
          expected_offenses = [{  message:  "Please use https:// for #{homepage}",
                                  severity: :convention,
                                  line: 2,
                                  column: 2,
                                  source: source }]
        end
        expected_offenses.zip([cop.offenses.last]).each do |expected, actual|
          expect_offense(expected, actual)
        end
      end
    end

    def expect_offense(expected, actual)
      expect(actual.message).to eq(expected[:message])
      expect(actual.severity).to eq(expected[:severity])
      expect(actual.line).to eq(expected[:line])
      expect(actual.column).to eq(expected[:column])
    end
  end
end