Skip to content

Commit ddfbeb1

Browse files
Added a more verbose error message to the constructor of the BufferedFile class.
1 parent 817b092 commit ddfbeb1

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

tests/BufferedFile.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Copyright (C) 2024 MariaDB Corporation.
2+
3+
This program is free software; you can redistribute it and/or
4+
modify it under the terms of the GNU General Public License
5+
as published by the Free Software Foundation; version 2 of
6+
the License.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
MA 02110-1301, USA. */
17+
18+
#include <cstdio>
19+
#include <fstream>
20+
#include <sys/stat.h>
21+
#include <gtest/gtest.h>
22+
#include "BufferedFile.h"
23+
24+
std::string message(const std::string& fname, int err)
25+
{
26+
return std::string("unable to open file: ") + fname + ", exception: " + strerror(err);
27+
}
28+
29+
TEST(BufferedFileTest, NoError)
30+
{
31+
const char fname[] = "/tmp/foo.bar";
32+
std::ofstream out(fname);
33+
34+
EXPECT_NO_THROW(idbdatafile::BufferedFile bf(fname, "r", 0));
35+
remove(fname);
36+
}
37+
38+
TEST(BufferedFileTest, NoEnt)
39+
{
40+
const char fname[] = "/home/BufferedFile_test";
41+
42+
EXPECT_THROW(
43+
{
44+
try
45+
{
46+
idbdatafile::BufferedFile bf(fname, "r", 0);
47+
}
48+
catch (const std::runtime_error& e)
49+
{
50+
EXPECT_EQ(message(fname, ENOENT), e.what());
51+
throw;
52+
}
53+
},
54+
std::runtime_error);
55+
}
56+
57+
TEST(BufferedFileTest, NotDir)
58+
{
59+
std::string fake_dir_name = "/tmp/foo";
60+
std::ofstream out(fake_dir_name);
61+
std::string fname = fake_dir_name + "/bar";
62+
63+
EXPECT_THROW(
64+
{
65+
try
66+
{
67+
idbdatafile::BufferedFile bf(fname.c_str(), "r", 0);
68+
}
69+
catch (const std::runtime_error& e)
70+
{
71+
EXPECT_EQ(message(fname, ENOTDIR), e.what());
72+
throw;
73+
}
74+
},
75+
std::runtime_error);
76+
remove(fake_dir_name.c_str());
77+
}

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ if(WITH_UNITTESTS)
101101
columnstore_link(bytestream ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit)
102102
add_test(NAME columnstore:bytestream COMMAND bytestream)
103103

104+
add_executable(idbdatafile_test BufferedFile.cpp)
105+
columnstore_link(idbdatafile_test ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} gtest gtest_main)
106+
add_test(NAME columnstore:idbdatafile_test COMMAND idbdatafile_test)
107+
104108
# standalone EM routines test
105109
add_executable(brm_em_standalone brm-em-standalone.cpp)
106110
columnstore_link(brm_em_standalone ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit)

utils/idbdatafile/BufferedFile.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ BufferedFile::BufferedFile(const char* fname, const char* mode, unsigned opts)
3131
: IDBDataFile(fname), m_fp(0), m_buffer(0)
3232
{
3333
m_fp = fopen(fname, mode);
34+
int err = errno;
3435

3536
if (m_fp == NULL)
3637
{
37-
throw std::runtime_error("unable to open Buffered file ");
38+
static string message = "unable to open file: ";
39+
40+
throw std::runtime_error(message + fname + ", exception: " + strerror(err));
3841
}
3942

4043
applyOptions(opts);

0 commit comments

Comments
 (0)