Skip to content

Commit

Permalink
Move unsafe override to Test.Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jul 19, 2022
1 parent 7132f61 commit a4b57b2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions distribution/lib/Standard/Test/0.0.0-dev/src/Environment.enso
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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}).
*
* <p>This is an internal function that should be used very carefully and only for testing.
*/
Expand Down
30 changes: 16 additions & 14 deletions test/Table_Tests/src/Database/Postgres_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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"]

Expand Down
15 changes: 8 additions & 7 deletions test/Tests/src/System/Environment_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a4b57b2

Please sign in to comment.