-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathgit_spec.rb
More file actions
104 lines (89 loc) · 4.17 KB
/
git_spec.rb
File metadata and controls
104 lines (89 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true
require_relative '../spec_helper'
require OctocatalogDiff::Spec.require_path('catalog-util/git')
require OctocatalogDiff::Spec.require_path('errors')
require OctocatalogDiff::Spec.require_path('util/scriptrunner')
require 'ostruct'
describe OctocatalogDiff::CatalogUtil::Git do
before(:each) do
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger
allow(File).to receive(:'directory?').with('/tmp/foo').and_return(false)
allow(File).to receive(:'directory?').with('/tmp/bar').and_return(true)
allow(File).to receive(:'directory?').with('.').and_return(true)
end
describe '#check_out_git_archive' do
context 'with invalid directory' do
it 'should raise OctocatalogDiff::Errors::GitCheckoutError if basedir is nil' do
opts = { branch: 'foo', path: '/tmp/foo', basedir: nil, logger: @logger }
expect do
described_class.check_out_git_archive(opts)
end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Source directory/)
end
it 'should raise OctocatalogDiff::Errors::GitCheckoutError if basedir does not exist' do
opts = { branch: 'foo', path: '/tmp/foo', basedir: '/tmp/foo', logger: @logger }
expect do
described_class.check_out_git_archive(opts)
end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Source directory/)
end
it 'should raise OctocatalogDiff::Errors::GitCheckoutError if path is nil' do
opts = { branch: 'foo', path: nil, basedir: '/tmp/bar', logger: @logger }
expect do
described_class.check_out_git_archive(opts)
end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Target directory/)
end
it 'should raise OctocatalogDiff::Errors::GitCheckoutError if path does not exist' do
opts = { branch: 'foo', path: '/tmp/foo', basedir: '/tmp/bar', logger: @logger }
expect do
described_class.check_out_git_archive(opts)
end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, /Target directory/)
end
end
context 'with valid directory' do
context 'with successful script run' do
it 'should log proper messages and not raise error' do
script_runner = double
expect(script_runner).to receive(:run).and_return('')
expect(OctocatalogDiff::Util::ScriptRunner).to receive(:new).and_return(script_runner)
opts = { branch: 'foo', path: '/tmp/bar', basedir: '/tmp/bar', logger: @logger }
described_class.check_out_git_archive(opts)
expect(@logger_str.string).to match(%r{Success git archive /tmp/bar:foo})
end
end
context 'with failed script run' do
it 'should raise OctocatalogDiff::Errors::GitCheckoutError' do
script_runner = double
expect(script_runner).to receive(:run).and_raise(OctocatalogDiff::Util::ScriptRunner::ScriptException)
expect(script_runner).to receive(:output).and_return('errors abound')
expect(OctocatalogDiff::Util::ScriptRunner).to receive(:new).and_return(script_runner)
opts = { branch: 'foo', path: '/tmp/bar', basedir: '/tmp/bar', logger: @logger }
expect do
described_class.check_out_git_archive(opts)
end.to raise_error(OctocatalogDiff::Errors::GitCheckoutError, 'Git archive foo->/tmp/bar failed: errors abound')
end
end
end
end
describe '#branch_sha' do
context 'with invalid directory' do
it 'should raise Errno::ENOENT if basedir is nil' do
opts = { branch: 'foo', basedir: nil }
expect do
described_class.branch_sha(opts)
end.to raise_error(Errno::ENOENT, /Git directory/)
end
it 'should raise Errno::ENOENT if basedir does not exist' do
opts = { branch: 'foo', basedir: '/tmp/foo' }
expect do
described_class.branch_sha(opts)
end.to raise_error(Errno::ENOENT, /Git directory/)
end
end
context 'with valid directory' do
it 'should return the sha' do
opts = { branch: 'master', basedir: '.' }
result = described_class.branch_sha(opts)
expect(result).to match(/^[0-9a-f]+$/)
end
end
end
end