Skip to content

Commit

Permalink
Fix: purduesigbots#8 Make LoggerTests work cross-platform
Browse files Browse the repository at this point in the history
The previous tests used a technically write-only stream in memory, which causes the tests to fail on some systems. Instead, we can use an `fmemopen` stream to ensure it is always readable.
  • Loading branch information
NoRePercussions committed Feb 9, 2023
1 parent 5397aa7 commit c4e0345
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions test/loggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ using namespace okapi;
class LoggerTest : public ::testing::Test {
protected:
virtual void SetUp() {
logFile = open_memstream(&logBuffer, &logSize);
logBuffer = new char[logSize];
// For testing, we always write, then rewind and read
// so it is not an issue to open in write vs append
logFile = fmemopen(logBuffer, logSize, "w+");
}

virtual void TearDown() {
// Call close after every case so other tests don't end up with a NULL logfile pointer
if (logger) {
logger->close();
}
free(logBuffer);
}

void logData(const std::shared_ptr<Logger> &) const {
Expand All @@ -32,7 +34,7 @@ class LoggerTest : public ::testing::Test {

FILE *logFile;
char *logBuffer;
size_t logSize;
size_t logSize = 10000;
std::shared_ptr<Logger> logger;
};

Expand All @@ -41,6 +43,8 @@ TEST_F(LoggerTest, OffLevel) {
std::make_unique<ConstantMockTimer>(0_ms), logFile, Logger::LogLevel::off);

logData(logger);
fputs("EMPTY_FILE", logFile);
rewind(logFile);

char *line = nullptr;
size_t len;
Expand All @@ -60,6 +64,7 @@ TEST_F(LoggerTest, ErrorLevel) {
std::make_unique<ConstantMockTimer>(0_ms), logFile, Logger::LogLevel::error);

logData(logger);
rewind(logFile);

char *line = nullptr;
size_t len;
Expand All @@ -78,6 +83,7 @@ TEST_F(LoggerTest, WarningLevel) {
std::make_unique<ConstantMockTimer>(0_ms), logFile, Logger::LogLevel::warn);

logData(logger);
rewind(logFile);

char *line = nullptr;
size_t len;
Expand All @@ -100,6 +106,7 @@ TEST_F(LoggerTest, InfoLevel) {
std::make_unique<ConstantMockTimer>(0_ms), logFile, Logger::LogLevel::info);

logData(logger);
rewind(logFile);

char *line = nullptr;
size_t len;
Expand All @@ -126,6 +133,7 @@ TEST_F(LoggerTest, DebugLevel) {
std::make_unique<ConstantMockTimer>(0_ms), logFile, Logger::LogLevel::debug);

logData(logger);
rewind(logFile);

char *line = nullptr;
size_t len;
Expand All @@ -152,6 +160,7 @@ TEST_F(LoggerTest, DebugLevel) {
}

TEST_F(LoggerTest, TestLazyLogging) {
rewind(logFile);
logger = std::make_shared<Logger>(
std::make_unique<ConstantMockTimer>(0_ms), logFile, Logger::LogLevel::info);

Expand Down

0 comments on commit c4e0345

Please sign in to comment.