|
| 1 | +/************************************************************\ |
| 2 | + * Copyright 2023 Lawrence Livermore National Security, LLC |
| 3 | + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | + * |
| 5 | + * This file is part of the Flux resource manager framework. |
| 6 | + * For details, see https://github.com/flux-framework. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: LGPL-3.0 |
| 9 | +\************************************************************/ |
| 10 | + |
| 11 | +#include <errno.h> |
| 12 | + |
| 13 | +#include "ccan/str/str.h" |
| 14 | +#include "src/common/libtap/tap.h" |
| 15 | +#include "src/common/libutil/environment.h" |
| 16 | + |
| 17 | +static void test_var_next () |
| 18 | +{ |
| 19 | + const char *entry = NULL; |
| 20 | + struct environment *e = environment_create (); |
| 21 | + if (e == NULL) |
| 22 | + BAIL_OUT ("Failed to create environment object!"); |
| 23 | + ok (environment_var_next (e, "PATH", NULL) == NULL, |
| 24 | + "environment_var_next () returns NULL for missing env var"); |
| 25 | + environment_set (e, "PATH", "/bin:/usr/bin:/usr/local/bin", ':'); |
| 26 | + diag ("set PATH=/bin:/usr/bin:/usr/local/bin"); |
| 27 | + ok ((entry = environment_var_next (e, "PATH", entry)) != NULL, |
| 28 | + "environment_var_next () works"); |
| 29 | + is (entry, "/bin", |
| 30 | + "environment_var_next returns first element"); |
| 31 | + ok ((entry = environment_var_next (e, "PATH", entry)) != NULL, |
| 32 | + "environment_var_next () works"); |
| 33 | + is (entry, "/usr/bin", |
| 34 | + "environment_var_next returns next element"); |
| 35 | + ok ((entry = environment_var_next (e, "PATH", entry)) != NULL, |
| 36 | + "environment_var_next () works"); |
| 37 | + is (entry, "/usr/local/bin", |
| 38 | + "environment_var_next returns last element"); |
| 39 | + ok (!(entry = environment_var_next (e, "PATH", entry)), |
| 40 | + "environment_var_next () returns NULL after last element"); |
| 41 | + environment_destroy (e); |
| 42 | +} |
| 43 | + |
| 44 | +static void test_insert () |
| 45 | +{ |
| 46 | + const char *entry = NULL; |
| 47 | + struct environment *e = environment_create (); |
| 48 | + if (e == NULL) |
| 49 | + BAIL_OUT ("Failed to create environment object!"); |
| 50 | + ok (environment_insert (e, "PATH", "/bin", "/foo") < 0 && errno == ENOENT, |
| 51 | + "environment_insert on missing key returns ENOENT"); |
| 52 | + environment_set (e, "PATH", "/bin:/usr/bin:/usr/local/bin", ':'); |
| 53 | + diag ("set PATH=/bin:/usr/bin:/usr/local/bin"); |
| 54 | + diag ("searching for entry=/usr/bin"); |
| 55 | + while ((entry = environment_var_next (e, "PATH", entry))) |
| 56 | + if (streq (entry, "/usr/bin")) |
| 57 | + break; |
| 58 | + diag ("entry=%s", entry); |
| 59 | + ok (environment_insert (e, "PATH", (char *) entry, "/new/path") == 0, |
| 60 | + "environment_insert /new/path before /usr/bin return success"); |
| 61 | + is (environment_get (e, "PATH"), |
| 62 | + "/bin:/new/path:/usr/bin:/usr/local/bin", |
| 63 | + "PATH is now /bin:/new/path:/usr/bin:/usr/local/bin"); |
| 64 | + environment_destroy (e); |
| 65 | +} |
| 66 | + |
| 67 | +int main (int argc, char *argv[]) |
| 68 | +{ |
| 69 | + plan (NO_PLAN); |
| 70 | + |
| 71 | + test_var_next (); |
| 72 | + test_insert (); |
| 73 | + |
| 74 | + done_testing (); |
| 75 | + |
| 76 | + return 0; |
| 77 | +}; |
| 78 | + |
| 79 | +/* |
| 80 | + * vi:tabstop=4 shiftwidth=4 expandtab |
| 81 | + */ |
0 commit comments