git_presenter 0.4.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,16 @@
1
1
  class GitPresenter::Presentation
2
2
  attr_reader :slides, :current_slide
3
3
 
4
- def initialize(presentation)
4
+ def initialize(presentation, shell=GitPresenter::Shell.new)
5
+ @branch = presentation["branch"]
5
6
  @slides = presentation["slides"].map{|slide| GitPresenter::Slide.new(slide["slide"])}
6
- @current_slide = slides.first
7
+ @shell = shell
8
+ @current_slide = find_current_slide
9
+ end
10
+
11
+ def find_current_slide
12
+ sha = @shell.run("git rev-parse HEAD").strip
13
+ @slides.detect{|s| s.commit == sha}
7
14
  end
8
15
 
9
16
  def command_for(command)
@@ -39,12 +46,12 @@ class GitPresenter::Presentation
39
46
  end
40
47
 
41
48
  def exit
42
- `git checkout -q master`
49
+ `git checkout -q #{@branch}`
43
50
  :exit
44
51
  end
45
52
 
46
53
  def position
47
- slides.index(@current_slide)
54
+ @slides.index(@current_slide)
48
55
  end
49
56
 
50
57
  def total_slides
@@ -0,0 +1,5 @@
1
+ class GitPresenter::Shell
2
+ def run(command)
3
+ `#{command}`.strip
4
+ end
5
+ end
@@ -7,7 +7,7 @@ describe "presentation should parse all commits for a repo" do
7
7
 
8
8
  it "should parse more than 10 commits" do
9
9
  @helper.initialise_presentation({:no_of_commits => 13}) do |commits, yaml|
10
- yaml["slides"].length.should eql 13
10
+ expect(yaml["slides"].length).to eql 13
11
11
  end
12
12
  end
13
13
  end
@@ -10,31 +10,37 @@ describe "initializing a presentation" do
10
10
  context ".presentation file" do
11
11
  it "should be written to root directory" do
12
12
  @helper.initialise_presentation do
13
- File.exists?(".presentation").should be_true
13
+ expect(File.exists?(".presentation")).to be_truthy
14
14
  end
15
15
  end
16
16
 
17
17
  it "should have a slides node" do
18
18
  @helper.initialise_presentation do |commits, yaml|
19
- yaml["slides"].should_not be_nil
19
+ expect(yaml["slides"]).not_to be_nil
20
+ end
21
+ end
22
+
23
+ it 'should have a branch note' do
24
+ @helper.initialise_presentation do |commits, yaml|
25
+ expect(yaml["branch"]).not_to be_nil
20
26
  end
21
27
  end
22
28
 
23
29
  it "should contain a line for each commit to the repository" do
24
30
  @helper.initialise_presentation do |commits, yaml|
25
- yaml["slides"].length.should eql commits.length
31
+ expect(yaml["slides"].length).not_to eql commits.length
26
32
  end
27
33
  end
28
34
 
29
35
  it "first line should contain the first commit number" do
30
36
  @helper.initialise_presentation({:delay => true}) do |commits, yaml|
31
- yaml["slides"][0]["slide"]["commit"].should eql commits.first.id
37
+ expect(yaml["slides"][0]["slide"]["commit"]).to eql commits.first.sha
32
38
  end
33
39
  end
34
40
 
35
41
  it "second line should contain the second commit number" do
36
42
  @helper.initialise_presentation({:delay => true}) do |commits, yaml|
37
- yaml["slides"][1]["slide"]["commit"].should eql commits[1].id
43
+ expect(yaml["slides"][1]["slide"]["commit"]).to eql commits[1].sha
38
44
  end
39
45
  end
40
46
  end
@@ -11,7 +11,7 @@ describe "while giving presentation" do
11
11
  it "should move to the next commit" do
12
12
  @helper.start_presentation do |commits, presenter|
13
13
  presenter.execute("next")
14
- @helper.head_position.should eql commits[1].id
14
+ expect(@helper.head_position).to eql commits[1].sha
15
15
  end
16
16
  end
17
17
 
@@ -19,7 +19,7 @@ describe "while giving presentation" do
19
19
  @helper.start_presentation do |commits, presenter|
20
20
  presenter.execute("next")
21
21
  presenter.execute("next")
22
- @helper.head_position.should eql commits[2].id
22
+ expect(@helper.head_position).to eql commits[2].sha
23
23
  end
24
24
  end
25
25
  end
@@ -30,7 +30,7 @@ describe "while giving presentation" do
30
30
  presenter.execute("next")
31
31
  @helper.edit_file("inner commit")
32
32
  presenter.execute("next")
33
- @helper.head_position.should eql commits[2].id
33
+ expect(@helper.head_position).to eql commits[2].sha
34
34
  end
