hrk 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af5f8cc47627b77665361e134f59b5537b13f29f
4
- data.tar.gz: 312f15c94cad27d4b50d88d9b5de959f820a922a
3
+ metadata.gz: 2a9d0caab344b968fe75fdf67544614eee7c8510
4
+ data.tar.gz: f6bf197cf5943045989c0ff77f0cc5a7174cb88f
5
5
  SHA512:
6
- metadata.gz: 9aecc66431b5a30bc2a4f083b67d40919a506b6bd8b165bc2ddf88ab56f0550986b84eddd969567c778e9324d076d117809478feb931b839d1f0cd491485b327
7
- data.tar.gz: 27b334401d917f7b37bd47cf43af2afd239a65ff1dee66a4823714c951a7e49e971b0a268542b1a9a1f31d543899688118aea6b43275972ade6e86b744286da4
6
+ metadata.gz: 8ad9226be51959e0d073fe622e86e817536e5b59658451576bbc28b2a717b6b23d3caace733612939cd928aab3c4f84f9705bbbedbf7d7d15af5e2ef58387662
7
+ data.tar.gz: bd3fda9fe1058571c9b02f567c1440135bc0a2a54b2418d94979254c32703836af4268268b4407741c583d020fb2c6dfeea8261c86d700c35288cf95d0f5420a
@@ -0,0 +1,25 @@
1
+ module Hrk
2
+ module Execute
3
+ class AskForRemote
4
+ def initialize next_callee
5
+ @next_callee = next_callee
6
+ end
7
+
8
+ def call *args
9
+ @next_callee.call *insure_remote(args)
10
+ end
11
+
12
+ private
13
+
14
+ def insure_remote args
15
+ return args unless remoteless? args
16
+ puts "Please state the remote you want to run these commands on (ex: -r demo):"
17
+ args + STDIN.gets.split(/\s+/)
18
+ end
19
+
20
+ def remoteless? args
21
+ args.reverse.take(2).none? { |a| a =~ /\A-[ar]\Z/ }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -10,26 +10,19 @@ module Hrk
10
10
  end
11
11
 
12
12
  def call *args
13
- remote, command = remote_and_command args
14
- if remote
15
- @env.remote = remote
16
- Hrk::Heroku.new(*remote).call(*command)
17
- else
18
- raise ArgumentError.new("No remote has been previously defined and the command does not explicitely mention a remote") unless @env.remote?
19
- Hrk::Heroku.new(*@env.remote).call(*command)
20
- end
13
+ command, remote = command_and_remote args
14
+ @env.remote = remote
15
+ Hrk::Heroku.new(*remote).call(*command)
21
16
  end
22
17
 
23
- def remote_and_command args
24
- args.slice_before { |arg| arg =~ ARG }.inject([nil, []]) do |r, slice|
25
- if slice.first =~ ARG
26
- raise ArgumentError.new('Remote option without value') if slice.length < 2
27
- raise ArgumentError.new('Too many remotes mentionned') if r.first
28
- [slice.take(2), r.last + slice.drop(2)]
29
- else
30
- [r.first, r.last + slice]
31
- end
32
- end
18
+ private
19
+
20
+ def command_and_remote args
21
+ command, remote, *other_remotes = ([nil] + args).slice_before(ARG).to_a
22
+ raise ArgumentError.new "Too many remotes or app" if other_remotes.any?
23
+ raise ArgumentError.new "No remote is mentionned" unless remote
24
+ raise ArgumentError.new "Incomplete remote option #{remote.first}" if remote.length != 2
25
+ [command.compact, remote]
33
26
  end
34
27
  end
35
28
  end
@@ -1,16 +1,16 @@
1
1
  module Hrk
2
2
  module Execute
3
3
  class ErrorTrap
4
- def initialize callee
5
- @callee = callee
4
+ def initialize next_callee
5
+ @next_callee = next_callee
6
6
  end
7
7
 
8
8
  def call *args
9
9
  if args.include? '--hrk-testing'
10
- @callee.call(*(args - ['--hrk-testing']))
10
+ @next_callee.call(*(args - ['--hrk-testing']))
11
11
  else
12
12
  begin
13
- @callee.call(*args)
13
+ @next_callee.call(*args)
14
14
  rescue
15
15
  puts "Error: #{$!.message}"
16
16
  false
@@ -1,15 +1,15 @@
1
1
  module Hrk
2
2
  module Execute
3
3
  class Help
4
- def initialize fallback = nil
5
- @fallback = fallback
4
+ def initialize next_callee = nil
5
+ @next_callee = next_callee
6
6
  end
7
7
 
8
8
  def call *args
9
9
  if args.empty? || args.first =~ %r(\A(?:(?:(?:-)?h)|(?:(?:--)?help))\Z)
10
10
  display || true
11
11
  else
12
- fallback(*args)
12
+ next_callee(*args)
13
13
  end
14
14
  end
15
15
 
@@ -44,9 +44,9 @@ https://github.com/Bastes/hrk
44
44
  eos
45
45
  end
46
46
 
47
- def fallback *args
48
- if @fallback
49
- @fallback.call(*args)
47
+ def next_callee *args
48
+ if @next_callee
49
+ @next_callee.call(*args)
50
50
  else
