Skip to content

Commit 2024010

Browse files
committed
Refactor tests and cleanup logic
Signed-off-by: Jared Quick <jquick@chef.io>
1 parent b4a4785 commit 2024010

7 files changed

Lines changed: 56 additions & 28 deletions

File tree

lib/train/plugins/base_connection.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ def platform
7272
# @param command [String] command string to execute
7373
# @return [CommandResult] contains the result of running the command
7474
def run_command_via_connection(_command)
75-
fail Train::ClientError, "#{self.class} does not implement #run_command_via_connection()"
75+
fail NotImplementedError, "#{self.class} does not implement #run_command_via_connection()"
7676
end
7777

7878
# run command with optional caching
7979
def run_command(command)
80-
return @cacher.run_command(command) if @cacher.cache_enabled[:command] == true
80+
return @cacher.run_command(command) if @cacher.cache_enabled?(:command)
8181

8282
run_command_via_connection(command)
8383
end
@@ -88,12 +88,12 @@ def run_command(command)
8888
# @param [String] path which is being inspected
8989
# @return [FileCommon] file object that allows for interaction
9090
def file_via_connection(_path, *_args)
91-
fail Train::ClientError, "#{self.class} does not implement #file_via_connection(...)"
91+
fail NotImplementedError, "#{self.class} does not implement #file_via_connection(...)"
9292
end
9393

9494
# file with optional caching
9595
def file(path, *args)
96-
return @cacher.file(path, *args) if @cacher.cache_enabled[:file] == true
96+
return @cacher.file(path, *args) if @cacher.cache_enabled?(:file)
9797

9898
file_via_connection(path, *args)
9999
end
@@ -103,7 +103,7 @@ def file(path, *args)
103103
#
104104
# @return [LoginCommand] array of command line tokens
105105
def login_command
106-
fail Train::ClientError, "#{self.class} does not implement #login_command()"
106+
fail NotImplementedError, "#{self.class} does not implement #login_command()"
107107
end
108108

109109
# Block and return only when the remote host is prepared and ready to

lib/train/plugins/cache_connection.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,24 @@ def initialize(connection)
1818
}
1919

2020
@cache_enabled.each_key do |type|
21-
@cache[type] = {}
21+
clear_cache(type)
2222
end
2323
end
2424

25+
def cache_enabled?(type)
26+
@cache_enabled[type.to_sym]
27+
end
28+
2529
def clear_cache(type)
2630
@cache[type.to_sym] = {}
2731
end
2832

2933
def file(path)
30-
if @cache[:file].key?(path)
31-
@cache[:file][path]
32-
else
33-
@cache[:file][path] = @connection.file_via_connection(path)
34-
end
34+
@cache[:file][path] ||= @connection.file_via_connection(path)
3535
end
3636

3737
def run_command(cmd)
38-
if @cache[:command].key?(cmd)
39-
@cache[:command][cmd]
40-
else
41-
@cache[:command][cmd] = @connection.run_command_via_connection(cmd)
42-
end
38+
@cache[:command][cmd] ||= @connection.run_command_via_connection(cmd)
4339
end
4440
end
4541
end

test/unit/plugins/cache_connection_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
it 'default connection cache settings' do
1717
cacher = connection.instance_variable_get(:@cacher)
1818
cacher.cache_enabled[:file].must_equal true
19+
cacher.cache_enabled?(:file).must_equal true
1920
cacher.cache_enabled[:command].must_equal false
21+
cacher.cache_enabled?(:command).must_equal false
2022
end
2123
end
2224

test/unit/plugins/connection_test.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@
1010
connection.close # wont raise
1111
end
1212

13-
it 'provides a run_command method' do
14-
proc { connection.run_command('') }.must_raise Train::ClientError
13+
it 'raises an exception for run_command' do
14+
proc { connection.run_command('') }.must_raise NotImplementedError
1515
end
1616

17-
it 'provides a run_command_via_connection method' do
18-
proc { connection.run_command_via_connection('') }.must_raise Train::ClientError
17+
it 'raises an exception for run_command_via_connection' do
18+
proc { connection.run_command_via_connection('') }.must_raise NotImplementedError
1919
end
2020

21-
it 'provides an os method' do
22-
proc { connection.os }.must_raise Train::ClientError
21+
it 'raises an exception for os method' do
22+
proc { connection.os }.must_raise NotImplementedError
2323
end
2424

25-
it 'provides a file method' do
26-
proc { connection.file('') }.must_raise Train::ClientError
25+
it 'raises an exception for file method' do
26+
proc { connection.file('') }.must_raise NotImplementedError
2727
end
2828

29-
it 'provides a file_via_connection method' do
30-
proc { connection.file_via_connection('') }.must_raise Train::ClientError
29+
it 'raises an exception for file_via_connection method' do
30+
proc { connection.file_via_connection('') }.must_raise NotImplementedError
3131
end
3232

33-
it 'provides a login command method' do
34-
proc { connection.login_command }.must_raise Train::ClientError
33+
it 'raises an exception for login command method' do
34+
proc { connection.login_command }.must_raise NotImplementedError
3535
end
3636

3737
it 'can wait until ready' do

test/unit/transports/local_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ def initialize
4343
connection.login_command.must_be_nil
4444
end
4545

46+
it 'provides a run_command_via_connection method' do
47+
methods = connection.class.instance_methods(false)
48+
methods.include?(:run_command_via_connection).must_equal true
49+
end
50+
51+
it 'provides a file_via_connection method' do
52+
methods = connection.class.instance_methods(false)
53+
methods.include?(:file_via_connection).must_equal true
54+
end
55+
4656
describe 'when running a local command' do
4757
let(:cmd_runner) { Minitest::Mock.new }
4858

test/unit/transports/mock_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
connection.uri.must_equal 'mock://'
2020
end
2121

22+
it 'provides a run_command_via_connection method' do
23+
methods = connection.class.instance_methods(false)
24+
methods.include?(:run_command_via_connection).must_equal true
25+
end
26+
27+
it 'provides a file_via_connection method' do
28+
methods = connection.class.instance_methods(false)
29+
methods.include?(:file_via_connection).must_equal true
30+
end
31+
2232
describe 'when running a mocked command' do
2333
let(:mock_cmd) { }
2434

test/unit/transports/ssh_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
let(:ssh) { cls.new(conf) }
5959
let(:connection) { ssh.connection }
6060

61+
it 'provides a run_command_via_connection method' do
62+
methods = connection.class.instance_methods(false)
63+
methods.include?(:run_command_via_connection).must_equal true
64+
end
65+
66+
it 'provides a file_via_connection method' do
67+
methods = connection.class.instance_methods(false)
68+
methods.include?(:file_via_connection).must_equal true
69+
end
70+
6171
it 'gets the connection' do
6272
connection.must_be_kind_of Train::Transports::SSH::Connection
6373
end

0 commit comments

Comments
 (0)