Skip to content

Commit

Permalink
Merge pull request #285 from petems/refactor_to_use_to_stdout_matcher
Browse files Browse the repository at this point in the history
Switches `$stdout` redirection to rspec method
  • Loading branch information
petems committed Nov 19, 2017
2 parents b00493d + 03f4d70 commit 0d34686
Show file tree
Hide file tree
Showing 37 changed files with 470 additions and 389 deletions.
4 changes: 2 additions & 2 deletions lib/tugboat/middleware/info_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def call(env)

attribute = env['user_attribute']

droplet_ip4_public = droplet.networks.v4.find { |address| address.type == 'public' }.ip_address
droplet_ip4_public = droplet.networks.v4.find { |address| address.type == 'public' }.ip_address unless droplet.networks.v4.empty?
droplet_ip6_public = droplet.networks.v6.find { |address| address.type == 'public' }.ip_address unless droplet.networks.v6.empty?
check_private_ip = droplet.networks.v4.find { |address| address.type == 'private' }
droplet_private_ip = check_private_ip.ip_address if check_private_ip
Expand Down Expand Up @@ -59,7 +59,7 @@ def call(env)
say "Name: #{droplet.name}"
say "ID: #{droplet.id}"
say "Status: #{status_color}#{droplet.status}#{CLEAR}"
say "IP4: #{droplet_ip4_public}"
say "IP4: #{droplet_ip4_public}" unless droplet.networks.v4.empty?
say "IP6: #{droplet_ip6_public}" unless droplet.networks.v6.empty?

say "Private IP: #{droplet_private_ip}" if droplet_private_ip
Expand Down
16 changes: 7 additions & 9 deletions spec/cli/add_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
to_return(status: 201, body: fixture('create_ssh_key'), headers: {})

cli.options = cli.options.merge(key: ssh_public_key.to_s)
cli.add_key(ssh_key_name)

expect($stdout.string).to eq <<-eos
add_key_with_name_and_keystring = <<-eos
Queueing upload of SSH key 'macbook_pro'...SSH Key uploaded
Name: macbook_pro
ID: 3
eos

expect { cli.add_key(ssh_key_name) }.to output(add_key_with_name_and_keystring).to_stdout

expect(a_request(:post, 'https://api.digitalocean.com/v2/account/keys')).to have_been_made
end

Expand All @@ -52,23 +53,20 @@
headers: { 'Accept' => '*/*', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json' }).
to_return(status: 201, body: fixture('create_ssh_key_from_file'), headers: {})

expect($stdout).to receive(:print).exactly(4).times
expect($stdout).to receive(:print).with('Enter the path to your SSH key: ')
expect($stdout).to receive(:print).with("Queueing upload of SSH key '#{ssh_key_name}'...")
expect($stdin).to receive(:gets).and_return("#{fake_home}/.ssh/id_rsa.pub")

cli.add_key(ssh_key_name)

expect($stdout.string).to eq <<-eos
with_name_prompts_from_file_folder_stdout = <<-eos
Possible public key paths from #{fake_home}/.ssh:
#{fake_home}/.ssh/id_rsa.pub
SSH Key uploaded
Enter the path to your SSH key: Queueing upload of SSH key 'macbook_pro'...SSH Key uploaded
Name: cool_key
ID: 5
eos

expect { cli.add_key(ssh_key_name) }.to output(with_name_prompts_from_file_folder_stdout).to_stdout
end

after do
Expand Down
38 changes: 2 additions & 36 deletions spec/cli/authorize_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,20 @@
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('show_droplets'), headers: {})

