Welcome to mirror list, hosted at ThFree Co, Russian Federation.

last_keyword_argument_spec.rb « lint « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f942390569bf6615657cb2f363e8b8bc1124383a (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
122
123
124
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/lint/last_keyword_argument'

RSpec.describe RuboCop::Cop::Lint::LastKeywordArgument, type: :rubocop do
  include CopHelper

  subject(:cop) { described_class.new }

  before do
    described_class.instance_variable_set(:@keyword_warnings, nil)
  end

  context 'deprecation files does not exist' do
    before do
      allow(Dir).to receive(:glob).and_return([])
      allow(File).to receive(:exist?).and_return(false)
    end

    it 'does not register an offense' do
      expect_no_offenses(<<~SOURCE)
        users.call(params)
      SOURCE
    end
  end

  context 'deprecation files does exist' do
    let(:create_spec_yaml) do
      <<~YAML
      ---
      test_mutations/boards/lists/create#resolve_with_proper_permissions_backlog_list_creates_one_and_only_one_backlog:
      - |
        DEPRECATION WARNING: /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/batch-loader-1.4.0/lib/batch_loader/graphql.rb:38: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
        /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/batch-loader-1.4.0/lib/batch_loader.rb:26: warning: The called method `batch' is defined here
      test_mutations/boards/lists/create#ready?_raises_an_error_if_required_arguments_are_missing:
      - |
        DEPRECATION WARNING: /Users/tkuah/code/ee-gdk/gitlab/create_service.rb:1: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
        /Users/tkuah/code/ee-gdk/gitlab/user.rb:17: warning: The called method `call' is defined here
      YAML
    end

    let(:projects_spec_yaml) do
      <<~YAML
      ---
      test_api/projects_get_/projects_when_unauthenticated_behaves_like_projects_response_returns_an_array_of_projects:
      - |
        DEPRECATION WARNING: /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/state_machines-activerecord-0.6.0/lib/state_machines/integrations/active_record.rb:511: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
        /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/suppressor.rb:43: warning: The called method `save' is defined here
      - |
        DEPRECATION WARNING: /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/builder.rb:158: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
        /Users/tkuah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/grape-1.4.0/lib/grape/middleware/error.rb:30: warning: The called method `initialize' is defined here
      YAML
    end

    before do
      allow(Dir).to receive(:glob).and_return(['deprecations/service/create_spec.yml', 'deprecations/api/projects_spec.yml'])
      allow(File).to receive(:read).and_return(create_spec_yaml, projects_spec_yaml)
    end

    it 'registers an offense' do
      expect_offense(<<~SOURCE, 'create_service.rb')
        users.call(params)
                   ^^^^^^ Using the last argument as keyword parameters is deprecated
      SOURCE

      expect_correction(<<~SOURCE)
        users.call(**params)
      SOURCE
    end

    it 'registers an offense and corrects by converting hash to kwarg' do
      expect_offense(<<~SOURCE, 'create_service.rb')
        users.call(id, { a: :b, c: :d })
                       ^^^^^^^^^^^^^^^^ Using the last argument as keyword parameters is deprecated
      SOURCE

      expect_correction(<<~SOURCE)
        users.call(id, a: :b, c: :d)
      SOURCE
    end

    it 'registers an offense and corrects by converting splat to double splat' do
      expect_offense(<<~SOURCE, 'create_service.rb')
        users.call(id, *params)
                       ^^^^^^^ Using the last argument as keyword parameters is deprecated
      SOURCE

      expect_correction(<<~SOURCE)
        users.call(id, **params)
      SOURCE
    end

    it 'does not register an offense if already a kwarg', :aggregate_failures do
      expect_no_offenses(<<~SOURCE, 'create_service.rb')
        users.call(**params)
      SOURCE

      expect_no_offenses(<<~SOURCE, 'create_service.rb')
        users.call(id, a: :b, c: :d)
      SOURCE
    end

    it 'does not register an offense if the method name does not match' do
      expect_no_offenses(<<~SOURCE, 'create_service.rb')
        users.process(params)
      SOURCE
    end

    it 'does not register an offense if the line number does not match' do
      expect_no_offenses(<<~SOURCE, 'create_service.rb')
        users.process
        users.call(params)
      SOURCE
    end

    it 'does not register an offense if the filename does not match' do
      expect_no_offenses(<<~SOURCE, 'update_service.rb')
        users.call(params)
      SOURCE
    end
  end
end