Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/dca/phys/parameters/output_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef DCA_PHYS_PARAMETERS_OUTPUT_PARAMETERS_HPP
#define DCA_PHYS_PARAMETERS_OUTPUT_PARAMETERS_HPP

#include <filesystem>
#include <stdexcept>
#include <string>
#include "dca/io/io_types.hpp"
Expand Down Expand Up @@ -64,6 +65,12 @@ class OutputParameters {
template <typename ReaderOrWriter>
void readWrite(ReaderOrWriter& reader_or_writer);

// Throws std::invalid_argument if the parsed parameters describe state that cannot be
// used in a run (e.g. the configured output directory does not exist on disk). Intended
// to be called by Parameters::readInput on the rank that parsed the input file.
// Issue #300: surface bad input at parse time instead of crashing later in HDF5.
void validate() const;

const std::string& get_directory() const {
return directory_;
}
Expand Down Expand Up @@ -272,6 +279,11 @@ void OutputParameters::readWrite(ReaderOrWriter& reader_or_writer) {
}
}

inline void OutputParameters::validate() const {
if (!std::filesystem::exists(directory_))
throw std::invalid_argument("Output directory does not exist: " + directory_);
}

} // namespace params
} // namespace phys
} // namespace dca
Expand Down
1 change: 1 addition & 0 deletions include/dca/phys/parameters/parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ void Parameters<Concurrency, Threading, Profiler, Model, RandomNumberGenerator,
read_obj.open_file(filename);
this->readWrite(read_obj);
read_obj.close_file();
OutputParameters::validate();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"output": {
"directory": "/nonexistent/path/for/dca_input_validation_test"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// TODO: Add tests for get_buffer_size, pack, unpack and writing.


#include <stdexcept>

#include "dca/config/haves_defines.hpp"
#include "dca/phys/parameters/output_parameters.hpp"
#include "dca/testing/gtest_h_w_warning_blocking.h"
Expand Down Expand Up @@ -88,3 +90,18 @@ TEST(OutputParametersTest, ReadAll) {
EXPECT_TRUE(pars.dump_Gamma_lattice());
EXPECT_TRUE(pars.dump_chi_0_lattice());
}

// Issue #300, bug #1: a non-existent output.directory should produce a clear error at
// parameter-read time. readWrite() parses the directory string without touching the
// disk; validate() is where the existence check and the throw live.
TEST(OutputParametersTest, MissingDirectoryThrows) {
dca::io::JSONReader reader;
dca::phys::params::OutputParameters pars;

reader.open_file(DCA_SOURCE_DIR
"/test/unit/phys/parameters/output_parameters/missing_directory.json");
pars.readWrite(reader);
reader.close_file();

EXPECT_THROW(pars.validate(), std::invalid_argument);
}
Loading