diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/gitlab_config.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/gitlab_config.rb index da8a2e46dd9f6f85cadb26d04282e02f79d069b4..c05faa14d0e27b9be0a3d3200d486d248fe939fd 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/gitlab_config.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/gitlab_config.rb @@ -45,11 +45,21 @@ def initialize(source) load! end + def loaded? + @config.present? + end + + private + def load! yaml = ActiveSupport::ConfigurationFile.parse(@source) all_configs = yaml.deep_stringify_keys @config = all_configs + rescue Errno::ENOENT + Gitlab::Backup::Cli::Output.error "GitLab configuration file: #{@source} does not exist" + rescue Errno::EACCES + Gitlab::Backup::Cli::Output.error "GitLab configuration file: #{@source} can't be read (permission denied)" end end end diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/gitlab_config_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/gitlab_config_spec.rb index 5e7a5753eaa4e3b756c844d4801bcdcce528cee9..65fab77a07acafe130155b15a679e5af948aab34 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/gitlab_config_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/gitlab_config_spec.rb @@ -11,5 +11,34 @@ expect(gitlab_config.keys).to include('test') end end + + context 'when provided with a filepath that doesnt exist' do + let(:config_fixture) { fixtures_path.join('unknown-gitlab.yml') } + + it 'does not raise an exception', :silence_output do + expect { gitlab_config }.not_to raise_error + expect(gitlab_config).not_to be_loaded + end + + it 'displays an error message' do + expect { gitlab_config }.to output(/GitLab configuration file: .+ does not exist/).to_stderr + end + end + + context 'when process lack enough permission to read provided config file' do + before do + allow(ActiveSupport::ConfigurationFile).to receive(:parse).and_raise(Errno::EACCES) + end + + it 'does not raise an exception', :silence_output do + expect { gitlab_config }.not_to raise_error + expect(gitlab_config).not_to be_loaded + end + + it 'displays an error message' do + error_message_pattern = /GitLab configuration file: .+ can't be read \(permission denied\)/ + expect { gitlab_config }.to output(error_message_pattern).to_stderr + end + end end end