Updated the change management documentation
[gromacs.git] / src / testutils / cmdlinetest.cpp
blob1c6bdc0f9f8b1b20f53199be6e238e7cc35305f8
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \internal \file
37 * \brief
38 * Implements classes from cmdlinetest.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_testutils
43 #include "gmxpre.h"
45 #include "cmdlinetest.h"
47 #include <cstdlib>
48 #include <cstring>
50 #include <memory>
51 #include <new>
52 #include <sstream>
53 #include <vector>
55 #include "gromacs/commandline/cmdlinehelpcontext.h"
56 #include "gromacs/commandline/cmdlineoptionsmodule.h"
57 #include "gromacs/commandline/cmdlineprogramcontext.h"
58 #include "gromacs/utility/arrayref.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/utility/gmxassert.h"
61 #include "gromacs/utility/strconvert.h"
62 #include "gromacs/utility/stringstream.h"
63 #include "gromacs/utility/stringutil.h"
64 #include "gromacs/utility/textreader.h"
65 #include "gromacs/utility/textwriter.h"
67 #include "testutils/filematchers.h"
68 #include "testutils/refdata.h"
69 #include "testutils/testfilemanager.h"
71 namespace gmx
73 namespace test
76 /********************************************************************
77 * CommandLine::Impl
80 class CommandLine::Impl
82 public:
83 Impl(const ArrayRef<const char* const>& cmdline);
84 Impl(const ArrayRef<const std::string>& cmdline);
85 ~Impl();
87 std::vector<char*> args_;
88 std::vector<char*> argv_;
89 int argc_;
92 CommandLine::Impl::Impl(const ArrayRef<const char* const>& cmdline)
94 args_.reserve(cmdline.size());
95 argv_.reserve(cmdline.size() + 1);
96 argc_ = ssize(cmdline);
97 for (const auto& arg : cmdline)
99 char* argCopy = strdup(arg);
100 if (argCopy == nullptr)
102 throw std::bad_alloc();
104 args_.push_back(argCopy);
105 argv_.push_back(argCopy);
107 argv_.push_back(nullptr);
110 namespace
113 //! Helper function so we can delegate from the std::string constructor to the const char * one.
114 std::vector<const char*> convertFromStringArrayRef(const ArrayRef<const std::string>& cmdline)
116 std::vector<const char*> v(cmdline.size());
117 std::transform(cmdline.begin(), cmdline.end(), v.begin(),
118 [](const std::string& s) { return s.c_str(); });
119 return v;
122 } // namespace
124 // This makes a new temporary vector of views of the const char * in
125 // the view passed in. Those are then deep copied in the constructor
126 // delegated to.
127 CommandLine::Impl::Impl(const ArrayRef<const std::string>& cmdline) :
128 Impl(convertFromStringArrayRef(cmdline))
132 CommandLine::Impl::~Impl()
134 for (size_t i = 0; i < args_.size(); ++i)
136 std::free(args_[i]);
140 /********************************************************************
141 * CommandLine
144 CommandLine::CommandLine() : impl_(new Impl(ArrayRef<const char*>{})) {}
146 CommandLine::CommandLine(const ArrayRef<const char* const>& cmdline) : impl_(new Impl(cmdline)) {}
148 CommandLine::CommandLine(const ArrayRef<const std::string>& cmdline) : impl_(new Impl(cmdline)) {}
150 CommandLine::CommandLine(const CommandLine& other) :
151 impl_(new Impl(arrayRefFromArray(other.argv(), other.argc())))
155 CommandLine::~CommandLine() {}
157 void CommandLine::initFromArray(const ArrayRef<const char* const>& cmdline)
159 impl_.reset(new Impl(cmdline));
162 void CommandLine::append(const char* arg)
164 GMX_RELEASE_ASSERT(impl_->argc_ == ssize(impl_->args_),
165 "Command-line has been modified externally");
166 size_t newSize = impl_->args_.size() + 1;
167 impl_->args_.reserve(newSize);
168 impl_->argv_.reserve(newSize + 1);
169 char* newArg = strdup(arg);
170 if (newArg == nullptr)
172 throw std::bad_alloc();
174 impl_->args_.push_back(newArg);
175 impl_->argv_.pop_back(); // Remove the trailing NULL.
176 impl_->argv_.push_back(newArg);
177 impl_->argv_.push_back(nullptr);
178 impl_->argc_ = static_cast<int>(newSize);
181 void CommandLine::addOption(const char* name)
183 append(name);
186 void CommandLine::addOption(const char* name, const char* value)
188 append(name);
189 append(value);
192 void CommandLine::addOption(const char* name, const std::string& value)
194 addOption(name, value.c_str());
197 void CommandLine::addOption(const char* name, int value)
199 append(name);
200 append(gmx::toString(value));
203 void CommandLine::addOption(const char* name, double value)
205 append(name);
206 append(gmx::toString(value));
209 void CommandLine::merge(const CommandLine& args)
211 if (args.argc() > 0)
213 // Skip first argument if it is the module name.
214 const int firstArg = (args.arg(0)[0] == '-' ? 0 : 1);
215 for (int i = firstArg; i < args.argc(); ++i)
217 append(args.arg(i));
222 int& CommandLine::argc()
224 return impl_->argc_;
226 char** CommandLine::argv()
228 return &impl_->argv_[0];
230 int CommandLine::argc() const
232 return impl_->argc_;
234 const char* const* CommandLine::argv() const
236 return &impl_->argv_[0];
238 const char* CommandLine::arg(int i) const
240 return impl_->argv_[i];
243 std::string CommandLine::toString() const
245 return CommandLineProgramContext(argc(), argv()).commandLine();
248 bool CommandLine::contains(const char* name) const
250 for (int i = 0; i < impl_->argc_; ++i)
252 if (std::strcmp(arg(i), name) == 0)
254 return true;
257 return false;
260 /********************************************************************
261 * CommandLineTestHelper::Impl
264 class CommandLineTestHelper::Impl
266 public:
267 struct OutputFileInfo
269 OutputFileInfo(const char* option, const std::string& path, FileMatcherPointer matcher) :
270 option(option),
271 path(path),
272 matcher(move(matcher))
276 std::string option;
277 std::string path;
278 FileMatcherPointer matcher;
281 typedef std::vector<OutputFileInfo> OutputFileList;
283 explicit Impl(TestFileManager* fileManager) : fileManager_(*fileManager) {}
285 TestFileManager& fileManager_;
286 OutputFileList outputFiles_;
289 /********************************************************************
290 * CommandLineTestHelper
293 // static
294 int CommandLineTestHelper::runModuleDirect(ICommandLineModule* module, CommandLine* commandLine)
296 CommandLineModuleSettings settings;
297 module->init(&settings);
298 return module->run(commandLine->argc(), commandLine->argv());
301 // static
302 int CommandLineTestHelper::runModuleDirect(std::unique_ptr<ICommandLineOptionsModule> module,
303 CommandLine* commandLine)
305 // The name and description are not used in the tests, so they can be NULL.
306 const std::unique_ptr<ICommandLineModule> wrapperModule(
307 ICommandLineOptionsModule::createModule(nullptr, nullptr, std::move(module)));
308 return runModuleDirect(wrapperModule.get(), commandLine);
311 // static
312 int CommandLineTestHelper::runModuleFactory(
313 const std::function<std::unique_ptr<ICommandLineOptionsModule>()>& factory,
314 CommandLine* commandLine)
316 return runModuleDirect(factory(), commandLine);
319 CommandLineTestHelper::CommandLineTestHelper(TestFileManager* fileManager) :
320 impl_(new Impl(fileManager))
324 CommandLineTestHelper::~CommandLineTestHelper() {}
326 void CommandLineTestHelper::setInputFileContents(CommandLine* args,
327 const char* option,
328 const char* extension,
329 const std::string& contents)
331 GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot");
332 std::string fullFilename =
333 impl_->fileManager_.getTemporaryFilePath(formatString("%d.%s", args->argc(), extension));
334 TextWriter::writeFileFromString(fullFilename, contents);
335 args->addOption(option, fullFilename);
338 void CommandLineTestHelper::setInputFileContents(CommandLine* args,
339 const char* option,
340 const char* extension,
341 const ArrayRef<const char* const>& contents)
343 GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot");
344 std::string fullFilename =
345 impl_->fileManager_.getTemporaryFilePath(formatString("%d.%s", args->argc(), extension));
346 TextWriter file(fullFilename);
347 ArrayRef<const char* const>::const_iterator i;
348 for (i = contents.begin(); i != contents.end(); ++i)
350 file.writeLine(*i);
352 file.close();
353 args->addOption(option, fullFilename);
356 void CommandLineTestHelper::setOutputFile(CommandLine* args,
357 const char* option,
358 const char* filename,
359 const ITextBlockMatcherSettings& matcher)
361 setOutputFile(args, option, filename, TextFileMatch(matcher));
364 void CommandLineTestHelper::setOutputFile(CommandLine* args,
365 const char* option,
366 const char* filename,
367 const IFileMatcherSettings& matcher)
369 std::string suffix(filename);
370 if (startsWith(filename, "."))
372 suffix = formatString("%d.%s", args->argc(), filename);
374 std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(suffix);
375 args->addOption(option, fullFilename);
376 impl_->outputFiles_.emplace_back(option, fullFilename, matcher.createFileMatcher());
379 void CommandLineTestHelper::checkOutputFiles(TestReferenceChecker checker) const
381 if (!impl_->outputFiles_.empty())
383 TestReferenceChecker outputChecker(checker.checkCompound("OutputFiles", "Files"));
384 for (const auto& outfile : impl_->outputFiles_)
386 TestReferenceChecker fileChecker(outputChecker.checkCompound("File", outfile.option.c_str()));
387 outfile.matcher->checkFile(outfile.path, &fileChecker);
392 /********************************************************************
393 * CommandLineTestBase::Impl
396 class CommandLineTestBase::Impl
398 public:
399 Impl() : helper_(&tempFiles_) { cmdline_.append("module"); }
401 TestReferenceData data_;
402 TestFileManager tempFiles_;
403 CommandLineTestHelper helper_;
404 CommandLine cmdline_;
407 /********************************************************************
408 * CommandLineTestBase
411 CommandLineTestBase::CommandLineTestBase() : impl_(new Impl) {}
413 CommandLineTestBase::~CommandLineTestBase() {}
415 void CommandLineTestBase::setInputFile(const char* option, const char* filename)
417 impl_->cmdline_.addOption(option, TestFileManager::getInputFilePath(filename));
420 void CommandLineTestBase::setInputFile(const char* option, const std::string& filename)
422 setInputFile(option, filename.c_str());
425 void CommandLineTestBase::setModifiableInputFile(const char* option, const std::string& filename)
427 setModifiableInputFile(option, filename.c_str());
430 void CommandLineTestBase::setModifiableInputFile(const char* option, const char* filename)
432 std::string originalFileName = gmx::test::TestFileManager::getInputFilePath(filename);
433 std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
434 gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
435 impl_->cmdline_.addOption(option, modifiableFileName);
438 void CommandLineTestBase::setInputFileContents(const char* option,
439 const char* extension,
440 const std::string& contents)
442 impl_->helper_.setInputFileContents(&impl_->cmdline_, option, extension, contents);
445 void CommandLineTestBase::setInputFileContents(const char* option,
446 const char* extension,
447 const ArrayRef<const char* const>& contents)
449 impl_->helper_.setInputFileContents(&impl_->cmdline_, option, extension, contents);
452 void CommandLineTestBase::setOutputFile(const char* option,
453 const char* filename,
454 const ITextBlockMatcherSettings& matcher)
456 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
459 void CommandLineTestBase::setOutputFile(const char* option,
460 const char* filename,
461 const IFileMatcherSettings& matcher)
463 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
466 void CommandLineTestBase::setInputAndOutputFile(const char* option,
467 const char* filename,
468 const ITextBlockMatcherSettings& matcher)
470 std::string originalFileName = gmx::test::TestFileManager::getInputFilePath(filename);
471 std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
472 gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
473 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
476 void CommandLineTestBase::setInputAndOutputFile(const char* option,
477 const char* filename,
478 const IFileMatcherSettings& matcher)
480 std::string originalFileName = gmx::test::TestFileManager::getInputFilePath(filename);
481 std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
482 gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
483 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
486 CommandLine& CommandLineTestBase::commandLine()
488 return impl_->cmdline_;
491 TestFileManager& CommandLineTestBase::fileManager()
493 return impl_->tempFiles_;
496 TestReferenceChecker CommandLineTestBase::rootChecker()
498 return impl_->data_.rootChecker();
501 void CommandLineTestBase::setDefaultTolerance(const FloatingPointTolerance& tolerance)
503 impl_->data_.rootChecker().setDefaultTolerance(tolerance);
506 void CommandLineTestBase::testWriteHelp(ICommandLineModule* module)
508 StringOutputStream stream;
509 TextWriter writer(&stream);
510 CommandLineHelpContext context(&writer, eHelpOutputFormat_Console, nullptr, "test");
511 context.setModuleDisplayName(formatString("%s %s", "test", module->name()));
512 module->writeHelp(context);
513 TestReferenceChecker checker(rootChecker());
514 checker.checkTextBlock(stream.toString(), "HelpOutput");
517 void CommandLineTestBase::checkOutputFiles()
519 impl_->helper_.checkOutputFiles(rootChecker());
522 } // namespace test
523 } // namespace gmx