Jlm
Loading...
Searching...
No Matches
FileTests.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 Nico Reißmann <nico.reissmann@gmail.com>
3 * Copyright 2025 Håvard Krogstie <krogstie.havard@gmail.com>
4 * See COPYING for terms of redistribution.
5 */
6
7#include <gtest/gtest.h>
8
9#include <jlm/util/file.hpp>
10
11#include <vector>
12
13TEST(FileTests, TestFilePathMethods)
14{
15 const jlm::util::FilePath f("/tmp/archive.tar.gz");
16
17 EXPECT_EQ(f.to_str(), "/tmp/archive.tar.gz");
18 EXPECT_EQ(f.name(), "archive.tar.gz");
19 EXPECT_EQ(f.base(), "archive");
20 EXPECT_EQ(f.suffix(), "gz");
21 EXPECT_EQ(f.complete_suffix(), "tar.gz");
22 EXPECT_EQ(f.Dirname(), "/tmp");
23
24 std::vector<std::pair<std::string, std::string>> pathPairs = { { "/tmp/jlm/", "/tmp" },
25 { "/tmp/jlm", "/tmp" },
26 { "/tmp/", "/" },
27 { "/tmp", "/" },
28 { "d/d2/file.txt", "d/d2" },
29 { "test.txt", "." },
30 { "./test2.txt", "." },
31 { "a/..", "a" },
32 { "/", "/" },
33 { ".", "." },
34 { "", "." } };
35 for (const auto & [fullPath, path] : pathPairs)
36 {
37 const auto result = jlm::util::FilePath(fullPath).Dirname();
38 EXPECT_EQ(result, path);
39 }
40}
41
42TEST(FileTests, TestCreateDirectory)
43{
44 const auto filePath = jlm::util::FilePath::TempDirectoryPath().Join("jlm-test-create-dir");
45
46 // Remove the directory if it survived from a previous test
47 if (filePath.Exists())
48 std::filesystem::remove(filePath.to_str());
49 EXPECT_FALSE(filePath.Exists());
50
51 // Act
52 filePath.CreateDirectory();
53
54 // Assert that the directory now exists
55 EXPECT_TRUE(filePath.Exists() && filePath.IsDirectory());
56
57 // Try creating a directory that already exists, should be no issue
58 filePath.CreateDirectory();
59
60 // Try creating a directory in a location that does not exist
61 jlm::util::FilePath noSuchParent("/non-existant/test-dir");
62 EXPECT_THROW(noSuchParent.CreateDirectory(), jlm::util::Error);
63
64 // Cleanup
65 std::filesystem::remove(filePath.to_str());
66}
67
68TEST(FileTests, TestFilepathJoin)
69{
70 const jlm::util::FilePath path1("tmp");
71 const jlm::util::FilePath path2("a/b/");
72 const jlm::util::FilePath path3("/c/d");
73 const jlm::util::FilePath path4(".");
74 const jlm::util::FilePath emptyPath("");
75
76 EXPECT_EQ(path1.Join(path2).to_str(), "tmp/a/b/");
77 EXPECT_EQ(path2.Join(path1).to_str(), "a/b/tmp");
78
79 EXPECT_EQ(path1.Join(path3).to_str(), "/c/d");
80 EXPECT_EQ(path3.Join(path1).to_str(), "/c/d/tmp");
81
82 EXPECT_EQ(path4.Join(path1).to_str(), "tmp");
83 EXPECT_EQ(path4.Join(path2).to_str(), "a/b/");
84 EXPECT_EQ(path1.Join(path4).to_str(), "tmp/.");
85 EXPECT_EQ(path2.Join(path4).to_str(), "a/b/.");
86
87 EXPECT_EQ(emptyPath.Join(path1).to_str(), "tmp");
88}
89
90TEST(FileTests, TestCreateUniqueFileName)
91{
92 // Arrange
93 auto randomString = jlm::util::CreateRandomAlphanumericString(6);
94 auto tmpDirectory = jlm::util::FilePath::TempDirectoryPath().Join(randomString);
95
96 // Act
97 auto filePath = jlm::util::FilePath::createUniqueFileName(tmpDirectory, "myPrefix", "mySuffix");
98
99 // Assert
100 EXPECT_EQ(filePath.Dirname(), tmpDirectory.to_str());
101}
TEST(FileTests, TestFilePathMethods)
Definition FileTests.cpp:13
static FilePath createUniqueFileName(const FilePath &directory, const std::string &fileNamePrefix, const std::string &fileNameSuffix)
Generates a unique file in a given directory with a prefix and suffix.
Definition file.hpp:301
std::string name() const noexcept
Returns the name of the file, excluding the path.
Definition file.hpp:81
void CreateDirectory() const
Definition file.hpp:258
static FilePath TempDirectoryPath()
Definition file.hpp:317
std::string complete_suffix() const noexcept
Returns the complete suffix (extension) of the file.
Definition file.hpp:98
FilePath Join(const std::string &other) const
Definition file.hpp:193
std::string base() const noexcept
Returns the base name of the file without the path.
Definition file.hpp:61
const std::string & to_str() const noexcept
Definition file.hpp:275
FilePath Dirname() const noexcept
Returns the path to the file or directory's parent directory. Emulates the behavior of the GNU coreut...
Definition file.hpp:148
std::string suffix() const noexcept
Returns the suffix (extension) of the file.
Definition file.hpp:116
std::string CreateRandomAlphanumericString(std::size_t length)
Definition strfmt.cpp:15