Skip to content

Commit 839c28c

Browse files
committed
Update how we set up mocking
Remove `:impl` option from calls. Instead configure the module to use - implementation or mock - in the application environment.
1 parent 7ffba1b commit 839c28c

File tree

9 files changed

+230
-206
lines changed

9 files changed

+230
-206
lines changed

config/config.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use Mix.Config
2+
3+
if Mix.env() == :test do
4+
config :sshkit, :ssh, MockErlangSsh
5+
config :sshkit, :ssh_connection, MockErlangSshConnection
6+
config :sshkit, :ssh_sftp, MockErlangSshSftp
7+
end

lib/sshkit/channel.ex

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ defmodule SSHKit.Channel do
99
* `id` - the unique channel id
1010
"""
1111

12-
defstruct [:connection, :type, :id, impl: :ssh_connection]
12+
defstruct [:connection, :type, :id]
1313

1414
@type t() :: %__MODULE__{}
1515

16+
# credo:disable-for-next-line
17+
@core Application.get_env(:sshkit, :ssh_connection, :ssh_connection)
18+
1619
@doc """
1720
Opens a channel on an SSH connection.
1821
@@ -31,15 +34,14 @@ defmodule SSHKit.Channel do
3134
timeout = Keyword.get(options, :timeout, :infinity)
3235
ini_window_size = Keyword.get(options, :initial_window_size, 128 * 1024)
3336
max_packet_size = Keyword.get(options, :max_packet_size, 32 * 1024)
34-
impl = Keyword.get(options, :impl, :ssh_connection)
3537

36-
with {:ok, id} <- impl.session_channel(conn.ref, ini_window_size, max_packet_size, timeout) do
37-
{:ok, new(conn, id, impl)}
38+
with {:ok, id} <- @core.session_channel(conn.ref, ini_window_size, max_packet_size, timeout) do
39+
{:ok, new(conn, id)}
3840
end
3941
end
4042

41-
defp new(conn, id, impl) do
42-
%__MODULE__{connection: conn, type: :session, id: id, impl: impl}
43+
defp new(conn, id) do
44+
%__MODULE__{connection: conn, type: :session, id: id}
4345
end
4446

