Test and typing improvements (#949)

* Minimize race condition in the frame update test

* Use defmacrop for building intellisense context

* Remove unnecessary cell view computation

* Fix nested assets resolution

* Fix typing errors

* Add missing async attribute to test suites

* Improve rendering synchronization

* Up
This commit is contained in:
Jonatan Kłosko 2022-01-29 16:39:41 +01:00 committed by GitHub
parent 372f086044
commit 4d70e5cceb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 31 additions and 47 deletions

View file

@ -119,6 +119,7 @@ defmodule Livebook.Application do
end
end
@spec invalid_hostname!(String.t()) :: no_return()
defp invalid_hostname!(prelude) do
Livebook.Config.abort!("""
#{prelude}, which indicates something wrong in your OS configuration.

View file

@ -226,7 +226,7 @@ defmodule Livebook.Notebook do
@doc """
Updates cells as `update_cells/2`, but carries an accumulator.
"""
@spec update_reduce_cells(t(), acc, ({Cell.t(), acc} -> {Cell.t(), acc})) :: t()
@spec update_reduce_cells(t(), acc, (Cell.t(), acc -> {Cell.t(), acc})) :: {t(), acc}
when acc: term()
def update_reduce_cells(notebook, acc, fun) do
{sections, acc} =
@ -441,8 +441,8 @@ defmodule Livebook.Notebook do
The cells are not ordered in any secific way.
"""
@spec cell_ids_with_children(t(), list(Cell.id())) :: list(Cell.id())
def cell_ids_with_children(data, parent_cell_ids) do
graph = cell_dependency_graph(data.notebook)
def cell_ids_with_children(notebook, parent_cell_ids) do
graph = cell_dependency_graph(notebook)
for parent_id <- parent_cell_ids,
leaf_id <- Graph.leaves(graph),

View file

@ -1266,7 +1266,7 @@ defmodule Livebook.Session do
end
defp extract_archive!(binary, path) do
:ok = :erl_tar.extract({:binary, binary}, [:compressed, {:cwd, path}])
:ok = :erl_tar.extract({:binary, binary}, [:compressed, {:cwd, String.to_charlist(path)}])
end
@doc """

View file

@ -847,7 +847,7 @@ defmodule Livebook.Session.Data do
|> set!(
notebook: Notebook.add_cell_output(data.notebook, cell.id, output),
input_values:
{-1, output}
{0, output}
|> Cell.Elixir.find_inputs_in_output()
|> Map.new(fn attrs -> {attrs.id, attrs.default} end)
|> Map.merge(data.input_values)
@ -1521,7 +1521,7 @@ defmodule Livebook.Session.Data do
uniq: true,
do: cell.id
cell_ids = Notebook.cell_ids_with_children(data, evaluable_cell_ids)
cell_ids = Notebook.cell_ids_with_children(data.notebook, evaluable_cell_ids)
for {cell, _} <- elixir_cells_with_section,
cell.id in cell_ids,

View file

@ -114,7 +114,7 @@ defmodule LivebookWeb.SessionController do
# The request comes from a cross-origin iframe
conn = allow_cors(conn)
case lookup_asset(hash, file_parts) do
case lookup_asset(hash, asset_path) do
{:ok, local_asset_path} ->
conn
|> put_content_type(asset_path)

View file

@ -85,7 +85,7 @@ defmodule LivebookWeb.HomeLive do
</.live_component>
</div>
<div class="py-12">
<div class="py-12" data-element="explore-section">
<div class="mb-4 flex justify-between items-center">
<h2 class="uppercase font-semibold text-gray-500">
Explore

View file

@ -3,7 +3,7 @@ defmodule LivebookWeb.SessionLive do
import LivebookWeb.UserHelpers
import LivebookWeb.SessionHelpers
import Livebook.Utils, only: [access_by_id: 1, format_bytes: 1]
import Livebook.Utils, only: [format_bytes: 1]
alias LivebookWeb.SidebarHelpers
alias Livebook.{Sessions, Session, Delta, Notebook, Runtime, LiveMarkdown}
@ -1453,10 +1453,8 @@ defmodule LivebookWeb.SessionLive do
{:report_cell_revision, _pid, _cell_id, _revision} ->
data_view
{:apply_cell_delta, _pid, cell_id, _delta, _revision} ->
data_view
|> update_cell_view(data, cell_id)
|> update_dirty_status(data)
{:apply_cell_delta, _pid, _cell_id, _delta, _revision} ->
update_dirty_status(data_view, data)
# For outputs that update existing outputs we send the update directly
# to the corresponding component, so the DOM patch is isolated and fast.
@ -1490,17 +1488,6 @@ defmodule LivebookWeb.SessionLive do
end
end
defp update_cell_view(data_view, data, cell_id) do
{:ok, cell, section} = Notebook.fetch_cell_and_section(data.notebook, cell_id)
cell_view = cell_to_view(cell, data)
put_in(
data_view,
[:section_views, access_by_id(section.id), :cell_views, access_by_id(cell.id)],
cell_view
)
end
defp prune_outputs(%{private: %{data: data}} = socket) do
assign_private(
socket,

View file

@ -1,11 +1,11 @@
defmodule Livebook.IntellisenseTest.Utils do
@moduledoc false
defmodule Livebook.IntellisenseTest do
use ExUnit.Case, async: true
@doc """
Returns intellisense context resulting from evaluating
the given block of code in a fresh context.
"""
defmacro eval(do: block) do
alias Livebook.Intellisense
# Returns intellisense context resulting from evaluating
# the given block of code in a fresh context.
defmacrop eval(do: block) do
quote do
block = unquote(Macro.escape(block))
binding = []
@ -21,14 +21,6 @@ defmodule Livebook.IntellisenseTest.Utils do
}
end
end
end
defmodule Livebook.IntellisenseTest do
use ExUnit.Case, async: true
import Livebook.IntellisenseTest.Utils
alias Livebook.Intellisense
describe "format_code/1" do
test "formats valid code" do

View file

@ -1,5 +1,5 @@
defmodule Livebook.SessionsTest do
use ExUnit.Case
use ExUnit.Case, async: true
alias Livebook.Sessions

View file

@ -1,5 +1,5 @@
defmodule LivebookWeb.ExploreLiveTest do
use LivebookWeb.ConnCase
use LivebookWeb.ConnCase, async: true
import Phoenix.LiveViewTest

View file

@ -1,5 +1,5 @@
defmodule LivebookWeb.HomeLiveTest do
use LivebookWeb.ConnCase
use LivebookWeb.ConnCase, async: true
import Phoenix.LiveViewTest
@ -206,7 +206,7 @@ defmodule LivebookWeb.HomeLiveTest do
assert {:error, {:live_redirect, %{to: to}}} =
view
|> element(~s{a}, "Welcome to Livebook")
|> element(~s{[data-element="explore-section"] a}, "Welcome to Livebook")
|> render_click()
|> follow_redirect(conn)

View file

@ -1,5 +1,5 @@
defmodule LivebookWeb.SessionLiveTest do
use LivebookWeb.ConnCase
use LivebookWeb.ConnCase, async: true
import Phoenix.LiveViewTest
@ -37,7 +37,7 @@ defmodule LivebookWeb.SessionLiveTest do
wait_for_session_update(session.pid)
# Wait for LV to update
render(view)
_ = render(view)
assert page_title(view) =~ "My notebook"
end
@ -319,8 +319,12 @@ defmodule LivebookWeb.SessionLiveTest do
wait_for_session_update(session.pid)
assert render(view) =~ "Updated frame"
refute render(view) =~ "In frame"
# Render once, so that frame send_update is processed
_ = render(view)
content = render(view)
assert content =~ "Updated frame"
refute content =~ "In frame"
end
end