diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-25 00:18:05 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-25 00:18:05 +0300 |
commit | 3d911c6c1f34df47036f1d7e0838e4d5876ee923 (patch) | |
tree | 492dfc9e4a3d3ea6d4749f35a78abafe634889f6 /spec/tooling/lib | |
parent | 0bf82aa5cb3b1ed826dd5c0d46331e17aa60d9e9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/tooling/lib')
-rw-r--r-- | spec/tooling/lib/tooling/find_files_using_feature_flags_spec.rb | 122 | ||||
-rw-r--r-- | spec/tooling/lib/tooling/helpers/predictive_tests_helper_spec.rb (renamed from spec/tooling/lib/tooling/mappings/base_spec.rb) | 13 |
2 files changed, 132 insertions, 3 deletions
diff --git a/spec/tooling/lib/tooling/find_files_using_feature_flags_spec.rb b/spec/tooling/lib/tooling/find_files_using_feature_flags_spec.rb new file mode 100644 index 00000000000..f553d34768f --- /dev/null +++ b/spec/tooling/lib/tooling/find_files_using_feature_flags_spec.rb @@ -0,0 +1,122 @@ +# frozen_string_literal: true + +require 'tempfile' +require_relative '../../../../tooling/lib/tooling/find_files_using_feature_flags' + +RSpec.describe Tooling::FindFilesUsingFeatureFlags, feature_category: :tooling do + attr_accessor :changed_files_file + + let(:changed_files_pathname) { changed_files_file.path } + let(:instance) { described_class.new(changed_files_pathname: changed_files_pathname) } + let(:changed_files_content) { '' } + + around do |example| + self.changed_files_file = Tempfile.new('changed_files_file') + + # See https://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/ + # Tempfile.html#class-Tempfile-label-Explicit+close + begin + example.run + ensure + changed_files_file.close + changed_files_file.unlink + end + end + + before do + allow(File).to receive(:exist?).and_call_original + allow(File).to receive(:read).and_call_original + + File.write(changed_files_pathname, changed_files_content) + end + + describe '#execute' do + subject { instance.execute } + + let(:valid_ff_pathname_1) { 'config/feature_flags/development/my_feature_flag.yml' } + let(:valid_ff_pathname_2) { 'config/feature_flags/development/my_other_feature_flag.yml' } + let(:changed_files_content) { "#{valid_ff_pathname_1} #{valid_ff_pathname_2}" } + let(:ruby_files) { [] } + + before do + allow(File).to receive(:exist?).with(valid_ff_pathname_1).and_return(true) + allow(File).to receive(:exist?).with(valid_ff_pathname_2).and_return(true) + allow(Dir).to receive(:[]).with('**/*.rb').and_return(ruby_files) + end + + context 'when no ruby files are using the modified feature flag' do + let(:ruby_files) { [] } + + it 'does not add anything to the input file' do + expect { subject }.not_to change { File.read(changed_files_pathname) } + end + end + + context 'when some ruby files are using the modified feature flags' do + let(:matching_ruby_file_1) { 'first-ruby-file' } + let(:matching_ruby_file_2) { 'second-ruby-file' } + let(:not_matching_ruby_file) { 'third-ruby-file' } + let(:ruby_files) { [matching_ruby_file_1, matching_ruby_file_2, not_matching_ruby_file] } + + before do + allow(File).to receive(:read).with(matching_ruby_file_1).and_return('my_feature_flag') + allow(File).to receive(:read).with(matching_ruby_file_2).and_return('my_other_feature_flag') + allow(File).to receive(:read).with(not_matching_ruby_file).and_return('other text') + end + + it 'add the matching ruby files to the input file' do + expect { subject }.to change { File.read(changed_files_pathname) } + .from(changed_files_content) + .to("#{changed_files_content} #{matching_ruby_file_1} #{matching_ruby_file_2}") + end + end + end + + describe '#filter_files' do + subject { instance.filter_files } + + let(:changed_files_content) { path_to_file } + + context 'when the file does not exist on disk' do + let(:path_to_file) { "config/other_feature_flags_folder/feature.yml" } + + before do + allow(File).to receive(:exist?).with(path_to_file).and_return(false) + end + + it 'returns an empty array' do + expect(subject).to be_empty + end + end + + context 'when the file exists on disk' do + before do + allow(File).to receive(:exist?).with(path_to_file).and_return(true) + end + + context 'when the file is not in the features folder' do + let(:path_to_file) { "config/other_folder/development/feature.yml" } + + it 'returns an empty array' do + expect(subject).to be_empty + end + end + + context 'when the filename does not have the correct extension' do + let(:path_to_file) { "config/feature_flags/development/feature.rb" } + + it 'returns an empty array' do + expect(subject).to be_empty + end + end + + context 'when the ruby file uses a valid feature flag file' do + let(:path_to_file) { "config/feature_flags/development/feature.yml" } + + it 'returns the file' do + expect(subject).to match_array(path_to_file) + end + end + end + end +end diff --git a/spec/tooling/lib/tooling/mappings/base_spec.rb b/spec/tooling/lib/tooling/helpers/predictive_tests_helper_spec.rb index 935f833fa8b..48a5866ac56 100644 --- a/spec/tooling/lib/tooling/mappings/base_spec.rb +++ b/spec/tooling/lib/tooling/helpers/predictive_tests_helper_spec.rb @@ -1,12 +1,19 @@ # frozen_string_literal: true -require_relative '../../../../../tooling/lib/tooling/mappings/view_to_js_mappings' +require 'tempfile' +require_relative '../../../../../tooling/lib/tooling/helpers/predictive_tests_helper' + +class MockClass # rubocop:disable Gitlab/NamespacedClass + include Tooling::Helpers::PredictiveTestsHelper +end + +RSpec.describe Tooling::Helpers::PredictiveTestsHelper, feature_category: :tooling do + let(:instance) { MockClass.new } -RSpec.describe Tooling::Mappings::Base, feature_category: :tooling do describe '#folders_for_available_editions' do let(:base_folder_path) { 'app/views' } - subject { described_class.new.folders_for_available_editions(base_folder_path) } + subject { instance.folders_for_available_editions(base_folder_path) } context 'when FOSS' do before do |