mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-09-06 04:54:29 +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()
|
||||
def home do
|
||||
Application.get_env(:livebook, :home) || System.user_home() || File.cwd!()
|
||||
Application.get_env(:livebook, :home) || user_home() || File.cwd!()
|
||||
end
|
||||
|
||||
defp user_home(), do: System.user_home() |> Path.expand()
|
||||
|
||||
@doc """
|
||||
Returns the configuration path.
|
||||
"""
|
||||
|
|
|
@ -29,7 +29,13 @@ defmodule Livebook.FileSystem.File do
|
|||
|
||||
path =
|
||||
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
|
||||
default_path
|
||||
end
|
||||
|
|
|
@ -6,18 +6,22 @@ defmodule Livebook.FileSystem.FileTest do
|
|||
alias Livebook.FileSystem
|
||||
|
||||
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()
|
||||
|
||||
assert FileSystem.File.new(file_system, "file.txt").path ==
|
||||
Path.join(File.cwd!(), "file.txt")
|
||||
assert_raise ArgumentError, ~s{expected an expanded absolute path, got: "file.txt"}, fn ->
|
||||
FileSystem.File.new(file_system, "file.txt")
|
||||
end
|
||||
end
|
||||
|
||||
test "resolves unexpanded paths" do
|
||||
test "raises an error when a unexpanded path is given" do
|
||||
file_system = FileSystem.Local.new()
|
||||
|
||||
assert FileSystem.File.new(file_system, "/dir/nested/../file.txt").path ==
|
||||
Path.expand("/dir/file.txt")
|
||||
assert_raise ArgumentError,
|
||||
~s{expected an expanded absolute path, got: "/dir/nested/../file.txt"},
|
||||
fn ->
|
||||
FileSystem.File.new(file_system, "/dir/nested/../file.txt")
|
||||
end
|
||||
end
|
||||
|
||||
test "uses default file system path if non is given" do
|
||||
|
|
Loading…
Add table
Reference in a new issue