From a4b57b2e57472aff9a412eff293b1ebc11b2b2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wa=C5=9Bko?= Date: Tue, 19 Jul 2022 17:43:22 +0200 Subject: [PATCH] Move unsafe override to Test.Environment --- .../0.0.0-dev/src/System/Environment.enso | 12 -------- .../Test/0.0.0-dev/src/Environment.enso | 15 ++++++++++ .../java/org/enso/base/Environment_Utils.java | 7 +++-- .../src/Database/Postgres_Spec.enso | 30 ++++++++++--------- test/Tests/src/System/Environment_Spec.enso | 15 +++++----- 5 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 distribution/lib/Standard/Test/0.0.0-dev/src/Environment.enso diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/System/Environment.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/System/Environment.enso index 9395bbcb3ba9..8dec0dc524a6 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/System/Environment.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/System/Environment.enso @@ -39,15 +39,3 @@ get_or_else : Text -> Text -> Text get_or_else key ~default = case get key of Nothing -> default value -> value - -## PRIVATE - ADVANCED - - Runs a given action with an environment variable modified to a given value. - The environment variable is restored to its original value after the action. - The environment variable override is only visible to the Enso - `Environment.get` method, the environment as seen from a direct - `System.getenv` Java call remains unchanged. -unsafe_with_environment_override : Text -> Text -> Any -> Any -unsafe_with_environment_override key value ~action = - Environment_Utils.with_environment_variable_override key value (_->action) diff --git a/distribution/lib/Standard/Test/0.0.0-dev/src/Environment.enso b/distribution/lib/Standard/Test/0.0.0-dev/src/Environment.enso new file mode 100644 index 000000000000..9ee5b4b25079 --- /dev/null +++ b/distribution/lib/Standard/Test/0.0.0-dev/src/Environment.enso @@ -0,0 +1,15 @@ +from Standard.Base import all + +polyglot java import org.enso.base.Environment_Utils + +## UNSTABLE + ADVANCED + + Runs a given action with an environment variable modified to a given value. + The environment variable is restored to its original value after the action. + The environment variable override is only visible to the Enso + `Environment.get` method, the environment as seen from a direct + `System.getenv` Java call remains unchanged. +unsafe_with_environment_override : Text -> Text -> Any -> Any +unsafe_with_environment_override key value ~action = + Environment_Utils.with_environment_variable_override key value (_->action) diff --git a/std-bits/base/src/main/java/org/enso/base/Environment_Utils.java b/std-bits/base/src/main/java/org/enso/base/Environment_Utils.java index 373319c50e0a..93fc49ef14f2 100644 --- a/std-bits/base/src/main/java/org/enso/base/Environment_Utils.java +++ b/std-bits/base/src/main/java/org/enso/base/Environment_Utils.java @@ -15,8 +15,11 @@ public static String get_environment_variable(String name) { } /** - * Overrides the System environment variable with a new value. The override is only visible from - * within Enso. + * Calls `action` with the provided environment variable. + * + *

The override is not persisted (its only visible from within the action called by this + * method) and it is only visible by the Enso `Environment.get` method (backed by {@code + * get_environment_variable}). * *

