forked from CocoaPods/CocoaPods
/
sources_manager_spec.rb
220 lines (186 loc) · 8.98 KB
/
sources_manager_spec.rb
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
require File.expand_path('../../spec_helper', __FILE__)
require 'webmock'
def set_up_test_repo_for_update
set_up_test_repo
upstream = SpecHelper.temporary_directory + 'upstream'
FileUtils.cp_r(test_repo_path, upstream)
Dir.chdir(test_repo_path) do
`git remote add origin #{upstream}`
`git remote -v`
`git fetch -q`
`git branch --set-upstream-to=origin/master master`
`git config branch.master.rebase true`
end
@sources_manager.stubs(:repos_dir).returns(SpecHelper.tmp_repos_path)
end
module Pod
describe Source::Manager do
before do
@test_source = Source.new(fixture('spec-repos/test_repo'))
@sources_manager = Source::Manager.new(config.repos_dir)
end
#-------------------------------------------------------------------------#
describe 'In general' do
before do
@sources_manager.stubs(:all).returns([@test_source])
end
#--------------------------------------#
it 'returns the path of the search index' do
Source::Manager.any_instance.unstub(:search_index_path)
config.cache_root = Config::DEFAULTS[:cache_root]
path = @sources_manager.search_index_path.to_s
path.should.end_with 'Library/Caches/CocoaPods/search_index.json'
end
describe 'managing sources by URL' do
describe 'finding or creating a source by URL' do
it 'returns an existing matching source' do
Source.any_instance.stubs(:url).returns('url')
@sources_manager.expects(:name_for_url).never
@sources_manager.find_or_create_source_with_url('url').url.
should == 'url'
end
it 'runs `pod repo add-cdn` when there is no matching source for CocoaPods Trunk' do
Command::Repo::AddCDN.any_instance.stubs(:run).once
REST.stubs(:head).returns(REST::Response.new(200, {}, ''))
@sources_manager.stubs(:source_with_url).returns(nil).then.returns(TrunkSource.new('trunk'))
@sources_manager.find_or_create_source_with_url(Pod::TrunkSource::TRUNK_REPO_URL).name.
should == 'trunk'
end
it 'runs `pod repo add` when there is no matching source' do
Command::Repo::Add.any_instance.stubs(:run).once
REST.stubs(:head).returns(REST::Response.new(404, {}, ''))
@sources_manager.stubs(:source_with_url).returns(nil).then.returns(Source.new('Source'))
@sources_manager.find_or_create_source_with_url('https://github.com/artsy/Specs.git').name.
should == 'Source'
end
it 'runs `pod repo add` when the url doesn\'t end in `.git`' do
Command::Repo::Add.any_instance.stubs(:run).once
REST.stubs(:head).returns(REST::Response.new(404, {}, ''))
@sources_manager.stubs(:source_with_url).returns(nil).then.returns(Source.new('Source'))
@sources_manager.find_or_create_source_with_url('https://github.com/artsy/Specs').name.
should == 'Source'
end
it 'runs `pod repo add-cdn` when there is no matching source and url is web' do
Command::Repo::AddCDN.any_instance.stubs(:run).once
REST.stubs(:head).returns(REST::Response.new(200, {}, ''))
@sources_manager.stubs(:source_with_url).returns(nil).then.returns(Source.new('Source'))
@sources_manager.find_or_create_source_with_url('https://website.com/Specs/').name.
should == 'Source'
end
it 'raises informative exception on network error' do
REST.stubs(:head).raises(StandardError.new('some network error'))
@sources_manager.stubs(:source_with_url).returns(nil)
should.raise(Informative) do
@sources_manager.find_or_create_source_with_url('https://website.com/Specs/')
end.message.should.include "Couldn't determine repo type for URL: `https://website.com/Specs/`: some network error"
end
it 'handles repositories without a remote url' do # for #2965
Command::Repo::Add.any_instance.stubs(:run).once
Source.any_instance.stubs(:url).returns(nil).then.returns('URL')
@sources_manager.find_or_create_source_with_url('URL').url.should == 'URL'
end
end
describe 'finding or creating a source by name or URL' do
it 'returns an existing source with a matching name' do
@sources_manager.expects(:name_for_url).never
@sources_manager.source_with_name_or_url('test_repo').name.
should == 'test_repo'
end
it 'tries by url when there is no matching name' do
Command::Repo::Add.any_instance.stubs(:run).once
REST.stubs(:head).returns(REST::Response.new(404, {}, ''))
@sources_manager.stubs(:source_with_url).returns(nil).then.returns('Source')
@sources_manager.source_with_name_or_url('https://github.com/artsy/Specs.git').
should == 'Source'
end
end
end
describe '#add_source' do
it 'adds the source to the list of sources' do
source = Source.new(fixture('spec-repos/test_repo1'))
@sources_manager.add_source(source)
@sources_manager.all.should == [@test_source, source]
end
end
end
#-------------------------------------------------------------------------#
describe 'Updating Sources' do
extend SpecHelper::TemporaryRepos
before do
Source.any_instance.stubs(:unchanged_github_repo?).returns(false)
end
it 'updates source backed by a git repository' do
set_up_test_repo_for_update
@sources_manager.expects(:update_search_index_if_needed_in_background).with({}).returns(nil)
repo_update = sequence('repo update')
Source.any_instance.
expects(:git!).
with(%W(-C #{test_repo_path} fetch origin --progress)).
in_sequence(repo_update)
Source.any_instance.
expects(:git!).
with(%W(-C #{test_repo_path} rev-parse --abbrev-ref HEAD)).
returns("my-special-branch\n").
in_sequence(repo_update)
Source.any_instance.
expects(:git!).
with(%W(-C #{test_repo_path} reset --hard origin/my-special-branch)).
in_sequence(repo_update)
@sources_manager.update(test_repo_path.basename.to_s, true)
end
it 'updates source with --silent flag' do
set_up_test_repo_for_update
@sources_manager.expects(:update_search_index_if_needed_in_background).with({}).returns(nil)
repo_update = sequence('repo update --silent')
Source.any_instance.
expects(:git!).
with(%W(-C #{test_repo_path} fetch origin)).
in_sequence(repo_update)
Source.any_instance.
expects(:git!).
with(%W(-C #{test_repo_path} rev-parse --abbrev-ref HEAD)).
returns("my-special-branch\n").
in_sequence(repo_update)
Source.any_instance.
expects(:git!).
with(%W(-C #{test_repo_path} reset --hard origin/my-special-branch)).
in_sequence(repo_update)
@sources_manager.update(test_repo_path.basename.to_s, false)
end
it 'prints a warning if the update failed' do
set_up_test_repo_for_update
Source.any_instance.stubs(:git).with do |options|
options.join(' ') == %W(-C #{test_repo_path} rev-parse HEAD).join(' ')
end.returns('aabbccd')
Source.any_instance.stubs(:git).with do |options|
options.join(' ') == %W(-C #{test_repo_path} diff --name-only aabbccd..HEAD).join(' ')
end.returns('')
Source.any_instance.expects(:git!).with(%W(-C #{test_repo_path} fetch origin --progress)).raises(<<-EOS)
fatal: '/dev/null' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
EOS
e = lambda { @sources_manager.update(test_repo_path.basename.to_s, true) }.should.raise Pod::Informative
e.message.should.include('not able to update the `master` repo')
end
it 'informs the user if there is an update for CocoaPods' do
master = @sources_manager.master.first
master.stubs(:metadata).returns(Source::Metadata.new('last' => '999.0'))
master.verify_compatibility!
UI.output.should.match /CocoaPods 999.0 is available/
end
it 'skips the update message if the user disabled the notification' do
config.new_version_message = false
master = @sources_manager.master.first
master.stubs(:metadata).returns(Source::Metadata.new('last' => '999.0'))
master.verify_compatibility!
UI.output.should.not.match /CocoaPods 999.0 is available/
end
it 'does not crash if the repos dir does not exist' do
sources_manager = Source::Manager.new(Pathname.new(Dir.tmpdir) + 'CocoaPods/RepoDir/DoesNotExist')
lambda { sources_manager.update }.should.not.raise
end
end
end
end