hrk 0.0.3 → 0.0.4

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: 67b379c0812c173de1d738d526b6b350947acf9c
4
- data.tar.gz: 30708b927366d6842771652f86fcd73b6a962593
3
+ metadata.gz: 172ab5ee4bd7bf4c32959c037180e9d0cba6b1ff
4
+ data.tar.gz: 7d03ead725cf5599a4262cc6b7a9cc095ee0bff8
5
5
  SHA512:
6
- metadata.gz: f74f6a0f912eb4baa1b6d02c202f71c9791b375c0d34b7e4a54598fb67c16a0ec1e262ce37414e672c0c459fc9b0d01b0727eb99847c939572a1a5d9b0748876
7
- data.tar.gz: 6b029a0c02a97c3f4632a90c4f157928e28e9a1cb7c28f085b944285d936933d917c4226cc03fd91a9a4dff4ea316b3d28af623f6fa96e1caebc9a4a15c05dce
6
+ metadata.gz: 307c5ffa6b227953e064367f35ec37b25ebc46e0e18b4abb99b358b1839bbb975d9218292691a3dde17bce86ebbf3272022fa5ad456696cfc9abbfe17e91ecdf
7
+ data.tar.gz: fd7c224100e26d2cc70b64f7eb69f10b923d13a8601207cfddc2f0dcced6379866f4c60cf6ce3b96bbed7be0cebcddb6af7bc7037bb112b126986555a75d4c1b
data/lib/hrk/env.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'pathname'
2
+ require 'fileutils'
3
+ require 'digest/md5'
4
+ require 'tmpdir'
5
+
6
+ module Hrk
7
+ class Env
8
+ def remote= remote_name
9
+ if remote_name
10
+ socket_path.write remote_name unless remote == remote_name
11
+ else
12
+ socket_path.delete if remote?
13
+ end
14
+ end
15
+
16
+ def remote
17
+ socket_path.read if remote?
18
+ end
19
+
20
+ def remote?
21
+ socket_path.exist?
22
+ end
23
+
24
+ def tmp_path
25
+ Pathname.
26
+ new(File.join(ENV['XDG_RUNTIME_DIR'] || Dir.tmpdir, 'hrk')).
27
+ tap { |path| FileUtils.mkdir_p(path) unless path.exist? }
28
+ end
29
+
30
+ def socket_path
31
+ tmp_path.join Digest::MD5.hexdigest(`tty`)
32
+ end
33
+ end
34
+ end
@@ -3,9 +3,22 @@ module Hrk
3
3
  class Command
4
4
  REMOTE_MATCHER = %r{\A(.+):\Z}
5
5
 
6
+ attr_reader :env
7
+
8
+ def initialize
9
+ @env = Hrk::Env.new
10
+ end
11
+
6
12
  def call remote, *args
7
- raise ArgumentError.new("#{remote.inspect} isn't a proper remote marker") unless remote =~ REMOTE_MATCHER
8
- Hrk::Heroku.new(remote[REMOTE_MATCHER, 1]).call(args.join(' '))
13
+ if remote == ':'
14
+ raise ArgumentError.new("No remote has been previously defined") unless @env.remote?
15
+ Hrk::Heroku.new(@env.remote).call(args.join(' '))
16
+ else
17
+ raise ArgumentError.new("#{remote.inspect} isn't a proper remote marker") unless remote =~ REMOTE_MATCHER
18
+ remote_name = remote[REMOTE_MATCHER, 1]
19
+ @env.remote = remote_name
20
+ Hrk::Heroku.new(remote_name).call(args.join(' '))
21
+ end
9
22
  end
10
23
  end
11
24
  end
data/lib/hrk.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Hrk
2
+ autoload :Env, 'hrk/env'
2
3
  autoload :Heroku, 'hrk/heroku'
3
4
  autoload :Execute, 'hrk/execute'
4
5
  end
@@ -4,26 +4,104 @@ RSpec.describe Hrk::Execute::Command do
4
4
  describe '#call' do
5
5
  subject(:command) { Hrk::Execute::Command.new }
6
6
 
7
- describe 'standard case (a remote, a command)' do
8
- let(:remote) { "remote-#{rand(1..9)}" }
9
- let(:args) { %w(whatever that:may -b) }
7
+ let(:heroku) { double("Hrk::Heroku") }
8
+ before { allow(heroku).to receive(:call) }
9
+ before { allow(Hrk::Heroku).to receive(:new).and_return(heroku) }
10
10
 
11
- let(:heroku) { double("Hrk::Heroku") }
12
- before { allow(heroku).to receive(:call) }
13
- before { allow(Hrk::Heroku).to receive(:new).and_return(heroku) }
11
+ before { allow(command.env).to receive(:remote=) }
14
12
 
15
- before { command.call "#{remote}:", *args }
13
+ context 'no remote was previously memorized' do
14
+ before { allow(command.env).to receive(:remote?).and_return(nil) }
16
15
 
