File: gemspec_git_spec.rb

package info (click to toggle)
ruby-rubocop-packaging 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 220 kB
  • sloc: ruby: 739; sh: 4; makefile: 4
file content (99 lines) | stat: -rw-r--r-- 3,151 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Packaging::GemspecGit do
  subject(:cop) { described_class.new(config) }

  let(:config) { RuboCop::Config.new }

  let(:message) { RuboCop::Cop::Packaging::GemspecGit::MSG }

  it "registers an offense when using `git` for :files=" do
    expect_offense(<<~RUBY)
      Gem::Specification.new do |spec|
        spec.files = `git ls-files`.split("\\n")
                     ^^^^^^^^^^^^^^ #{message}
      end
    RUBY
  end

  it "registers an offense when using `git ls-files filename` for :files=" do
    expect_offense(<<~RUBY)
      Gem::Specification.new do |s|
        s.files         = `git ls-files LICENSE docs lib`.split("\\n")
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
      end
    RUBY
  end

  it "registers an offense when using `git` for :files= but differently" do
    expect_offense(<<~RUBY)
      Gem::Specification.new do |spec|
        spec.files = `git ls-files`.split + %w(
                     ^^^^^^^^^^^^^^ #{message}
                        lib/parser/lexer.rb
                        lib/parser/ruby18.rb
                        lib/parser/ruby19.rb
                        lib/parser/ruby20.rb
                        lib/parser/ruby21.rb
                        lib/parser/ruby22.rb
                        lib/parser/ruby23.rb
                        lib/parser/ruby24.rb
                        lib/parser/ruby25.rb
                        lib/parser/ruby26.rb
                        lib/parser/ruby27.rb
                        lib/parser/ruby28.rb
                        lib/parser/macruby.rb
                        lib/parser/rubymotion.rb
                     )
      end
    RUBY
  end

  it "registers an offense when using `git` for :files= with more stuff" do
    expect_offense(<<~RUBY)
      Gem::Specification.new do |spec|
        spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
          `git ls-files -z`.split("\\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
          ^^^^^^^^^^^^^^^^^ #{message}
        end
      end
    RUBY
  end

  it "registers an offense when using `git` for :executables=" do
    expect_offense(<<~RUBY)
      Gem::Specification.new do |spec|
        spec.executables = `git ls-files`.split("\\n")
                           ^^^^^^^^^^^^^^ #{message}
      end
    RUBY
  end

  it "does not register an offense when the file just has comments" do
    expect_no_offenses(<<~RUBY)
      # Dummy comments.
      # Blank file.
      # Copyright 2020, Utkarsh Gupta <utkarsh@debian.org>
      # This is an important test.
    RUBY
  end

  it "does not register an offense when the file is empty/blank" do
    expect_no_offenses(<<~RUBY)
    RUBY
  end

  it "does not register an offense not in a specification" do
    expect_no_offenses(<<~RUBY)
      spec.files = `git ls-files`
    RUBY
  end

  it "does not register an offense when not using `git` for :files=" do
    expect_no_offenses(<<~RUBY)
      Gem::Specification.new do |spec|
        spec.files = Dir["docs/**/*", "lib/**/*", "LICENSE"].reject { |f| File.directory?(f) }.sort
      end
    RUBY
  end
end