35
35
  end
36
36
  end
@@ -42,7 +42,7 @@ describe "while giving presentation" do
42
42
  presenter.execute("next")
43
43
  presenter.execute("next")
44
44
  presenter.execute("next")
45
- @helper.head_position.should eql commits[2].id
45
+ expect(@helper.head_position).to eql commits[2].sha
46
46
  end
47
47
  end
48
48
  end
@@ -53,7 +53,7 @@ describe "while giving presentation" do
53
53
  presenter.execute("next")
54
54
  presenter.execute("next")
55
55
  presenter.execute("back")
56
- @helper.head_position.should eql commits[1].id
56
+ expect(@helper.head_position).to eql commits[1].sha
57
57
  end
58
58
  end
59
59
  end
@@ -67,7 +67,7 @@ describe "while giving presentation" do
67
67
  presenter.execute("back")
68
68
  presenter.execute("back")
69
69
  presenter.execute("back")
70
- @helper.head_position.should eql commits[0].id
70
+ expect(@helper.head_position).to eql commits[0].sha
71
71
  end
72
72
  end
73
73
  end
@@ -78,7 +78,7 @@ describe "while giving presentation" do
78
78
  presenter.execute("next")
79
79
  presenter.execute("next")
80
80
  presenter.execute("start")
81
- @helper.head_position.should eql commits[0].id
81
+ expect(@helper.head_position).to eql commits[0].sha
82
82
  end
83
83
  end
84
84
  end
@@ -87,7 +87,7 @@ describe "while giving presentation" do
87
87
  it "should move the last commit" do
88
88
  @helper.start_presentation do |commits, presenter|
89
89
  presenter.execute("end")
90
- @helper.head_position.should eql commits.last.id
90
+ expect(@helper.head_position).to eql commits.last.sha
91
91
  end
92
92
  end
93
93
  end
@@ -96,7 +96,7 @@ describe "while giving presentation" do
96
96
  it "should checkout the specific commit" do
97
97
  @helper.start_presentation do |commits, presenter|
98
98
  presenter.execute("2")
99
- @helper.head_position.should eql commits[1].id
99
+ expect(@helper.head_position).to eql commits[1].sha
100
100
  end
101
101
  end
102
102
  end
@@ -104,9 +104,9 @@ describe "while giving presentation" do
104
104
  context "list presentation" do
105
105
  it "should print a list of commits" do
106
106
  @helper.start_presentation do |commits, presenter|
107
- expected_output = (["*#{commits.first.id[0..9]}, #{commits.first.message}"] + commits[1..-1].map{|commit| "#{commit.id[0..9]}, #{commit.message}"}).join("\n")
107
+ expected_output = (["*#{commits.first.sha[0..9]}, #{commits.first.message}"] + commits[1..-1].map{|commit| "#{commit.sha[0..9]}, #{commit.message}"}).join("\n")
108
108
  presentation = presenter.execute("list")
109
- presentation.should eql expected_output
109
+ expect(presentation).to eql expected_output
110
110
  end
111
111
  end
112
112
  end
@@ -127,7 +127,7 @@ help/h: display this message
127
127
  exit: exit from the presentation
128
128
  EOH
129
129
  message = presenter.execute("help")
130
- message.should eql help_text
130
+ expect(message).to eql help_text
131
131
  end
132
132
  end
133
133
  end
@@ -138,7 +138,7 @@ EOH
138
138
  @helper.start_presentation([:run => "echo hello world"]) do |commits, presenter|
139
139
  presenter.execute("next")
140
140
  presenter.execute("next")
141
- presenter.execute("next").strip.should eql "hello world"
141
+ expect(presenter.execute("next").strip).to eql "hello world"
142
142
  end
143
143
  end
144
144
  end
@@ -148,7 +148,7 @@ EOH
148
148
  command_line_helper = CommandLineHelper.capture_output
149
149
  @helper.start_presentation([:run => "echo hello world",:on_slide => 2]) do |commits, presenter|
150
150
  presenter.execute("next")
151
- presenter.execute("next").should eql "#{commits[2].message}\nhello world\n"
151
+ expect(presenter.execute("next")).to eql "#{commits[2].message}\nhello world\n"
152
152
  end
153
153
  end
154
154
  end
@@ -158,7 +158,7 @@ EOH
158
158
  command_line_helper = CommandLineHelper.capture_output
159
159
  @helper.start_presentation do |commits, presenter|
160
160
  presenter.execute("!echo hello world")
161
- command_line_helper.command_output.strip.should end_with "hello world"
161
+ expect(command_line_helper.command_output.strip).to end_with "hello world"
162
162
  end
163
163
  end
164
164
  end
@@ -167,7 +167,7 @@ EOH
167
167
  it "should open the with launchy" do
168
168
  command_line_helper = CommandLineHelper.capture_output
169
169
  @helper.start_presentation([:launch => 'readme', :on_slide => 2]) do |commits, presenter|
170
- Launchy.should_receive(:open).with('readme').once
170
+ expect(Launchy).to receive(:open).with('readme').once
171
171
  presenter.execute("next")
172
172
  presenter.execute("next")
173
173
  end
@@ -179,8 +179,17 @@ EOH
179
179
  @helper.start_presentation do |commits, presenter|
180
180
  presenter.execute("next")
181
181
  presenter.execute("exit")
182
- @helper.current_branch.should_not be_nil
183
- @helper.current_branch.name.should eql "master"
182
+ expect(@helper.current_branch).not_to be_nil
183
+ expect(@helper.current_branch).to eql "master"
184
+ end
185
+ end
186
+ end
187
+
188
+ context "when running in command mode" do
189
+ it "should set the current slide to commit" do
190
+ @helper.start_presentation([], 1) do |commits, presenter|
191
+ presenter.execute("list")
192
+ expect(presenter.current_slide.commit).to eql commits[1].sha
184
193
  end
185
194
  end
186
195
  end
@@ -13,32 +13,32 @@ describe "starting a presentation" do
13
13
  Dir.chdir(presentation_dir) do
14
14
  presenter = GitPresenter.new(".", false)
15
15
  presentation = presenter.execute('start')
16
- presentation.slides.length.should eql 4
16
+ expect(presentation.slides.length).to eql 4
17
17
  end
18
18
  end
19
19
 
20
20
  it "first commit should be first commit in file" do
21
21
  @helper.start_presentation do |commits, presenter|
22
- presenter.slides[0].commit.should eql commits[0].id
22
+ expect(presenter.slides[0].commit).to eql commits[0].sha
23
23
  end
24
24
  end
25
25
 
26
26
  it "second commit should be second commit in file" do
27
27
  @helper.start_presentation do |commits, presenter|
28
- presenter.slides[1].commit.should eql commits[1].id
28
+ expect(presenter.slides[1].commit).to eql commits[1].sha
29
29
  end
30
30
  end
31
31
 
32
32
  it "the last commit should be a command" do
33
33
  command = "echo hello world"
34
34
  @helper.start_presentation([:run => command]) do |commits, presenter|
35
- presenter.slides[3].run.should eql command
35
+ expect(presenter.slides[3].run).to eql command
36
36
  end
37
37
  end
38
38
 
39
39
  it "should have the presentation at first commit" do
40
40
  @helper.start_presentation do |commits, presenter|
41
- @helper.head_position.should eql commits.first.id
41
+ expect(@helper.head_position).to eql commits.first.sha
42
42
  end
43
43
  end
44
44
  end
@@ -10,7 +10,7 @@ describe "update presentation with any new commits" do
10
10
  commits = @helper.initialise_presentation({:delay => true})
11
11
  new_commit = @helper.edit_file_and_commit("forth commit", "d")
12
12
  @helper.update_presentation do |yaml|
13
- yaml["slides"].length.should eql (commits.length + 1)
13
+ expect(yaml["slides"].length).to eql (commits.length + 1)
14
14
  end
15
15
  end
16
16
 
@@ -18,7 +18,7 @@ describe "update presentation with any new commits" do
18
18
  commits = @helper.initialise_presentation({:delay => true})
19
19
  new_commit = @helper.edit_file_and_commit("forth commit", "d")
20
20
  @helper.update_presentation do |yaml|
21
- yaml["slides"].last["slide"]["commit"].should eql new_commit.id
21
+ expect(yaml["slides"].last["slide"]["commit"]).to eql new_commit.sha
22
22
  end
23
23
  end
24
24
 
@@ -27,7 +27,7 @@ describe "update presentation with any new commits" do
27
27
  removed_commit = @helper.remove_from_presentation_at(1)
28
28
  new_commit = @helper.edit_file_and_commit("forth commit", "d")
29
29
  @helper.update_presentation do |yaml|
30
- yaml["slides"].length.should eql (commits.length)
30
+ expect(yaml["slides"].length).to eql (commits.length)
31
31
  end
32
32
  end
33
33
 
@@ -36,7 +36,7 @@ describe "update presentation with any new commits" do
36
36
  removed_commit = @helper.remove_from_presentation_at(1)
37
37
  new_commit = @helper.edit_file_and_commit("forth commit", "d")
38
38
  @helper.update_presentation
39
- @command_line.command_output.should include "Your presentation has been updated"
39
+ expect(@command_line.command_output).to include "Your presentation has been updated"
40
40
  end
