Skip to content

Commit ef70638

Browse files
committed
Use Ruby's callable pattern
Rather than bespoke and inconsistent "action" methods for these execution objects, rely on a norm.
1 parent 3413e45 commit ef70638

File tree

7 files changed

+25
-39
lines changed

7 files changed

+25
-39
lines changed

exe/git-tracker

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env ruby
22

33
require "git_tracker"
4-
GitTracker::Runner.execute(*ARGV)
4+
GitTracker::Runner.call(*ARGV)

lib/git_tracker/prepare_commit_message.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module GitTracker
55
class PrepareCommitMessage
66
attr_reader :file, :source, :commit_sha
77

8-
def self.run(file, source = nil, commit_sha = nil)
9-
new(file, source, commit_sha).run
8+
def self.call(file, source = nil, commit_sha = nil)
9+
new(file, source, commit_sha).call
1010
end
1111

1212
def initialize(file, source = nil, commit_sha = nil)
@@ -15,7 +15,7 @@ def initialize(file, source = nil, commit_sha = nil)
1515
@commit_sha = commit_sha
1616
end
1717

18-
def run
18+
def call
1919
exit_when_commit_exists
2020

2121
story = story_number_from_branch

lib/git_tracker/runner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module GitTracker
77
module Runner
8-
def self.execute(cmd_arg = "help", *args)
8+
def self.call(cmd_arg = "help", *args)
99
command = cmd_arg.tr("-", "_")
1010

1111
abort("[git_tracker] command: '#{cmd_arg}' does not exist.") unless respond_to?(command)
@@ -14,7 +14,7 @@ def self.execute(cmd_arg = "help", *args)
1414
end
1515

1616
def self.prepare_commit_msg(*args)
17-
PrepareCommitMessage.run(*args)
17+
PrepareCommitMessage.call(*args)
1818
end
1919

2020
def self.init

lib/git_tracker/standalone.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def build(io)
2222
end
2323
end
2424

25-
io.puts("GitTracker::Runner.execute(*ARGV)")
25+
io.puts("GitTracker::Runner.call(*ARGV)")
2626
io
2727
end
2828

spec/git_tracker/prepare_commit_message_spec.rb

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11
require "git_tracker/prepare_commit_message"
22

33
RSpec.describe GitTracker::PrepareCommitMessage do
4-
subject(:prepare_commit_message) { GitTracker::PrepareCommitMessage }
5-
6-
describe ".run" do
7-
let(:hook) { double("PrepareCommitMessage") }
8-
before do
9-
allow(prepare_commit_message).to receive(:new) { hook }
10-
end
11-
12-
it "runs the hook" do
13-
expect(hook).to receive(:run)
14-
prepare_commit_message.run("FILE1", "hook_source", "sha1234")
15-
end
16-
end
17-
18-
describe ".new" do
4+
describe "initialization" do
195
it "requires the name of the commit message file" do
20-
expect { prepare_commit_message.new }.to raise_error(ArgumentError)
6+
expect { described_class.new }.to raise_error(ArgumentError)
217
end
228

239
it "remembers the name of the commit message file" do
24-
expect(prepare_commit_message.new("FILE1").file).to eq("FILE1")
10+
expect(described_class.new("FILE1").file).to eq("FILE1")
2511
end
2612

2713
it "optionally accepts a message source" do
28-
hook = prepare_commit_message.new("FILE1", "merge").source
14+
hook = described_class.new("FILE1", "merge").source
2915

3016
expect(hook).to eq("merge")
3117
end
3218

3319
it "optionally accepts the SHA-1 of a commit" do
34-
hook = prepare_commit_message.new("FILE1", "commit", "abc1234").commit_sha
20+
hook = described_class.new("FILE1", "commit", "abc1234").commit_sha
3521

3622
expect(hook).to eq("abc1234")
3723
end
3824
end
3925

40-
describe "#run" do
26+
describe "#call" do
4127
let(:hook) { GitTracker::PrepareCommitMessage.new("FILE1") }
4228
let(:commit_message) { double("CommitMessage", append: nil) }
4329

@@ -50,15 +36,15 @@
5036
let(:hook) { described_class.new("FILE2", "commit", "60a086f3") }
5137

5238
it "exits with status code 0" do
53-
expect { hook.run }.to succeed
39+
expect { hook.call }.to succeed
5440
end
5541
end
5642

5743
context "branch name without a Pivotal Tracker story number" do
5844
let(:story) { nil }
5945

6046
it "exits without updating the commit message" do
61-
expect { hook.run }.to succeed
47+
expect { hook.call }.to succeed
6248
expect(commit_message).to_not have_received(:append)
6349
end
6450
end
@@ -71,7 +57,7 @@
7157
end
7258

7359
it "appends the number to the commit message" do
74-
hook.run
60+
hook.call
7561
expect(commit_message).to have_received(:append).with("[#8675309]")
7662
end
7763

@@ -81,7 +67,7 @@
8167
end
8268

8369
it "appends the keyword and the story number" do
84-
hook.run
70+
hook.call
8571
expect(commit_message).to have_received(:append).with("[Delivers #8675309]")
8672
end
8773
end
@@ -92,7 +78,7 @@
9278
end
9379

9480
it "exits without updating the commit message" do
95-
expect { hook.run }.to succeed
81+
expect { hook.call }.to succeed
9682
expect(commit_message).to_not have_received(:append)
9783
end
9884
end

spec/git_tracker/runner_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
subject(:runner) { described_class }
55
let(:args) { ["a_file", "the_source", "sha1234"] }
66

7-
describe ".execute" do
7+
describe "::call" do
88
include OutputHelper
99

1010
before do
@@ -13,20 +13,20 @@
1313

1414
it "runs the hook, passing the args" do
1515
expect(runner).to receive(:prepare_commit_msg).with(*args)
16-
runner.execute("prepare-commit-msg", *args)
16+
runner.call("prepare-commit-msg", *args)
1717
end
1818

1919
it "does not run hooks we do not know about" do
2020
errors = capture_stderr {
21-
expect { runner.execute("non-existent-hook", *args) }.to_not succeed
21+
expect { runner.call("non-existent-hook", *args) }.to_not succeed
2222
}
2323
expect(errors.chomp).to eq("[git_tracker] command: 'non-existent-hook' does not exist.")
2424
end
2525
end
2626

2727
describe ".prepare_commit_msg" do
2828
it "runs the hook, passing the args" do
29-
expect(GitTracker::PrepareCommitMessage).to receive(:run).with(*args)
29+
expect(GitTracker::PrepareCommitMessage).to receive(:call).with(*args)
3030
runner.prepare_commit_msg(*args)
3131
end
3232
end
@@ -43,6 +43,6 @@
4343

4444
it ".help reports that it was run" do
4545
expect(runner).to receive(:puts).with(/git-tracker #{GitTracker::VERSION} is installed\./)
46-
runner.execute("help")
46+
runner.call("help")
4747
end
4848
end

spec/git_tracker/standalone_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
expect(standalone_script).to_not include("module Standalone")
5656
end
5757

58-
it "includes the call to execute the hook" do
59-
expect(standalone_script).to include("GitTracker::Runner.execute(*ARGV)")
58+
it "includes the call to call the hook" do
59+
expect(standalone_script).to include("GitTracker::Runner.call(*ARGV)")
6060
end
6161

6262
it "excludes requiring git_tracker code" do

0 commit comments

Comments
 (0)