Commit a1f50b47 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'jf_stop_overwriting_stdout' into 'master'

Add silence_stdout option to explicitly redirect stdout in rake task specs [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!62536
parents 2b40566e f651787a
......@@ -11,19 +11,21 @@ in lieu of the standard Spec helper. Instead of `require 'spec_helper'`, use
`require 'rake_helper'`. The helper includes `spec_helper` for you, and configures
a few other things to make testing Rake tasks easier.
At a minimum, requiring the Rake helper redirects `stdout`, include the
runtime task helpers, and include the `RakeHelpers` Spec support module.
At a minimum, requiring the Rake helper includes the runtime task helpers, and
includes the `RakeHelpers` Spec support module.
The `RakeHelpers` module exposes a `run_rake_task(<task>)` method to make
executing tasks simple. See `spec/support/helpers/rake_helpers.rb` for all available
methods.
`$stdout` can be redirected by adding `:silence_stdout`.
Example:
```ruby
require 'rake_helper'
describe 'gitlab:shell rake tasks' do
describe 'gitlab:shell rake tasks', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/shell'
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::Geo::AuthorizedKeysCheck do
RSpec.describe SystemCheck::Geo::AuthorizedKeysCheck, :silence_stdout do
describe '#multi_check' do
subject { described_class.new }
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::Geo::AuthorizedKeysFlagCheck do
before do
silence_output
end
RSpec.describe SystemCheck::Geo::AuthorizedKeysFlagCheck, :silence_stdout do
describe '#check?' do
it 'fails when write to authorized_keys still enabled' do
stub_application_setting(authorized_keys_enabled: true)
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::Geo::CurrentNodeCheck, :geo do
RSpec.describe SystemCheck::Geo::CurrentNodeCheck, :geo, :silence_stdout do
describe '#check?' do
context 'when the current machine has a matching GeoNode' do
it 'returns true' do
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck do
before do
silence_output
end
RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do
subject { described_class.new }
describe '#multi_check' do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'geo rake tasks', :geo do
RSpec.describe 'geo rake tasks', :geo, :silence_stdout do
include ::EE::GeoHelpers
before do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'EE check.rake' do
RSpec.describe 'EE check.rake', :silence_stdout do
before do
Rake.application.rake_require 'ee/lib/tasks/gitlab/check', [Rails.root.to_s]
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:elastic namespace rake tasks', :elastic do
RSpec.describe 'gitlab:elastic namespace rake tasks', :elastic, :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/elastic'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:geo rake tasks', :geo do
RSpec.describe 'gitlab:geo rake tasks', :geo, :silence_stdout do
include ::EE::GeoHelpers
before do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:license namespace rake tasks' do
RSpec.describe 'gitlab:license namespace rake tasks', :silence_stdout do
let(:default_license_path) { Settings.source.dirname + 'Gitlab.gitlab-license' }
before do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:seed:group_seed rake task' do
RSpec.describe 'gitlab:seed:group_seed rake task', :silence_stdout do
let(:username) { 'group_seed' }
let!(:user) { create(:user, username: username) }
let(:task_params) { [2, username] }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:rake tasks' do
RSpec.describe 'gitlab:rake tasks', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/spdx'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:uploads:migrate and migrate_to_local rake tasks' do
RSpec.describe 'gitlab:uploads:migrate and migrate_to_local rake tasks', :silence_stdout do
let(:model_class) { nil }
let(:uploader_class) { nil }
let(:mounted_as) { nil }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.configure do |config|
# Redirect stdout so specs don't have so much noise
config.before(:all) do
$stdout = StringIO.new
end
# Reset stdout
config.after(:all) do
$stdout = STDOUT
end
end
# frozen_string_literal: true
require 'generator_helper'
require 'spec_helper'
RSpec.describe Gitlab::SnowplowEventDefinitionGenerator do
RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
let(:ce_temp_dir) { Dir.mktmpdir }
let(:ee_temp_dir) { Dir.mktmpdir }
let(:generator_options) { { 'category' => 'Groups::EmailCampaignsController', 'action' => 'click' } }
......
# frozen_string_literal: true
require 'generator_helper'
require 'spec_helper'
RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator do
RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator, :silence_stdout do
include UsageDataHelpers
let(:category) { 'test_category' }
......
# frozen_string_literal: true
require 'generator_helper'
require 'spec_helper'
RSpec.describe Gitlab::UsageMetricDefinitionGenerator do
RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
include UsageDataHelpers
let(:key_path) { 'counts_weekly.test_metric' }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe Gitlab::ImportExport::Project::ExportTask do
RSpec.describe Gitlab::ImportExport::Project::ExportTask, :silence_stdout do
let_it_be(:username) { 'root' }
let(:namespace_path) { username }
let_it_be(:user) { create(:user, username: username) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe Gitlab::ImportExport::Project::ImportTask, :request_store do
RSpec.describe Gitlab::ImportExport::Project::ImportTask, :request_store, :silence_stdout do
let(:username) { 'root' }
let(:namespace_path) { username }
let!(:user) { create(:user, username: username) }
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::App::HashedStorageAllProjectsCheck do
before do
silence_output
end
RSpec.describe SystemCheck::App::HashedStorageAllProjectsCheck, :silence_stdout do
describe '#check?' do
it 'fails when at least one project is in legacy storage' do
create(:project, :legacy_storage)
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::App::HashedStorageEnabledCheck do
before do
silence_output
end
RSpec.describe SystemCheck::App::HashedStorageEnabledCheck, :silence_stdout do
describe '#check?' do
it 'fails when hashed storage is disabled' do
stub_application_setting(hashed_storage_enabled: false)
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::Orphans::NamespaceCheck do
RSpec.describe SystemCheck::Orphans::NamespaceCheck, :silence_stdout do
let(:storages) { Gitlab.config.repositories.storages.reject { |key, _| key.eql? 'broken' } }
before do
allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
allow(subject).to receive(:fetch_disk_namespaces).and_return(disk_namespaces)
silence_output
end
describe '#multi_check' do
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::Orphans::RepositoryCheck do
RSpec.describe SystemCheck::Orphans::RepositoryCheck, :silence_stdout do
let(:storages) { Gitlab.config.repositories.storages.reject { |key, _| key.eql? 'broken' } }
before do
allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
allow(subject).to receive(:fetch_disk_namespaces).and_return(disk_namespaces)
allow(subject).to receive(:fetch_disk_repositories).and_return(disk_repositories)
# silence_output
end
describe '#multi_check' do
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck::SimpleExecutor do
RSpec.describe SystemCheck::SimpleExecutor, :silence_stdout do
before do
stub_const('SimpleCheck', Class.new(SystemCheck::BaseCheck))
stub_const('OtherCheck', Class.new(SystemCheck::BaseCheck))
......@@ -154,8 +153,6 @@ RSpec.describe SystemCheck::SimpleExecutor do
describe '#execute' do
before do
silence_output
subject << SimpleCheck
subject << OtherCheck
end
......
# frozen_string_literal: true
require 'spec_helper'
require 'rake_helper'
RSpec.describe SystemCheck do
RSpec.describe SystemCheck, :silence_stdout do
before do
stub_const('SimpleCheck', Class.new(SystemCheck::BaseCheck))
stub_const('OtherCheck', Class.new(SystemCheck::BaseCheck))
......@@ -19,8 +18,6 @@ RSpec.describe SystemCheck do
false
end
end
silence_output
end
describe '.run' do
......
......@@ -6,18 +6,12 @@ require 'rake'
RSpec.configure do |config|
config.include RakeHelpers
# Redirect stdout so specs don't have so much noise
config.before(:all) do
$stdout = StringIO.new
Rake.application.rake_require 'tasks/gitlab/helpers'
Rake::Task.define_task :environment
end
# Reset stdout
config.after(:all) do
$stdout = STDOUT
delete_from_all_tables!
end
end
......@@ -403,6 +403,15 @@ RSpec.configure do |config|
end
end
# Allows stdout to be redirected to reduce noise
config.before(:each, :silence_stdout) do
$stdout = StringIO.new
end
config.after(:each, :silence_stdout) do
$stdout = STDOUT
end
config.disable_monkey_patching!
end
......
......@@ -10,11 +10,6 @@ module RakeHelpers
allow(main_object).to receive(:warn_user_is_not_gitlab)
end
def silence_output
allow(main_object).to receive(:puts)
allow(main_object).to receive(:print)
end
def silence_progress_bar
allow_any_instance_of(ProgressBar::Output).to receive(:stream).and_return(double.as_null_object)
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'admin mode on tasks' do
RSpec.describe 'admin mode on tasks', :silence_stdout do
before do
allow(::Gitlab::Runtime).to receive(:test_suite?).and_return(false)
allow(::Gitlab::Runtime).to receive(:rake?).and_return(true)
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'clearing redis cache', :clean_gitlab_redis_cache do
RSpec.describe 'clearing redis cache', :clean_gitlab_redis_cache, :silence_stdout do
before do
Rake.application.rake_require 'tasks/cache'
end
......
......@@ -3,7 +3,7 @@
require 'rake_helper'
Rake.application.rake_require 'tasks/config_lint'
RSpec.describe ConfigLint do
RSpec.describe ConfigLint, :silence_stdout do
let(:files) { ['lib/support/fake.sh'] }
it 'errors out if any bash scripts have errors' do
......@@ -15,7 +15,7 @@ RSpec.describe ConfigLint do
end
end
RSpec.describe 'config_lint rake task' do
RSpec.describe 'config_lint rake task', :silence_stdout do
before do
# Prevent `system` from actually being called
allow(Kernel).to receive(:system).and_return(true)
......
# frozen_string_literal: true
require "rake_helper"
require 'rake_helper'
RSpec.describe 'gettext' do
RSpec.describe 'gettext', :silence_stdout do
let(:locale_path) { Rails.root.join('tmp/gettext_spec') }
let(:pot_file_path) { File.join(locale_path, 'gitlab.pot') }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:artifacts rake tasks' do
RSpec.describe 'gitlab:artifacts rake tasks', :silence_stdout do
describe 'check' do
let!(:artifact) { create(:ci_job_artifact, :archive, :correct_checksum) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:artifacts namespace rake task' do
RSpec.describe 'gitlab:artifacts namespace rake task', :silence_stdout do
before(:context) do
Rake.application.rake_require 'tasks/gitlab/artifacts/migrate'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'check.rake' do
RSpec.describe 'check.rake', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/check'
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:cleanup rake tasks' do
RSpec.describe 'gitlab:cleanup rake tasks', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/cleanup'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:container_registry namespace rake tasks' do
RSpec.describe 'gitlab:container_registry namespace rake tasks', :silence_stdout do
let_it_be(:api_url) { 'http://registry.gitlab' }
before :all do
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
require 'rake'
RSpec.describe 'gitlab:db namespace rake task' do
RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'active_record/railties/databases'
Rake.application.rake_require 'tasks/seed_fu'
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:external_diffs rake tasks' do
RSpec.describe 'gitlab:external_diffs rake tasks', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/external_diffs'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:generate_sample_prometheus_data rake task' do
RSpec.describe 'gitlab:generate_sample_prometheus_data rake task', :silence_stdout do
let(:cluster) { create(:cluster, :provided_by_user, :project) }
let(:environment) { create(:environment, project: cluster.project) }
let(:sample_query_file) { File.join(Rails.root, Metrics::SampleMetricsService::DIRECTORY, 'test_query_result.yml') }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:git rake tasks' do
RSpec.describe 'gitlab:git rake tasks', :silence_stdout do
let(:base_path) { 'tmp/tests/default_storage' }
let!(:project) { create(:project, :repository) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:gitaly namespace rake task' do
RSpec.describe 'gitlab:gitaly namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'tasks/gitlab/gitaly'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:env:info' do
RSpec.describe 'gitlab:env:info', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/info'
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:ldap:rename_provider rake task' do
RSpec.describe 'gitlab:ldap:rename_provider rake task', :silence_stdout do
it 'completes without error' do
Rake.application.rake_require 'tasks/gitlab/ldap'
stub_warn_user_is_not_gitlab
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:lfs rake tasks' do
RSpec.describe 'gitlab:lfs rake tasks', :silence_stdout do
describe 'check' do
let!(:lfs_object) { create(:lfs_object, :with_file, :correct_oid) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:lfs namespace rake task' do
RSpec.describe 'gitlab:lfs namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'tasks/gitlab/lfs/migrate'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:packages:build_composer_cache namespace rake task' do
RSpec.describe 'gitlab:packages:build_composer_cache namespace rake task', :silence_stdout do
let_it_be(:package_name) { 'sample-project' }
let_it_be(:package_name2) { 'sample-project2' }
let_it_be(:json) { { 'name' => package_name } }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:packages:events namespace rake task' do
RSpec.describe 'gitlab:packages:events namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'tasks/gitlab/packages/events'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:packages namespace rake task' do
RSpec.describe 'gitlab:packages namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'tasks/gitlab/packages/migrate'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:pages' do
RSpec.describe 'gitlab:pages', :silence_stdout do
before(:context) do
Rake.application.rake_require 'tasks/gitlab/pages'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:password rake tasks' do
RSpec.describe 'gitlab:password rake tasks', :silence_stdout do
let_it_be(:user_1) { create(:user, username: 'foobar', password: 'initial_password') }
def stub_username(username)
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:praefect:replicas' do
RSpec.describe 'gitlab:praefect:replicas', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/praefect'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:seed:group_seed rake task' do
RSpec.describe 'gitlab:seed:group_seed rake task', :silence_stdout do
let(:username) { 'group_seed' }
let!(:user) { create(:user, username: username) }
let(:task_params) { [2, username] }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:shell rake tasks' do
RSpec.describe 'gitlab:shell rake tasks', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/shell'
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'sidekiq.rake', :aggregate_failures do
RSpec.describe 'sidekiq.rake', :aggregate_failures, :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/sidekiq'
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:snippets namespace rake task' do
RSpec.describe 'gitlab:snippets namespace rake task', :silence_stdout do
let_it_be(:user) { create(:user)}
let_it_be(:migrated) { create(:personal_snippet, :repository, author: user) }
let(:non_migrated) { create_list(:personal_snippet, 3, author: user) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'rake gitlab:storage:*' do
RSpec.describe 'rake gitlab:storage:*', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/storage'
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:terraform_states' do
RSpec.describe 'gitlab:terraform_states', :silence_stdout do
let_it_be(:version) { create(:terraform_state_version) }
let(:logger) { instance_double(Logger) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:update_project_templates rake task' do
RSpec.describe 'gitlab:update_project_templates rake task', :silence_stdout do
let!(:tmpdir) { Dir.mktmpdir }
before do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:uploads rake tasks' do
RSpec.describe 'gitlab:uploads rake tasks', :silence_stdout do
describe 'check' do
let!(:upload) { create(:upload, path: Rails.root.join('spec/fixtures/banana_sample.gif')) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:uploads:migrate and migrate_to_local rake tasks' do
RSpec.describe 'gitlab:uploads:migrate and migrate_to_local rake tasks', :silence_stdout do
let(:model_class) { nil }
let(:uploader_class) { nil }
let(:mounted_as) { nil }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:usage data take tasks' do
RSpec.describe 'gitlab:usage data take tasks', :silence_stdout do
include UsageDataHelpers
before do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:user_management tasks' do
RSpec.describe 'gitlab:user_management tasks', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/user_management'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:web_hook namespace rake tasks' do
RSpec.describe 'gitlab:web_hook namespace rake tasks', :silence_stdout do
let_it_be(:group, refind: true) { create(:group) }
let_it_be(:project1, reload: true) { create(:project, namespace: group) }
let_it_be(:project2, reload: true) { create(:project, namespace: group) }
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:workhorse namespace rake task' do
RSpec.describe 'gitlab:workhorse namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'tasks/gitlab/workhorse'
end
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:x509 namespace rake task' do
RSpec.describe 'gitlab:x509 namespace rake task', :silence_stdout do
before :all do
Rake.application.rake_require 'tasks/gitlab/x509/update'
end
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
require 'rake'
RSpec.describe 'schema_version_check rake task' do
RSpec.describe 'schema_version_check rake task', :silence_stdout do
include StubENV
before :all do
......
......@@ -2,7 +2,7 @@
require 'rake_helper'
RSpec.describe 'tokens rake tasks' do
RSpec.describe 'tokens rake tasks', :silence_stdout do
let!(:user) { create(:user) }
before do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment