pgit 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.agignore +3 -0
  3. data/.travis.yml +1 -0
  4. data/README.markdown +32 -9
  5. data/Rakefile +12 -0
  6. data/bin/pgit +167 -25
  7. data/lib/pgit.rb +40 -10
  8. data/lib/pgit/bilateral/handle_back.rb +22 -0
  9. data/lib/pgit/bilateral/handle_choose_story.rb +31 -0
  10. data/lib/pgit/bilateral/story.rb +44 -0
  11. data/lib/pgit/command.rb +83 -0
  12. data/lib/pgit/command/add.rb +36 -0
  13. data/lib/pgit/command/application.rb +21 -0
  14. data/lib/pgit/command/edit.rb +36 -0
  15. data/lib/pgit/command/remove.rb +36 -0
  16. data/lib/pgit/command/run.rb +32 -0
  17. data/lib/pgit/command/show.rb +53 -0
  18. data/lib/pgit/configuration.rb +27 -3
  19. data/lib/pgit/current_project.rb +9 -45
  20. data/lib/pgit/current_project/validator.rb +2 -1
  21. data/lib/pgit/error/external.rb +11 -0
  22. data/lib/pgit/error/user.rb +12 -0
  23. data/lib/pgit/helpers/heredoc.rb +17 -0
  24. data/lib/pgit/helpers/query_methods.rb +63 -0
  25. data/lib/pgit/helpers/string_extensions.rb +29 -0
  26. data/lib/pgit/installer/bash_auto_completion.rb +57 -0
  27. data/lib/pgit/pivotal/collection_request.rb +21 -0
  28. data/lib/pgit/pivotal/individual_request.rb +47 -0
  29. data/lib/pgit/pivotal/iteration.rb +6 -0
  30. data/lib/pgit/pivotal/iterations.rb +15 -0
  31. data/lib/pgit/pivotal/project.rb +6 -0
  32. data/lib/pgit/pivotal/projects.rb +20 -0
  33. data/lib/pgit/pivotal/query.rb +8 -0
  34. data/lib/pgit/pivotal/request.rb +33 -0
  35. data/lib/pgit/pivotal/request/query.rb +25 -0
  36. data/lib/pgit/pivotal/story.rb +38 -0
  37. data/lib/pgit/pivotal_request_validator.rb +1 -1
  38. data/lib/pgit/project.rb +78 -0
  39. data/lib/pgit/project/add.rb +28 -0
  40. data/lib/pgit/project/application.rb +21 -0
  41. data/lib/pgit/project/interactive_adder.rb +41 -0
  42. data/lib/pgit/project/remove.rb +41 -0
  43. data/lib/pgit/project/reuse_api_token_adder.rb +48 -0
  44. data/lib/pgit/response_handler.rb +16 -0
  45. data/lib/pgit/root.rb +5 -0
  46. data/lib/pgit/status.rb +16 -0
  47. data/lib/pgit/story_branch/application.rb +3 -3
  48. data/lib/pgit/{name_parser.rb → story_branch/name_parser.rb} +0 -0
  49. data/lib/pgit/story_branch/story_id_parser.rb +11 -0
  50. data/lib/pgit/validators/project_validator.rb +20 -0
  51. data/lib/pgit/version.rb +1 -1
  52. data/lib/pivotal +0 -0
  53. data/pgit.gemspec +5 -0
  54. data/spec/fixtures/iterations +1 -0
  55. data/spec/pgit/bilateral/handle_back_spec.rb +29 -0
  56. data/spec/pgit/bilateral/handle_choose_story_spec.rb +17 -0
  57. data/spec/pgit/bilateral/story_spec.rb +178 -0
  58. data/spec/pgit/command/add_spec.rb +68 -0
  59. data/spec/pgit/command/application_spec.rb +110 -0
  60. data/spec/pgit/command/edit_spec.rb +61 -0
  61. data/spec/pgit/command/remove_spec.rb +76 -0
  62. data/spec/pgit/command/run_spec.rb +49 -0
  63. data/spec/pgit/command/show_spec.rb +95 -0
  64. data/spec/pgit/command_spec.rb +299 -0
  65. data/spec/pgit/configuration_spec.rb +121 -18
  66. data/spec/pgit/current_project/validator_spec.rb +2 -1
  67. data/spec/pgit/current_project_spec.rb +20 -71
  68. data/spec/pgit/{external_error_spec.rb → error/external_spec.rb} +3 -3
  69. data/spec/pgit/error/user_spec.rb +17 -0
  70. data/spec/pgit/helpers/heredoc_spec.rb +33 -0
  71. data/spec/pgit/helpers/query_methods_spec.rb +24 -0
  72. data/spec/pgit/helpers/string_extensions_spec.rb +49 -0
  73. data/spec/pgit/installer/bash_auto_completion_spec.rb +134 -0
  74. data/spec/pgit/pivotal/individual_request_spec.rb +32 -0
  75. data/spec/pgit/pivotal/iteration_spec.rb +19 -0
  76. data/spec/pgit/pivotal/iterations_spec.rb +37 -0
  77. data/spec/pgit/pivotal/project_spec.rb +9 -0
  78. data/spec/pgit/pivotal/projects_spec.rb +48 -0
  79. data/spec/pgit/pivotal/request/query_spec.rb +24 -0
  80. data/spec/pgit/pivotal/story_spec.rb +113 -0
  81. data/spec/pgit/pivotal_request_validator_spec.rb +3 -3
  82. data/spec/pgit/project/add_spec.rb +52 -0
  83. data/spec/pgit/project/application_spec.rb +69 -0
  84. data/spec/pgit/project/interactive_adder_spec.rb +45 -0
  85. data/spec/pgit/project/remove_spec.rb +86 -0
  86. data/spec/pgit/project/reuse_api_token_adder_spec.rb +41 -0
  87. data/spec/pgit/project_spec.rb +513 -0
  88. data/spec/pgit/status_spec.rb +40 -0
  89. data/spec/pgit/story_branch/application_spec.rb +5 -8
  90. data/spec/pgit/story_branch/name_parser_spec.rb +3 -3
  91. data/spec/pgit/story_branch/story_id_parser_spec.rb +17 -0
  92. data/spec/pgit/validators/project_validator_spec.rb +39 -0
  93. metadata +146 -21
  94. data/lib/pgit/configuration/layout_error.rb +0 -9
  95. data/lib/pgit/configuration/missing_attributes_error.rb +0 -10
  96. data/lib/pgit/configuration/not_found_error.rb +0 -10
  97. data/lib/pgit/configuration/project_missing_error.rb +0 -10
  98. data/lib/pgit/configuration/validator.rb +0 -41
  99. data/lib/pgit/current_project/no_paths_match_working_dir_error.rb +0 -10
  100. data/lib/pgit/external_error.rb +0 -9
  101. data/lib/pgit/installer/configuration.rb +0 -34
  102. data/lib/pgit/story.rb +0 -44
  103. data/spec/pgit/configuration/missing_attributes_error_spec.rb +0 -30
  104. data/spec/pgit/configuration/not_found_error_spec.rb +0 -17
  105. data/spec/pgit/configuration/project_missing_error_spec.rb +0 -30
  106. data/spec/pgit/configuration/validator_spec.rb +0 -79
  107. data/spec/pgit/current_project/no_paths_match_working_dir_error_spec.rb +0 -17
  108. data/spec/pgit/installer/configuration_spec.rb +0 -162
  109. data/spec/pgit/story_spec.rb +0 -35
