Skip to content

Commit c310eb1

Browse files
committed
Adding inspection capability
1 parent d373a41 commit c310eb1

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

GitVersionExe/GemAssets/lib/git_version.rb

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,47 @@ def json
2323
end
2424

2525
def gitversion_exe
26-
@gitversion_exe ||= File.join(File.dirname(__FILE__), '../bin/GitVersion.exe')
26+
@gitversion_exe ||= File.expand_path(File.join(File.dirname(__FILE__), '../bin/GitVersion.exe'))
27+
end
28+
29+
def inspect
30+
unless @json
31+
32+
return <<EOF
33+
#{to_s}
34+
Will invoke #{cmd_string} when first used.
35+
EOF
36+
37+
else
38+
39+
return <<EOF
40+
#{to_s}
41+
Invoked #{cmd_string} and parsed its output:
42+
#{json.inspect}
43+
EOF
44+
45+
end
2746
end
2847

2948
private
3049
def run_gitversion
31-
cmd = [gitversion_exe]
32-
cmd << args
33-
cmd = cmd.flatten.reject(&:nil?)
34-
3550
stdout_and_stderr, status = Open3.capture2e(*cmd)
3651

37-
raise StandardError.new("Failed running #{cmd.join(' ')}, #{status}. We received the following output:\n#{stdout_and_stderr}") unless status.success?
52+
raise StandardError.new("Failed running #{cmd_string}, #{status}. We received the following output:\n#{stdout_and_stderr}") unless status.success?
3853

3954
JSON.parse(stdout_and_stderr)
4055
end
4156

57+
def cmd
58+
cmd = [gitversion_exe]
59+
cmd << args
60+
cmd.flatten.reject(&:nil?)
61+
end
62+
63+
def cmd_string
64+
cmd.join(' ')
65+
end
66+
4267
def pascal_case(str)
4368
str
4469
.to_s

GitVersionExe/GemAssets/spec/lib/git_version_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe GitVersion do
44
describe 'defaults' do
5-
its(:gitversion_exe) { should match(%r|\.\./bin/GitVersion.exe$|) }
5+
its(:gitversion_exe) { should match(%r|/bin/GitVersion.exe$|) }
66
its(:args) { should be_empty }
77
end
88

@@ -80,4 +80,23 @@
8080
end
8181
end
8282
end
83+
84+
describe '#inspect' do
85+
context 'no properties accessed yet' do
86+
it 'writes what will happen' do
87+
expect(subject.inspect).to match(/.+GitVersion.+\nWill invoke .+GitVersion.exe when first used./)
88+
end
89+
end
90+
91+
context 'properties accessed' do
92+
before {
93+
allow(Open3).to receive(:capture2e).and_return(['{ "Sha": 1234 }', OpenStruct.new(success?: true)])
94+
}
95+
96+
it 'writes what happened' do
97+
subject.sha
98+
expect(subject.inspect).to match(/.+GitVersion.+\nInvoked .+GitVersion.exe and parsed its output:\n.+/)
99+
end
100+
end
101+
end
83102
end

0 commit comments

Comments
 (0)