Skip to content

Commit

Permalink
Add parse_sources to prevent tests parsing envvars
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Nov 20, 2017
1 parent 2cc1005 commit 3ee80f7
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 55 deletions.
3 changes: 2 additions & 1 deletion source/cath_superpose/cath_superposer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ void cath::test::cath_superposer_test_suite_fixture::check_cath_superposer_use_c

const auto my_cath_superpose_options = make_and_parse_options<cath_superpose_options>(
faked_argc_and_argv.get_argc(),
faked_argc_and_argv.get_argv()
faked_argc_and_argv.get_argv(),
parse_sources::CMND_LINE_ONLY
);

// Prepare an ostringstream to capture the output
Expand Down
11 changes: 6 additions & 5 deletions source/cluster/cath_cluster_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ using std::istream;
using std::ostream;

/// \brief Perform map-clusters according to the specified arguments strings with the specified i/o streams
void cath::clust::perform_map_clusters(const str_vec &args, ///< The arguments strings specifying the map-clusters action to perform
istream &arg_istream, ///< The input stream
ostream &arg_stdout, ///< The output stream
ostream &arg_stderr ///< The error stream
void cath::clust::perform_map_clusters(const str_vec &args, ///< The arguments strings specifying the map-clusters action to perform
istream &arg_istream, ///< The input stream
ostream &arg_stdout, ///< The output stream
ostream &arg_stderr, ///< The error stream
const parse_sources &arg_parse_sources ///< The sources from which options should be parsed
) {
perform_map_clusters(
make_and_parse_options<clustmap_options>( args ),
make_and_parse_options<clustmap_options>( args, arg_parse_sources ),
arg_istream,
arg_stdout,
arg_stderr
Expand Down
4 changes: 3 additions & 1 deletion source/cluster/cath_cluster_mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define _CATH_TOOLS_SOURCE_CLUSTER_CATH_CLUSTER_MAPPER_H

#include "common/type_aliases.hpp"
#include "options/executable/parse_sources.hpp"

#include <iostream>

Expand All @@ -36,7 +37,8 @@ namespace cath {
void perform_map_clusters(const str_vec &,
std::istream & = std::cin,
std::ostream & = std::cout,
std::ostream & = std::cerr);
std::ostream & = std::cerr,
const opts::parse_sources & = opts::parse_sources::CMND_ENV_AND_FILE);

void perform_map_clusters(const clustmap_options &,
std::istream & = std::cin,
Expand Down
2 changes: 1 addition & 1 deletion source/cluster/cath_cluster_mapper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace cath {
progname_rng,
arg_arguments
) ),
input_ss, output_ss, err_ss
input_ss, output_ss, err_ss, parse_sources::CMND_LINE_ONLY
);
}

Expand Down
82 changes: 48 additions & 34 deletions source/options/executable/executable_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "common/file/find_file.hpp"
#include "common/file/open_fstream.hpp"
#include "common/optional/make_optional_if.hpp"
#include "common/test_or_exe_run_mode.hpp"
#include "options/executable/env_var_option_name_handler.hpp"
#include "options/options_block/misc_help_version_options_block.hpp"

Expand Down Expand Up @@ -182,8 +183,9 @@ void executable_options::add_visble_options_to_description(options_description &
/// so they will remain untouched.
///
/// \post The options will be parsed and ready for querying
void executable_options::parse_options(const int &argc, ///< The argc from command line parameters
const char * const argv[] ///< The argv from command line parameters
void executable_options::parse_options(const int &argc, ///< The argc from command line parameters
const char * const argv[], ///< The argv from command line parameters
const parse_sources &arg_parse_sources ///< The sources from which options should be parsed
) {
// Check the options haven't already been processed
if ( processed_options ) {
Expand Down Expand Up @@ -298,44 +300,56 @@ void executable_options::parse_options(const int &argc, ///< The argc
}
);

// Parse any environment variables prefixed with "CATH_TOOLS_"
// and just silently ignore any unknown options
//
// The remaining parses are performed in decreasing order of precedence
// (ie options specified via the command line should take precedence over those
// specified via environment variables so it comes first)
prog_opts_try(
error_or_help_string,
[&] {
store(
parse_environment(
full_po_desc,
env_var_option_name_handler(
CATH_TOOLS_ENVIRONMENT_VARIABLE_PREFIX,
true,
full_po_desc
)
),
vm
);
},
"[whilst parsing from global environment variables with prefix " + CATH_TOOLS_ENVIRONMENT_VARIABLE_PREFIX + "]"
);
if ( arg_parse_sources == parse_sources::CMND_ENV_AND_FILE ) {

// If running in test mode, complain that shouldn't be parsing env-vars or config files
//
// Note an alternative way to implement this is to get the global fixture to
// detect and remove any CATH_TOOLS environment variables (though that doesn't
// handle config files).
if ( run_mode_flag::value == run_mode::TEST ) {
BOOST_THROW_EXCEPTION(runtime_error_exception("Requested to parse command line options from environment variables and configuration file but this is a bad idea because currently running in test mode"));
}

// Parse any configuration file called cath_tools.conf
const path located_cath_tools_conf_file = find_file( CATH_TOOLS_CONF_FILE_SEARCH_PATH(), CATH_TOOLS_CONF_FILE.string() );
if ( ! located_cath_tools_conf_file.empty() ) {
// cerr << "Parsing configuration from file " << CATH_TOOLS_CONF_FILE << endl;
ifstream config_file_stream;
// Parse any environment variables prefixed with "CATH_TOOLS_"
// and just silently ignore any unknown options
//
// The remaining parses are performed in decreasing order of precedence
// (ie options specified via the command line should take precedence over those
// specified via environment variables so it comes first)
prog_opts_try(
error_or_help_string,
[&] {
open_ifstream(config_file_stream, CATH_TOOLS_CONF_FILE);
store( parse_config_file( config_file_stream, full_po_desc, true ), vm );
config_file_stream.close();
store(
parse_environment(
full_po_desc,
env_var_option_name_handler(
CATH_TOOLS_ENVIRONMENT_VARIABLE_PREFIX,
true,
full_po_desc
)
),
vm
);
},
"[whilst parsing from the global configuration file " + located_cath_tools_conf_file.string() + "]"
"[whilst parsing from global environment variables with prefix " + CATH_TOOLS_ENVIRONMENT_VARIABLE_PREFIX + "]"
);

// Parse any configuration file called cath_tools.conf
const path located_cath_tools_conf_file = find_file( CATH_TOOLS_CONF_FILE_SEARCH_PATH(), CATH_TOOLS_CONF_FILE.string() );
if ( ! located_cath_tools_conf_file.empty() ) {
// cerr << "Parsing configuration from file " << CATH_TOOLS_CONF_FILE << endl;
ifstream config_file_stream;
prog_opts_try(
error_or_help_string,
[&] {
open_ifstream(config_file_stream, CATH_TOOLS_CONF_FILE);
store( parse_config_file( config_file_stream, full_po_desc, true ), vm );
config_file_stream.close();
},
"[whilst parsing from the global configuration file " + located_cath_tools_conf_file.string() + "]"
);
}
}

// All parsing is complete so call notify, which will trigger any
Expand Down
19 changes: 12 additions & 7 deletions source/options/executable/executable_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "common/boost_addenda/program_options/set_opt_str_from_prog_opts_try.hpp"
#include "common/path_type_aliases.hpp"
#include "common/type_aliases.hpp"
#include "options/executable/parse_sources.hpp"
#include "options/options_block/string_options_block.hpp"

#include <string>
Expand Down Expand Up @@ -67,7 +68,7 @@ namespace cath {

static const std::string CATH_TOOLS_ENVIRONMENT_VARIABLE_PREFIX;
static const boost::filesystem::path CATH_TOOLS_CONF_FILE;

static path_vec CATH_TOOLS_CONF_FILE_SEARCH_PATH();

/// \brief A list of string_options_block objects that just serve to put strings
Expand Down Expand Up @@ -153,7 +154,8 @@ namespace cath {
executable_options & operator=(executable_options &&) = default;

void parse_options(const int &,
const char * const []);
const char * const [],
const parse_sources & = parse_sources::CMND_ENV_AND_FILE);

str_opt get_error_or_help_string() const;
};
Expand All @@ -174,26 +176,29 @@ namespace cath {

/// \brief Return a new instance of the specified type of executable_options with the specified options parsed into it
template <typename T>
T make_and_parse_options(const int &argc, ///< The main()-style argc parameter
const char * const argv[] ///< The main()-style argv parameter
T make_and_parse_options(const int &argc, ///< The main()-style argc parameter
const char * const argv[], ///< The main()-style argv parameter
const parse_sources &arg_parse_sources = parse_sources::CMND_ENV_AND_FILE ///< The sources from which options should be parsed
) {
static_assert(
std::is_base_of<executable_options, T>::value,
"make_and_parse_options() can only be used to make types that inherit from executable_options."
);
T new_options;
new_options.parse_options( argc, argv );
new_options.parse_options( argc, argv, arg_parse_sources );
return new_options;
}

/// \brief Return a new instance of the specified type of executable_options with the specified options parsed into it
template <typename T>
T make_and_parse_options(const str_vec &args ///< The arguments
T make_and_parse_options(const str_vec &args, ///< The arguments
const parse_sources &arg_parse_sources = parse_sources::CMND_ENV_AND_FILE ///< The sources from which options should be parsed
) {
argc_argv_faker my_argc_argv_faker( args );
return make_and_parse_options<T>(
my_argc_argv_faker.get_argc(),
my_argc_argv_faker.get_argv()
my_argc_argv_faker.get_argv(),
arg_parse_sources
);
}
} // namespace opts
Expand Down
36 changes: 36 additions & 0 deletions source/options/executable/parse_sources.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// \file
/// \brief The parse_sources class header

/// \copyright
/// CATH Tools - Protein structure comparison tools such as SSAP and SNAP
/// Copyright (C) 2011, Orengo Group, University College London
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef _CATH_TOOLS_SOURCE_OPTIONS_EXECUTABLE_PARSE_SOURCES_H
#define _CATH_TOOLS_SOURCE_OPTIONS_EXECUTABLE_PARSE_SOURCES_H

namespace cath {
namespace opts {

/// \brief Represent the sources from which options should be parsed
enum class parse_sources : bool {
CMND_LINE_ONLY, ///< Only parse options from the command-line
CMND_ENV_AND_FILE ///< Parse options from the command-line, environment-variables and configuration file
};

} // namespace opts
} // namespace cath

#endif
9 changes: 5 additions & 4 deletions source/resolve_hits/cath_hit_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ using std::ostream;
using std::ofstream;

/// \brief Perform resolve-hits according to the specified arguments strings with the specified i/o streams
void cath::rslv::perform_resolve_hits(const str_vec &args, ///< The arguments strings specifying the resolve-hits action to perform
istream &arg_istream, ///< The input stream
ostream &arg_stdout ///< The output stream
void cath::rslv::perform_resolve_hits(const str_vec &args, ///< The arguments strings specifying the resolve-hits action to perform
istream &arg_istream, ///< The input stream
ostream &arg_stdout, ///< The output stream
const parse_sources &arg_parse_sources ///< The sources from which options should be parsed
) {
perform_resolve_hits(
make_and_parse_options<crh_options>( args ),
make_and_parse_options<crh_options>( args, arg_parse_sources ),
arg_istream,
arg_stdout
);
Expand Down
4 changes: 3 additions & 1 deletion source/resolve_hits/cath_hit_resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define _CATH_TOOLS_SOURCE_RESOLVE_HITS_CATH_HIT_RESOLVER_H

#include "common/type_aliases.hpp"
#include "options/executable/parse_sources.hpp"

#include <iostream>

Expand All @@ -33,7 +34,8 @@ namespace cath {

void perform_resolve_hits(const str_vec &,
std::istream & = std::cin,
std::ostream & = std::cout);
std::ostream & = std::cout,
const opts::parse_sources & = opts::parse_sources::CMND_ENV_AND_FILE);

void perform_resolve_hits(const crh_options &,
std::istream & = std::cin,
Expand Down
3 changes: 2 additions & 1 deletion source/resolve_hits/cath_hit_resolver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace cath { namespace test { } }

using namespace cath;
using namespace cath::common;
using namespace cath::opts;
using namespace cath::rslv;
using namespace cath::test;
using namespace std::literals::string_literals;
Expand Down Expand Up @@ -81,7 +82,7 @@ namespace cath {
progname_vec,
arg_arguments
) ),
input_ss, output_ss
input_ss, output_ss, parse_sources::CMND_LINE_ONLY
);
}

Expand Down

0 comments on commit 3ee80f7

Please sign in to comment.