File tree Expand file tree Collapse file tree 4 files changed +93
-1
lines changed
lib/create_github_release
spec/create_github_release/assertions Expand file tree Collapse file tree 4 files changed +93
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ module Assertions; end
99end
1010
1111require_relative 'assertions/bundle_is_up_to_date'
12+ require_relative 'assertions/gh_authenticated'
1213require_relative 'assertions/gh_command_exists'
1314require_relative 'assertions/git_command_exists'
1415require_relative 'assertions/in_git_repo'
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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:\n XXXERRORXXX" )
44+ end
45+ end
46+ end
47+ end
You can’t perform that action at this time.
0 commit comments