summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/unittests/env_ut.cpp
blob: 55cfa86c835ebac7bcef293982a55a8eeaee980d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/yt/system/env.h>

#include <library/cpp/yt/misc/guid.h>

#include <library/cpp/yt/string/format.h>

#include <util/generic/scope.h>

#include <util/system/env.h>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

TEST(TParseEnvironNameValuePairTest, NonNull)
{
    auto pair = ParseEnvironNameValuePair("var=value");
    EXPECT_EQ(pair.first, "var");
    EXPECT_EQ(pair.second, "value");
}

TEST(TParseEnvironNameValuePairTest, Null)
{
    auto pair = ParseEnvironNameValuePair("some");
    EXPECT_EQ(pair.first, "some");
    EXPECT_EQ(pair.second, std::nullopt);
}

////////////////////////////////////////////////////////////////////////////////

#if defined(_linux_) || defined(_darwin_)

TEST(TGetEnvironNameValuePairsTest, Simple)
{
    auto key = Format("var%v", TGuid::Create());
    auto value = TString("value");
    auto pair = Format("%v=%v", key, value);
    SetEnv(key, value);
    auto pairs = GetEnvironNameValuePairs();
    EXPECT_TRUE(std::ranges::find(pairs, pair) != pairs.end());
    UnsetEnv(key);
}

TEST(TGetEnvValueOrThrowTest, Typed)
{
    auto key = Format("var%v", TGuid::Create());
    SetEnv(key, "42");
    Y_DEFER {
        UnsetEnv(key);
    };
    EXPECT_EQ(GetEnvValueOrThrow<int>(key), 42);
    EXPECT_EQ(GetEnvValueOrThrow(key), "42");
}

TEST(TGetEnvValueOrThrowTest, Missing)
{
    auto key = Format("var%v", TGuid::Create());
    EXPECT_THROW(GetEnvValueOrThrow<int>(key), std::exception);
}

TEST(TGetEnvValueOrThrowTest, Unparseable)
{
    auto key = Format("var%v", TGuid::Create());
    SetEnv(key, "not-a-number");
    Y_DEFER {
        UnsetEnv(key);
    };
    EXPECT_THROW(GetEnvValueOrThrow<int>(key), std::exception);
}

#endif

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT