question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting environment variables to non-constant expressions

See original GitHub issue

Recap #369:

Annotation values must be constant expressions. That is:

@ClearSystemProperty(key = "must be a constant expression")

A user may have to set a system property to a value that is not a constant expression. In that case, they can still leverage the restore mechanism of our system property extension:

@ParameterizedTest
@ValueSource(strings = { "foo", "bar" })
@ClearSystemProperty(key = "some property")
void test(String value) {
	System.setProperty("some property", value);
}

So while there is a workaround for this in the case of system properties, there is none (to my knowledge) for environment variables. A use case for this (see Stack Overflow):

The https://junit-pioneer.org/ alternative requires environment variable values to be known at compile time. The above [using System Stubs for JUnit Jupiter] also supports the setting of environment variables in the @BeforeAll [we do this too], which means it interoperates well with things like Testcontainers that might set up some resources needed by child tests.

To support this, we could provide something like:

@ParameterizedTest
@ValueSource(strings = { "foo", "bar" })
@ClearEnvironmentVariable(key = "some property")
void test(String value) {
	EnvironmentVariable.set("some environment variable", value);
}

Internally, we currently do this using the non-public class EnvironmentVariableUtils.

What do you think?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
kiviewcommented, Apr 9, 2021

To give a concrete example, a user might need to inject the result of getJdbcUrl() into their SUT after the container has started. However, the way by which the value needs to be injected is specific to their SUT or framework and independent from using Testcontainers (and outside of the responsibilities of Testcontainers).

1reaction
nipafxcommented, Dec 21, 2021

I agree that referencing a method may be too limiting and it’s pretty boilerplatey. Also, it would be nice to bring our resetting machinery to the table. What about this?

  • provide arguments for parameters of type SystemProperties and EnvironmentVariables
  • they can be used to clear and set properties/variables
  • on clear and set, they store the initial value and reset it after the method

That would also alleviate the need for @Clear... on top of the method. Although we still need some way to apply the extensions…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Set Environment Variables - Twilio
Set Environment Variables using PowerShell​​ After $Env , add a colon, followed by the environment variable's name, followed by the equals sign, ...
Read more >
Set - Environment Variable - Windows CMD - SS64.com
SET. Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session....
Read more >
htaccess set environment variable using another environment ...
I can't find an answer for my question. Can I set an environment variable using another environment variable ...
Read more >
set (environment variable) - Windows Commands
Displays, sets, or removes cmd.exe environment variables. If used without parameters, set displays the current environment variable settings ...
Read more >
Understanding Context and Env variables with ${{ Expression }}
In this video, we will understand the context and environment variables with Github actions Expressions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found