2021-08-14 03:17:43 +08:00
|
|
|
defmodule Livebook.TestHelpers do
|
|
|
|
@moduledoc false
|
|
|
|
|
2022-03-05 20:19:42 +08:00
|
|
|
alias Livebook.Session.Data
|
|
|
|
|
2021-08-14 03:17:43 +08:00
|
|
|
@doc """
|
|
|
|
Creates file structure according to the given specification.
|
|
|
|
"""
|
|
|
|
def create_tree!(path, items) do
|
|
|
|
for {name, content} <- items do
|
|
|
|
child_path = Path.join(path, to_string(name))
|
|
|
|
|
|
|
|
case content do
|
|
|
|
items when is_list(items) ->
|
|
|
|
File.mkdir!(child_path)
|
|
|
|
create_tree!(child_path, items)
|
|
|
|
|
|
|
|
content when is_binary(content) ->
|
|
|
|
File.write!(child_path, content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-05 20:19:42 +08:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Applies the given list of operations to `Livebook.Session.Data`.
|
|
|
|
|
|
|
|
Raises if any of the operations results in an error.
|
|
|
|
"""
|
|
|
|
def data_after_operations!(data \\ Data.new(), operations) do
|
2022-03-29 03:36:57 +08:00
|
|
|
operations
|
|
|
|
|> List.flatten()
|
|
|
|
|> Enum.reduce(data, fn operation, data ->
|
2022-03-05 20:19:42 +08:00
|
|
|
case Data.apply_operation(data, operation) do
|
|
|
|
{:ok, data, _action} ->
|
|
|
|
data
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
raise "failed to set up test data, operation #{inspect(operation)} returned an error"
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2022-04-12 02:34:31 +08:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts a Unix-like absolute path into OS-compatible absolute path.
|
|
|
|
"""
|
|
|
|
defmacro p("/" <> path), do: Path.expand("/") <> path
|
2021-08-14 03:17:43 +08:00
|
|
|
end
|