Skip to content

Commit

Permalink
- F Introduce ApprovalFileLog (currently writing to working directory)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Jan 14, 2025
1 parent 19d4d3b commit 795f5eb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ApprovalTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ add_library(${PROJECT_NAME}
launchers/CommandLauncher.h
launchers/SystemLauncher.h
launchers/SystemLauncher.cpp
logs/ApprovalFileLog.cpp
logs/ApprovalFileLog.hpp
namers/ApprovalTestNamer.h
namers/ApprovalTestNamer.cpp
namers/DefaultNamerDisposer.h
Expand Down
8 changes: 6 additions & 2 deletions ApprovalTests/core/FileApprover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ApprovalTests/utilities/FileUtils.h"
#include "ApprovalTests/utilities/SystemUtils.h"
#include "ApprovalTests/reporters/FrontLoadedReporterFactory.h"
#include <ApprovalTests/logs/ApprovalFileLog.hpp>
#include "ApprovalException.h"

#include <sstream>
Expand Down Expand Up @@ -63,8 +64,10 @@ namespace ApprovalTests
const ApprovalWriter& s,
const Reporter& r)
{
std::string approvedPath = n.getApprovedFile(s.getFileExtensionWithDot());
std::string receivedPath = n.getReceivedFile(s.getFileExtensionWithDot());
std::string const approvedPath = n.getApprovedFile(s.getFileExtensionWithDot());
std::string const receivedPath = n.getReceivedFile(s.getFileExtensionWithDot());

ApprovalFileLog::log(approvedPath);
SystemUtils::ensureParentDirectoryExists(approvedPath);
SystemUtils::ensureParentDirectoryExists(receivedPath);
s.write(receivedPath);
Expand All @@ -76,6 +79,7 @@ namespace ApprovalTests
}
catch (const ApprovalException&)
{
// FailedFileLog::log(receivedPath, approvedPath);
reportAfterTryingFrontLoadedReporter(receivedPath, approvedPath, r);
throw;
}
Expand Down
31 changes: 31 additions & 0 deletions ApprovalTests/logs/ApprovalFileLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "ApprovalFileLog.hpp"
#include <fstream>

void ApprovalFileLog::initialize()
{
static bool isInitialized{false};
if (isInitialized)
{
return;
}
isInitialized = true;
ApprovalTests::FileUtils::writeToFile(getLogFilePath(), "");
}

std::string ApprovalFileLog::getLogFilePath()
{
//auto const rootDirectory = ApprovalTests::TestName::getRootDirectory();
auto const rootDirectory = "";
// Note: Need to figure out where the actual root directory is.
std::string const filePath =
rootDirectory + std::string(APPROVAL_TEMP_DIRECTORY) + "/.approved_files.log";
ApprovalTests::SystemUtils::ensureDirectoryExists(APPROVAL_TEMP_DIRECTORY);
return filePath;
}

void ApprovalFileLog::log(const std::string& filePath)
{
initialize();
std::ofstream ofs(getLogFilePath(), std::ios::app);
ofs << filePath << std::endl;
}
17 changes: 17 additions & 0 deletions ApprovalTests/logs/ApprovalFileLog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include <utilities/FileUtils.h>
#include <utilities/SystemUtils.h>

auto constexpr APPROVAL_TEMP_DIRECTORY = "/.approval_tests_temp";

class ApprovalFileLog {
public:
// Static initializer to ensure an _empty_ log file is created
static void initialize();

// Function to get the log file path
static std::string getLogFilePath();

// Function to log the approved file path
static void log(const std::string& filePath);
};
1 change: 0 additions & 1 deletion ApprovalTests/namers/ApprovalTestNamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace ApprovalTests

class ApprovalTestNamer : public ApprovalNamer
{
private:
public:
ApprovalTestNamer() = default;

Expand Down

0 comments on commit 795f5eb

Please # to comment.