expect($stdout).to receive(:print).exactly(6).times
expect($stdout).to receive(:print).with('Enter your access token: ')
expect($stdin).to receive(:gets).and_return(access_token)
expect($stdout).to receive(:print).with('Enter your default timeout for connections in seconds (optional, defaults to 10): ')
expect($stdin).to receive(:gets).and_return(timeout)
expect($stdout).to receive(:print).with('Enter your SSH key path (optional, defaults to ~/.ssh/id_rsa): ')
expect($stdin).to receive(:gets).and_return(ssh_key_path)
expect($stdout).to receive(:print).with('Enter your SSH user (optional, defaults to root): ')
expect($stdin).to receive(:gets).and_return(ssh_user)
expect($stdout).to receive(:print).with('Enter your SSH port number (optional, defaults to 22): ')
expect($stdin).to receive(:gets).and_return(ssh_port)
expect($stdout).to receive(:print).with('Enter your default region (optional, defaults to nyc1): ')
expect($stdin).to receive(:gets).and_return(region)
expect($stdout).to receive(:print).with('Enter your default image ID or image slug (optional, defaults to ubuntu-14-04-x64): ')
expect($stdin).to receive(:gets).and_return(image)
expect($stdout).to receive(:print).with('Enter your default size (optional, defaults to 512mb)): ')
expect($stdin).to receive(:gets).and_return(size)
expect($stdout).to receive(:print).with("Enter your default ssh key IDs (optional, defaults to none, array of IDs of ssh keys eg. ['1234']): ")
expect($stdin).to receive(:gets).and_return(ssh_key_id)
expect($stdout).to receive(:print).with('Enter your default for private networking (optional, defaults to false): ')
expect($stdin).to receive(:gets).and_return(private_networking)
expect($stdout).to receive(:print).with('Enter your default for enabling backups (optional, defaults to false): ')
expect($stdin).to receive(:gets).and_return(backups_enabled)
expect($stdout).to receive(:print).with('Enter your default for IPv6 (optional, defaults to false): ')
expect($stdin).to receive(:gets).and_return(ip6)

cli.authorize

expect($stdout.string).to include('Note: You can get your Access Token from https://cloud.digitalocean.com/settings/tokens/new')
expect($stdout.string).to include("To retrieve region, image, size and key ID's, you can use the corresponding tugboat command, such as `tugboat images`.")
expect($stdout.string).to include('Defaults can be changed at any time in your ~/.tugboat configuration file.')
expect { cli.authorize }.to output(/Note: You can get your Access Token from https:\/\/cloud.digitalocean.com\/settings\/tokens\/new/).to_stdout

config = YAML.load_file(tmp_path)

Expand All @@ -65,37 +48,20 @@
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => %r{Bearer}, 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('show_droplets'), headers: {})

expect($stdout).to receive(:print).exactly(6).times
expect($stdout).to receive(:print).with('Enter your access token: ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default timeout for connections in seconds (optional, defaults to 10): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your SSH key path (optional, defaults to ~/.ssh/id_rsa): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your SSH user (optional, defaults to root): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your SSH port number (optional, defaults to 22): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default region (optional, defaults to nyc1): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default image ID or image slug (optional, defaults to ubuntu-14-04-x64): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default size (optional, defaults to 512mb)): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with("Enter your default ssh key IDs (optional, defaults to none, array of IDs of ssh keys eg. ['1234']): ")
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default for private networking (optional, defaults to false): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default for enabling backups (optional, defaults to false): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default for IPv6 (optional, defaults to false): ')
expect($stdin).to receive(:gets).and_return('')

cli.authorize

expect($stdout.string).to include('Note: You can get your Access Token from https://cloud.digitalocean.com/settings/tokens/new')
expect($stdout.string).to include("To retrieve region, image, size and key ID's, you can use the corresponding tugboat command, such as `tugboat images`.")
expect($stdout.string).to include('Defaults can be changed at any time in your ~/.tugboat configuration file.')
expect { cli.authorize }.to output(/Note: You can get your Access Token from https:\/\/cloud.digitalocean.com\/settings\/tokens\/new/).to_stdout

config = YAML.load_file(tmp_path)

Expand Down
11 changes: 6 additions & 5 deletions spec/cli/config_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

describe 'config' do
it 'shows the full config' do
cli.config

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Current Config\x20
Path: #{Dir.pwd}/tmp/tugboat
---
Expand All @@ -28,13 +26,14 @@
backups_enabled: 'false'
ip6: 'false'
eos

expect { cli.config }.to output(expected_string).to_stdout
end

it 'hides sensitive data if option given' do
cli.options = cli.options.merge(hide: true)
cli.config

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Current Config (Keys Redacted)
Path: #{Dir.pwd}/tmp/tugboat
---
Expand All @@ -55,6 +54,8 @@
backups_enabled: 'false'
ip6: 'false'
eos

expect { cli.config }.to output(expected_string).to_stdout
end
end
end
37 changes: 20 additions & 17 deletions spec/cli/create_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('create_droplet'), headers: {})

