LivebookTest.Exporter (livebook_test v0.1.0)

Copy Markdown View Source

Converts Livebook .livemd notebooks into executable Elixir scripts.

This module is the second stage in the LivebookTest pipeline:

Discovery  **Exporter**  DependencyPatcher  Runner  Report

It uses Livebook.live_markdown_to_elixir/1 to transform the Markdown-based Livebook format into a standalone .exs script that can be executed by the Elixir runtime.

How exporting works

  1. Read the .livemd file content
  2. Pass it to Livebook.live_markdown_to_elixir/1
  3. Receive an Elixir script string
  4. Optionally write the script to a temporary file for execution

Examples

iex> {:ok, script} = LivebookTest.Exporter.to_elixir("examples/basic.livemd")
iex> is_binary(script)
true

Summary

Types

Export result

Functions

Converts a .livemd notebook file to an Elixir script string.

Converts notebook content (already read) to an Elixir script string.

Exports a notebook to a temporary .exs file.

Exports notebook content to a temporary .exs file.

Types

export_result()

@type export_result() :: {:ok, String.t()} | {:error, term()}

Export result

Functions

to_elixir(notebook_path)

@spec to_elixir(Path.t()) :: export_result()

Converts a .livemd notebook file to an Elixir script string.

Reads the file and passes its content through Livebook.live_markdown_to_elixir/1.

Examples

iex> {:ok, script} = LivebookTest.Exporter.to_elixir("examples/basic.livemd")
iex> String.contains?(script, "IO")
true

to_elixir_from_string(content)

@spec to_elixir_from_string(String.t()) :: export_result()

Converts notebook content (already read) to an Elixir script string.

Useful when the notebook content has already been read, or when working with in-memory notebook strings.

Examples

iex> content = "# \\n\\n## Section\\n\\n```elixir\\nIO.puts(:hello)\\n```"
iex> {:ok, _script} = LivebookTest.Exporter.to_elixir_from_string(content)

to_temp_file(notebook_path)

@spec to_temp_file(Path.t()) :: {:ok, Path.t()} | {:error, term()}

Exports a notebook to a temporary .exs file.

Converts the notebook and writes the resulting script to a temporary file, returning the path. The caller is responsible for cleaning up the file after use.

Examples

iex> {:ok, path} = LivebookTest.Exporter.to_temp_file("examples/basic.livemd")
iex> String.ends_with?(path, ".exs")
true

to_temp_file_from_string(content)

@spec to_temp_file_from_string(String.t()) :: {:ok, Path.t()} | {:error, term()}

Exports notebook content to a temporary .exs file.

Like to_temp_file/1 but accepts already-read content.

Examples

iex> content = "# \\n\\n## Section\\n\\n```elixir\\n1 + 1\\n```"
iex> {:ok, path} = LivebookTest.Exporter.to_temp_file_from_string(content)
iex> File.exists?(path)
true