Skip to content
代码片段 群组 项目
提交 e4a9912b 编辑于 作者: Gabriel Mazetto's avatar Gabriel Mazetto
浏览文件

Add tests for `gitlab-backup-cli` thor application

上级 c6a96bc5
No related branches found
No related tags found
1 合并请求!2419Fix TanukiBot spec relying on outdated code
# frozen_string_literal: true # frozen_string_literal: true
require 'thor'
require "gitlab/backup/cli" require "gitlab/backup/cli"
require 'tmpdir' require 'tmpdir'
require 'fileutils' require 'fileutils'
require 'factory_bot' require 'factory_bot'
require 'gitlab/rspec/next_instance_of'
# Load spec support code # Load spec support code
Dir['spec/support/**/*.rb'].each { |f| load f } Dir['spec/support/**/*.rb'].each { |f| load f }
......
# frozen_string_literal: true # frozen_string_literal: true
FakeContext = Struct.new(:gitlab_version, :backup_basedir, :env, keyword_init: true) module GitlabBackupHelpers
FakeContext = Struct.new(:gitlab_version, :backup_basedir, :env, keyword_init: true)
def spec_path def spec_path
Pathname.new(__dir__).join('..').expand_path Pathname.new(__dir__).join('..').expand_path
end end
def temp_path def temp_path
spec_path.join('..', 'tmp').expand_path spec_path.join('..', 'tmp').expand_path
end end
def stub_env(var, return_value)
stub_const('ENV', ENV.to_hash.merge(var => return_value))
end
def stub_env(var, return_value) def build_fake_context
stub_const('ENV', ENV.to_hash.merge(var => return_value)) FakeContext.new(
gitlab_version: '16.10',
backup_basedir: temp_path.join('backups'),
env: ActiveSupport::EnvironmentInquirer.new('test')
)
end
end end
def build_fake_context RSpec.configure do |config|
FakeContext.new( config.include GitlabBackupHelpers
gitlab_version: '16.10', # from gitlab-rspec
backup_basedir: temp_path.join('backups'), config.include NextInstanceOf
env: ActiveSupport::EnvironmentInquirer.new('test')
)
end end
# frozen_string_literal: true
RSpec.configure do |config|
config.define_derived_metadata(file_path: Regexp.new('spec/thor/')) do |metadata|
metadata[:type] = :thor
end
# Set terminal columns to be 120, so all specs behave the same no matter where it is being tested
config.before(:each, type: :thor) { stub_env('THOR_COLUMNS', 120) }
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'gitlab-backup-cli backup subcommand', type: :thor do
subject(:cli) { Gitlab::Backup::Cli::Runner }
let(:backup_subcommand) { Gitlab::Backup::Cli::Commands::BackupSubcommand }
let(:context) { build_fake_context }
let(:expected_help_output) do
<<~COMMAND
Backup commands:
gitlab-backup-cli backup all # Creates a backup including repositories, database and local files
gitlab-backup-cli backup help [COMMAND] # Describe subcommands or one specific subcommand
COMMAND
end
context 'with gitlab-backup-cli backup' do
it 'returns a list of supported commands' do
expect { cli.start(%w[backup]) }.to output(expected_help_output).to_stdout
end
end
context 'with gitlab-backup-cli backup help' do
it 'returns a list of supported commands' do
expect { cli.start(%w[backup help]) }.to output(expected_help_output).to_stdout
end
end
context 'with gitlab-backup-cli backup all' do
it 'delegates backup execution to backup executor' do
# Simulate real execution
executor = Gitlab::Backup::Cli::BackupExecutor
expect_next_instance_of(executor) do |instance|
expect(instance).to receive(:execute)
end
expect_next_instance_of(backup_subcommand) do |instance|
expect(instance).to receive(:build_context).and_return(context)
end
expect(Gitlab::Backup::Cli).to receive(:rails_environment!)
expected_backup_output = /.*Starting GitLab backup.*Backup finished:.*/m
expect { cli.start(%w[backup all]) }.to output(expected_backup_output).to_stdout
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'gitlab-backup-cli commands', type: :thor do
subject(:cli) { Gitlab::Backup::Cli::Runner }
let(:expected_help_output) do
<<~COMMAND
GitLab Backup CLI commands:
gitlab-backup-cli backup # Manage repositories, database and files backup creation
gitlab-backup-cli help [COMMAND] # Describe available commands or one specific command
gitlab-backup-cli version # Display the version information
COMMAND
end
describe 'Default behavior' do
it 'returns subcommand information with listed known methods' do
expect { cli.start([]) }.to output(expected_help_output).to_stdout
end
end
describe 'gitlab-backup-cli help' do
it 'returns subcommand information with listed known methods' do
expect { cli.start(%w[help]) }.to output(expected_help_output).to_stdout
end
end
describe 'gitlab-backup-cli version' do
it 'returns the current version' do
expect { cli.start(%w[version]) }.to output(
/GitLab Backup CLI \(#{Gitlab::Backup::Cli::VERSION}\)\n/o
).to_stdout
end
end
describe 'gitlab-backup-cli backup' do
it 'returns a list of backup subcommands' do
expect { cli.start(%w[backup]) }.to output(/Backup commands:.*/).to_stdout
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册