-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assert that gh has been authenticated (#29)
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
spec/create_github_release/assertions/gh_authenticated_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |