File: delete_old_feature_flags_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (95 lines) | stat: -rw-r--r-- 3,055 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
# frozen_string_literal: true

require 'spec_helper'
require './keeps/delete_old_feature_flags'

RSpec.describe Keeps::DeleteOldFeatureFlags, feature_category: :tooling do
  let(:groups) do
    {
      foo: {
        label: 'group::foo',
        backend_engineers: ['@john_doe']
      }
    }
  end

  let(:feature_flag_name) { 'feature_flag_name' }
  let(:tmp_dir) { Pathname(Dir.mktmpdir) }
  let(:feature_flag_milestone) { '15.8' }
  let(:feature_flag_file) do
    file_path = tmp_dir.join('feature_flag.yml')

    File.write(file_path, {
      name: feature_flag_name,
      milestone: feature_flag_milestone,
      group: groups.dig(:foo, :label)
    }.to_yaml)

    file_path.to_s
  end

  let(:feature_flag_patch_path) { feature_flag_file.sub(/.yml$/, '.patch') }

  let(:milestones_helper) { instance_double(Keeps::Helpers::Milestones) }

  subject(:keep) { described_class.new }

  before do
    stub_request(:get, Keeps::Helpers::Groups::GROUPS_JSON_URL).to_return(status: 200, body: groups.to_json)

    allow(keep).to receive(:all_feature_flag_files).and_return([feature_flag_file])
    allow(keep).to receive(:milestones_helper).and_return(milestones_helper)

    allow(milestones_helper)
      .to receive(:before_cuttoff?).with(milestone: feature_flag_milestone, milestones_ago: 12)
      .and_return(true)

    File.write(feature_flag_patch_path, <<~DIFF)
    diff --git a/foobar.txt b/foobar.txt
    index 2ef267e25bd6..0fecdb8e98f3 100644
    --- a/foobar.txt
    +++ b/foobar.txt
    @@ -1 +1 @@
    -some content
    +some content updated
    DIFF
  end

  after do
    FileUtils.rm_rf(tmp_dir)
  end

  describe '#each_change' do
    let(:expected_change) { instance_double(Gitlab::Housekeeper::Change) }

    it 'returns a Gitlab::Housekeeper::Change', :aggregate_failures do
      expect(Gitlab::Housekeeper::Shell).to receive(:execute).with(
        'git', 'grep', '--heading', '--line-number', '--break',
        feature_flag_name, '--', ':^locale/', ':^db/structure.sql'
      )

      expect(FileUtils).to receive(:rm).with(feature_flag_file)
      expect(Gitlab::Housekeeper::Shell).to receive(:execute).with(
        'git', 'apply', feature_flag_patch_path
      )
      expect(FileUtils).to receive(:rm).with(feature_flag_patch_path)

      actual_changes = keep.each_change(&:itself)

      expect(actual_changes.size).to eq(1)

      actual_change = actual_changes.first
      expect(actual_change).to be_a(Gitlab::Housekeeper::Change)
      expect(actual_change.changelog_type).to eq('removed')
      expect(actual_change.title).to eq("Delete the `#{feature_flag_name}` feature flag")
      expect(actual_change.identifiers).to eq([described_class.name.demodulize, feature_flag_name])
      expect(actual_change.changed_files).to contain_exactly(
        feature_flag_file,
        feature_flag_patch_path,
        'foobar.txt'
      )
      expect(actual_change.reviewers).to eq(['@john_doe'])
      expect(actual_change.labels).to eq(['maintenance::removal', 'feature flag', groups.dig(:foo, :label)])
    end
  end
end