aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/mktemp_ut.cpp
blob: 38eab941c741326dcaea84327cce5e2077f6c804 (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
#include <library/cpp/testing/unittest/registar.h>

#include <filesystem>

#include "tempfile.h"

#include <util/folder/dirut.h>

Y_UNIT_TEST_SUITE(MakeTempFileSuite) {
    static const char TestDir[] = "Test";
    static const char Prefix[] = "PREFIX_____PREFIX";
    static const char Extension[] = "txt";
    static const unsigned int PathSegmentSizeNormal = 55;
    static const unsigned int PathSegmentSizeLong = 255;

    Y_UNIT_TEST(TestMakeTempName) {
        const TFsPath systemTemp{GetSystemTempDir()};
        UNIT_ASSERT(systemTemp.Exists());

        for (auto dirNameLength : {PathSegmentSizeNormal, PathSegmentSizeLong}) {
            const TFsPath testDir{systemTemp / TestDir};
            testDir.MkDir();
            UNIT_ASSERT(testDir.Exists());
            Y_DEFER {
                std::filesystem::remove_all(testDir.c_str());
            };

            const TString dirName(dirNameLength, 'X');
            const TFsPath dirPath = testDir / dirName;
            UNIT_ASSERT(std::filesystem::create_directory(dirPath.GetPath().c_str()));

            TString tempFilePath;
            try {
                tempFilePath = MakeTempName(dirPath.c_str(), Prefix, Extension);
            } catch (const TSystemError& ex) {
                Cerr << "Unexpected exception: " << ex.what() << Endl;
            }
            UNIT_ASSERT(TFsPath{tempFilePath}.Exists());
        }
    }
} // Y_UNIT_TEST_SUITE(MakeTempFileSuite)