Environment variablesΒΆ

FORWARD_ENV option can be used to forward environment variable from host to target. Feature is useful while running tests on iOS and Android devices since environment for such tests created from scratch and is not the same as local user environment. For other platforms there are no extra functionality introduced.

As an example let assume test is reading environment variables MY_GAUZE_VARIABLE_1 and MY_GAUZE_VARIABLE_2:

#include <iostream> // std::cout
#include <cstdlib> // EXIT_SUCCESS

int gauze_main(int argc, char** argv)
{
  if (argc != 1)
  {
    std::cerr << "No arguments expected" << std::endl;
    return EXIT_FAILURE;
  }

  const char* var_1_name = "MY_GAUZE_VARIABLE_1";
  const char* var_1 = std::getenv(var_1_name);

  if (var_1 == nullptr)
  {
    std::cerr << "Variable " << var_1_name << " not found" << std::endl;
    return EXIT_FAILURE;
  }

  if (std::string(var_1) != "42")
  {
    std::cerr << "Variable " << var_1_name << " unexpected value" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "Variable " << var_1_name << " found!" << std::endl;

  const char* var_2_name = "MY_GAUZE_VARIABLE_2";
  const char* var_2 = std::getenv(var_2_name);

  if (var_2 != nullptr)
  {
    std::cout << var_2_name << " value: " << var_2 << std::endl;
  }

  return EXIT_SUCCESS;
}

Environment variable MY_GAUZE_VARIABLE_1 will be set by CTest:

add_executable(gauze_forward_env main.cpp)

gauze_add_test(
    NAME gauze_forward_env
    COMMAND gauze_forward_env
    FORWARD_ENV MY_GAUZE_VARIABLE_1 MY_GAUZE_VARIABLE_2
)

set_tests_properties(
    gauze_forward_env
    PROPERTIES
    ENVIRONMENT
    MY_GAUZE_VARIABLE_1=42
)

Run test (Android build):

> ctest -VV -R gauze_forward_env
...
5: Forwarding user's variable 'MY_GAUZE_VARIABLE_1' with value '42'
5: Forwarding user's variable 'MY_GAUZE_VARIABLE_2' with value ''
5: Command output (with exit code):
5: *** BEGIN ***
5: Variable MY_GAUZE_VARIABLE_1 found!
5: MY_GAUZE_VARIABLE_2 value:
5: 0
5: *** END ***
5: Done
1/1 Test #5: gauze_forward_env ................   Passed    0.53 sec

If environment variable MY_GAUZE_VARIABLE_2 will be set on host then Gauze will forward it to the Android test environment:

> export MY_GAUZE_VARIABLE_2=hello
> ctest -VV -R gauze_forward_env
...
5: Forwarding user's variable 'MY_GAUZE_VARIABLE_1' with value '42'
5: Forwarding user's variable 'MY_GAUZE_VARIABLE_2' with value 'hello'
5: Command output (with exit code):
5: *** BEGIN ***
5: Variable MY_GAUZE_VARIABLE_1 found!
5: MY_GAUZE_VARIABLE_2 value: hello
5: 0
5: *** END ***
5: Done
1/1 Test #5: gauze_forward_env ................   Passed    0.48 sec

There is no need to rebuild test or reconfigure CMake project.