17
- it { expect(Hrk::Heroku).to have_received(:new).with(remote) }
18
- it { expect(heroku).to have_received(:call).with("whatever that:may -b") }
16
+ describe 'standard case (a remote, a command)' do
17
+ let(:remote) { "remote-#{rand(1..9)}" }
18
+ let(:args) { %w(whatever that:may -b) }
19
+
20
+ [true, false].each do |result|
21
+ describe "when the command returns #{result}" do
22
+ before { allow(heroku).to receive(:call).and_return(result) }
23
+
24
+ it { expect(command.call("#{remote}:", *args)).to eq result }
25
+ end
26
+ end
27
+
28
+ describe 'interactions' do
29
+ before { command.call "#{remote}:", *args }
30
+
31
+ it { expect(Hrk::Heroku).to have_received(:new).with(remote) }
32
+ it { expect(heroku).to have_received(:call).with("whatever that:may -b") }
33
+ it { expect(command.env).to have_received(:remote=).with(remote) }
34
+ end
35
+ end
36
+
37
+ describe 'edge cases (no remote or improper remote)' do
38
+ let(:other_args) { %W(something-#{rand(1..9)} or-another) }
39
+
40
+ before { expect(heroku).not_to receive(:call) }
41
+ before { expect(command.env).not_to receive(:remote=) }
42
+
43
+ it { expect { command.call }.to raise_error ArgumentError }
44
+ it { expect { command.call "bad-remote", *other_args }.to raise_error ArgumentError }
45
+ it { expect { command.call ":misplaced-marker", *other_args }.to raise_error ArgumentError }
46
+ it { expect { command.call ":", *other_args }.to raise_error ArgumentError }
47
+ end
19
48
  end
20
49
 
21
- describe 'edge cases (no remote or improper remote)' do
22
- let(:other_args) { %w(something or-another) }
23
- it { expect { command.call }.to raise_error ArgumentError }
24
- it { expect { command.call "bad-remote", *other_args }.to raise_error ArgumentError }
25
- it { expect { command.call ":misplaced-marker", *other_args }.to raise_error ArgumentError }
26
- it { expect { command.call ":", *other_args }.to raise_error ArgumentError }
50
+ context 'a remote was previously memorized' do
51
+ let(:previous_remote) { "ye-olde-remote#{rand(1..9)}" }
52
+ before { allow(command.env).to receive(:remote).and_return(previous_remote) }
53
+ before { allow(command.env).to receive(:remote?).and_return(true) }
54
+
55
+ describe 'standard case (a remote, a command)' do
56
+ let(:remote) { "remote-#{rand(1..9)}" }
57
+ let(:args) { %w(whatever that:may -b) }
58
+
59
+ [true, false].each do |result|
60
+ describe "when the command returns #{result}" do
61
+ before { allow(heroku).to receive(:call).and_return(result) }
62
+
63
+ it { expect(command.call("#{remote}:", *args)).to eq result }
64
+ end
65
+ end
66
+
67
+ describe 'interactions' do
68
+ before { command.call "#{remote}:", *args }
69
+
70
+ it { expect(Hrk::Heroku).to have_received(:new).with(remote) }
71
+ it { expect(heroku).to have_received(:call).with("whatever that:may -b") }
72
+ it { expect(command.env).to have_received(:remote=).with(remote) }
73
+ end
74
+ end
75
+
76
+ describe 'use previous remote' do
77
+ let(:args) { %w(whatever that:may -b) }
78
+
79
+ [true, false].each do |result|
80
+ describe "when the command returns #{result}" do
81
+ before { allow(heroku).to receive(:call).and_return(result) }
82
+
83
+ it { expect(command.call(":", *args)).to eq result }
84
+ end
85
+ end
86
+
87
+ describe 'interactions' do
88
+ before { command.call ":", *args }
89
+
90
+ it { expect(Hrk::Heroku).to have_received(:new).with(previous_remote) }
91
+ it { expect(heroku).to have_received(:call).with("whatever that:may -b") }
92
+ end
93
+ end
94
+
95
+ describe 'edge cases (no remote or improper remote)' do
96
+ let(:other_args) { %W(something-#{rand(1..9)} or-another) }
97
+
98
+ before { expect(heroku).not_to receive(:call) }
99
+ before { expect(command.env).not_to receive(:remote=) }
100
+
101
+ it { expect { command.call }.to raise_error ArgumentError }
102
+ it { expect { command.call "bad-remote", *other_args }.to raise_error ArgumentError }
103
+ it { expect { command.call ":misplaced-marker", *other_args }.to raise_error ArgumentError }
104
+ end
27
105
  end
28
106
  end
29
107
  end
@@ -1,4 +1,4 @@
1
- require 'hrk/execute'
1
+ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Hrk::Execute do
4
4
  describe '.call' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hrk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Belleville
@@ -62,6 +62,7 @@ files:
62
62
  - README.md
63
63
  - bin/hrk
64
64
  - lib/hrk.rb
65
+ - lib/hrk/env.rb
65
66
  - lib/hrk/execute.rb
66
67
  - lib/hrk/execute/command.rb
67
68
  - lib/hrk/execute/help.rb