51
51
  raise ArgumentError.new
52
52
  end
@@ -1,13 +1,13 @@
1
1
  module Hrk
2
2
  module Execute
3
3
  class HerokuDetector
4
- def initialize callee
5
- @callee = callee
4
+ def initialize next_callee
5
+ @next_callee = next_callee
6
6
  end
7
7
 
8
8
  def call *args
9
9
  if heroku_present?
10
- @callee.call(*args)
10
+ @next_callee.call(*args)
11
11
  else
12
12
  puts <<-eos
13
13
  Error: The heroku command is missing!
@@ -0,0 +1,24 @@
1
+ module Hrk
2
+ module Execute
3
+ class Remember
4
+ def initialize next_callee
5
+ @next_callee = next_callee
6
+ end
7
+
8
+ def call *args
9
+ env = Hrk::Env.new
10
+ if remoteless?(args) && env.remote?
11
+ @next_callee.call(*args, *env.remote)
12
+ else
13
+ @next_callee.call(*args)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def remoteless? args
20
+ args.reverse.take(2).none? { |a| a =~ /\A-[ar]\Z/ }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,9 +1,9 @@
1
1
  module Hrk
2
2
  module Execute
3
3
  class RemoteDisplay
4
- def initialize fallback
4
+ def initialize next_callee
5
5
  @env = Hrk::Env.new
6
- @fallback = fallback
6
+ @next_callee = next_callee
7
7
  end
8
8
 
9
9
  def call *args
@@ -15,7 +15,7 @@ module Hrk
15
15
  false
16
16
  end
17
17
  else
18
- @fallback.call(*args)
18
+ @next_callee.call(*args)
19
19
  end
20
20
  end
21
21
  end
@@ -0,0 +1,26 @@
1
+ module Hrk
2
+ module Execute
3
+ class RemoteLast
4
+ ARG = /\A-[ar]\Z/
5
+
6
+ def initialize next_callee
7
+ @next_callee = next_callee
8
+ end
9
+
10
+ def call *args
11
+ @next_callee.call(*order(args))
12
+ end
13
+
14
+ def order args
15
+ args.slice_before { |arg| arg =~ ARG }.inject([[], nil]) do |r, slice|
16
+ if slice.first =~ ARG
17
+ raise ArgumentError.new("Too many remotes mentionned") if r.last
18
+ [r.first + slice.drop(2), slice.take(2)]
19
+ else
20
+ [r.first + slice, r.last]
21
+ end
22
+ end.flatten.compact
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/hrk/execute.rb CHANGED
@@ -1,17 +1,27 @@
1
1
  module Hrk
2
2
  module Execute
3
- autoload :Command, 'hrk/execute/command'
4
- autoload :Help, 'hrk/execute/help'
5
- autoload :ErrorTrap, 'hrk/execute/error_trap'
6
- autoload :HerokuDetector, 'hrk/execute/heroku_detector'
7
- autoload :RemoteDisplay, 'hrk/execute/remote_display'
3
+ autoload :Command, "hrk/execute/command"
4
+ autoload :AskForRemote, "hrk/execute/ask_for_remote"
5
+ autoload :Remember, "hrk/execute/remember"
6
+ autoload :RemoteLast, "hrk/execute/remote_last"
7
+ autoload :Help, "hrk/execute/help"
8
+ autoload :ErrorTrap, "hrk/execute/error_trap"
9
+ autoload :HerokuDetector, "hrk/execute/heroku_detector"
10
+ autoload :RemoteDisplay, "hrk/execute/remote_display"
8
11
 
9
12
  def self.call *args
10
13
  executer.call(*args)
11
14
  end
12
15
 
13
16
  def self.executer
14
- ErrorTrap.new HerokuDetector.new RemoteDisplay.new Help.new Command.new
17
+ ErrorTrap.new \
18
+ HerokuDetector.new \
19
+ RemoteDisplay.new \
20
+ Help.new \
21
+ RemoteLast.new \
22
+ Remember.new \
23
+ AskForRemote.new \
24
+ Command.new
15
25
  end
16
26
  end
17
27
  end
data/lib/hrk/heroku.rb CHANGED
@@ -6,20 +6,20 @@ module Hrk
6
6
 
7
7
  def call *command
8
8
  validate! command
9
- puts "Executing `#{(['heroku'] + command + @remote).join ' '}`..."
10
- exec 'heroku', *(command + @remote)
9
+ puts "Executing `#{(["heroku"] + command + @remote).join " "}`..."
10
+ exec "heroku", *(command + @remote)
11
11
  end
12
12
 
13
13
  private
14
14
 
15
15
  def validate! command
16
- remote = (command.each_cons(2).detect { |(parameter, _)| parameter =~ %r{\A-[ar]\Z} }.join ' ' rescue nil)
16
+ remote = (command.each_cons(2).detect { |(parameter, _)| parameter =~ %r{\A-[ar]\Z} }.join " " rescue nil)
17
17
  raise ExplicitApplicationError.new, "You're calling a command on remote #{@remote.join " "} yet the command explicitly references #{remote}" if remote
18
18
  end
19
19
 
20
20
  def exec *command
21
21
  Signal.trap("INT") {}
