mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-09-06 13:04:53 +08:00
Bring path verification and add normalization (#1016)
* Bring path verification and add normalization * Normalize user home Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
parent
e71f5b8fa6
commit
3bc219e5d8
3 changed files with 20 additions and 8 deletions
|
@ -55,9 +55,11 @@ defmodule Livebook.Config do
|
||||||
"""
|
"""
|
||||||
@spec home() :: String.t()
|
@spec home() :: String.t()
|
||||||
def home do
|
def home do
|
||||||
Application.get_env(:livebook, :home) || System.user_home() || File.cwd!()
|
Application.get_env(:livebook, :home) || user_home() || File.cwd!()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp user_home(), do: System.user_home() |> Path.expand()
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns the configuration path.
|
Returns the configuration path.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -29,7 +29,13 @@ defmodule Livebook.FileSystem.File do
|
||||||
|
|
||||||
path =
|
path =
|
||||||
if path do
|
if path do
|
||||||
FileSystem.resolve_path(file_system, default_path, path)
|
resolved_path = FileSystem.resolve_path(file_system, default_path, path)
|
||||||
|
|
||||||
|
unless path == resolved_path do
|
||||||
|
raise ArgumentError, "expected an expanded absolute path, got: #{inspect(path)}"
|
||||||
|
end
|
||||||
|
|
||||||
|
path
|
||||||
else
|
else
|
||||||
default_path
|
default_path
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,18 +6,22 @@ defmodule Livebook.FileSystem.FileTest do
|
||||||
alias Livebook.FileSystem
|
alias Livebook.FileSystem
|
||||||
|
|
||||||
describe "new/2" do
|
describe "new/2" do
|
||||||
test "resolves relative paths" do
|
test "raises an error when a relative path is given" do
|
||||||
file_system = FileSystem.Local.new()
|
file_system = FileSystem.Local.new()
|
||||||
|
|
||||||
assert FileSystem.File.new(file_system, "file.txt").path ==
|
assert_raise ArgumentError, ~s{expected an expanded absolute path, got: "file.txt"}, fn ->
|
||||||
Path.join(File.cwd!(), "file.txt")
|
FileSystem.File.new(file_system, "file.txt")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "resolves unexpanded paths" do
|
test "raises an error when a unexpanded path is given" do
|
||||||
file_system = FileSystem.Local.new()
|
file_system = FileSystem.Local.new()
|
||||||
|
|
||||||
assert FileSystem.File.new(file_system, "/dir/nested/../file.txt").path ==
|
assert_raise ArgumentError,
|
||||||
Path.expand("/dir/file.txt")
|
~s{expected an expanded absolute path, got: "/dir/nested/../file.txt"},
|
||||||
|
fn ->
|
||||||
|
FileSystem.File.new(file_system, "/dir/nested/../file.txt")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "uses default file system path if non is given" do
|
test "uses default file system path if non is given" do
|
||||||
|
|
Loading…
Add table
Reference in a new issue