aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/env_ut.cpp
blob: 6023ce2afdd83260be6b5fd111d3846cbf3e050f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/string.h>
#include "env.h"

Y_UNIT_TEST_SUITE(EnvTest) {
    Y_UNIT_TEST(GetSetEnvTest) {
        TString key = "util_GETENV_TestVar";
        TString value = "Some value for env var";
        TString def = "Some default value for env var";
        // first of all, it should be clear
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), TString());
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, def), def);
        SetEnv(key, value);
        // set and see what value we get here
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), value);
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, def), value);
        // set empty value
        SetEnv(key, TString());
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(key), TString());

        // check for long values, see IGNIETFERRO-214
        TString longKey = "util_GETENV_TestVarLong";
        TString longValue{1500, 't'};
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), TString());
        SetEnv(longKey, longValue);
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), longValue);
        SetEnv(longKey, TString());
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(longKey), TString());
    }

    Y_UNIT_TEST(TryGetEnv) {
        TString key = "util_TryGetEnv_var";
        UNIT_ASSERT_NO_EXCEPTION(TryGetEnv(key));
        SetEnv(key, "value");
        UNIT_ASSERT(TryGetEnv(key).Defined());
        UNIT_ASSERT_VALUES_EQUAL(*TryGetEnv(key), "value");
        UnsetEnv(key);
        UNIT_ASSERT(TryGetEnv(key).Empty());
    }

    Y_UNIT_TEST(UnsetEnv) {
        TString key = "util_UnsetEnv_var";
        SetEnv(key, "value");
        UnsetEnv(key);
        UNIT_ASSERT_VALUES_EQUAL(GetEnv(key, "default_value"), "default_value");
    }

    Y_UNIT_TEST(UnsetNonexistingEnv) {
        TString key = "util_UnsetNonexistingEnv_var";
        UNIT_ASSERT_NO_EXCEPTION(UnsetEnv(key));
        UNIT_ASSERT_NO_EXCEPTION(UnsetEnv(key));
    }

    Y_UNIT_TEST(SetEnvInvalidName) {
        UNIT_ASSERT_EXCEPTION(SetEnv("", "value"), yexception);
        UNIT_ASSERT_EXCEPTION(SetEnv("A=B", "C=D"), yexception);
    }
}