blob: a749b05469ba9c5c35899fcbe2244903de697709 (
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include "tempfile.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/folder/dirut.h>
#include <util/generic/yexception.h>
#include <util/stream/file.h>
#include <algorithm>
Y_UNIT_TEST_SUITE(TTempFileHandle) {
Y_UNIT_TEST(Create) {
TString path;
{
TTempFileHandle tmp;
path = tmp.Name();
tmp.Write("hello world\n", 12);
tmp.FlushData();
UNIT_ASSERT_STRINGS_EQUAL(TUnbufferedFileInput(tmp.Name()).ReadAll(), "hello world\n");
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(InCurrentDir) {
#ifndef _win32_
static const TString TEST_PREFIX = "unique_prefix";
#else
static const TString TEST_PREFIX = "uni";
#endif
TString path;
{
TTempFileHandle tmp = TTempFileHandle::InCurrentDir(TEST_PREFIX);
path = tmp.Name();
UNIT_ASSERT(NFs::Exists(path));
TVector<TString> names;
TFsPath(".").ListNames(names);
bool containsFileWithPrefix = std::any_of(names.begin(), names.end(), [&](const TString& name) {
return name.Contains(TEST_PREFIX);
});
UNIT_ASSERT(containsFileWithPrefix);
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(UseExtensionWithoutDot) {
TString path;
{
TTempFileHandle tmp = TTempFileHandle::InCurrentDir("hello", "world");
path = tmp.Name();
UNIT_ASSERT(NFs::Exists(path));
#ifndef _win32_
UNIT_ASSERT(path.Contains("hello"));
UNIT_ASSERT(path.EndsWith(".world"));
UNIT_ASSERT(!path.EndsWith("..world"));
#else
UNIT_ASSERT(path.Contains("hel"));
UNIT_ASSERT(path.EndsWith(".tmp"));
#endif
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(UseExtensionWithDot) {
TString path;
{
TTempFileHandle tmp = TTempFileHandle::InCurrentDir("lorem", ".ipsum");
path = tmp.Name();
UNIT_ASSERT(NFs::Exists(path));
#ifndef _win32_
UNIT_ASSERT(path.Contains("lorem"));
UNIT_ASSERT(path.EndsWith(".ipsum"));
UNIT_ASSERT(!path.EndsWith("..ipsum"));
#else
UNIT_ASSERT(path.Contains("lor"));
UNIT_ASSERT(path.EndsWith(".tmp"));
#endif
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(SafeDestructor) {
TString path;
{
path = MakeTempName();
UNIT_ASSERT(NFs::Exists(path));
TTempFileHandle tmp(path);
Y_UNUSED(tmp);
UNIT_ASSERT(NFs::Exists(path));
TTempFileHandle anotherTmp(path);
Y_UNUSED(anotherTmp);
UNIT_ASSERT(NFs::Exists(path));
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(RemovesOpen) {
TString path;
{
TTempFileHandle tmp;
path = tmp.Name();
tmp.Write("hello world\n", 12);
tmp.FlushData();
UNIT_ASSERT(NFs::Exists(path));
UNIT_ASSERT(tmp.IsOpen());
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(NonExistingDirectory) {
UNIT_ASSERT_EXCEPTION(TTempFileHandle::InDir("nonexsistingdirname"), TSystemError);
}
} // Y_UNIT_TEST_SUITE(TTempFileHandle)
Y_UNIT_TEST_SUITE(MakeTempName) {
Y_UNIT_TEST(Default) {
TString path;
{
TTempFile tmp(MakeTempName());
path = tmp.Name();
UNIT_ASSERT(!path.Contains('\0'));
UNIT_ASSERT(NFs::Exists(path));
UNIT_ASSERT(path.EndsWith(".tmp"));
#ifndef _win32_
UNIT_ASSERT(path.Contains("yandex"));
#else
UNIT_ASSERT(path.Contains("yan"));
#endif
}
UNIT_ASSERT(!NFs::Exists(path));
}
Y_UNIT_TEST(UseNullptr) {
TString path;
{
TTempFile tmp(MakeTempName(nullptr, nullptr, nullptr));
path = tmp.Name();
UNIT_ASSERT(!path.Contains('\0'));
UNIT_ASSERT(NFs::Exists(path));
}
UNIT_ASSERT(!NFs::Exists(path));
}
} // Y_UNIT_TEST_SUITE(MakeTempName)
|