4547
@doc """
@@ -53,7 +55,7 @@ defmodule SSHKit.Channel do
5355
:success | :failure | {:error, reason :: String.t()}
5456
def subsystem(channel, subsystem, options \\ []) do
5557
timeout = Keyword.get(options, :timeout, :infinity)
56-
channel.impl.subsystem(channel.connection.ref, channel.id, to_charlist(subsystem), timeout)
58+
@core.subsystem(channel.connection.ref, channel.id, to_charlist(subsystem), timeout)
5759
end
5860

5961
@doc """
@@ -64,7 +66,7 @@ defmodule SSHKit.Channel do
6466
For more details, see [`:ssh_connection.close/2`](http://erlang.org/doc/man/ssh_connection.html#close-2).
6567
"""
6668
def close(channel) do
67-
channel.impl.close(channel.connection.ref, channel.id)
69+
@core.close(channel.connection.ref, channel.id)
6870
end
6971

7072
@doc """
@@ -86,7 +88,7 @@ defmodule SSHKit.Channel do
8688
end
8789

8890
def exec(channel, command, timeout) do
89-
channel.impl.exec(channel.connection.ref, channel.id, command, timeout)
91+
@core.exec(channel.connection.ref, channel.id, command, timeout)
9092
end
9193

9294
@doc """
@@ -97,7 +99,7 @@ defmodule SSHKit.Channel do
9799
For more details, see [`:ssh_connection.ptty_alloc/4`](http://erlang.org/doc/man/ssh_connection.html#ptty_alloc-4).
98100
"""
99101
def ptty(channel, options \\ [], timeout \\ :infinity) do
100-
channel.impl.ptty_alloc(channel.connection.ref, channel.id, options, timeout)
102+
@core.ptty_alloc(channel.connection.ref, channel.id, options, timeout)
101103
end
102104

103105
@doc """
@@ -112,7 +114,7 @@ defmodule SSHKit.Channel do
112114
def send(channel, type \\ 0, data, timeout \\ :infinity)
113115

114116
def send(channel, type, data, timeout) when is_binary(data) or is_list(data) do
115-
channel.impl.send(channel.connection.ref, channel.id, type, data, timeout)
117+
@core.send(channel.connection.ref, channel.id, type, data, timeout)
116118
end
117119

118120
def send(channel, type, data, timeout) do
@@ -130,7 +132,7 @@ defmodule SSHKit.Channel do
130132
For more details, see [`:ssh_connection.send_eof/2`](http://erlang.org/doc/man/ssh_connection.html#send_eof-2).
131133
"""
132134
def eof(channel) do
133-
channel.impl.send_eof(channel.connection.ref, channel.id)
135+
@core.send_eof(channel.connection.ref, channel.id)
134136
end
135137

136138
@doc """
@@ -190,6 +192,6 @@ defmodule SSHKit.Channel do
190192
For more details, see [`:ssh_connection.adjust_window/3`](http://erlang.org/doc/man/ssh_connection.html#adjust_window-3).
191193
"""
192194
def adjust(channel, size) when is_integer(size) do
193-
channel.impl.adjust_window(channel.connection.ref, channel.id, size)
195+
@core.adjust_window(channel.connection.ref, channel.id, size)
194196
end
195197
end

lib/sshkit/connection.ex

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ defmodule SSHKit.Connection do
1313
alias SSHKit.Utils
1414

1515
# TODO: Add :tag allowing arbitrary data to be attached?
16-
defstruct [:host, :port, :options, :ref, impl: :ssh]
16+
defstruct [:host, :port, :options, :ref]
1717

1818
@type t() :: %__MODULE__{}
1919

20-
@default_impl_options [user_interaction: false]
21-
@default_connect_options [port: 22, timeout: :infinity, impl: :ssh]
20+
# credo:disable-for-next-line
21+
@core Application.get_env(:sshkit, :ssh, :ssh)
22+
23+
@default_ssh_options [user_interaction: false]
24+
@default_connect_options [port: 22, timeout: :infinity]
2225

2326
@doc """
2427
Opens a connection to an SSH server.
@@ -52,32 +55,31 @@ defmodule SSHKit.Connection do
5255

5356
port = details[:port]
5457
timeout = details[:timeout]
55-
impl = details[:impl]
5658

57-
case impl.connect(host, port, opts, timeout) do
58-
{:ok, ref} -> {:ok, new(host, port, opts, ref, impl)}
59+
case @core.connect(host, port, opts, timeout) do
60+
{:ok, ref} -> {:ok, new(host, port, opts, ref)}
5961
err -> err
6062
end
6163
end
6264

6365
defp extract(options) do
6466
connect_option_keys = Keyword.keys(@default_connect_options)
65-
{connect_options, impl_options} = Keyword.split(options, connect_option_keys)
67+
{connect_options, ssh_options} = Keyword.split(options, connect_option_keys)
6668

6769
connect_options =
6870
@default_connect_options
6971
|> Keyword.merge(connect_options)
7072

71-
impl_options =
72-
@default_impl_options
73-
|> Keyword.merge(impl_options)
73+
ssh_options =
74+
@default_ssh_options
75+
|> Keyword.merge(ssh_options)
7476
|> Utils.charlistify()
7577

76-
{connect_options, impl_options}
78+
{connect_options, ssh_options}
7779
end
7880

79-
defp new(host, port, options, ref, impl) do
80-
%__MODULE__{host: host, port: port, options: options, ref: ref, impl: impl}
81+
defp new(host, port, options, ref) do
82+
%__MODULE__{host: host, port: port, options: options, ref: ref}
8183
end
8284

8385
@doc """
@@ -88,7 +90,7 @@ defmodule SSHKit.Connection do
8890
For details, see [`:ssh.close/1`](http://erlang.org/doc/man/ssh.html#close-1).
8991
"""
9092
def close(conn) do
91-
conn.impl.close(conn.ref)
93+
@core.close(conn.ref)
9294
end
9395

9496
@doc """
@@ -105,7 +107,6 @@ defmodule SSHKit.Connection do
105107
options =
106108
conn.options
107109
|> Keyword.put(:port, conn.port)
108-
|> Keyword.put(:impl, conn.impl)
109110
|> Keyword.merge(options)
110111

111112
open(conn.host, options)

lib/sshkit/context.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule SSHKit.Context do
1414

1515
import SSHKit.Utils
1616

17-
defstruct env: nil, path: nil, umask: nil, user: nil, group: nil
17+
defstruct [:env, :path, :umask, :user, :group]
1818

1919
@type t() :: %__MODULE__{}
2020

lib/sshkit/sftp/channel.ex

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,48 @@ defmodule SSHKit.SFTP.Channel do
33

44
alias SSHKit.Connection
55

6-
defstruct [:connection, :id, impl: :ssh_sftp]
6+
defstruct [:connection, :id]
77

88
@type t() :: %__MODULE__{}
99
@type handle() :: term()
1010

11+
# credo:disable-for-next-line
12+
@core Application.get_env(:sshkit, :ssh_sftp, :ssh_sftp)
13+
1114
@spec start(Connection.t(), keyword()) :: {:ok, t()} | {:error, term()}
1215
def start(conn, options \\ []) do
13-
{impl, options} = Keyword.pop(options, :impl, :ssh_sftp)
14-
15-
with {:ok, id} <- impl.start_channel(conn.ref, options) do
16-
{:ok, new(conn, id, impl)}
16+
with {:ok, id} <- @core.start_channel(conn.ref, options) do
17+
{:ok, new(conn, id)}
1718
end
1819
end
1920

20-
defp new(conn, id, impl) do
21-
%__MODULE__{connection: conn, id: id, impl: impl}
21+
defp new(conn, id) do
22+
%__MODULE__{connection: conn, id: id}
2223
end
2324

2425
@spec stop(t()) :: :ok
2526
def stop(chan) do
26-
chan.impl.stop_channel(chan.id)
27+
@core.stop_channel(chan.id)
2728
end
2829

2930
@spec mkdir(t(), binary(), timeout()) :: :ok | {:error, term()}
3031
def mkdir(chan, name, timeout \\ :infinity) do
31-
chan.impl.make_dir(chan.id, name, timeout)
32+
@core.make_dir(chan.id, name, timeout)
3233
end
3334

3435
@spec open(t(), binary(), [:read | :write | :append | :binary | :raw], timeout()) ::
3536
{:ok, handle()} | {:error, term()}
3637
def open(chan, name, mode, timeout \\ :infinity) do
37-
chan.impl.open(chan.id, name, mode, timeout)
38+
@core.open(chan.id, name, mode, timeout)
3839
end
3940

4041
@spec close(t(), handle(), timeout()) :: :ok | {:error, term()}
4142
def close(chan, handle, timeout \\ :infinity) do
42-
chan.impl.close(chan.id, handle, timeout)
43+
@core.close(chan.id, handle, timeout)
4344
end
4445

4546
@spec write(t(), handle(), iodata(), timeout()) :: :ok | {:error, term()}
4647
def write(chan, handle, data, timeout \\ :infinity) do
47-
chan.impl.write(chan.id, handle, data, timeout)
48+
@core.write(chan.id, handle, data, timeout)
4849
end
4950
end

0 commit comments

Comments
 (0)