cli.create(droplet_name)

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Queueing creation of droplet '#{droplet_name}'...Droplet created! Droplet ID is 3164494
eos

expect { cli.create(droplet_name) }.to output(expected_string).to_stdout
end

it 'with args does not use defaults from configuration' do
Expand All @@ -24,11 +24,12 @@
to_return(status: 200, body: fixture('create_droplet'), headers: {})

cli.options = cli.options.merge(image: 'ubuntu-12-04-x64', size: '1gb', region: 'nyc3', keys: 'foo_bar_key')
cli.create('example.com')

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Queueing creation of droplet 'example.com'...Droplet created! Droplet ID is 3164494
eos

expect { cli.create('example.com') }.to output(expected_string).to_stdout
end

it 'with ip6 enable args' do
Expand All @@ -38,11 +39,12 @@
to_return(status: 200, body: fixture('create_droplet'), headers: {})

cli.options = cli.options.merge(ip6: 'true')
cli.create('example.com')

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Queueing creation of droplet 'example.com'...Droplet created! Droplet ID is 3164494
eos

expect { cli.create('example.com') }.to output(expected_string).to_stdout
end

it 'with user data args' do
Expand All @@ -52,26 +54,28 @@
to_return(status: 200, body: fixture('create_droplet'), headers: {})

cli.options = cli.options.merge(user_data: project_path + '/spec/fixtures/user_data.sh')
cli.create('example.com')

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Queueing creation of droplet 'example.com'...Droplet created! Droplet ID is 3164494
eos

expect { cli.create('example.com') }.to output(expected_string).to_stdout
end

it 'fails when user data file does not exist' do
cli.options = cli.options.merge(user_data: '/foo/bar/baz.sh')
expect { cli.create('example.com') }.to raise_error(SystemExit)

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Queueing creation of droplet 'example.com'...Could not find file: /foo/bar/baz.sh, check your user_data setting
eos

expect { expect { cli.create('example.com') }.to raise_error(SystemExit) }.to output(expected_string).to_stdout
end

context "doesn't create a droplet when mistyping help command" do
['help', '--help', '-h'].each do |help_attempt|
it "tugboat create #{help_attempt}" do
help_text = <<-eos
expected_string = <<-eos
Usage:
rspec create NAME
Expand All @@ -89,8 +93,7 @@
Create a droplet.
eos

cli.create(help_attempt)
expect($stdout.string).to eq help_text
expect { cli.create(help_attempt) }.to output(expected_string).to_stdout
end
end
end
Expand All @@ -101,11 +104,11 @@
headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('create_droplet'), headers: {})

cli.create('somethingblahblah--help')

expect($stdout.string).to eq <<-eos
expected_string = <<-eos
Queueing creation of droplet 'somethingblahblah--help'...Droplet created! Droplet ID is 3164494
eos

expect { cli.create('somethingblahblah--help') }.to output(expected_string).to_stdout
end
end
end
17 changes: 7 additions & 10 deletions spec/cli/debug_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('show_droplets'), headers: {})

cli.droplets

expect($stdout.string).to include 'Started GET request to: https://api.digitalocean.com/v2/droplets?page=1&per_page=200'
expect($stdout.string).to include 'DEBUG -- : Request Headers:'

expect($stdout.string).to include 'Bearer foo'
expect { cli.droplets }.to output(%r{DEBUG -- : Request Headers:}).to_stdout
expect { cli.droplets }.to output(%r{Bearer foo}).to_stdout
expect { cli.droplets }.to output(%r{Started GET request to}).to_stdout
end
end

Expand All @@ -47,12 +44,12 @@
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=200').
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('show_droplets'), headers: {})
cli.droplets

expect($stdout.string).to include 'Started GET request to: https://api.digitalocean.com/v2/droplets?page=1&per_page=200'
expect($stdout.string).to include 'DEBUG -- : Request Headers:'
expect { cli.droplets }.to output(%r{Started GET request to}).to_stdout
expect { cli.droplets }.to output(%r{DEBUG -- : Request Headers:}).to_stdout
expect { cli.droplets }.to output(%r{Bearer \[TOKEN REDACTED\]}).to_stdout

expect($stdout.string).not_to include 'Bearer foo'
expect { cli.droplets }.not_to output(%r{Bearer foo}).to_stdout
end
end
end
Loading

0 comments on commit 0d34686

Please sign in to comment.