Skip to content

Commit 1b3505b

Browse files
authored
Assert that gh has been authenticated (#29)
1 parent c7bd12d commit 1b3505b

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

lib/create_github_release/assertions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Assertions; end
99
end
1010

1111
require_relative 'assertions/bundle_is_up_to_date'
12+
require_relative 'assertions/gh_authenticated'
1213
require_relative 'assertions/gh_command_exists'
1314
require_relative 'assertions/git_command_exists'
1415
require_relative 'assertions/in_git_repo'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'English'
4+
require 'create_github_release/assertion_base'
5+
6+
module CreateGithubRelease
7+
module Assertions
8+
# Assert that the 'gh' command is in the path
9+
#
10+
# @api public
11+
#
12+
class GhAuthenticated < AssertionBase
13+
# Make sure that the 'gh' command is authenticated
14+
#
15+
# @example
16+
# require 'create_github_release'
17+
#
18+
# options = CreateGithubRelease::CommandLineOptions.new { |o| o.release_type = 'major' }
19+
# project = CreateGithubRelease::Project.new(options)
20+
# assertion = CreateGithubRelease::Assertions::GhAuthenticated.new(project)
21+
# begin
22+
# assertion.assert
23+
# puts 'Assertion passed'
24+
# rescue SystemExit
25+
# puts 'Assertion failed'
26+
# end
27+
#
28+
# @return [void]
29+
#
30+
# @raise [SystemExit] if the assertion fails
31+
#
32+
def assert
33+
print 'Checking that the gh command is authenticated...'
34+
output = `gh auth status 2>&1`
35+
if $CHILD_STATUS.success?
36+
puts 'OK'
37+
else
38+
error "gh not authenticated:\n#{output}"
39+
end
40+
end
41+
end
42+
end
43+
end

lib/create_github_release/release_assertions.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def initialize(options)
6060
CreateGithubRelease::Assertions::RemoteReleaseTagDoesNotExist,
6161
CreateGithubRelease::Assertions::LocalReleaseBranchDoesNotExist,
6262
CreateGithubRelease::Assertions::RemoteReleaseBranchDoesNotExist,
63-
CreateGithubRelease::Assertions::GhCommandExists
63+
CreateGithubRelease::Assertions::GhCommandExists,
64+
CreateGithubRelease::Assertions::GhAuthenticated
6465
].freeze
6566

6667
# Run all assertions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe CreateGithubRelease::Assertions::GhAuthenticated do
4+
let(:assertion) { described_class.new(project) }
5+
let(:options) { CreateGithubRelease::CommandLineOptions.new { |o| o.release_type = 'major' } }
6+
let(:project) { CreateGithubRelease::Project.new(options) }
7+
8+
before do
9+
allow(assertion).to receive(:`).with(String) { |command| execute_mocked_command(mocked_commands, command) }
10+
end
11+
12+
describe '#assert' do
13+
subject do
14+
@stdout, @stderr, exception = capture_output { assertion.assert }
15+
raise exception if exception
16+
end
17+
let(:stdout) { @stdout }
18+
let(:stderr) { @stderr }
19+
20+
before do
21+
# allow(File).to receive(:exist?).and_call_original
22+
end
23+
24+
let(:mocked_commands) do
25+
[
26+
MockedCommand.new('gh auth status 2>&1', stdout: stdout, exitstatus: exitstatus)
27+
]
28+
end
29+
30+
context 'when gh command is authenticated' do
31+
let(:stdout) { '' }
32+
let(:exitstatus) { 0 }
33+
it 'should succeed' do
34+
expect { subject }.not_to raise_error
35+
end
36+
end
37+
38+
context 'when gh command is NOT authenticated' do
39+
let(:stdout) { 'XXXERRORXXX' }
40+
let(:exitstatus) { 1 }
41+
it 'should fail' do
42+
expect { subject }.to raise_error(SystemExit)
43+
expect(stderr).to start_with("ERROR: gh not authenticated:\nXXXERRORXXX")
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)