41
41
 
42
42
  end
@@ -1,24 +1,37 @@
1
1
  require "spec_helper"
2
2
 
3
+ class FakeShell
4
+ attr_reader :ran_commands
5
+ def initialize()
6
+ @ran_commands = []
7
+ end
8
+ def run(command)
9
+ @ran_commands << command
10
+ end
11
+ end
3
12
  describe GitPresenter::Presentation do
4
13
  let(:presentation){ {"slides" => [
5
14
  {"slide" => {"commit" => "0"}},
6
15
  {"slide" => {"commit" => "1"}},
7
- {"slide" => {"commit" => "2"}}]
16
+ {"slide" => {"commit" => "2"}}],
17
+ "branch" => "test"
8
18
  }
9
19
  }
20
+ let(:fake_shell){ FakeShell.new }
10
21
  context "when displaying the command line" do
11
22
  it "should display the current position" do
12
- presenter = GitPresenter::Presentation.new(presentation)
13
- presenter.status_line.should eql "1/3 >"
23
+ fake_shell.should_receive(:run).with("git rev-parse HEAD").and_return("0")
24
+ presenter = GitPresenter::Presentation.new(presentation, fake_shell)
25
+ expect(presenter.status_line).to eql "1/3 >"
14
26
  end
15
27
  end
16
28
 
17
29
 
18
30
  context "when calculating the position" do
19
31
  it "should return the index of the current commit" do
20
- presenter = GitPresenter::Presentation.new(presentation)
21
- presenter.position.should eql 0
32
+ expect(fake_shell).to receive(:run).with("git rev-parse HEAD").and_return("0")
33
+ presenter = GitPresenter::Presentation.new(presentation, fake_shell)
34
+ expect(presenter.position).to eql 0
22
35
  end
23
36
  end
24
37
 
@@ -29,59 +42,70 @@ describe GitPresenter::Presentation do
29
42
  end
30
43
 
31
44
  context "with bash command" do
32
- it { given_command("!echo hello world").should eql :command }
45
+ it { expect(given_command("!echo hello world")).to eql :command }
33
46
  end
34
47
 
35
48
  context "with next" do
36
- it { given_command("next").should eql :next }
49
+ it { expect(given_command("next")).to eql :next }
37
50
  end
38
51
 
39
52
  context "with next" do
40
- it { given_command("n").should eql :next }
53
+ it { expect(given_command("n")).to eql :next }
41
54
  end
42
55
 
43
56
  context "with back" do
44
- it { given_command("back").should eql :previous }
57
+ it { expect(given_command("back")).to eql :previous }
45
58
  end
46
59
 
47
60
  context "with b" do
48
- it { given_command("b").should eql :previous }
61
+ it { expect(given_command("b")).to eql :previous }
49
62
  end
50
63
 
51
64
  context "with start" do
52
- it { given_command("start").should eql :start }
65
+ it { expect(given_command("start")).to eql :start }
53
66
  end
54
67
 
55
68
  context "with s" do
56
- it { given_command("s").should eql :start }
69
+ it { expect(given_command("s")).to eql :start }
57
70
  end
58
71
 
59
72
  context "with end" do
60
- it { given_command("end").should eql :end }
73
+ it { expect(given_command("end")).to eql :end }
61
74
  end
62
75
 
63
76
  context "with e" do
64
- it { given_command("e").should eql :end }
77
+ it { expect(given_command("e")).to eql :end }
65
78
  end
66
79
 
67
80
  context "with list" do
68
- it { given_command("list").should eql :list }
81
+ it { expect(given_command("list")).to eql :list }
69
82
  end
70
83
 
71
84
  context "with l" do
72
- it { given_command("l").should eql :list }
85
+ it { expect(given_command("l")).to eql :list }
73
86
  end
74
87
 
75
88
  context "with any number" do
76
- it { given_command("6").should eql :commit}
89
+ it { expect(given_command("6")).to eql :commit}
77
90
  end
78
91
 
79
92
  context "with h" do
80
- it { given_command("h").should eql :help}
93
+ it { expect(given_command("h")).to eql :help}
81
94
  end
82
95
 
83
96
  context "with help" do
84
- it { given_command("help").should eql :help}
97
+ it { expect(given_command("help")).to eql :help}
98
+ end
99
+
100
+ context "with exit" do
101
+ it { given_command("exit").should eql :exit}
102
+ it 'checks out to the correct branch' do
103
+ presenter = GitPresenter::Presentation.new(presentation)
104
+
105
+ expect(presenter).to receive(:'`').with("git checkout -q #{presentation['branch']}")
106
+
107
+ presenter.exit
108
+ end
85
109
  end
86
110
  end
87
111
  end