This is an internal function that should be used very carefully and only for testing. */ diff --git a/test/Table_Tests/src/Database/Postgres_Spec.enso b/test/Table_Tests/src/Database/Postgres_Spec.enso index 9549e5edb15a..ed6a5ce66c10 100644 --- a/test/Table_Tests/src/Database/Postgres_Spec.enso +++ b/test/Table_Tests/src/Database/Postgres_Spec.enso @@ -7,7 +7,6 @@ from Standard.Base.System.Process.Exit_Code import Exit_Success from Standard.Database import all from Standard.Database.Connection.Connection import Sql_Error -import Standard.Test import Standard.Table as Materialized_Table import project.Database.Common_Spec import project.Database.Helpers.Name_Generator @@ -17,6 +16,9 @@ from Standard.Table.Data.Aggregate_Column import all from Standard.Database.Data.Sql import Sql_Type from Standard.Database.Internal.Postgres.Pgpass import Pgpass_Entry +import Standard.Test +import Standard.Test.Environment as Test_Environment + postgres_specific_spec connection pending = Test.group "[PostgreSQL] Info" pending=pending <| tinfo = Name_Generator.random_name "Tinfo" @@ -158,17 +160,17 @@ pgpass_spec = Test.group "[PostgreSQL] .pgpass" <| if Platform.is_unix then Test.specify "should only accept the .pgpass file if it has correct permissions" <| Process.run "chmod" ["0777", pgpass_file.absolute.path] . should_equal Exit_Success - Environment.unsafe_with_environment_override "PGPASSFILE" (pgpass_file.absolute.path) <| + Test_Environment.unsafe_with_environment_override "PGPASSFILE" (pgpass_file.absolute.path) <| Pgpass.verify pgpass_file . should_equal False Pgpass.read "passwords should preserve leading space" "1" "some database name that is really : weird" . should_equal [] Process.run "chmod" ["0400", pgpass_file.absolute.path] . should_equal Exit_Success - Environment.unsafe_with_environment_override "PGPASSFILE" (pgpass_file.absolute.path) <| + Test_Environment.unsafe_with_environment_override "PGPASSFILE" (pgpass_file.absolute.path) <| Pgpass.verify pgpass_file . should_equal True Pgpass.read "passwords should preserve leading space" "1" "some database name that is really : weird" . should_equal (make_pair "*" " pass") Test.specify "should correctly match wildcards and use the first matching entry" <| - Environment.unsafe_with_environment_override "PGPASSFILE" (pgpass_file.absolute.path) <| + Test_Environment.unsafe_with_environment_override "PGPASSFILE" (pgpass_file.absolute.path) <| Pgpass.read "localhost" 5432 "postgres" . should_equal (make_pair "postgres" "postgres") Pgpass.read "192.168.4.0" "1234" "foo" . should_equal (make_pair "bar" "baz") Pgpass.read "" "" "" . should_equal (make_pair "*" "fallback_password") @@ -182,9 +184,9 @@ connection_setup_spec = Test.group "[PostgreSQL] Connection setup" <| Test.specify "should use environment variables as host, port and database defaults and fall back to hardcoded defaults" <| c1 = Postgres "example.com" 12345 "my_db" c2 = Postgres - c3 = Environment.unsafe_with_environment_override "PGHOST" "192.168.0.1" <| - Environment.unsafe_with_environment_override "PGPORT" "1000" <| - Environment.unsafe_with_environment_override "PGDATABASE" "ensoDB" <| + c3 = Test_Environment.unsafe_with_environment_override "PGHOST" "192.168.0.1" <| + Test_Environment.unsafe_with_environment_override "PGPORT" "1000" <| + Test_Environment.unsafe_with_environment_override "PGDATABASE" "ensoDB" <| Postgres c1.host . should_equal "example.com" @@ -204,7 +206,7 @@ connection_setup_spec = Test.group "[PostgreSQL] Connection setup" <| ## Currently we require the port to be numeric. When we support Unix-sockets, we may lift that restriction. - c4 = Environment.unsafe_with_environment_override "PGPORT" "foobar" <| + c4 = Test_Environment.unsafe_with_environment_override "PGPORT" "foobar" <| Postgres c4.host . should_equal "localhost" c4.port . should_equal 5432 @@ -222,11 +224,11 @@ connection_setup_spec = Test.group "[PostgreSQL] Connection setup" <| c1.jdbc_url . should_equal "jdbc:postgresql://localhost:5432" c1.jdbc_properties . should_equal <| add_ssl [] - Environment.unsafe_with_environment_override "PGPASSWORD" "somepassword" <| + Test_Environment.unsafe_with_environment_override "PGPASSWORD" "somepassword" <| c1.jdbc_properties . should_fail_with Illegal_State_Error c1.jdbc_properties.catch.message . should_equal "PGPASSWORD is set, but PGUSER is not." - Environment.unsafe_with_environment_override "PGUSER" "someuser" <| + Test_Environment.unsafe_with_environment_override "PGUSER" "someuser" <| c1.jdbc_properties . should_equal <| add_ssl [Pair "user" "someuser", Pair "password" "somepassword"] c2 = Postgres "192.168.4.0" 1234 "foo" @@ -236,23 +238,23 @@ connection_setup_spec = Test.group "[PostgreSQL] Connection setup" <| c3.jdbc_properties . should_equal <| add_ssl [] c4.jdbc_properties . should_equal <| add_ssl [] - Environment.unsafe_with_environment_override "PGPASSFILE" pgpass_file.absolute.path <| + Test_Environment.unsafe_with_environment_override "PGPASSFILE" pgpass_file.absolute.path <| c2.jdbc_properties . should_equal <| add_ssl [Pair "user" "bar", Pair "password" "baz"] c3.jdbc_properties . should_equal <| add_ssl [Pair "user" "user_that_has_no_password", Pair "password" ""] c4.jdbc_properties . should_equal <| add_ssl [Pair "user" "*", Pair "password" "fallback_password"] - Environment.unsafe_with_environment_override "PGUSER" "bar" <| + Test_Environment.unsafe_with_environment_override "PGUSER" "bar" <| c2.jdbc_properties . should_equal <| add_ssl [Pair "user" "bar", Pair "password" "baz"] [c3, c4].each c-> c.jdbc_properties . should_equal <| add_ssl [Pair "user" "*", Pair "password" "fallback_password"] - Environment.unsafe_with_environment_override "PGUSER" "other user" <| + Test_Environment.unsafe_with_environment_override "PGUSER" "other user" <| [c2, c3, c4].each c-> c.jdbc_properties . should_equal <| add_ssl [Pair "user" "*", Pair "password" "fallback_password"] - Environment.unsafe_with_environment_override "PGPASSWORD" "other password" <| + Test_Environment.unsafe_with_environment_override "PGPASSWORD" "other password" <| [c2, c3, c4].each c-> c.jdbc_properties . should_equal <| add_ssl [Pair "user" "other user", Pair "password" "other password"] diff --git a/test/Tests/src/System/Environment_Spec.enso b/test/Tests/src/System/Environment_Spec.enso index 0df4f5961a99..54af3efad620 100644 --- a/test/Tests/src/System/Environment_Spec.enso +++ b/test/Tests/src/System/Environment_Spec.enso @@ -2,30 +2,31 @@ from Standard.Base import all import Standard.Base.System.Environment import Standard.Test +import Standard.Test.Environment as Test_Environment spec = Test.group "Environment" <| Test.specify "should allow to internally override environment variables for testing purposes" <| old = Environment.get "foobar" - result_0 = Environment.unsafe_with_environment_override "foobar" "value1" 23 + result_0 = Test_Environment.unsafe_with_environment_override "foobar" "value1" 23 result_0 . should_equal 23 - result_1 = Environment.unsafe_with_environment_override "foobar" "value1" <| + result_1 = Test_Environment.unsafe_with_environment_override "foobar" "value1" <| Environment.get "foobar" . should_equal "value1" 42 - result_2 = Environment.unsafe_with_environment_override "foobar" "other interesting value" <| + result_2 = Test_Environment.unsafe_with_environment_override "foobar" "other interesting value" <| Environment.get "foobar" result_1 . should_equal 42 result_2 . should_equal "other interesting value" Environment.get "foobar" . should_equal old - result_3 = Environment.unsafe_with_environment_override "foo" "1" <| + result_3 = Test_Environment.unsafe_with_environment_override "foo" "1" <| Environment.get "foo" . should_equal "1" - x = Environment.unsafe_with_environment_override "foo" "2" <| + x = Test_Environment.unsafe_with_environment_override "foo" "2" <| Environment.get "foo" . should_equal "2" - Environment.unsafe_with_environment_override "bar" "3" <| - Environment.unsafe_with_environment_override "baz" "4" <| + Test_Environment.unsafe_with_environment_override "bar" "3" <| + Test_Environment.unsafe_with_environment_override "baz" "4" <| [Environment.get "foo", Environment.get "bar", Environment.get "baz"] Environment.get "foo" . should_equal "1" x