LCOV - code coverage report
Current view: top level - libs/testitall/src - execute_and_set_variable.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 73.3 % 15 11
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 1 1

            Line data    Source code
       1              : #include "testitall.h"
       2              : 
       3              : /**
       4              :  * @brief Executes a command and sets its output as an environment variable
       5              :  *
       6              :  * @param variable Name of the environment variable to set
       7              :  * @param command Command to execute
       8              :  * @param expected_return_code Expected return code from the command execution
       9              :  *
      10              :  * @return SUCCESS if command executed successfully and variable was set
      11              :  *         FAILURE if command failed or output was empty
      12              :  *
      13              :  * @note The function allocates memory for storing command output temporarily
      14              :  * @note Environment variable will only be set if command produces non-empty output
      15              :  */
      16            1 : Return execute_and_set_variable(
      17              :         const char *variable,
      18              :         const char *command,
      19              :         const int  expected_return_code)
      20              : {
      21            1 :         if(!variable || !command)
      22              :         {
      23            0 :                 serp("NULL pointer passed to execute_and_set_variable");
      24            0 :                 return FAILURE;
      25              :         }
      26              : 
      27              :         /// The status that will be passed to return() before exiting.
      28              :         /// By default, the function worked without errors.
      29            1 :         Return status = SUCCESS;
      30              : 
      31              :         // Create memory for storing command output
      32            1 :         create(char,result);
      33              : 
      34              :         // Execute command and capture output
      35            1 :         call(execute_command(command,result,expected_return_code,ALLOW_BOTH));
      36              : 
      37            1 :         if(SUCCESS == status)
      38              :         {
      39              :                 // Only set environment variable if we got some output
      40            1 :                 if(result->length > 0)
      41              :                 {
      42            1 :                         run(trim_trailing_eol(result));
      43              : 
      44            1 :                         run(set_environment_variable(variable,getcstring(result)));
      45              : 
      46              :                 } else {
      47              :                         // Empty output is considered a failure
      48            0 :                         echo(STDERR,"Command produced no output: %s\n",command);
      49            0 :                         status = FAILURE;
      50              :                 }
      51              :         }
      52              : 
      53              :         // Cleanup allocated memory
      54            1 :         call(del(result));
      55              : 
      56            1 :         return(status);
      57              : }
        

Generated by: LCOV version 2.0-1