1

I've used the framework Criterion to write unit tests. I need to pass string from main to macros, that executes source code and check the result. I can't use global variable. Here is the full code:

#include <stdio.h>
#include <criterion/criterion.h>
#include <criterion/parameterized.h>

char* g_path_to_dir; //it doesn't work, because global variable is accessible within the same process 

struct input_data_t {
    const   char* path;
};

ParameterizedTestParameters(ArgsForenforce_perms, input_data_for_enforce_perms) {
    static struct input_data_t params[] = {
"some_data", 
"some_data2" 
};

    size_t nb_params = sizeof (params) / sizeof (struct input_data_t);
    return cr_make_param_array(struct input_data_t, params, nb_params);
}

ParameterizedTest(struct input_data_t *param, ArgsForenforce_perms,
          input_data_for_enforce_perms) {
    printf("pid of process in ParameterizedTest = %d \n", getpid());
}

int main(int argc, char *argv[])
{
    printf("pid of process in main = %d \n", getpid());
    g_path_to_dir = argv[1]; //it doesn't work, because global variable is accessible within the same process 

    struct criterion_test_set *tests = criterion_initialize();
    if (criterion_handle_args(argc, argv, true)) {
        result = !criterion_run_all_tests(tests);
    }

    criterion_finalize(tests);
    return result;
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
Sfdfsg
  • 127
  • 1
  • 11
  • When you say macros, are you referring to the pre-processor directives? https://stackoverflow.com/questions/13862910/unit-testing-methods-created-by-a-macro – acarlstein Mar 11 '19 at 16:29
  • I mean, that my code is executed inside ParameterizedTest. It is not related to your link. – Sfdfsg Mar 11 '19 at 16:40
  • Why can't you use global variables? – Andreas Mar 11 '19 at 19:55
  • Where is g_path_to_dir used? In your example code it is not used. – Dirk Herrmann Mar 11 '19 at 21:14
  • @Andreas I can not use a global variable, because global variable is initialized and visible within first process, and when ParameterizedTest is executed in the second process, which has other global variable. – Sfdfsg Mar 12 '19 at 10:05
  • @DirkHerrmann g_path_to_dir will be used in macros ParameterizedTest. I simplified this example. – Sfdfsg Mar 12 '19 at 10:06
  • Do you mean to say that g_path_to_dir is used in the implementation of the ParameterizedTest macro that is part of the Criterion framework? – Dirk Herrmann Mar 12 '19 at 10:59
  • @DirkHerrmann no, it is my variable. It is not the part of Criterion. – Sfdfsg Mar 12 '19 at 13:28
  • 1
    depending on how criterion works internally maybe the environment variables are inherited, allowing you to use `setenv` and `getenv` for passing data from parent to child process: https://unix.stackexchange.com/questions/17758/exception-of-inheritance-of-environment-variables – Andreas Mar 12 '19 at 16:56
  • It is a bit surprising that global variables can't be used - I wonder how this should allow to test a SUT that uses global variables with Criterion. In any case, I suspect that there might also be a better solution for this particular case: The name g_path_to_dir indicates to me that you might be doing something for which better solutions exist. Without knowing the code it is only a suspicion. That said, the proposal from Andreas regarding the use of environment variables might be a workable solution. – Dirk Herrmann Mar 12 '19 at 19:07

0 Answers0