2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements classes from cmdlinetest.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_testutils
44 #include "cmdlinetest.h"
54 #include "gromacs/commandline/cmdlinehelpcontext.h"
55 #include "gromacs/commandline/cmdlineoptionsmodule.h"
56 #include "gromacs/commandline/cmdlineprogramcontext.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/futil.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/strconvert.h"
61 #include "gromacs/utility/stringstream.h"
62 #include "gromacs/utility/stringutil.h"
63 #include "gromacs/utility/textreader.h"
64 #include "gromacs/utility/textwriter.h"
66 #include "testutils/filematchers.h"
67 #include "testutils/refdata.h"
68 #include "testutils/testfilemanager.h"
75 /********************************************************************
79 class CommandLine::Impl
82 Impl(const ArrayRef<const char* const>& cmdline);
83 Impl(const ArrayRef<const std::string>& cmdline);
86 std::vector<char*> args_;
87 std::vector<char*> argv_;
91 CommandLine::Impl::Impl(const ArrayRef<const char* const>& cmdline)
93 args_.reserve(cmdline.size());
94 argv_.reserve(cmdline.size() + 1);
95 argc_ = ssize(cmdline);
96 for (const auto& arg : cmdline)
98 char* argCopy = strdup(arg);
99 if (argCopy == nullptr)
101 throw std::bad_alloc();
103 args_.push_back(argCopy);
104 argv_.push_back(argCopy);
106 argv_.push_back(nullptr);
112 //! Helper function so we can delegate from the std::string constructor to the const char * one.
113 std::vector<const char*> convertFromStringArrayRef(const ArrayRef<const std::string>& cmdline)
115 std::vector<const char*> v(cmdline.size());
116 std::transform(cmdline.begin(), cmdline.end(), v.begin(),
117 [](const std::string& s) { return s.c_str(); });
123 // This makes a new temporary vector of views of the const char * in
124 // the view passed in. Those are then deep copied in the constructor
126 CommandLine::Impl::Impl(const ArrayRef<const std::string>& cmdline) :
127 Impl(convertFromStringArrayRef(cmdline))
131 CommandLine::Impl::~Impl()
133 for (size_t i = 0; i < args_.size(); ++i)
139 /********************************************************************
143 CommandLine::CommandLine() : impl_(new Impl(ArrayRef<const char*>{})) {}
145 CommandLine::CommandLine(const ArrayRef<const char* const>& cmdline) : impl_(new Impl(cmdline)) {}
147 CommandLine::CommandLine(const ArrayRef<const std::string>& cmdline) : impl_(new Impl(cmdline)) {}
149 CommandLine::CommandLine(const CommandLine& other) :
150 impl_(new Impl(arrayRefFromArray(other.argv(), other.argc())))
154 CommandLine::~CommandLine() {}
156 void CommandLine::initFromArray(const ArrayRef<const char* const>& cmdline)
158 impl_.reset(new Impl(cmdline));
161 void CommandLine::append(const char* arg)
163 GMX_RELEASE_ASSERT(impl_->argc_ == ssize(impl_->args_),
164 "Command-line has been modified externally");
165 size_t newSize = impl_->args_.size() + 1;
166 impl_->args_.reserve(newSize);
167 impl_->argv_.reserve(newSize + 1);
168 char* newArg = strdup(arg);
169 if (newArg == nullptr)
171 throw std::bad_alloc();
173 impl_->args_.push_back(newArg);
174 impl_->argv_.pop_back(); // Remove the trailing NULL.
175 impl_->argv_.push_back(newArg);
176 impl_->argv_.push_back(nullptr);
177 impl_->argc_ = static_cast<int>(newSize);
180 void CommandLine::addOption(const char* name)
185 void CommandLine::addOption(const char* name, const char* value)
191 void CommandLine::addOption(const char* name, const std::string& value)
193 addOption(name, value.c_str());
196 void CommandLine::addOption(const char* name, int value)
199 append(gmx::toString(value));
202 void CommandLine::addOption(const char* name, double value)
205 append(gmx::toString(value));
208 void CommandLine::merge(const CommandLine& args)
212 // Skip first argument if it is the module name.
213 const int firstArg = (args.arg(0)[0] == '-' ? 0 : 1);
214 for (int i = firstArg; i < args.argc(); ++i)
221 int& CommandLine::argc()
225 char** CommandLine::argv()
227 return &impl_->argv_[0];
229 int CommandLine::argc() const
233 const char* const* CommandLine::argv() const
235 return &impl_->argv_[0];
237 const char* CommandLine::arg(int i) const
239 return impl_->argv_[i];
242 std::string CommandLine::toString() const
244 return CommandLineProgramContext(argc(), argv()).commandLine();
247 bool CommandLine::contains(const char* name) const
249 for (int i = 0; i < impl_->argc_; ++i)
251 if (std::strcmp(arg(i), name) == 0)
259 /********************************************************************
260 * CommandLineTestHelper::Impl
263 class CommandLineTestHelper::Impl
266 struct OutputFileInfo
268 OutputFileInfo(const char* option, const std::string& path, FileMatcherPointer matcher) :
271 matcher(move(matcher))
277 FileMatcherPointer matcher;
280 typedef std::vector<OutputFileInfo> OutputFileList;
282 explicit Impl(TestFileManager* fileManager) : fileManager_(*fileManager) {}
284 TestFileManager& fileManager_;
285 OutputFileList outputFiles_;
288 /********************************************************************
289 * CommandLineTestHelper
293 int CommandLineTestHelper::runModuleDirect(ICommandLineModule* module, CommandLine* commandLine)
295 CommandLineModuleSettings settings;
296 module->init(&settings);
297 return module->run(commandLine->argc(), commandLine->argv());
301 int CommandLineTestHelper::runModuleDirect(std::unique_ptr<ICommandLineOptionsModule> module,
302 CommandLine* commandLine)
304 // The name and description are not used in the tests, so they can be NULL.
305 const std::unique_ptr<ICommandLineModule> wrapperModule(
306 ICommandLineOptionsModule::createModule(nullptr, nullptr, std::move(module)));
307 return runModuleDirect(wrapperModule.get(), commandLine);
311 int CommandLineTestHelper::runModuleFactory(
312 const std::function<std::unique_ptr<ICommandLineOptionsModule>()>& factory,
313 CommandLine* commandLine)
315 return runModuleDirect(factory(), commandLine);
318 CommandLineTestHelper::CommandLineTestHelper(TestFileManager* fileManager) :
319 impl_(new Impl(fileManager))
323 CommandLineTestHelper::~CommandLineTestHelper() {}
325 void CommandLineTestHelper::setInputFileContents(CommandLine* args,
327 const char* extension,
328 const std::string& contents)
330 GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot");
331 std::string fullFilename =
332 impl_->fileManager_.getTemporaryFilePath(formatString("%d.%s", args->argc(), extension));
333 TextWriter::writeFileFromString(fullFilename, contents);
334 args->addOption(option, fullFilename);
337 void CommandLineTestHelper::setInputFileContents(CommandLine* args,
339 const char* extension,
340 const ArrayRef<const char* const>& contents)
342 GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot");
343 std::string fullFilename =
344 impl_->fileManager_.getTemporaryFilePath(formatString("%d.%s", args->argc(), extension));
345 TextWriter file(fullFilename);
346 ArrayRef<const char* const>::const_iterator i;
347 for (i = contents.begin(); i != contents.end(); ++i)
352 args->addOption(option, fullFilename);
355 void CommandLineTestHelper::setOutputFile(CommandLine* args,
357 const char* filename,
358 const ITextBlockMatcherSettings& matcher)
360 setOutputFile(args, option, filename, TextFileMatch(matcher));
363 void CommandLineTestHelper::setOutputFile(CommandLine* args,
365 const char* filename,
366 const IFileMatcherSettings& matcher)
368 std::string suffix(filename);
369 if (startsWith(filename, "."))
371 suffix = formatString("%d.%s", args->argc(), filename);
373 std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(suffix);
374 args->addOption(option, fullFilename);
375 impl_->outputFiles_.emplace_back(option, fullFilename, matcher.createFileMatcher());
378 void CommandLineTestHelper::checkOutputFiles(TestReferenceChecker checker) const
380 if (!impl_->outputFiles_.empty())
382 TestReferenceChecker outputChecker(checker.checkCompound("OutputFiles", "Files"));
383 for (const auto& outfile : impl_->outputFiles_)
385 TestReferenceChecker fileChecker(outputChecker.checkCompound("File", outfile.option.c_str()));
386 outfile.matcher->checkFile(outfile.path, &fileChecker);
391 /********************************************************************
392 * CommandLineTestBase::Impl
395 class CommandLineTestBase::Impl
398 Impl() : helper_(&tempFiles_) { cmdline_.append("module"); }
400 TestReferenceData data_;
401 TestFileManager tempFiles_;
402 CommandLineTestHelper helper_;
403 CommandLine cmdline_;
406 /********************************************************************
407 * CommandLineTestBase
410 CommandLineTestBase::CommandLineTestBase() : impl_(new Impl) {}
412 CommandLineTestBase::~CommandLineTestBase() {}
414 void CommandLineTestBase::setInputFile(const char* option, const char* filename)
416 impl_->cmdline_.addOption(option, TestFileManager::getInputFilePath(filename));
419 void CommandLineTestBase::setInputFile(const char* option, const std::string& filename)
421 setInputFile(option, filename.c_str());
424 void CommandLineTestBase::setModifiableInputFile(const char* option, const std::string& filename)
426 setModifiableInputFile(option, filename.c_str());
429 void CommandLineTestBase::setModifiableInputFile(const char* option, const char* filename)
431 std::string originalFileName = gmx::test::TestFileManager::getInputFilePath(filename);
432 std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
433 gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
434 impl_->cmdline_.addOption(option, modifiableFileName);
437 void CommandLineTestBase::setInputFileContents(const char* option,
438 const char* extension,
439 const std::string& contents)
441 impl_->helper_.setInputFileContents(&impl_->cmdline_, option, extension, contents);
444 void CommandLineTestBase::setInputFileContents(const char* option,
445 const char* extension,
446 const ArrayRef<const char* const>& contents)
448 impl_->helper_.setInputFileContents(&impl_->cmdline_, option, extension, contents);
451 void CommandLineTestBase::setOutputFile(const char* option,
452 const char* filename,
453 const ITextBlockMatcherSettings& matcher)
455 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
458 void CommandLineTestBase::setOutputFile(const char* option,
459 const char* filename,
460 const IFileMatcherSettings& matcher)
462 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
465 void CommandLineTestBase::setInputAndOutputFile(const char* option,
466 const char* filename,
467 const ITextBlockMatcherSettings& matcher)
469 std::string originalFileName = gmx::test::TestFileManager::getInputFilePath(filename);
470 std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
471 gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
472 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
475 void CommandLineTestBase::setInputAndOutputFile(const char* option,
476 const char* filename,
477 const IFileMatcherSettings& matcher)
479 std::string originalFileName = gmx::test::TestFileManager::getInputFilePath(filename);
480 std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
481 gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
482 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
485 CommandLine& CommandLineTestBase::commandLine()
487 return impl_->cmdline_;
490 TestFileManager& CommandLineTestBase::fileManager()
492 return impl_->tempFiles_;
495 TestReferenceChecker CommandLineTestBase::rootChecker()
497 return impl_->data_.rootChecker();
500 void CommandLineTestBase::setDefaultTolerance(const FloatingPointTolerance& tolerance)
502 impl_->data_.rootChecker().setDefaultTolerance(tolerance);
505 void CommandLineTestBase::testWriteHelp(ICommandLineModule* module)
507 StringOutputStream stream;
508 TextWriter writer(&stream);
509 CommandLineHelpContext context(&writer, eHelpOutputFormat_Console, nullptr, "test");
510 context.setModuleDisplayName(formatString("%s %s", "test", module->name()));
511 module->writeHelp(context);
512 TestReferenceChecker checker(rootChecker());
513 checker.checkTextBlock(stream.toString(), "HelpOutput");
516 void CommandLineTestBase::checkOutputFiles()
518 impl_->helper_.checkOutputFiles(rootChecker());