2021-10-18 20:10:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
2022-08-18 14:10:07 -04:00
|
|
|
require 'tmpdir'
|
2021-10-18 20:10:29 -04:00
|
|
|
|
|
|
|
load File.expand_path('../../scripts/changed-feature-flags', __dir__)
|
|
|
|
|
|
|
|
RSpec.describe 'scripts/changed-feature-flags' do
|
|
|
|
describe GetFeatureFlagsFromFiles do
|
2022-05-15 23:07:37 -04:00
|
|
|
let!(:feature_flag_definition1) do
|
|
|
|
file = File.open(File.join(ff_dir, "#{file_name1}.yml"), 'w+')
|
2021-10-18 20:10:29 -04:00
|
|
|
file.write(<<~YAML)
|
|
|
|
---
|
|
|
|
name: foo_flag
|
|
|
|
default_enabled: true
|
|
|
|
YAML
|
|
|
|
file.rewind
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
2022-05-15 23:07:37 -04:00
|
|
|
let!(:feature_flag_definition2) do
|
|
|
|
file = File.open(File.join(ff_dir, "#{file_name2}.yml"), 'w+')
|
2021-10-18 20:10:29 -04:00
|
|
|
file.write(<<~YAML)
|
|
|
|
---
|
|
|
|
name: bar_flag
|
|
|
|
default_enabled: false
|
|
|
|
YAML
|
|
|
|
file.rewind
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
2022-05-15 23:07:37 -04:00
|
|
|
let!(:feature_flag_diff1) do
|
|
|
|
FileUtils.mkdir_p(File.join(diffs_dir, ff_sub_dir))
|
|
|
|
file = File.open(File.join(diffs_dir, ff_sub_dir, "#{file_name1}.yml.diff"), 'w+')
|
|
|
|
file.write(<<~YAML)
|
|
|
|
@@ -5,4 +5,4 @@
|
|
|
|
name: foo_flag
|
|
|
|
-default_enabled: false
|
|
|
|
+default_enabled: true
|
|
|
|
YAML
|
|
|
|
file.rewind
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:feature_flag_diff2) do
|
|
|
|
FileUtils.mkdir_p(File.join(diffs_dir, ff_sub_dir))
|
|
|
|
file = File.open(File.join(diffs_dir, ff_sub_dir, "#{file_name2}.yml.diff"), 'w+')
|
|
|
|
file.write(<<~YAML)
|
|
|
|
@@ -0,0 +0,0 @@
|
|
|
|
name: bar_flag
|
|
|
|
-default_enabled: true
|
|
|
|
+default_enabled: false
|
|
|
|
YAML
|
|
|
|
file.rewind
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:deleted_feature_flag_diff) do
|
|
|
|
FileUtils.mkdir_p(File.join(diffs_dir, ff_sub_dir))
|
|
|
|
file = File.open(File.join(diffs_dir, ff_sub_dir, "foobar_ff_#{SecureRandom.hex(8)}.yml.deleted.diff"), 'w+')
|
|
|
|
file.write(<<~YAML)
|
|
|
|
@@ -0,0 +0,0 @@
|
|
|
|
-name: foobar_flag
|
|
|
|
-default_enabled: true
|
|
|
|
YAML
|
|
|
|
file.rewind
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Dir).to receive(:pwd).and_return(Dir.tmpdir)
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:10:29 -04:00
|
|
|
after do
|
2022-05-15 23:07:37 -04:00
|
|
|
feature_flag_definition1.close
|
|
|
|
feature_flag_definition2.close
|
|
|
|
feature_flag_diff1.close
|
|
|
|
feature_flag_diff2.close
|
|
|
|
deleted_feature_flag_diff.close
|
|
|
|
FileUtils.rm_r(ff_dir)
|
|
|
|
FileUtils.rm_r(diffs_dir)
|
2021-10-18 20:10:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.extracted_flags' do
|
2022-08-17 05:11:44 -04:00
|
|
|
let(:file_name1) { "foo_ff_#{SecureRandom.hex(8)}" }
|
|
|
|
let(:file_name2) { "bar_ff_#{SecureRandom.hex(8)}" }
|
2022-05-15 23:07:37 -04:00
|
|
|
let(:ff_dir) { FileUtils.mkdir_p(File.join(Dir.tmpdir, ff_sub_dir)) }
|
|
|
|
let(:diffs_dir) { FileUtils.mkdir_p(File.join(Dir.tmpdir, 'diffs')).first }
|
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
shared_examples 'extract feature flags' do
|
|
|
|
it 'returns feature flags on their own' do
|
2022-05-15 23:07:37 -04:00
|
|
|
subject = described_class.new({ files: diffs_dir })
|
2021-11-16 22:09:55 -05:00
|
|
|
|
2022-05-15 23:07:37 -04:00
|
|
|
expect(subject.extracted_flags.split(',')).to include('foo_flag', 'bar_flag')
|
2021-11-16 22:09:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns feature flags and their state as enabled' do
|
2022-05-15 23:07:37 -04:00
|
|
|
subject = described_class.new({ files: diffs_dir, state: 'enabled' })
|
2021-10-18 20:10:29 -04:00
|
|
|
|
2022-05-15 23:07:37 -04:00
|
|
|
expect(subject.extracted_flags.split(',')).to include('foo_flag=enabled', 'bar_flag=enabled')
|
2021-11-16 22:09:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns feature flags and their state as disabled' do
|
2022-05-15 23:07:37 -04:00
|
|
|
subject = described_class.new({ files: diffs_dir, state: 'disabled' })
|
|
|
|
|
|
|
|
expect(subject.extracted_flags.split(',')).to include('foo_flag=disabled', 'bar_flag=disabled')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return feature flags when there are mixed deleted and non-deleted definition files' do
|
|
|
|
subject = described_class.new({ files: diffs_dir, state: 'deleted' })
|
2021-11-16 22:09:55 -05:00
|
|
|
|
2022-05-15 23:07:37 -04:00
|
|
|
expect(subject.extracted_flags).to eq('')
|
2021-11-16 22:09:55 -05:00
|
|
|
end
|
2021-10-18 20:10:29 -04:00
|
|
|
end
|
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
context 'with definition files in the development directory' do
|
2022-05-15 23:07:37 -04:00
|
|
|
let(:ff_sub_dir) { %w[feature_flags development] }
|
2021-10-18 20:10:29 -04:00
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
it_behaves_like 'extract feature flags'
|
2021-10-18 20:10:29 -04:00
|
|
|
end
|
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
context 'with definition files in the ops directory' do
|
2022-05-15 23:07:37 -04:00
|
|
|
let(:ff_sub_dir) { %w[feature_flags ops] }
|
2021-10-18 20:10:29 -04:00
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
it_behaves_like 'extract feature flags'
|
2021-10-18 20:10:29 -04:00
|
|
|
end
|
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
context 'with definition files in the experiment directory' do
|
2022-05-15 23:07:37 -04:00
|
|
|
let(:ff_sub_dir) { %w[feature_flags experiment] }
|
2021-11-16 22:09:55 -05:00
|
|
|
|
|
|
|
it 'ignores the files' do
|
2022-05-15 23:07:37 -04:00
|
|
|
subject = described_class.new({ files: diffs_dir })
|
|
|
|
|
|
|
|
expect(subject.extracted_flags).to eq('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with only deleted definition files' do
|
|
|
|
let(:ff_sub_dir) { %w[feature_flags development] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
feature_flag_diff1.close
|
|
|
|
feature_flag_diff2.close
|
|
|
|
FileUtils.rm_r(feature_flag_diff1)
|
|
|
|
FileUtils.rm_r(feature_flag_diff2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns feature flags and their state as deleted' do
|
|
|
|
subject = described_class.new({ files: diffs_dir, state: 'deleted' })
|
|
|
|
|
|
|
|
expect(subject.extracted_flags).to eq('foobar_flag=deleted')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return feature flags when the desired state is enabled' do
|
|
|
|
subject = described_class.new({ files: diffs_dir, state: 'enabled' })
|
|
|
|
|
|
|
|
expect(subject.extracted_flags).to eq('')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return feature flags when the desired state is disabled' do
|
|
|
|
subject = described_class.new({ files: diffs_dir, state: 'disabled' })
|
2021-10-18 20:10:29 -04:00
|
|
|
|
2021-11-16 22:09:55 -05:00
|
|
|
expect(subject.extracted_flags).to eq('')
|
|
|
|
end
|
2021-10-18 20:10:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|