
include(${TEST_SOURCE_DIR}/AppLauncherTestMacros.cmake)
include(${TEST_BINARY_DIR}/AppLauncherTestPrerequisites.cmake)

# --------------------------------------------------------------------------
# Debug flags - Set to True to display the command as string
set(PRINT_COMMAND 0)

# --------------------------------------------------------------------------
if(WIN32)
  set(pathsep ";")
else()
  set(pathsep ":")
endif()

set(sys_env_var_name "CTKAPPLAUNCHER_TEST_ENV_EXPRESSION_NOTFOUND")
set(sys_env_var_value "Powder as in snow")
set(regular_env_var_name_1 "SOMETHING_NICE")
set(regular_env_var_value_1 "Chocolate")
set(regular_env_var_name_2 "SOMETHING_AWESOME")
set(regular_env_var_value_2 "Rock climbing !")
set(regular_env_var_name_3 "SOMETHING_GREAT")
set(regular_env_var_value_3 "<env:${sys_env_var_name}>")
set(regular_env_var_name_4 "SOME_PATH")
set(regular_env_var_value_4 "/farm/cow${pathsep}/farm/pig")

unset(ENV{${sys_env_var_name}})

# --------------------------------------------------------------------------
# Configure settings file
file(WRITE "${launcher}LauncherSettings.ini" "
[Application]
path=${application}
arguments=--check-env

[LibraryPaths]
1\\path=${library_path}
size=1

[Environment]
additionalPathVariables=SOME_PATH

[EnvironmentVariables]
${regular_env_var_name_1}=${regular_env_var_value_1}
${regular_env_var_name_2}=${regular_env_var_value_2}
${regular_env_var_name_3}=${regular_env_var_value_3}

[SOME_PATH]
1\\path=/farm/cow
2\\path=/farm/pig
size=2
")

# --------------------------------------------------------------------------
# Make sure the launcher passes the environment variable to the application
set(command ${launcher_exe} --launcher-no-splash)
execute_process(
  COMMAND ${command}
  WORKING_DIRECTORY ${launcher_binary_dir}
  ERROR_VARIABLE ev
  OUTPUT_VARIABLE ov
  RESULT_VARIABLE rv
  )

print_command_as_string("${command}")

if(rv)
  message(FATAL_ERROR "[${launcher_exe}] failed to start application [${application}] from "
                      "directory [${launcher_binary_dir}]\n${ev}")
endif()

set(expected_regular_env_var_name_1 "${regular_env_var_name_1}")
set(expected_regular_env_var_value_1 "${regular_env_var_value_1}")
set(expected_regular_env_var_name_2 "${regular_env_var_name_2}")
set(expected_regular_env_var_value_2 "${regular_env_var_value_2}")
set(expected_regular_env_var_name_3 "${regular_env_var_name_3}")
set(expected_regular_env_var_value_3 "<env-NOTFOUND:${sys_env_var_name}>")
set(expected_regular_env_var_name_4 "${regular_env_var_name_4}")
set(expected_regular_env_var_value_4 "${regular_env_var_value_4}")

set(expected_msg "
${expected_regular_env_var_name_1}=${expected_regular_env_var_value_1}
${expected_regular_env_var_name_2}=${expected_regular_env_var_value_2}
${expected_regular_env_var_name_3}=${expected_regular_env_var_value_3}
${expected_regular_env_var_name_4}=${expected_regular_env_var_value_4}
")
string(REGEX MATCH "${expected_msg}" current_msg "${ov}")
if(NOT "${expected_msg}" STREQUAL "${current_msg}")
  message(FATAL_ERROR "Failed to pass environment variable from ${launcher_name} "
                      "to ${application_name} - expected_msg:${expected_msg} not found in text:\n${ov}")
endif()
