From 0399a4570d9281c8044c6bee7949807208ef40eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wa=C5=9Bko?= Date: Wed, 2 Oct 2024 20:49:43 +0200 Subject: [PATCH] Resolving `~` in Enso asset paths to user home (#11235) - Closes #11226 --- CHANGELOG.md | 7 +++++++ .../Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso | 15 +-------------- .../src/Enso_Cloud/Internal/Enso_Path.enso | 10 +++++++++- .../Postgres/Postgres_Data_Link_Setup.enso | 3 ++- .../src/Inter_Backend_File_Operations_Spec.enso | 2 +- .../src/Network/Enso_Cloud/Enso_File_Spec.enso | 15 +++++++++++++++ .../src/Database/Common/Audit_Spec.enso | 2 +- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc485ac81de..9ec9b7eb6be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ [11151]: https://github.com/enso-org/enso/pull/11151 +#### Enso Standard Library + +- [The `enso://~` path now resolves to user's home directory in the + cloud.][11235] + +[11235]: https://github.com/enso-org/enso/pull/11235 + # Enso 2024.4 #### Enso IDE diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso index dbb90793967..49d6b1a06fa 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso @@ -53,19 +53,6 @@ type Enso_File Arguments: - path: The `enso://` path to a file or directory. - - ? Enso Cloud Paths - - The paths consist of the organization (user) name followed by a path to - the file/directory delimited by `/`. - For example `enso://my_org/some_dir/some-file.txt`. - - ! Work in progress - only existing resources - - Currently the API is only able to resolve paths to existing files or - directories. This is a temporary limitation and it will be improved in - the future, alongside with implementing the capabilities to write new - files. new : Text -> Enso_File ! Not_Found new (path : Text) = Enso_File.Value (Enso_Path.parse path) @@ -77,7 +64,7 @@ type Enso_File Represents the current user's home directory. home : Enso_File home -> Enso_File = - Enso_File.root / "Users" / Enso_User.current.name + Enso_File.root / "Users" / Enso_User.current.name ## ICON folder diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_Path.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_Path.enso index c1c34bec66e..cf521841068 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_Path.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_Path.enso @@ -63,4 +63,12 @@ type Enso_Path ## PRIVATE normalize segments = - Path_Helpers.normalize_segments segments (x->x) + after_resolving_dots = Path_Helpers.normalize_segments segments (x->x) + starts_with_tilde = after_resolving_dots.take 1 == ["~"] + if starts_with_tilde.not then after_resolving_dots else + # If after resolution our path starts with `~`, we replace that with a home directory. + home_path = Enso_File.home.enso_path + new_segments = home_path.path_segments + after_resolving_dots.drop 1 + ## We need to call normalize again, because technically a path `enso://a/../~/../../~` is a valid path + that points to the user home and it should be correctly normalized, but requires numerous passes to do so. + @Tail_Call normalize new_segments diff --git a/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/Postgres/Postgres_Data_Link_Setup.enso b/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/Postgres/Postgres_Data_Link_Setup.enso index 7f9f1376d90..774e21f226b 100644 --- a/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/Postgres/Postgres_Data_Link_Setup.enso +++ b/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/Postgres/Postgres_Data_Link_Setup.enso @@ -4,6 +4,7 @@ from Standard.Base import all import Standard.Base.Enso_Cloud.Data_Link.Data_Link import Standard.Base.Errors.File_Error.File_Error import Standard.Base.Errors.Illegal_Argument.Illegal_Argument +import Standard.Base.Errors.Illegal_State.Illegal_State import Standard.Base.Runtime.Context from Standard.Base.Enso_Cloud.Data_Link_Helpers import data_link_extension, secure_value_to_json @@ -53,7 +54,7 @@ prepare_credentials data_link_location:Enso_File details:Postgres -> JS_Object | secret_password = case credentials.password of secret : Enso_Secret -> secret plain_text_password : Text -> - secret_location = data_link_location.parent.if_nothing Enso_File.root + secret_location = data_link_location.parent.if_nothing (Error.throw (Illegal_State.Error "Trying to create a secret to store the Data Link password, but the provided data link location: "+data_link_location.to_text+" does not have a parent directory. This should not happen.")) location_name = if data_link_location.name.ends_with data_link_extension then data_link_location.name.drop (..Last data_link_extension.length) else data_link_location.name create_fresh_secret ix = diff --git a/test/AWS_Tests/src/Inter_Backend_File_Operations_Spec.enso b/test/AWS_Tests/src/Inter_Backend_File_Operations_Spec.enso index 9383b3b3d65..7c287b79cef 100644 --- a/test/AWS_Tests/src/Inter_Backend_File_Operations_Spec.enso +++ b/test/AWS_Tests/src/Inter_Backend_File_Operations_Spec.enso @@ -42,7 +42,7 @@ type Temporary_Enso_Cloud_File Value ~get make tmp_dir_name name = Temporary_Enso_Cloud_File.Value <| - location = Enso_File.root / tmp_dir_name + location = Enso_File.home / tmp_dir_name Panic.rethrow <| location.create_directory location / ("cloud-"+name) diff --git a/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso b/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso index 7e59f3c2ca9..cccc3033a79 100644 --- a/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso +++ b/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso @@ -270,6 +270,21 @@ add_specs suite_builder setup:Cloud_Tests_Setup = suite_builder.group "Enso Clou r.should_fail_with Illegal_Argument r.catch.to_display_text . should_contain "Cannot move above root" + group_builder.specify "resolves ~ to user home" <| + Enso_File.new "enso://~" . should_equal Enso_File.home + Enso_File.new "enso://~/a/b/c" . should_equal (Enso_File.home / "a/b/c") + + # It also works when resolving a path + (Enso_File.root / "~") . should_equal Enso_File.home + (Enso_File.root / "a/../~/b/c") . should_equal (Enso_File.home / "b/c") + Enso_File.new "enso://a/../~/b/c" . should_equal (Enso_File.home / "b/c") + + # But a filename only containing ~ is not resolved + Enso_File.new "enso://~~~" . should_not_equal Enso_File.home + + # If the `~` is not at the beginning, it is not resolved + Enso_File.new "enso://a/b/c/~" . should_not_equal Enso_File.home + group_builder.specify "currently does not support metadata for directories" <| # TODO this test should be 'reversed' and merged with above once the metadata is implemented dir = test_root.get / "test-directory" diff --git a/test/Table_Tests/src/Database/Common/Audit_Spec.enso b/test/Table_Tests/src/Database/Common/Audit_Spec.enso index ca0cf0b13cd..0ae48c26e99 100644 --- a/test/Table_Tests/src/Database/Common/Audit_Spec.enso +++ b/test/Table_Tests/src/Database/Common/Audit_Spec.enso @@ -34,7 +34,7 @@ add_specs suite_builder prefix ~datalink_to_connection database_pending = # Retrying is needed as there may be some delay before the background thread finishes processing the logs. Test.with_retries <| - Thread.sleep 500 + Thread.sleep 1000 all_events = get_audit_log_events relevant_events = all_events.filter e-> e.message.contains table_name