Skip to content

Commit

Permalink
Assert that gh has been authenticated (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouball authored Jan 29, 2023
1 parent c7bd12d commit 1b3505b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/create_github_release/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Assertions; end
end

require_relative 'assertions/bundle_is_up_to_date'
require_relative 'assertions/gh_authenticated'
require_relative 'assertions/gh_command_exists'
require_relative 'assertions/git_command_exists'
require_relative 'assertions/in_git_repo'
Expand Down
43 changes: 43 additions & 0 deletions lib/create_github_release/assertions/gh_authenticated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'English'
require 'create_github_release/assertion_base'

module CreateGithubRelease
module Assertions
# Assert that the 'gh' command is in the path
#
# @api public
#
class GhAuthenticated < AssertionBase
# Make sure that the 'gh' command is authenticated
#
# @example
# require 'create_github_release'
#
# options = CreateGithubRelease::CommandLineOptions.new { |o| o.release_type = 'major' }
# project = CreateGithubRelease::Project.new(options)
# assertion = CreateGithubRelease::Assertions::GhAuthenticated.new(project)
# begin
# assertion.assert
# puts 'Assertion passed'
# rescue SystemExit
# puts 'Assertion failed'
# end
#
# @return [void]
#
# @raise [SystemExit] if the assertion fails
#
def assert
print 'Checking that the gh command is authenticated...'
output = `gh auth status 2>&1`
if $CHILD_STATUS.success?
puts 'OK'
else
error "gh not authenticated:\n#{output}"
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/create_github_release/release_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def initialize(options)
CreateGithubRelease::Assertions::RemoteReleaseTagDoesNotExist,
CreateGithubRelease::Assertions::LocalReleaseBranchDoesNotExist,
CreateGithubRelease::Assertions::RemoteReleaseBranchDoesNotExist,
CreateGithubRelease::Assertions::GhCommandExists
CreateGithubRelease::Assertions::GhCommandExists,
CreateGithubRelease::Assertions::GhAuthenticated
].freeze

# Run all assertions
Expand Down
47 changes: 47 additions & 0 deletions spec/create_github_release/assertions/gh_authenticated_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

RSpec.describe CreateGithubRelease::Assertions::GhAuthenticated do
let(:assertion) { described_class.new(project) }
let(:options) { CreateGithubRelease::CommandLineOptions.new { |o| o.release_type = 'major' } }
let(:project) { CreateGithubRelease::Project.new(options) }

before do
allow(assertion).to receive(:`).with(String) { |command| execute_mocked_command(mocked_commands, command) }
end

describe '#assert' do
subject do
@stdout, @stderr, exception = capture_output { assertion.assert }
raise exception if exception
end
let(:stdout) { @stdout }
let(:stderr) { @stderr }

before do
# allow(File).to receive(:exist?).and_call_original
end

let(:mocked_commands) do
[
MockedCommand.new('gh auth status 2>&1', stdout: stdout, exitstatus: exitstatus)
]
end

context 'when gh command is authenticated' do
let(:stdout) { '' }
let(:exitstatus) { 0 }
it 'should succeed' do
expect { subject }.not_to raise_error
end
end

context 'when gh command is NOT authenticated' do
let(:stdout) { 'XXXERRORXXX' }
let(:exitstatus) { 1 }
it 'should fail' do
expect { subject }.to raise_error(SystemExit)
expect(stderr).to start_with("ERROR: gh not authenticated:\nXXXERRORXXX")
end
end
end
end

0 comments on commit 1b3505b

Please sign in to comment.