1 static const char help[] = "Tests env: directive in test harness language.\n\n";
2
3 #include <petscsys.h>
4
main(int argc,char * argv[])5 int main(int argc, char *argv[])
6 {
7 PetscBool env_set;
8 char env_vars[PETSC_MAX_PATH_LEN];
9 int num_env;
10 char **env_vars_arr;
11
12 PetscFunctionBeginUser;
13 PetscCall(PetscInitialize(&argc, &argv, NULL, help));
14 PetscCall(PetscArrayzero(env_vars, PETSC_MAX_PATH_LEN));
15
16 PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Test Options", NULL);
17 PetscCall(PetscOptionsString("-env_vars_def", "Environment variables set", NULL, env_vars, env_vars, sizeof(env_vars), &env_set));
18 PetscOptionsEnd();
19
20 PetscCall(PetscStrToArray(env_vars, ' ', &num_env, &env_vars_arr));
21 for (int i = 0; i < num_env; ++i) {
22 const char *current_var = env_vars_arr[i];
23 PetscBool set, equal;
24 size_t name_len;
25 char env[PETSC_MAX_PATH_LEN];
26 char *name, *value = NULL;
27
28 // given FOO=bar we want to extract
29 // name = FOO
30 // value = bar
31 PetscCall(PetscStrchr(current_var, '=', &value));
32 PetscCheck(value, PETSC_COMM_SELF, PETSC_ERR_PLIB, "= not found in %s", current_var);
33 PetscCheck(value >= current_var, PETSC_COMM_SELF, PETSC_ERR_PLIB, "= not found in %s", current_var);
34 // value points to '=' so increment it first
35 ++value;
36
37 name_len = (size_t)(value - current_var);
38 PetscCall(PetscMalloc1(name_len, &name));
39 PetscCall(PetscStrncpy(name, env_vars_arr[i], name_len));
40
41 PetscCall(PetscArrayzero(env, PETSC_MAX_PATH_LEN));
42 PetscCall(PetscOptionsGetenv(PETSC_COMM_WORLD, name, env, sizeof(env), &set));
43 PetscCheck(set, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Test harness failed to set %s", name);
44 PetscCall(PetscStrcmp(value, env, &equal));
45 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Test harness failed to set %s to the right value. Expected '%s', have '%s'", name, value, env);
46 PetscCall(PetscFree(name));
47 }
48 PetscCall(PetscStrToArrayDestroy(num_env, env_vars_arr));
49
50 PetscCall(PetscFinalize());
51 return 0;
52 }
53
54 /*TEST
55
56 testset:
57 args: -env_vars_def 'FOO=1 BAR=0 BAZ= BOP=1'
58 suffix: env_set
59 output_file: output/empty.out
60 test:
61 env: FOO=1 BAR=0 BAZ= BOP=${FOO}
62 suffix: all_one_line
63 test:
64 env: FOO=1
65 env: BAR=0
66 env: BAZ=
67 env: BOP=${FOO}
68 suffix: all_separate_lines
69
70 test:
71 args: -env_vars_def 'FOO=hello'
72 env: FOO='hello'
73 suffix: env_set_quoted
74 output_file: output/empty.out
75
76 test:
77 suffix: env_not_set
78 output_file: output/empty.out
79
80 TEST*/
81