22
- Process.wait fork { Kernel.exec *command }
22
+ Process.wait fork { Kernel.exec(*command) }
23
23
  $?.success?.tap { Signal.trap("INT", "DEFAULT") }
24
24
  end
25
25
 
data/lib/hrk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hrk
2
- VERSION = '1.0.6'
2
+ VERSION = '1.0.7'
3
3
  end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Hrk::Execute::AskForRemote do
4
+ describe "#call" do
5
+ let(:next_callee) { double(Hrk::Execute::Command) }
6
+ let(:env) { double(Hrk::Env) }
7
+
8
+ subject(:ask_for_remote) { Hrk::Execute::AskForRemote.new next_callee }
9
+
10
+ let(:users_remote) { [%w(-a -r).sample, "remote-#{rand(0..9)}"] }
11
+ let(:users_input) { users_remote.join(" ") }
12
+
13
+ before { allow(next_callee).to receive(:call) }
14
+ before { allow(Hrk::Env).to receive(:new).and_return(env) }
15
+ before { allow(ask_for_remote).to receive(:puts) }
16
+ before { allow(STDIN).to receive(:gets).and_return(users_input) }
17
+ before { ask_for_remote.call(*command) }
18
+
19
+ context "there was no remote" do
20
+ let(:command) { %w(run console) }
21
+
22
+ it { expect(ask_for_remote).to have_received(:puts) }
23
+ it { expect(next_callee).to have_received(:call).with(*command, *users_remote) }
24
+ end
25
+
26
+ context "there was a remote" do
27
+ let(:command) { %w(logs -t) + [%w(-a -r).sample, "remote-#{rand(0..9)}"] }
28
+
29
+ it { expect(ask_for_remote).not_to have_received(:puts) }
30
+ it { expect(STDIN).not_to have_received(:gets) }
31
+ it { expect(next_callee).to have_received(:call).with(*command) }
32
+ end
33
+ end
34
+ end
@@ -1,7 +1,7 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  RSpec.describe Hrk::Execute::Command do
4
- describe '#call' do
4
+ describe "#call" do
5
5
  subject(:command) { Hrk::Execute::Command.new }
6
6
 
7
7
  let(:heroku) { double("Hrk::Heroku") }
@@ -10,10 +10,10 @@ RSpec.describe Hrk::Execute::Command do
10
10
 
11
11
  before { allow(command.env).to receive(:remote=) }
12
12
 
13
- context 'no remote was previously memorized' do
13
+ context "no remote was previously memorized" do
14
14
  before { allow(command.env).to receive(:remote?).and_return(nil) }
15
15
 
16
- describe 'standard case (a remote, a command)' do
16
+ describe "standard case (a remote, a command)" do
17
17
  let(:remote) { "remote-#{rand(1..9)}" }
18
18
  let(:args) { %w(whatever that:may -b) }
19
19
 
@@ -23,12 +23,12 @@ RSpec.describe Hrk::Execute::Command do
23
23
  context "when the command returns #{result}" do
24
24
  before { allow(heroku).to receive(:call).and_return(result) }
25
25
 
26
- it { expect(command.call(opt, remote, *args)).to eq result }
26
+ it { expect(command.call(*args, opt, remote)).to eq result }
27
27
  end
28
28
  end
29
29
 
30
- describe 'interactions' do
31
- before { command.call(opt, remote, *args) }
30
+ describe "interactions" do
31
+ before { command.call(*args, opt, remote) }
32
32
 
33
33
  it { expect(Hrk::Heroku).to have_received(:new).with(opt, remote) }
34
34
  it { expect(heroku).to have_received(:call).with(*%w{whatever that:may -b}) }
@@ -38,103 +38,47 @@ RSpec.describe Hrk::Execute::Command do
38
38
  end
39
39
  end
40
40
 
41
- describe 'edge cases (no remote, improper remote, too many remotes...)' do
42
- let(:other_args) { %W(something-#{rand(1..9)} -o r --another) }
43
-
44
- before { expect(heroku).not_to receive(:call) }
45
- before { expect(command.env).not_to receive(:remote=) }
46
-
47
- it { expect { command.call }.to raise_error ArgumentError }
48
- it { expect { command.call "parameterless-remote", *other_args }.to raise_error ArgumentError }
49
- it { expect { command.call(*other_args) }.to raise_error ArgumentError }
50
- it { expect { command.call(*other_args, '-r') }.to raise_error ArgumentError }
51
- it { expect { command.call(*other_args, '-a') }.to raise_error ArgumentError }
52
- it { expect { command.call '-r', 'remote', *other_args, '-r', 'other-remote' }.to raise_error ArgumentError }
53
- it { expect { command.call '-r', 'remote', *other_args, '-a', 'app' }.to raise_error ArgumentError }
54
- it { expect { command.call '-a', 'app', *other_args, '-a', 'other-app' }.to raise_error ArgumentError }
55
- it { expect { command.call '-r', 'something', *other_args, '-a', 'something-else' }.to raise_error ArgumentError }
56
- end
57
- end
41
+ describe "edge cases" do
42
+ describe "a remote, nocommand" do
43
+ let(:remote) { "remote-#{rand(1..9)}" }
58
44
 
59
- context 'a remote was previously memorized' do
60
- let(:previous_remote) { "ye-olde-remote#{rand(1..9)}" }
61
- before { allow(command.env).to receive(:remote).and_return(['-r', previous_remote]) }
62
- before { allow(command.env).to receive(:remote?).and_return(true) }
45
+ %w(-a -r).each do |opt|
46
+ context "using #{opt}" do
47
+ [true, false].each do |result|
48
+ context "when the command returns #{result}" do
49
+ before { allow(heroku).to receive(:call).and_return(result) }
63
50
 
64
- describe 'standard case (a remote, a command)' do
65
- let(:remote) { "remote-#{rand(1..9)}" }
66
- let(:args) { %w(whatever that:may -b) }
51
+ it { expect(command.call(opt, remote)).to eq result }
52
+ end
53
+ end
67
54
 
68
- [true, false].each do |result|
69
- describe "when the command returns #{result}" do
70
- before { allow(heroku).to receive(:call).and_return(result) }
55
+ describe "interactions" do
56
+ before { command.call(opt, remote) }
71
57
 
72
- it { expect(command.call('-r', remote, *args)).to eq result }
58
+ it { expect(Hrk::Heroku).to have_received(:new).with(opt, remote) }
59
+ it { expect(heroku).to have_received(:call).with no_args }
60
+ it { expect(command.env).to have_received(:remote=).with([opt, remote]) }
61
+ end
62
+ end
73
63
  end
74
64
  end
75
65
 
76
- describe 'interactions' do
77
- before { command.call '-r', remote, *args }
66
+ describe "no remote, improper remote..." do
67
+ let(:other_args) { %W(something-#{rand(1..9)} -o r --another) }
78
68
 
79
- it { expect(Hrk::Heroku).to have_received(:new).with('-r', remote) }
80
- it { expect(heroku).to have_received(:call).with(*%w{whatever that:may -b}) }
81
- it { expect(command.env).to have_received(:remote=).with(['-r', remote]) }
82
- end
83
- end
84
-
85
- describe 'use previous remote' do
86
- let(:args) { %w(whatever that:may -b) }
69
+ before { expect(heroku).not_to receive(:call) }
70
+ before { expect(command.env).not_to receive(:remote=) }
87
71
 
88
- [true, false].each do |result|
89
- describe "when the command returns #{result}" do
90
- before { allow(heroku).to receive(:call).and_return(result) }
91
-
92
- it { expect(command.call(*args)).to eq result }
93
- end
94
- end
95
-
96
- describe 'interactions' do
97
- before { command.call(*args) }
98
-
99
- it { expect(Hrk::Heroku).to have_received(:new).with('-r', previous_remote) }
100
- it { expect(heroku).to have_received(:call).with(*%w{whatever that:may -b}) }
72
+ it { expect { command.call }.to raise_error ArgumentError }
73
+ it { expect { command.call(*other_args) }.to raise_error ArgumentError }
74
+ it { expect { command.call(*other_args, "-r") }.to raise_error ArgumentError }
75
+ it { expect { command.call(*other_args, "-a") }.to raise_error ArgumentError }
76
+ it { expect { command.call(*other_args, *%w(-r demo -r prod)) }.to raise_error ArgumentError }
77
+ it { expect { command.call(*other_args, *%w(-a app -a other-app)) }.to raise_error ArgumentError }
78
+ it { expect { command.call(*other_args, *%w(-r staging -a server)) }.to raise_error ArgumentError }
79
+ it { expect { command.call(*other_args, *%w(-a machine -r dennis)) }.to raise_error ArgumentError }
101
80
  end
102
81
  end
103
-
104
- describe 'edge cases (improper remote, too many remotes...)' do
105
- let(:other_args) { %W(--whatever#{rand(1..9)} dude) }
106
-
107
- before { expect(heroku).not_to receive(:call) }
108
- before { expect(command.env).not_to receive(:remote=) }
109
-
110
- it { expect { command.call(*other_args, '-r') }.to raise_error ArgumentError }
111
- it { expect { command.call(*other_args, '-a') }.to raise_error ArgumentError }
112
- it { expect { command.call('-r', 'remote', *other_args, '-r', 'other-remote') }.to raise_error ArgumentError }
113
- it { expect { command.call('-r', 'remote', *other_args, '-a', 'app') }.to raise_error ArgumentError }
114
- it { expect { command.call('-a', 'app', *other_args, '-a', 'other-app') }.to raise_error ArgumentError }
115
- it { expect { command.call('-r', 'something', *other_args, '-a', 'something-else') }.to raise_error ArgumentError }
116
- end
117
- end
118
- end
119
-
120
- describe '#remote_and_command' do
121
- subject(:command) { Hrk::Execute::Command.new }
122
-
123
- context '(standard cases, arguments parsed)' do
124
- it { expect(command.remote_and_command(%w(run rake db:migrate))).to eq [nil, %w(run rake db:migrate)] }
125
- it { expect(command.remote_and_command(%w(restart))).to eq [nil, %w(restart)] }
126
- it { expect(command.remote_and_command(%w(-r demo restart))).to eq [%w(-r demo), %w(restart)] }
127
- it { expect(command.remote_and_command(%w(run rake db:migrate -r prod))).to eq [%w(-r prod), %w(run rake db:migrate)] }
128
- it { expect(command.remote_and_command(%w(some command -r test -i dont know --about))).to eq [%w(-r test), %w(some command -i dont know --about)] }
129
- end
130
-
131
- context '(edge cases, argument errors)' do
132
- it { expect { command.remote_and_command(%w(-r staging logs -t -r test)) }.to raise_error ArgumentError }
133
- it { expect { command.remote_and_command(%w(-a prod run rake db:migrate -r echo)) }.to raise_error ArgumentError }
134
- it { expect { command.remote_and_command(%w(run console -r demo -a app)) }.to raise_error ArgumentError }
135
- it { expect { command.remote_and_command(%w(-a one -a other maintenance:on)) }.to raise_error ArgumentError }
136
- it { expect { command.remote_and_command(%w(maintenance:off -a)) }.to raise_error ArgumentError }
137
- it { expect { command.remote_and_command(%w(restart -r)) }.to raise_error ArgumentError }
138
82
  end
139
83
  end
140
84
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Hrk::Execute::ErrorTrap do
4
4
  describe '#call' do
5
- let(:next_callee) { double }
5
+ let(:next_callee) { double(Hrk::Execute::Command) }
6
6
 
7
7
  subject(:error_trap) { Hrk::Execute::ErrorTrap.new(next_callee) }
8
8
 
@@ -2,27 +2,50 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Hrk::Execute::Help do
4
4
  describe '#call' do
5
- subject(:help) { Hrk::Execute::Help.new }
5
+ subject(:help) { Hrk::Execute::Help.new the_next_callee_or_not }
6
6
 
7
7
  describe 'with the right arguments' do
8
- before { expect(help).to receive :display }
9
-
10
- it { expect(help.call).to eq true }
11
- it { expect(help.call('h')).to eq true }
12
- it { expect(help.call('-h')).to eq true }
13
- it { expect(help.call('help')).to eq true }
14
- it { expect(help.call('--help')).to eq true }
8
+ [true, false].each do |there_is_one|
9
+ context "and #{there_is_one ? "a" : "no"} next callee" do
10
+ if there_is_one
11
+ let(:the_next_callee_or_not) { double }
12
+ before { allow(the_next_callee_or_not).to receive(:call).never }
13
+ else
14
+ let(:the_next_callee_or_not) { nil }
15
+ end
16
+
17
+ before { expect(help).to receive :display }
18
+
19
+ it { expect(help.call).to eq true }
20
+ it { expect(help.call('h')).to eq true }
21
+ it { expect(help.call('-h')).to eq true }
22
+ it { expect(help.call('help')).to eq true }
23
+ it { expect(help.call('--help')).to eq true }
24
+ end
25
+ end
15
26
  end
16
27
 
17
- describe 'with wrong arguments' do
28
+ describe 'with other arguments' do
18
29
  [
19
30
  %w(run rake db:migrate),
20
31
  %w(helpe),
21
32
  %w(ahelp)
22
33
  ].each do |args|
23
- before { allow(help).to receive(:fallback).with(*args).and_return :fallouts }
34
+ context "and a next callee" do
35
+ let(:the_next_callee_or_not) { double }
36
+ before { allow(the_next_callee_or_not).to receive(:call).and_return :fallouts }
37
+
38
+ it do
39
+ expect(help.call(*args)).to eq :fallouts
40
+ expect(the_next_callee_or_not).to have_received(:call).with(*args)
41
+ end
42
+ end
24
43
 
25
- it { expect(help.call(*args)).to eq :fallouts}
44
+ context "and no next callee" do
45
+ let(:the_next_callee_or_not) { nil }
46
+
47
+ it { expect { help.call(*args) }.to raise_error ArgumentError }
48
+ end
26
49
  end
27
50
  end
28
51
  end
@@ -35,23 +58,4 @@ RSpec.describe Hrk::Execute::Help do
35
58
 
36
59
  it { expect(help).to have_received(:puts).at_least(1).times }
37
60
  end
38
-
39
- describe '#fallback' do
40
- describe 'without a fallback command' do
41
- subject(:help) { Hrk::Execute::Help.new }
42
-
43
- it { expect { help.fallback(*%w(whatever arguments it received)) }.to raise_error ArgumentError }
44
- end
45
-
46
- describe 'with a fallback command' do
47
- let(:fallback) { double }
48
- before { allow(fallback).to receive :call }
49
-
50
- subject(:help) { Hrk::Execute::Help.new fallback }
51
-
52
- before { expect(fallback).to receive(:call).with(*%w(whatever arguments it received)).and_return(:some_result) }
53
-
54
- it { expect(help.fallback(*%w(whatever arguments it received))).to eq :some_result }
55
- end
56
- end
57
61
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Hrk::Execute::HerokuDetector do
4
4
  describe '#call' do
5
- let(:next_callee) { double }
5
+ let(:next_callee) { double(Hrk::Execute::Command) }
6
6
 
7
7
  subject(:heroku_detector) { Hrk::Execute::HerokuDetector.new(next_callee) }
8
8
 
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Hrk::Execute::Remember do
4
+ describe '#call' do
5
+ let(:next_callee) { double(Hrk::Execute::Command) }
6
+
7
+ subject(:remember) { Hrk::Execute::Remember.new next_callee }
8
+ subject(:env) { double(Hrk::Env) }
9
+
10
+ before { allow(next_callee).to receive(:call) }
11
+ before { allow(Hrk::Env).to receive(:new).and_return(env) }
12
+
13
+ def self.receiving args, it_should_pass_on: []
14
+ context "receiving #{args.join " "}" do
15
+ let(:what_it_should_pass_on) do
16
+ if it_should_pass_on.respond_to?(:call)
17
+ instance_exec(&it_should_pass_on)
18
+ else
19
+ it_should_pass_on
20
+ end
21
+ end
22
+ subject!(:result) { remember.call(*args) }
23
+
24
+ it { expect(next_callee).to have_received(:call).with(*what_it_should_pass_on) }
25
+ end
26
+ end
27
+
28
+ context "no remote memorized" do
29
+ before { allow(env).to receive(:remote?).and_return(false) }
30
+ before { allow(env).to receive(:remote).and_return(nil) }
31
+
32
+ context "a remote in the args" do
33
+ receiving %w(logs -t -r demo), it_should_pass_on: %w(logs -t -r demo)
34
+ receiving %w(run rake db:migrate -a that-one-cloud), it_should_pass_on: %w(run rake db:migrate -a that-one-cloud)
35
+ end
36
+
37
+ context "no remote in the args" do
38
+ receiving %w(run rake db:rollback), it_should_pass_on: %w(run rake db:rollback)
39
+ receiving %w(run console), it_should_pass_on: %w(run console)
40
+ end
41
+
42
+ context "part of a remote in the args" do
43
+ receiving %w(maintenance:off -r), it_should_pass_on: %w(maintenance:off -r)
44
+ receiving %w(logs -a), it_should_pass_on: %w(logs -a)
45
+ end
46
+ end
47
+
48
+ context "a remote memorized" do
49
+ let(:remote) { [%w(-r -a).sample, %w(demo staging prod test app).sample] }
50
+ before { allow(env).to receive(:remote?).and_return(true) }
51
+ before { allow(env).to receive(:remote).and_return(remote) }
52
+
53
+ context "a remote in the args" do
54
+ receiving %w(run rake some:task -r a-remote), it_should_pass_on: %w(run rake some:task -r a-remote)
55
+ receiving %w(maintenance:on -a another-app), it_should_pass_on: %w(maintenance:on -a another-app)
56
+ end
57
+
58
+ context "no remote in the args" do
59
+ receiving %w(restart), it_should_pass_on: -> { %w(restart) + remote }
60
+ end
61
+
62
+ context "part of a remote in the args" do
63
+ receiving %w(run console -r), it_should_pass_on: %w(run console -r)
64
+ receiving %w(maintenance:on -a), it_should_pass_on: %w(maintenance:on -a)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -2,16 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Hrk::Execute::RemoteDisplay do
4
4
  describe '#call' do
5
- let(:fallback) { double }
6
- let(:fallback_result) { [true, false].sample }
7
- let(:remote) { [%w(-a -r).sample, "demo#{rand(1..9)}"] }
8
- let(:there_be_a_remote) { [true, false].sample }
5
+ let(:next_callee) { double }
6
+ let(:next_callee_result) { [true, false].sample }
7
+ let(:remote) { [%w(-a -r).sample, "demo#{rand(1..9)}"] }
8
+ let(:there_be_a_remote) { [true, false].sample }
9
9
 
10
- subject(:remote_display) { Hrk::Execute::RemoteDisplay.new(fallback) }
10
+ subject(:remote_display) { Hrk::Execute::RemoteDisplay.new(next_callee) }
11
11
 
12
12
  before { allow_any_instance_of(Hrk::Env).to receive(:remote?).and_return(there_be_a_remote) }
13
13
  before { allow_any_instance_of(Hrk::Env).to receive(:remote).and_return(remote) }
14
- before { allow(fallback).to receive(:call).and_return(fallback_result) }
14
+ before { allow(next_callee).to receive(:call).and_return(next_callee_result) }
15
15
  before { allow(remote_display).to receive(:puts) }
16
16
 
17
17
  subject!(:result) { remote_display.call(*args) }
@@ -24,7 +24,7 @@ RSpec.describe Hrk::Execute::RemoteDisplay do
24
24
 
25
25
  it { expect(result).to eq true }
26
26
  it { expect(remote_display).to have_received(:puts).with(remote.join(' ')) }
27
- it { expect(fallback).not_to have_received(:call) }
27
+ it { expect(next_callee).not_to have_received(:call) }
28
28
  end
29
29
 
30
30
  context 'and no remote' do
@@ -32,16 +32,16 @@ RSpec.describe Hrk::Execute::RemoteDisplay do
32
32
 
33
33
  it { expect(result).to eq false }
34
34
  it { expect(remote_display).not_to have_received(:puts) }
35
- it { expect(fallback).not_to have_received(:call) }
35
+ it { expect(next_callee).not_to have_received(:call) }
36
36
  end
37
37
  end
38
38
 
39
39
  context 'there are arguments' do
40
40
  let(:args) { %W(what#{rand(1..9)} --will we --do -to -- the drunken --whaler?) }
41
41
 
42
- it { expect(result).to eq fallback_result }
42
+ it { expect(result).to eq next_callee_result }
43
43
  it { expect(remote_display).not_to have_received(:puts) }
44
- it { expect(fallback).to have_received(:call).with(*args) }
44
+ it { expect(next_callee).to have_received(:call).with(*args) }
45
45
  end
46
46
  end
47
47
  end
@@ -0,0 +1,60 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Hrk::Execute::RemoteLast do
4
+ describe "#call" do
5
+ let(:next_callee) { double(Hrk::Execute::Command) }
6
+
7
+ subject(:remote_last) { Hrk::Execute::RemoteLast.new next_callee }
8
+
9
+ before { allow(next_callee).to receive(:call) }
10
+
11
+ context "normal cases" do
12
+ def self.receiving args, it_should_pass_on: []
13
+ context "receiving #{args.join " "}" do
14
+ subject!(:result) { remote_last.call(*args) }
15
+
16
+ it { expect(next_callee).to have_received(:call).with(*it_should_pass_on) }
17
+ end
18
+ end
19
+
20
+ receiving %w(run console -r demo),
21
+ it_should_pass_on: %w(run console -r demo)
22
+ receiving %w(logs -t -a some-app-prod),
23
+ it_should_pass_on: %w(logs -t -a some-app-prod)
24
+ receiving %w(-r test run rake db:migrate),
25
+ it_should_pass_on: %w(run rake db:migrate -r test)
26
+ receiving %w(-a another-app-demo config),
27
+ it_should_pass_on: %w(config -a another-app-demo)
28
+ receiving %w(run -r testing rake db:migrate),
29
+ it_should_pass_on: %w(run rake db:migrate -r testing)
30
+ receiving %w(config:set -a the-main-app whatever="some_value"),
31
+ it_should_pass_on: %w(config:set whatever="some_value" -a the-main-app)
32
+ receiving %w(rake db:rollback),
33
+ it_should_pass_on: %w(rake db:rollback)
34
+ receiving %w(run console -a),
35
+ it_should_pass_on: %w(run console -a)
36
+ receiving %w(logs -t -r),
37
+ it_should_pass_on: %w(logs -t -r)
38
+ end
39
+
40
+ context "edge cases" do
41
+ def self.receiving args, it_should_raise: ArgumentError
42
+ context "receiving #{args.join " "}" do
43
+ subject(:the_command) { -> { remote_last.call(*args) } }
44
+
45
+ it { expect(&the_command).to raise_error it_should_raise }
46
+
47
+ context "the next callee" do
48
+ before { the_command.call rescue nil }
49
+
50
+ it { expect(next_callee).not_to have_received(:call) }
51
+ end
52
+ end
53
+ end
54
+
55
+ receiving %w(-r test run console -r demo), it_should_raise: ArgumentError
56
+ receiving %w(rake -a my-way db:migrate -a my-way-tmp), it_should_raise: ArgumentError
57
+ receiving %w(-a super-app -r staging rake db:migrate), it_should_raise: ArgumentError
58
+ end
59
+ end
60
+ end
@@ -22,13 +22,19 @@ RSpec.describe Hrk::Execute do
22
22
  let(:heroku_detector) { double(Hrk::Execute::HerokuDetector) }
23
23
  let(:remote_display) { double(Hrk::Execute::RemoteDisplay) }
24
24
  let(:help) { double(Hrk::Execute::Help) }
25
+ let(:remote_last) { double(Hrk::Execute::RemoteLast) }
26
+ let(:remember) { double(Hrk::Execute::Remember) }
27
+ let(:ask_for_remote) { double(Hrk::Execute::AskForRemote) }
25
28
  let(:command) { double(Hrk::Execute::Command) }
26
29
 
27
- before { allow(Hrk::Execute::Command).to receive(:new).and_return(command) }
28
- before { allow(Hrk::Execute::Help).to receive(:new).with(command).and_return(help) }
29
- before { allow(Hrk::Execute::RemoteDisplay).to receive(:new).with(help).and_return(remote_display) }
30
+ before { allow(Hrk::Execute::Command). to receive(:new).and_return(command) }
31
+ before { allow(Hrk::Execute::AskForRemote). to receive(:new).with(command).and_return(ask_for_remote) }
32
+ before { allow(Hrk::Execute::Remember). to receive(:new).with(ask_for_remote).and_return(remember) }
33
+ before { allow(Hrk::Execute::RemoteLast). to receive(:new).with(remember).and_return(remote_last) }
34
+ before { allow(Hrk::Execute::Help). to receive(:new).with(remote_last).and_return(help) }
35
+ before { allow(Hrk::Execute::RemoteDisplay). to receive(:new).with(help).and_return(remote_display) }
30
36
  before { allow(Hrk::Execute::HerokuDetector).to receive(:new).with(remote_display).and_return(heroku_detector) }
31
- before { allow(Hrk::Execute::ErrorTrap).to receive(:new).with(heroku_detector).and_return(error_trap) }
37
+ before { allow(Hrk::Execute::ErrorTrap). to receive(:new).with(heroku_detector).and_return(error_trap) }
32
38
 
33
39
  it { expect(Hrk::Execute.executer).to eq error_trap }
34
40
  end
@@ -1,19 +1,18 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  RSpec.describe Hrk::Heroku do
4
- describe '#call' do
5
- describe 'the exec command ran' do
6
- def self.calling command, on_remote: %W(-r whatever-app), starts: [], and_outputs: ''
7
- describe "calling '#{command.join ' '}' on remote '#{on_remote}', exec" do
4
+ describe "#call" do
5
+ describe "the exec command ran" do
6
+ def self.calling command, on_remote: %W(-r whatever-app), starts: [], and_outputs: ""
7
+ describe "calling `#{command.join " "}` on remote #{on_remote}" do
8
8
  subject(:heroku) { Hrk::Heroku.new(*on_remote) }
9
9
 
10
10
  before { allow(heroku).to receive(:puts) }
11
11
 
12
- specify do
13
- heroku.call(*command)
14
- expect(heroku).to have_received(:exec).with(*starts)
15
- expect(heroku).to have_received(:puts).with(and_outputs)
16
- end
12
+ subject!(:result) { heroku.call(*command) }
13
+
14
+ it { expect(heroku).to have_received(:exec).with(*starts) }
15
+ it { expect(heroku).to have_received(:puts).with(and_outputs) }
17
16
  end
18
17
  end
19
18
 
@@ -44,35 +43,38 @@ RSpec.describe Hrk::Heroku do
44
43
  end
45
44
  end
46
45
 
47
- describe '(edge case)' do
46
+ describe "(edge case)" do
48
47
  subject(:heroku) { Hrk::Heroku.new(*%w(-r some-remote)) }
49
48
 
50
49
  before { allow(heroku).to receive(:puts) }
50
+ before { allow(heroku).to receive(:exec) }
51
51
 
52
- specify 'another remote is mentionned' do
52
+ specify "another remote is mentionned" do
53
53
  expect { heroku.call(*%w(run rake rake:db:migrate -r some-other-remote)) }.to raise_exception(Hrk::Heroku::ExplicitApplicationError)
54
54
  expect(heroku).not_to have_received(:puts)
55
+ expect(heroku).not_to have_received(:exec)
55
56
  end
56
- specify 'another app is mentionned' do
57
+ specify "another app is mentionned" do
57
58
  expect { heroku.call(*%w(run rake rake:db:migrate -a different-app)) }.to raise_exception(Hrk::Heroku::ExplicitApplicationError)
58
59
  expect(heroku).not_to have_received(:puts)
60
+ expect(heroku).not_to have_received(:exec)
59
61
  end
60
62
  end
61
63
  end
62
64
 
63
- describe 'the result of the command' do
65
+ describe "the result of the command" do
64
66
  subject(:heroku) { Hrk::Heroku.new(*%w(-r some-remote)) }
65
- before { allow(heroku).to receive(:exec).with(*%W(heroku some command -r some-remote)).and_return(exec_returns) }
66
- before { allow(heroku).to receive(:puts) }
67
+ before { expect(heroku).to receive(:exec).with(*%W(heroku some command -r some-remote)).and_return(exec_return) }
68
+ before { expect(heroku).to receive(:puts) }
67
69
 
68
- context 'the command result is truthy' do
69
- let(:exec_returns) { true }
70
+ context "the command result is truthy" do
71
+ let(:exec_return) { true }
70
72
 
71
73
  it { expect(heroku.call(*%w(some command))).to be_truthy }
72
74
  end
73
75
 
74
- context 'the command result is falsy' do
75
- let(:exec_returns) { false }
76
+ context "the command result is falsy" do
77
+ let(:exec_return) { false }
76
78
 
77
79
  it { expect(heroku.call(*%w(some command))).to be_falsy }
78
80
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hrk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Belleville
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -165,19 +165,25 @@ files:
165
165
  - lib/hrk.rb
166
166
  - lib/hrk/env.rb
167
167
  - lib/hrk/execute.rb
168
+ - lib/hrk/execute/ask_for_remote.rb
168
169
  - lib/hrk/execute/command.rb
169
170
  - lib/hrk/execute/error_trap.rb
170
171
  - lib/hrk/execute/help.rb
171
172
  - lib/hrk/execute/heroku_detector.rb
173
+ - lib/hrk/execute/remember.rb
172
174
  - lib/hrk/execute/remote_display.rb
175
+ - lib/hrk/execute/remote_last.rb
173
176
  - lib/hrk/heroku.rb
174
177
  - lib/hrk/version.rb
175
178
  - spec/hrk/env_spec.rb
179
+ - spec/hrk/execute/ask_for_remote_spec.rb
176
180
  - spec/hrk/execute/command_spec.rb
177
181
  - spec/hrk/execute/error_trap_spec.rb
178
182
  - spec/hrk/execute/help_spec.rb
179
183
  - spec/hrk/execute/heroku_detector_spec.rb
184
+ - spec/hrk/execute/remember_spec.rb
180
185
  - spec/hrk/execute/remote_display_spec.rb
186
+ - spec/hrk/execute/remote_last_spec.rb
181
187
  - spec/hrk/execute_spec.rb
182
188
  - spec/hrk/heroku_spec.rb
183
189
  - spec/spec_helper.rb