File tree 3 files changed +42
-0
lines changed
3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,10 @@ appear at the top.
17
17
[ PR #308 ] ( https://github.com/capistrano/sshkit/pull/308 ) @mattbrictson
18
18
* ` SSHKit::Backend::Printer#test ` now always returns true
19
19
[ PR #312 ] ( https://github.com/capistrano/sshkit/pull/312 ) @mikz
20
+ * Add ` SSHKit::Backend.current ` so that Capistrano plugin authors can refactor
21
+ helper methods and still have easy access to the currently-executing Backend
22
+ without having to use global variables.
23
+ [ PR #319 ] ( https://github.com/capistrano/sshkit/pull/319 ) @mattbrictson
20
24
21
25
## 1.8.1
22
26
Original file line number Diff line number Diff line change @@ -4,6 +4,19 @@ module Backend
4
4
5
5
MethodUnavailableError = Class . new ( SSHKit ::StandardError )
6
6
7
+ # The Backend instance that is running in the current thread. If no Backend
8
+ # is running, returns `nil` instead.
9
+ #
10
+ # Example:
11
+ #
12
+ # on(:local) do
13
+ # self == SSHKit::Backend.current # => true
14
+ # end
15
+ #
16
+ def self . current
17
+ Thread . current [ "sshkit_backend" ]
18
+ end
19
+
7
20
class Abstract
8
21
9
22
extend Forwardable
@@ -12,7 +25,10 @@ class Abstract
12
25
attr_reader :host
13
26
14
27
def run
28
+ Thread . current [ "sshkit_backend" ] = self
15
29
instance_exec ( @host , &@block )
30
+ ensure
31
+ Thread . current [ "sshkit_backend" ] = nil
16
32
end
17
33
18
34
def initialize ( host , &block )
Original file line number Diff line number Diff line change @@ -131,6 +131,28 @@ def test_invoke_raises_no_method_error
131
131
end
132
132
end
133
133
134
+ def test_current_refers_to_currently_executing_backend
135
+ backend = nil
136
+ current = nil
137
+
138
+ backend = ExampleBackend . new do
139
+ backend = self
140
+ current = SSHKit ::Backend . current
141
+ end
142
+ backend . run
143
+
144
+ assert_equal ( backend , current )
145
+ end
146
+
147
+ def test_current_is_nil_outside_of_the_block
148
+ backend = ExampleBackend . new do
149
+ # nothing
150
+ end
151
+ backend . run
152
+
153
+ assert_nil ( SSHKit ::Backend . current )
154
+ end
155
+
134
156
# Use a concrete ExampleBackend rather than a mock for improved assertion granularity
135
157
class ExampleBackend < Abstract
136
158
attr_writer :full_stdout
You can’t perform that action at this time.
0 commit comments