@@ -0,0 +1,299 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PGit::Command' do
4
+ before { Rainbow.enabled = false }
5
+
6
+ describe '#remove!' do
7
+ it 'removes the (optional) key-value pair from the current project' do
8
+ name = "finish"
9
+ steps = ["git checkout master", "git fetch"]
10
+ fake_commands = instance_double('Array', reject!: nil, :<< => nil)
11
+ current_project = instance_double('PGit::CurrentProject',
12
+ commands: fake_commands,
13
+ save!: nil)
14
+
15
+ command = PGit::Command.new(name, steps, current_project)
16
+ allow(current_project).to receive(:remove!).with(command)
17
+ command.remove!
18
+
19
+ expect(current_project.commands).to have_received(:reject!)
20
+ expect(current_project).to have_received(:save!)
21
+ end
22
+ end
23
+
24
+ describe '#save!' do
25
+ it 'saves the command if the key does not exist for the current project' do
26
+ name = "finish"
27
+ steps = ["git checkout master", "git fetch"]
28
+ fake_commands = instance_double('Array', reject!: nil, :<< => nil)
29
+ current_project = instance_double('PGit::CurrentProject',
30
+ commands: fake_commands,
31
+ :commands= => nil)
32
+ allow(current_project).to receive(:save!)
33
+
34
+ command = PGit::Command.new(name, steps, current_project)
35
+ command.save!
36
+
37
+ expect(current_project.commands).to have_received(:reject!)
38
+ expect(current_project).to have_received(:save!)
39
+ end
40
+ end
41
+
42
+ describe '#to_h' do
43
+ it 'explicitly returns a hash' do
44
+ chompable = double('String')
45
+ allow(chompable).to receive(:chomp).and_return('Y')
46
+ allow(STDIN).to receive(:gets).and_return(chompable)
47
+ fake_first_step = "echo hi"
48
+ fake_second_step = "echo hello"
49
+ name = "finish"
50
+ steps = [fake_first_step, fake_second_step]
51
+
52
+ fake_project = instance_double('PGit::CurrentProject')
53
+ command = PGit::Command.new(name, steps, fake_project)
54
+ expected_hash = {
55
+ "finish" => ["echo hi", "echo hello"],
56
+ "start" => ["git checkout bla-123"]
57
+ }
58
+
59
+ expect(command.to_h.merge({ "start" => ["git checkout bla-123"]})).to eq expected_hash
60
+ end
61
+ end
62
+
63
+ describe '#to_hash' do
64
+ it 'implicitly converts the object into a hash' do
65
+ chompable = double('String')
66
+ allow(chompable).to receive(:chomp).and_return('Y')
67
+ allow(STDIN).to receive(:gets).and_return(chompable)
68
+ fake_first_step = "echo hi"
69
+ fake_second_step = "echo hello"
70
+ name = "finish"
71
+ steps = [fake_first_step, fake_second_step]
72
+ current_project = instance_double('PGit::CurrentProject')
73
+ command = PGit::Command.new(name, steps, current_project)
74
+ expected_hash = {
75
+ "finish" => ["echo hi", "echo hello"],
76
+ "start" => ["git checkout bla-123"]
77
+ }
78
+
79
+ merged = { "start" => [ "git checkout bla-123"] }.merge(command)
80
+ expect(merged).to eq expected_hash
81
+ end
82
+ end
83
+
84
+ describe '#name' do
85
+ it 'should return the name' do
86
+ chompable = double('String')
87
+ allow(chompable).to receive(:chomp).and_return('Y')
88
+ allow(STDIN).to receive(:gets).and_return(chompable)
89
+ fake_first_step = "echo hi"
90
+ fake_second_step = "echo hello"
91
+ name = "finish"
92
+ steps = [fake_first_step, fake_second_step]
93
+ current_project = instance_double('PGit::CurrentProject')
94
+ command = PGit::Command.new(name, steps, current_project)
95
+ name = command.name
96
+
97
+ expect(name).to eq "finish"
98
+ end
99
+ end
100
+
101
+ describe '#steps' do
102
+ it 'should return the steps' do
103
+ chompable = double('String')
104
+ allow(chompable).to receive(:chomp).and_return('Y')
105
+ allow(STDIN).to receive(:gets).and_return(chompable)
106
+ fake_first_step = "echo hi"
107
+ fake_second_step = "echo hello"
108
+ steps = [fake_first_step, fake_second_step]
109
+ name = "finish"
110
+ current_project = instance_double('PGit::CurrentProject')
111
+ command = PGit::Command.new(name, steps, current_project)
112
+ steps = command.steps
113
+
114
+ expect(steps).to eq steps
115
+ end
116
+ end
117
+
118
+ describe '#to_s' do
119
+ it 'should return the string version' do
120
+ stringified = <<-TO_S
121
+ finish:
122
+ echo hi
123
+ echo hello
124
+ TO_S
125
+
126
+ stringified = PGit::Helpers::Heredoc.remove_front_spaces(stringified)
127
+ chompable = double('String')
128
+ allow(chompable).to receive(:chomp).and_return('Y')
129
+ allow(STDIN).to receive(:gets).and_return(chompable)
130
+ fake_first_step = "echo hi"
131
+ fake_second_step = "echo hello"
132
+ steps = [fake_first_step, fake_second_step]
133
+ name = "finish"
134
+ current_project = instance_double('PGit::CurrentProject')
135
+ command = PGit::Command.new(name, steps, current_project)
136
+ string = command.to_s
137
+
138
+ expect(string).to eq stringified
139
+ end
140
+ end
141
+
142
+ describe '#execute' do
143
+ it 'executes each line' do
144
+ chompable = double('String')
145
+ allow(chompable).to receive(:chomp).and_return('Y')
146
+ allow(STDIN).to receive(:gets).and_return(chompable)
147
+ fake_current_branch = "some_feature_branch_12345678"
148
+ fake_first_step = "echo hi"
149
+ fake_second_step = "echo hello"
150
+ fake_first_response = "hi"
151
+ fake_second_response = "hello"
152
+ name = "finish"
153
+ steps = [fake_first_step, fake_second_step]
154
+ allow_any_instance_of(PGit::CurrentBranch).to receive(:name).
155
+ and_return(fake_current_branch)
156
+ allow_any_instance_of(PGit::Command).to receive(:`).
157
+ with(fake_first_step).and_return(fake_first_response)
158
+ allow_any_instance_of(PGit::Command).to receive(:`).
159
+ with(fake_second_step).and_return(fake_second_response)
160
+ allow_any_instance_of(PGit::Command).to receive(:puts)
161
+ current_project = instance_double('PGit::CurrentProject')
162
+ command = PGit::Command.new(name, steps, current_project)
163
+
164
+ command.execute
165
+
166
+ expect(command).to have_received(:puts).with fake_first_response
167
+ expect(command).to have_received(:puts).with fake_second_response
168
+ end
169
+
170
+ it 'replaces STORY_BRANCH with the current branch' do
171
+ chompable = double('String')
172
+ allow(chompable).to receive(:chomp).and_return('Y')
173
+ allow(STDIN).to receive(:gets).and_return(chompable)
174
+ fake_current_branch = "some_feature_branch_12345678"
175
+ fake_command_step = "git branch -d #{fake_current_branch}"
176
+ fake_command_step_response = "Deleted branch some_feature_branch_12345678"
177
+ allow_any_instance_of(PGit::CurrentBranch).to receive(:name).
178
+ and_return(fake_current_branch)
179
+ allow_any_instance_of(PGit::Command).to receive(:`).
180
+ with(fake_command_step).and_return(fake_command_step_response)
181
+ allow_any_instance_of(PGit::Command).to receive(:puts)
182
+ name = "finish"
183
+ steps = [fake_command_step]
184
+ current_project = instance_double('PGit::CurrentProject')
185
+ command = PGit::Command.new(name, steps, current_project)
186
+
187
+ command.execute
188
+
189
+ expect(command).to have_received(:puts).with fake_command_step_response
190
+ end
191
+
192
+ it 'says what is about to be executed and asks for the next step' do
193
+ chompable = double('String')
194
+ allow_any_instance_of(PGit::Command).to receive(:puts)
195
+ allow(chompable).to receive(:chomp).and_return('Y')
196
+ allow(STDIN).to receive(:gets).and_return(chompable)
197
+ fake_command_step = "echo hello"
198
+ name = "finish"
199
+ message_to_be_executed = "About to execute 'echo hello'. Proceed? [Y/s/q]"
200
+ steps = [fake_command_step]
201
+ current_project = instance_double('PGit::CurrentProject')
202
+ command = PGit::Command.new(name, steps, current_project)
203
+
204
+ command.execute
205
+
206
+ expect(command).to have_received(:puts).with message_to_be_executed
207
+ end
208
+
209
+ describe 'user answers "Y"' do
210
+ it 'should go to the next step' do
211
+ allow_any_instance_of(PGit::Command).to receive(:puts)
212
+
213
+ chompable = double('String')
214
+ allow(chompable).to receive(:chomp).and_return('Y')
215
+ allow(STDIN).to receive(:gets).and_return(chompable)
216
+ fake_command_step = "echo hello"
217
+ name = "finish"
218
+ message_to_be_executed = "About to execute 'echo hello'. Proceed? [Y/s/q]"
219
+ steps = [fake_command_step]
220
+ current_project = instance_double('PGit::CurrentProject')
221
+ command = PGit::Command.new(name, steps, current_project)
222
+
223
+ command.execute
224
+
225
+ expect(command).to have_received(:puts).with message_to_be_executed
226
+ end
227
+ end
228
+
229
+ describe 'user answers "s"' do
230
+ it 'should go to the next step' do
231
+ allow_any_instance_of(PGit::Command).to receive(:puts)
232
+
233
+ chompable = double('String')
234
+ allow(chompable).to receive(:chomp).and_return('s')
235
+ allow(STDIN).to receive(:gets).and_return(chompable)
236
+ fake_command_step = "echo hello"
237
+ name = "finish"
238
+ message_to_be_executed = "Skipping..."
239
+ steps = [fake_command_step]
240
+ current_project = instance_double('PGit::CurrentProject')
241
+ command = PGit::Command.new(name, steps, current_project)
242
+
243
+ command.execute
244
+
245
+ expect(command).to have_received(:puts).with message_to_be_executed
246
+ end
247
+ end
248
+
249
+ describe 'user answers "q"' do
250
+ it 'should quit' do
251
+ allow_any_instance_of(PGit::Command).to receive(:puts)
252
+
253
+ chompable = double('String')
254
+ allow(chompable).to receive(:chomp).and_return('q', 's')
255
+ allow(STDIN).to receive(:gets).and_return(chompable)
256
+ fake_first_step = "echo hello"
257
+ fake_second_step = "echo hi"
258
+ name = "finish"
259
+ message_to_be_executed = "Quitting..."
260
+ steps = [fake_first_step, fake_second_step]
261
+ current_project = instance_double('PGit::CurrentProject')
262
+ command = PGit::Command.new(name, steps, current_project)
263
+
264
+ command.execute
265
+
266
+ expect(command).to have_received(:puts).with message_to_be_executed
267
+ expect(command).not_to have_received(:puts).with "Skipping..."
268
+ end
269
+ end
270
+
271
+ describe 'user answers with nonsense' do
272
+ it 'should repeat the legal options without moving to the end' do
273
+ allow_any_instance_of(PGit::Command).to receive(:puts)
274
+
275
+ chompable = double('String')
276
+ allow(chompable).to receive(:chomp).and_return('l', 'y')
277
+ allow(STDIN).to receive(:gets).and_return(chompable)
278
+ fake_command_step = "echo hello"
279
+ execution_message = "Executing 'echo hello'..."
280
+ name = "finish"
281
+ message = <<-LEGAL_OPTIONS
282
+ y - yes
283
+ s - skip
284
+ q - quit
285
+ LEGAL_OPTIONS
286
+
287
+ message = PGit::Helpers::Heredoc.remove_front_spaces(message)
288
+ steps = [fake_command_step]
289
+ current_project = instance_double('PGit::CurrentProject')
290
+ command = PGit::Command.new(name, steps, current_project)
291
+
292
+ command.execute
293
+
294
+ expect(command).to have_received(:puts).with message
295
+ expect(command).to have_received(:puts).with execution_message
296
+ end
297
+ end
298
+ end
299
+ end
@@ -21,36 +21,139 @@ describe 'PGit::Configuration' do
21
21
  end
22
22
  end
23
23
 
24
- describe '#new (without any arguments)' do
25
- it 'should delegate the default path to PGit::Configuration::Validator instance' do
26
- fake_validator = instance_double('PGit::Configuration::Validator')
27
- allow(PGit::Configuration::Validator).to receive(:new).with("~/.pgit.rc.yml").and_return fake_validator
24
+ describe '#new while file did not exist' do
25
+ it 'should create the file' do
26
+ file = instance_double('File', close: nil)
27
+ config_path = '~/.pgit.rc.yml'
28
+ expanded_path = '/expanded/.pgit.rc.yml'
29
+ error = "No such file or directory"
30
+ allow(File).to receive(:expand_path).with(config_path).and_return(expanded_path)
31
+ allow(YAML).to receive(:load_file).with(expanded_path).and_raise(error)
32
+ allow(File).to receive(:new).with(expanded_path, 'w').and_return(file)
33
+
28
34
  PGit::Configuration.new
35
+ expect(file).to have_received(:close)
36
+ end
37
+ end
38
+
39
+ describe '#yaml' do
40
+ it 'defaults to empty hash if configuration does not exist' do
41
+ expanded_path = "/some/path/to/.pgit.rc.yml"
42
+ config_path = '~/.pgit.rc.yml'
43
+ allow(File).to receive(:expand_path).with(config_path).and_return(expanded_path)
44
+ file = instance_double('File', close: nil)
45
+ allow(File).to receive(:new).with(expanded_path, 'w').and_return(file)
46
+ allow(YAML).to receive(:load_file).with(expanded_path).and_return(false)
29
47
 
30
- expect(PGit::Configuration::Validator).to have_received(:new).with("~/.pgit.rc.yml")
48
+ configuration = PGit::Configuration.new
49
+
50
+ expect(configuration.yaml).to eq Hash.new
31
51
  end
32
52
  end
33
53
 
34
- describe '#new ("~/some/path")' do
35
- it 'should delegate the path to PGit::Configuration::Validator instance' do
54
+ describe '#to_hash' do
55
+ it 'returns the hash' do
36
56
  fake_validator = instance_double('PGit::Configuration::Validator')
37
- fake_path = "~/some/path"
38
- allow(PGit::Configuration::Validator).to receive(:new).with(fake_path).and_return fake_validator
39
- PGit::Configuration.new(fake_path)
57
+ hash = { "projects" => [] }
58
+ allow(fake_validator).to receive(:yaml).and_return(hash)
59
+ expanded_path = "/some/path/to/.pgit.rc.yml"
60
+ config_path = '~/.pgit.rc.yml'
61
+ allow(File).to receive(:expand_path).with(config_path).and_return(expanded_path)
62
+ file = instance_double('File', close: nil)
63
+ allow(File).to receive(:new).with(expanded_path, 'w').and_return(file)
64
+ allow(YAML).to receive(:load_file).with(expanded_path).and_return(hash)
65
+
66
+ configuration = PGit::Configuration.new
40
67
 
41
- expect(PGit::Configuration::Validator).to have_received(:new).with(fake_path)
68
+ expect(configuration.to_hash).to eq hash
42
69
  end
43
70
  end
44
71
 
45
- describe '#to_yaml' do
46
- it 'should delegate #yaml to validator' do
47
- fake_validator = instance_double('PGit::Configuration::Validator')
48
- allow(fake_validator).to receive(:yaml)
49
- allow(PGit::Configuration::Validator).to receive(:new).with("~/.pgit.rc.yml").and_return fake_validator
72
+ describe '#projects' do
73
+ it 'returns the projects' do
74
+ proj1 = instance_double('PGit::Project')
75
+ proj2 = instance_double('PGit::Project')
76
+ proj_hash1 = instance_double('Hash')
77
+ proj_hash2 = instance_double('Hash')
78
+ expanded_path = "/some/path/to/.pgit.rc.yml"
79
+ projects = [proj1, proj2]
80
+ project_hashes = [proj_hash1, proj_hash2]
81
+ yaml = { 'projects' => project_hashes }
82
+
83
+ config_path = '~/.pgit.rc.yml'
84
+ allow(File).to receive(:expand_path).with(config_path).and_return(expanded_path)
85
+ file = instance_double('File', close: nil)
86
+ allow(File).to receive(:new).with(expanded_path, 'w').and_return(file)
87
+
88
+ allow(YAML).to receive(:load_file).with(expanded_path).and_return(yaml)
89
+
90
+ configuration = PGit::Configuration.new
91
+
92
+ allow(PGit::Project).to receive(:new).with(configuration, proj_hash1).and_return(proj1)
93
+ allow(PGit::Project).to receive(:new).with(configuration, proj_hash2).and_return(proj2)
94
+
95
+ expect(configuration.projects).to eq projects
96
+ end
97
+ end
98
+
99
+ describe '#projects=(some projects)' do
100
+ it 'should set the projects' do
101
+ proj_hash1 = instance_double('Hash')
102
+ proj_hash2 = instance_double('Hash')
103
+ proj1 = instance_double('PGit::Project', path: 'proj1/path', to_hash: proj_hash1)
104
+ proj2 = instance_double('PGit::Project', path: 'proj2/path', to_hash: proj_hash2)
105
+ expanded_path = "/some/path/to/.pgit.rc.yml"
106
+ projects = [proj1, proj2]
107
+ project_hashes = [proj_hash1, proj_hash2]
108
+ yaml = { 'projects' => project_hashes }
109
+
110
+ config_path = '~/.pgit.rc.yml'
111
+ allow(File).to receive(:expand_path).with(config_path).and_return(expanded_path)
112
+ file = instance_double('File', close: nil)
113
+ allow(File).to receive(:new).with(expanded_path, 'w').and_return(file)
114
+
115
+ allow(YAML).to receive(:load_file).with(expanded_path).and_return(yaml)
116
+
50
117
  configuration = PGit::Configuration.new
51
- configuration.to_yaml
52
118
 
53
- expect(fake_validator).to have_received(:yaml)
119
+ allow(PGit::Project).to receive(:new).with(configuration, proj_hash1).and_return(proj1)
120
+ allow(PGit::Project).to receive(:new).with(configuration, proj_hash2).and_return(proj2)
121
+
122
+ configuration.projects = projects
123
+
124
+ expect(configuration.projects.first.path).to eq 'proj1/path'
125
+ expect(configuration.projects[1].path).to eq 'proj2/path'
126
+ end
127
+ end
128
+
129
+ describe '#save' do
130
+ it 'should save the configuration to the config path' do
131
+ proj_hash1 = instance_double('Hash')
132
+ proj_hash2 = instance_double('Hash')
133
+ proj1 = instance_double('PGit::Project', to_hash: proj_hash1)
134
+ proj2 = instance_double('PGit::Project', to_hash: proj_hash2)
135
+ expanded_path = "/some/path/to/.pgit.rc.yml"
136
+ projects = [proj1, proj2]
137
+ project_hashes = [proj_hash1, proj_hash2]
138
+ yaml = { 'projects' => project_hashes }
139
+
140
+ config_path = '~/.pgit.rc.yml'
141
+ allow(File).to receive(:expand_path).with(config_path).and_return(expanded_path)
142
+ file = instance_double('File', close: nil)
143
+ allow(File).to receive(:new).with(expanded_path, 'w').and_return(file)
144
+
145
+ allow(YAML).to receive(:load_file).with(expanded_path).and_return(yaml)
146
+ allow(YAML).to receive(:dump).with(yaml, file).and_return(yaml)
147
+
148
+ configuration = PGit::Configuration.new
149
+
150
+ allow(PGit::Project).to receive(:new).with(configuration, proj_hash1).and_return(proj1)
151
+ allow(PGit::Project).to receive(:new).with(configuration, proj_hash2).and_return(proj2)
152
+
153
+ configuration.projects = projects
154
+ configuration.save!
155
+
156
+ expect(YAML).to have_received(:dump).with(yaml, file)
54
157
  end
55
158
  end
56
159
  end