2019-04-11 08:02:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-07 16:52:30 -04:00
|
|
|
describe QA::Git::Repository do
|
2019-02-11 04:04:59 -05:00
|
|
|
include Helpers::StubENV
|
2018-10-16 05:38:57 -04:00
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
shared_context 'git directory' do
|
|
|
|
let(:repository) { described_class.new }
|
|
|
|
let(:tmp_git_dir) { Dir.mktmpdir }
|
|
|
|
let(:tmp_netrc_dir) { Dir.mktmpdir }
|
2018-06-07 16:52:30 -04:00
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
before do
|
|
|
|
stub_env('GITLAB_USERNAME', 'root')
|
|
|
|
cd_empty_temp_directory
|
|
|
|
set_bad_uri
|
2018-06-07 16:52:30 -04:00
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
allow(repository).to receive(:tmp_home_dir).and_return(tmp_netrc_dir)
|
2018-06-07 16:52:30 -04:00
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
after do
|
|
|
|
# Switch to a safe dir before deleting tmp dirs to avoid dir access errors
|
|
|
|
FileUtils.cd __dir__
|
|
|
|
FileUtils.remove_entry_secure(tmp_git_dir, true)
|
|
|
|
FileUtils.remove_entry_secure(tmp_netrc_dir, true)
|
2018-06-07 16:52:30 -04:00
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
def cd_empty_temp_directory
|
|
|
|
FileUtils.cd tmp_git_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_bad_uri
|
|
|
|
repository.uri = 'http://foo/bar.git'
|
2018-06-07 16:52:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
context 'with default credentials' do
|
|
|
|
include_context 'git directory' do
|
|
|
|
before do
|
|
|
|
repository.use_default_credentials
|
2018-10-23 14:54:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
describe '#clone' do
|
|
|
|
it 'is unable to resolve host' do
|
2018-11-29 10:39:00 -05:00
|
|
|
expect { repository.clone }.to raise_error(described_class::RepositoryCommandError, /The command .* failed \(128\) with the following output/)
|
2019-01-25 16:47:06 -05:00
|
|
|
end
|
2018-10-23 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
describe '#push_changes' do
|
|
|
|
before do
|
|
|
|
`git init` # need a repo to push from
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails to push changes' do
|
2018-11-29 10:39:00 -05:00
|
|
|
expect { repository.push_changes }.to raise_error(described_class::RepositoryCommandError, /The command .* failed \(1\) with the following output/)
|
2019-01-25 16:47:06 -05:00
|
|
|
end
|
2018-10-23 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
describe '#git_protocol=' do
|
|
|
|
[0, 1, 2].each do |version|
|
|
|
|
it "configures git to use protocol version #{version}" do
|
|
|
|
expect(repository).to receive(:run).with("git config protocol.version #{version}")
|
|
|
|
repository.git_protocol = version
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if the version is unsupported' do
|
|
|
|
expect { repository.git_protocol = 'foo' }.to raise_error(ArgumentError, "Please specify the protocol you would like to use: 0, 1, or 2")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fetch_supported_git_protocol' do
|
|
|
|
it "reports the detected version" do
|
|
|
|
expect(repository).to receive(:run).and_return("packet: git< version 2")
|
|
|
|
expect(repository.fetch_supported_git_protocol).to eq('2')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reports unknown if version is unknown' do
|
|
|
|
expect(repository).to receive(:run).and_return("packet: git< version -1")
|
|
|
|
expect(repository.fetch_supported_git_protocol).to eq('unknown')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reports unknown if content does not identify a version' do
|
|
|
|
expect(repository).to receive(:run).and_return("foo")
|
|
|
|
expect(repository.fetch_supported_git_protocol).to eq('unknown')
|
|
|
|
end
|
2018-10-23 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
describe '#use_default_credentials' do
|
|
|
|
it 'adds credentials to .netrc' do
|
|
|
|
expect(File.read(File.join(tmp_netrc_dir, '.netrc')))
|
|
|
|
.to eq("machine foo login #{QA::Runtime::User.default_username} password #{QA::Runtime::User.default_password}\n")
|
|
|
|
end
|
2018-10-23 14:54:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-25 16:47:06 -05:00
|
|
|
context 'with specific credentials' do
|
|
|
|
include_context 'git directory'
|
|
|
|
|
|
|
|
context 'before setting credentials' do
|
|
|
|
it 'does not add credentials to .netrc' do
|
|
|
|
expect(repository).not_to receive(:save_netrc_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#password=' do
|
|
|
|
it 'raises an error if no username was given' do
|
|
|
|
expect { repository.password = 'foo' }
|
|
|
|
.to raise_error(QA::Git::Repository::InvalidCredentialsError,
|
|
|
|
"Please provide a username when setting a password")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds credentials to .netrc' do
|
|
|
|
repository.username = 'user'
|
|
|
|
repository.password = 'foo'
|
|
|
|
|
|
|
|
expect(File.read(File.join(tmp_netrc_dir, '.netrc')))
|
|
|
|
.to eq("machine foo login user password foo\n")
|
|
|
|
end
|
2019-05-05 23:07:31 -04:00
|
|
|
|
|
|
|
it 'adds credentials with special characters' do
|
2019-05-06 03:30:24 -04:00
|
|
|
password = %q[!"#$%&')(*+,-./:;<=>?]
|
2019-05-05 23:07:31 -04:00
|
|
|
repository.username = 'user'
|
|
|
|
repository.password = password
|
|
|
|
|
|
|
|
expect(File.read(File.join(tmp_netrc_dir, '.netrc')))
|
|
|
|
.to eq("machine foo login user password #{password}\n")
|
|
|
|
end
|
2019-01-25 16:47:06 -05:00
|
|
|
end
|
2018-06-07 16:52:30 -04:00
|
|
|
end
|
|
|
|
end
|