Blame engine/kyuafile.cpp

Packit 209faa
// Copyright 2010 The Kyua Authors.
Packit 209faa
// All rights reserved.
Packit 209faa
//
Packit 209faa
// Redistribution and use in source and binary forms, with or without
Packit 209faa
// modification, are permitted provided that the following conditions are
Packit 209faa
// met:
Packit 209faa
//
Packit 209faa
// * Redistributions of source code must retain the above copyright
Packit 209faa
//   notice, this list of conditions and the following disclaimer.
Packit 209faa
// * Redistributions in binary form must reproduce the above copyright
Packit 209faa
//   notice, this list of conditions and the following disclaimer in the
Packit 209faa
//   documentation and/or other materials provided with the distribution.
Packit 209faa
// * Neither the name of Google Inc. nor the names of its contributors
Packit 209faa
//   may be used to endorse or promote products derived from this software
Packit 209faa
//   without specific prior written permission.
Packit 209faa
//
Packit 209faa
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 209faa
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 209faa
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 209faa
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 209faa
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 209faa
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 209faa
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 209faa
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 209faa
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 209faa
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 209faa
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 209faa
Packit 209faa
#include "engine/kyuafile.hpp"
Packit 209faa
Packit 209faa
#include <algorithm>
Packit 209faa
#include <iterator>
Packit 209faa
#include <stdexcept>
Packit 209faa
Packit 209faa
#include <lutok/exceptions.hpp>
Packit 209faa
#include <lutok/operations.hpp>
Packit 209faa
#include <lutok/stack_cleaner.hpp>
Packit 209faa
#include <lutok/state.ipp>
Packit 209faa
Packit 209faa
#include "engine/exceptions.hpp"
Packit 209faa
#include "engine/scheduler.hpp"
Packit 209faa
#include "model/metadata.hpp"
Packit 209faa
#include "model/test_program.hpp"
Packit 209faa
#include "utils/config/exceptions.hpp"
Packit 209faa
#include "utils/config/tree.ipp"
Packit 209faa
#include "utils/datetime.hpp"
Packit 209faa
#include "utils/format/macros.hpp"
Packit 209faa
#include "utils/fs/lua_module.hpp"
Packit 209faa
#include "utils/fs/operations.hpp"
Packit 209faa
#include "utils/logging/macros.hpp"
Packit 209faa
#include "utils/noncopyable.hpp"
Packit 209faa
#include "utils/optional.ipp"
Packit 209faa
#include "utils/sanity.hpp"
Packit 209faa
Packit 209faa
namespace config = utils::config;
Packit 209faa
namespace datetime = utils::datetime;
Packit 209faa
namespace fs = utils::fs;
Packit 209faa
namespace scheduler = engine::scheduler;
Packit 209faa
Packit 209faa
using utils::none;
Packit 209faa
using utils::optional;
Packit 209faa
Packit 209faa
Packit 209faa
// History of Kyuafile file versions:
Packit 209faa
//
Packit 209faa
// 3 - DOES NOT YET EXIST.  Pending changes for when this is introduced:
Packit 209faa
//
Packit 209faa
//     * Revisit what to do about the test_suite definition.  Support for
Packit 209faa
//       per-test program overrides is deprecated and should be removed.
Packit 209faa
//       But, maybe, the whole test_suite definition idea is wrong and we
Packit 209faa
//       should instead be explicitly telling which configuration variables
Packit 209faa
//       to "inject" into each test program.
Packit 209faa
//
Packit 209faa
// 2 - Changed the syntax() call to take only a version number, instead of the
Packit 209faa
//     word 'config' as the first argument and the version as the second one.
Packit 209faa
//     Files now start with syntax(2) instead of syntax('kyuafile', 1).
Packit 209faa
//
Packit 209faa
// 1 - Initial version.
Packit 209faa
Packit 209faa
Packit 209faa
namespace {
Packit 209faa
Packit 209faa
Packit 209faa
static int lua_current_kyuafile(lutok::state&);
Packit 209faa
static int lua_generic_test_program(lutok::state&);
Packit 209faa
static int lua_include(lutok::state&);
Packit 209faa
static int lua_syntax(lutok::state&);
Packit 209faa
static int lua_test_suite(lutok::state&);
Packit 209faa
Packit 209faa
Packit 209faa
/// Concatenates two paths while avoiding paths to start with './'.
Packit 209faa
///
Packit 209faa
/// \param root Path to the directory containing the file.
Packit 209faa
/// \param file Path to concatenate to root.  Cannot be absolute.
Packit 209faa
///
Packit 209faa
/// \return The concatenated path.
Packit 209faa
static fs::path
Packit 209faa
relativize(const fs::path& root, const fs::path& file)
Packit 209faa
{
Packit 209faa
    PRE(!file.is_absolute());
Packit 209faa
Packit 209faa
    if (root == fs::path("."))
Packit 209faa
        return file;
Packit 209faa
    else
Packit 209faa
        return root / file;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Implementation of a parser for Kyuafiles.
Packit 209faa
///
Packit 209faa
/// The main purpose of having this as a class is to keep track of global state
Packit 209faa
/// within the Lua files and allowing the Lua callbacks to easily access such
Packit 209faa
/// data.
Packit 209faa
class parser : utils::noncopyable {
Packit 209faa
    /// Lua state to parse a single Kyuafile file.
Packit 209faa
    lutok::state _state;
Packit 209faa
Packit 209faa
    /// Root directory of the test suite represented by the Kyuafile.
Packit 209faa
    const fs::path _source_root;
Packit 209faa
Packit 209faa
    /// Root directory of the test programs.
Packit 209faa
    const fs::path _build_root;
Packit 209faa
Packit 209faa
    /// Name of the Kyuafile to load relative to _source_root.
Packit 209faa
    const fs::path _relative_filename;
Packit 209faa
Packit 209faa
    /// Version of the Kyuafile file format requested by the parsed file.
Packit 209faa
    ///
Packit 209faa
    /// This is set once the Kyuafile invokes the syntax() call.
Packit 209faa
    optional< int > _version;
Packit 209faa
Packit 209faa
    /// Name of the test suite defined by the Kyuafile.
Packit 209faa
    ///
Packit 209faa
    /// This is set once the Kyuafile invokes the test_suite() call.
Packit 209faa
    optional< std::string > _test_suite;
Packit 209faa
Packit 209faa
    /// Collection of test programs defined by the Kyuafile.
Packit 209faa
    ///
Packit 209faa
    /// This acts as an accumulator for all the *_test_program() calls within
Packit 209faa
    /// the Kyuafile.
Packit 209faa
    model::test_programs_vector _test_programs;
Packit 209faa
Packit 209faa
    /// Safely gets _test_suite and respects any test program overrides.
Packit 209faa
    ///
Packit 209faa
    /// \param program_override The test program-specific test suite name.  May
Packit 209faa
    ///     be empty to indicate no override.
Packit 209faa
    ///
Packit 209faa
    /// \return The name of the test suite.
Packit 209faa
    ///
Packit 209faa
    /// \throw std::runtime_error If program_override is empty and the Kyuafile
Packit 209faa
    /// did not yet define the global name of the test suite.
Packit 209faa
    std::string
Packit 209faa
    get_test_suite(const std::string& program_override)
Packit 209faa
    {
Packit 209faa
        std::string test_suite;
Packit 209faa
Packit 209faa
        if (program_override.empty()) {
Packit 209faa
            if (!_test_suite) {
Packit 209faa
                throw std::runtime_error("No test suite defined in the "
Packit 209faa
                                         "Kyuafile and no override provided in "
Packit 209faa
                                         "the test_program definition");
Packit 209faa
            }
Packit 209faa
            test_suite = _test_suite.get();
Packit 209faa
        } else {
Packit 209faa
            test_suite = program_override;
Packit 209faa
        }
Packit 209faa
Packit 209faa
        return test_suite;
Packit 209faa
    }
Packit 209faa
Packit 209faa
public:
Packit 209faa
    /// Initializes the parser and the Lua state.
Packit 209faa
    ///
Packit 209faa
    /// \param source_root_ The root directory of the test suite represented by
Packit 209faa
    ///     the Kyuafile.
Packit 209faa
    /// \param build_root_ The root directory of the test programs.
Packit 209faa
    /// \param relative_filename_ Name of the Kyuafile to load relative to
Packit 209faa
    ///     source_root_.
Packit 209faa
    /// \param user_config User configuration holding any test suite properties
Packit 209faa
    ///     to be passed to the list operation.
Packit 209faa
    /// \param scheduler_handle The scheduler context to use for loading the
Packit 209faa
    ///     test case lists.
Packit 209faa
    parser(const fs::path& source_root_, const fs::path& build_root_,
Packit 209faa
           const fs::path& relative_filename_,
Packit 209faa
           const config::tree& user_config,
Packit 209faa
           scheduler::scheduler_handle& scheduler_handle) :
Packit 209faa
        _source_root(source_root_), _build_root(build_root_),
Packit 209faa
        _relative_filename(relative_filename_)
Packit 209faa
    {
Packit 209faa
        lutok::stack_cleaner cleaner(_state);
Packit 209faa
Packit 209faa
        _state.push_cxx_function(lua_syntax);
Packit 209faa
        _state.set_global("syntax");
Packit 209faa
Packit 209faa
        *_state.new_userdata< parser* >() = this;
Packit 209faa
        _state.set_global("_parser");
Packit 209faa
Packit 209faa
        _state.push_cxx_function(lua_current_kyuafile);
Packit 209faa
        _state.set_global("current_kyuafile");
Packit 209faa
Packit 209faa
        *_state.new_userdata< const config::tree* >() = &user_config;
Packit 209faa
        *_state.new_userdata< scheduler::scheduler_handle* >() =
Packit 209faa
            &scheduler_handle;
Packit 209faa
        _state.push_cxx_closure(lua_include, 2);
Packit 209faa
        _state.set_global("include");
Packit 209faa
Packit 209faa
        _state.push_cxx_function(lua_test_suite);
Packit 209faa
        _state.set_global("test_suite");
Packit 209faa
Packit 209faa
        const std::set< std::string > interfaces =
Packit 209faa
            scheduler::registered_interface_names();
Packit 209faa
        for (std::set< std::string >::const_iterator iter = interfaces.begin();
Packit 209faa
             iter != interfaces.end(); ++iter) {
Packit 209faa
            const std::string& interface = *iter;
Packit 209faa
Packit 209faa
            _state.push_string(interface);
Packit 209faa
            *_state.new_userdata< const config::tree* >() = &user_config;
Packit 209faa
            *_state.new_userdata< scheduler::scheduler_handle* >() =
Packit 209faa
                &scheduler_handle;
Packit 209faa
            _state.push_cxx_closure(lua_generic_test_program, 3);
Packit 209faa
            _state.set_global(interface + "_test_program");
Packit 209faa
        }
Packit 209faa
Packit 209faa
        _state.open_base();
Packit 209faa
        _state.open_string();
Packit 209faa
        _state.open_table();
Packit 209faa
        fs::open_fs(_state, callback_current_kyuafile().branch_path());
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Destructor.
Packit 209faa
    ~parser(void)
Packit 209faa
    {
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Gets the parser object associated to a Lua state.
Packit 209faa
    ///
Packit 209faa
    /// \param state The Lua state from which to obtain the parser object.
Packit 209faa
    ///
Packit 209faa
    /// \return A pointer to the parser.
Packit 209faa
    static parser*
Packit 209faa
    get_from_state(lutok::state& state)
Packit 209faa
    {
Packit 209faa
        lutok::stack_cleaner cleaner(state);
Packit 209faa
        state.get_global("_parser");
Packit 209faa
        return *state.to_userdata< parser* >(-1);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Callback for the Kyuafile current_kyuafile() function.
Packit 209faa
    ///
Packit 209faa
    /// \return Returns the absolute path to the current Kyuafile.
Packit 209faa
    fs::path
Packit 209faa
    callback_current_kyuafile(void) const
Packit 209faa
    {
Packit 209faa
        const fs::path file = relativize(_source_root, _relative_filename);
Packit 209faa
        if (file.is_absolute())
Packit 209faa
            return file;
Packit 209faa
        else
Packit 209faa
            return file.to_absolute();
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Callback for the Kyuafile include() function.
Packit 209faa
    ///
Packit 209faa
    /// \post _test_programs is extended with the the test programs defined by
Packit 209faa
    /// the included file.
Packit 209faa
    ///
Packit 209faa
    /// \param raw_file Path to the file to include.
Packit 209faa
    /// \param user_config User configuration holding any test suite properties
Packit 209faa
    ///     to be passed to the list operation.
Packit 209faa
    /// \param scheduler_handle Scheduler context to run test programs in.
Packit 209faa
    void
Packit 209faa
    callback_include(const fs::path& raw_file,
Packit 209faa
                     const config::tree& user_config,
Packit 209faa
                     scheduler::scheduler_handle& scheduler_handle)
Packit 209faa
    {
Packit 209faa
        const fs::path file = relativize(_relative_filename.branch_path(),
Packit 209faa
                                         raw_file);
Packit 209faa
        const model::test_programs_vector subtps =
Packit 209faa
            parser(_source_root, _build_root, file, user_config,
Packit 209faa
                   scheduler_handle).parse();
Packit 209faa
Packit 209faa
        std::copy(subtps.begin(), subtps.end(),
Packit 209faa
                  std::back_inserter(_test_programs));
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Callback for the Kyuafile syntax() function.
Packit 209faa
    ///
Packit 209faa
    /// \post _version is set to the requested version.
Packit 209faa
    ///
Packit 209faa
    /// \param version Version of the Kyuafile syntax requested by the file.
Packit 209faa
    ///
Packit 209faa
    /// \throw std::runtime_error If the format or the version are invalid, or
Packit 209faa
    /// if syntax() has already been called.
Packit 209faa
    void
Packit 209faa
    callback_syntax(const int version)
Packit 209faa
    {
Packit 209faa
        if (_version)
Packit 209faa
            throw std::runtime_error("Can only call syntax() once");
Packit 209faa
Packit 209faa
        if (version < 1 || version > 2)
Packit 209faa
            throw std::runtime_error(F("Unsupported file version %s") %
Packit 209faa
                                     version);
Packit 209faa
Packit 209faa
        _version = utils::make_optional(version);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Callback for the various Kyuafile *_test_program() functions.
Packit 209faa
    ///
Packit 209faa
    /// \post _test_programs is extended to include the newly defined test
Packit 209faa
    /// program.
Packit 209faa
    ///
Packit 209faa
    /// \param interface Name of the test program interface.
Packit 209faa
    /// \param raw_path Path to the test program, relative to the Kyuafile.
Packit 209faa
    ///     This has to be adjusted according to the relative location of this
Packit 209faa
    ///     Kyuafile to _source_root.
Packit 209faa
    /// \param test_suite_override Name of the test suite this test program
Packit 209faa
    ///     belongs to, if explicitly defined at the test program level.
Packit 209faa
    /// \param metadata Metadata variables passed to the test program.
Packit 209faa
    /// \param user_config User configuration holding any test suite properties
Packit 209faa
    ///     to be passed to the list operation.
Packit 209faa
    /// \param scheduler_handle Scheduler context to run test programs in.
Packit 209faa
    ///
Packit 209faa
    /// \throw std::runtime_error If the test program definition is invalid or
Packit 209faa
    ///     if the test program does not exist.
Packit 209faa
    void
Packit 209faa
    callback_test_program(const std::string& interface,
Packit 209faa
                          const fs::path& raw_path,
Packit 209faa
                          const std::string& test_suite_override,
Packit 209faa
                          const model::metadata& metadata,
Packit 209faa
                          const config::tree& user_config,
Packit 209faa
                          scheduler::scheduler_handle& scheduler_handle)
Packit 209faa
    {
Packit 209faa
        if (raw_path.is_absolute())
Packit 209faa
            throw std::runtime_error(F("Got unexpected absolute path for test "
Packit 209faa
                                       "program '%s'") % raw_path);
Packit 209faa
        else if (raw_path.str() != raw_path.leaf_name())
Packit 209faa
            throw std::runtime_error(F("Test program '%s' cannot contain path "
Packit 209faa
                                       "components") % raw_path);
Packit 209faa
Packit 209faa
        const fs::path path = relativize(_relative_filename.branch_path(),
Packit 209faa
                                         raw_path);
Packit 209faa
Packit 209faa
        if (!fs::exists(_build_root / path))
Packit 209faa
            throw std::runtime_error(F("Non-existent test program '%s'") %
Packit 209faa
                                     path);
Packit 209faa
Packit 209faa
        const std::string test_suite = get_test_suite(test_suite_override);
Packit 209faa
Packit 209faa
        _test_programs.push_back(model::test_program_ptr(
Packit 209faa
            new scheduler::lazy_test_program(interface, path, _build_root,
Packit 209faa
                                             test_suite, metadata, user_config,
Packit 209faa
                                             scheduler_handle)));
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Callback for the Kyuafile test_suite() function.
Packit 209faa
    ///
Packit 209faa
    /// \post _version is set to the requested version.
Packit 209faa
    ///
Packit 209faa
    /// \param name Name of the test suite.
Packit 209faa
    ///
Packit 209faa
    /// \throw std::runtime_error If test_suite() has already been called.
Packit 209faa
    void
Packit 209faa
    callback_test_suite(const std::string& name)
Packit 209faa
    {
Packit 209faa
        if (_test_suite)
Packit 209faa
            throw std::runtime_error("Can only call test_suite() once");
Packit 209faa
        _test_suite = utils::make_optional(name);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    /// Parses the Kyuafile.
Packit 209faa
    ///
Packit 209faa
    /// \pre Can only be invoked once.
Packit 209faa
    ///
Packit 209faa
    /// \return The collection of test programs defined by the Kyuafile.
Packit 209faa
    ///
Packit 209faa
    /// \throw load_error If there is any problem parsing the file.
Packit 209faa
    const model::test_programs_vector&
Packit 209faa
    parse(void)
Packit 209faa
    {
Packit 209faa
        PRE(_test_programs.empty());
Packit 209faa
Packit 209faa
        const fs::path load_path = relativize(_source_root, _relative_filename);
Packit 209faa
        try {
Packit 209faa
            lutok::do_file(_state, load_path.str(), 0, 0, 0);
Packit 209faa
        } catch (const std::runtime_error& e) {
Packit 209faa
            // It is tempting to think that all of our various auxiliary
Packit 209faa
            // functions above could raise load_error by themselves thus making
Packit 209faa
            // this exception rewriting here unnecessary.  Howver, that would
Packit 209faa
            // not work because the helper functions above are executed within a
Packit 209faa
            // Lua context, and we lose their type when they are propagated out
Packit 209faa
            // of it.
Packit 209faa
            throw engine::load_error(load_path, e.what());
Packit 209faa
        }
Packit 209faa
Packit 209faa
        if (!_version)
Packit 209faa
            throw engine::load_error(load_path, "syntax() never called");
Packit 209faa
Packit 209faa
        return _test_programs;
Packit 209faa
    }
Packit 209faa
};
Packit 209faa
Packit 209faa
Packit 209faa
/// Glue to invoke parser::callback_test_program() from Lua.
Packit 209faa
///
Packit 209faa
/// This is a helper function for the various *_test_program() calls, as they
Packit 209faa
/// only differ in the interface of the defined test program.
Packit 209faa
///
Packit 209faa
/// \pre state(-1) A table with the arguments that define the test program.  The
Packit 209faa
/// special argument 'test_suite' provides an override to the global test suite
Packit 209faa
/// name.  The rest of the arguments are part of the test program metadata.
Packit 209faa
/// \pre state(upvalue 1) String with the name of the interface.
Packit 209faa
/// \pre state(upvalue 2) User configuration with the per-test suite settings.
Packit 209faa
/// \pre state(upvalue 3) Scheduler context to run test programs in.
Packit 209faa
///
Packit 209faa
/// \param state The Lua state that executed the function.
Packit 209faa
///
Packit 209faa
/// \return Number of return values left on the Lua stack.
Packit 209faa
///
Packit 209faa
/// \throw std::runtime_error If the arguments to the function are invalid.
Packit 209faa
static int
Packit 209faa
lua_generic_test_program(lutok::state& state)
Packit 209faa
{
Packit 209faa
    if (!state.is_string(state.upvalue_index(1)))
Packit 209faa
        throw std::runtime_error("Found corrupt state for test_program "
Packit 209faa
                                 "function");
Packit 209faa
    const std::string interface = state.to_string(state.upvalue_index(1));
Packit 209faa
Packit 209faa
    if (!state.is_userdata(state.upvalue_index(2)))
Packit 209faa
        throw std::runtime_error("Found corrupt state for test_program "
Packit 209faa
                                 "function");
Packit 209faa
    const config::tree* user_config = *state.to_userdata< const config::tree* >(
Packit 209faa
        state.upvalue_index(2));
Packit 209faa
Packit 209faa
    if (!state.is_userdata(state.upvalue_index(3)))
Packit 209faa
        throw std::runtime_error("Found corrupt state for test_program "
Packit 209faa
                                 "function");
Packit 209faa
    scheduler::scheduler_handle* scheduler_handle =
Packit 209faa
        *state.to_userdata< scheduler::scheduler_handle* >(
Packit 209faa
            state.upvalue_index(3));
Packit 209faa
Packit 209faa
    if (!state.is_table(-1))
Packit 209faa
        throw std::runtime_error(
Packit 209faa
            F("%s_test_program expects a table of properties as its single "
Packit 209faa
              "argument") % interface);
Packit 209faa
Packit 209faa
    scheduler::ensure_valid_interface(interface);
Packit 209faa
Packit 209faa
    lutok::stack_cleaner cleaner(state);
Packit 209faa
Packit 209faa
    state.push_string("name");
Packit 209faa
    state.get_table(-2);
Packit 209faa
    if (!state.is_string(-1))
Packit 209faa
        throw std::runtime_error("Test program name not defined or not a "
Packit 209faa
                                 "string");
Packit 209faa
    const fs::path path(state.to_string(-1));
Packit 209faa
    state.pop(1);
Packit 209faa
Packit 209faa
    state.push_string("test_suite");
Packit 209faa
    state.get_table(-2);
Packit 209faa
    std::string test_suite;
Packit 209faa
    if (state.is_nil(-1)) {
Packit 209faa
        // Leave empty to use the global test-suite value.
Packit 209faa
    } else if (state.is_string(-1)) {
Packit 209faa
        test_suite = state.to_string(-1);
Packit 209faa
    } else {
Packit 209faa
        throw std::runtime_error(F("Found non-string value in the test_suite "
Packit 209faa
                                   "property of test program '%s'") % path);
Packit 209faa
    }
Packit 209faa
    state.pop(1);
Packit 209faa
Packit 209faa
    model::metadata_builder mdbuilder;
Packit 209faa
    state.push_nil();
Packit 209faa
    while (state.next(-2)) {
Packit 209faa
        if (!state.is_string(-2))
Packit 209faa
            throw std::runtime_error(F("Found non-string metadata property "
Packit 209faa
                                       "name in test program '%s'") %
Packit 209faa
                                     path);
Packit 209faa
        const std::string property = state.to_string(-2);
Packit 209faa
Packit 209faa
        if (property != "name" && property != "test_suite") {
Packit 209faa
            std::string value;
Packit 209faa
            if (state.is_boolean(-1)) {
Packit 209faa
                value = F("%s") % state.to_boolean(-1);
Packit 209faa
            } else if (state.is_number(-1)) {
Packit 209faa
                value = F("%s") % state.to_integer(-1);
Packit 209faa
            } else if (state.is_string(-1)) {
Packit 209faa
                value = state.to_string(-1);
Packit 209faa
            } else {
Packit 209faa
                throw std::runtime_error(
Packit 209faa
                    F("Metadata property '%s' in test program '%s' cannot be "
Packit 209faa
                      "converted to a string") % property % path);
Packit 209faa
            }
Packit 209faa
Packit 209faa
            mdbuilder.set_string(property, value);
Packit 209faa
        }
Packit 209faa
Packit 209faa
        state.pop(1);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    parser::get_from_state(state)->callback_test_program(
Packit 209faa
        interface, path, test_suite, mdbuilder.build(), *user_config,
Packit 209faa
        *scheduler_handle);
Packit 209faa
    return 0;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Glue to invoke parser::callback_current_kyuafile() from Lua.
Packit 209faa
///
Packit 209faa
/// \param state The Lua state that executed the function.
Packit 209faa
///
Packit 209faa
/// \return Number of return values left on the Lua stack.
Packit 209faa
static int
Packit 209faa
lua_current_kyuafile(lutok::state& state)
Packit 209faa
{
Packit 209faa
    state.push_string(parser::get_from_state(state)->
Packit 209faa
                      callback_current_kyuafile().str());
Packit 209faa
    return 1;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Glue to invoke parser::callback_include() from Lua.
Packit 209faa
///
Packit 209faa
/// \param state The Lua state that executed the function.
Packit 209faa
///
Packit 209faa
/// \pre state(upvalue 1) User configuration with the per-test suite settings.
Packit 209faa
/// \pre state(upvalue 2) Scheduler context to run test programs in.
Packit 209faa
///
Packit 209faa
/// \return Number of return values left on the Lua stack.
Packit 209faa
static int
Packit 209faa
lua_include(lutok::state& state)
Packit 209faa
{
Packit 209faa
    if (!state.is_userdata(state.upvalue_index(1)))
Packit 209faa
        throw std::runtime_error("Found corrupt state for test_program "
Packit 209faa
                                 "function");
Packit 209faa
    const config::tree* user_config = *state.to_userdata< const config::tree* >(
Packit 209faa
        state.upvalue_index(1));
Packit 209faa
Packit 209faa
    if (!state.is_userdata(state.upvalue_index(2)))
Packit 209faa
        throw std::runtime_error("Found corrupt state for test_program "
Packit 209faa
                                 "function");
Packit 209faa
    scheduler::scheduler_handle* scheduler_handle =
Packit 209faa
        *state.to_userdata< scheduler::scheduler_handle* >(
Packit 209faa
            state.upvalue_index(2));
Packit 209faa
Packit 209faa
    parser::get_from_state(state)->callback_include(
Packit 209faa
        fs::path(state.to_string(-1)), *user_config, *scheduler_handle);
Packit 209faa
    return 0;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Glue to invoke parser::callback_syntax() from Lua.
Packit 209faa
///
Packit 209faa
/// \pre state(-2) The syntax format name, if a v1 file.
Packit 209faa
/// \pre state(-1) The syntax format version.
Packit 209faa
///
Packit 209faa
/// \param state The Lua state that executed the function.
Packit 209faa
///
Packit 209faa
/// \return Number of return values left on the Lua stack.
Packit 209faa
static int
Packit 209faa
lua_syntax(lutok::state& state)
Packit 209faa
{
Packit 209faa
    if (!state.is_number(-1))
Packit 209faa
        throw std::runtime_error("Last argument to syntax must be a number");
Packit 209faa
    const int syntax_version = state.to_integer(-1);
Packit 209faa
Packit 209faa
    if (syntax_version == 1) {
Packit 209faa
        if (state.get_top() != 2)
Packit 209faa
            throw std::runtime_error("Version 1 files need two arguments to "
Packit 209faa
                                     "syntax()");
Packit 209faa
        if (!state.is_string(-2) || state.to_string(-2) != "kyuafile")
Packit 209faa
            throw std::runtime_error("First argument to syntax must be "
Packit 209faa
                                     "'kyuafile' for version 1 files");
Packit 209faa
    } else {
Packit 209faa
        if (state.get_top() != 1)
Packit 209faa
            throw std::runtime_error("syntax() only takes one argument");
Packit 209faa
    }
Packit 209faa
Packit 209faa
    parser::get_from_state(state)->callback_syntax(syntax_version);
Packit 209faa
    return 0;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Glue to invoke parser::callback_test_suite() from Lua.
Packit 209faa
///
Packit 209faa
/// \param state The Lua state that executed the function.
Packit 209faa
///
Packit 209faa
/// \return Number of return values left on the Lua stack.
Packit 209faa
static int
Packit 209faa
lua_test_suite(lutok::state& state)
Packit 209faa
{
Packit 209faa
    parser::get_from_state(state)->callback_test_suite(state.to_string(-1));
Packit 209faa
    return 0;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
}  // anonymous namespace
Packit 209faa
Packit 209faa
Packit 209faa
/// Constructs a kyuafile form initialized data.
Packit 209faa
///
Packit 209faa
/// Use load() to parse a test suite configuration file and construct a
Packit 209faa
/// kyuafile object.
Packit 209faa
///
Packit 209faa
/// \param source_root_ The root directory for the test suite represented by the
Packit 209faa
///     Kyuafile.  In other words, the directory containing the first Kyuafile
Packit 209faa
///     processed.
Packit 209faa
/// \param build_root_ The root directory for the test programs themselves.  In
Packit 209faa
///     general, this will be the same as source_root_.  If different, the
Packit 209faa
///     specified directory must follow the exact same layout of source_root_.
Packit 209faa
/// \param tps_ Collection of test programs that belong to this test suite.
Packit 209faa
engine::kyuafile::kyuafile(const fs::path& source_root_,
Packit 209faa
                           const fs::path& build_root_,
Packit 209faa
                           const model::test_programs_vector& tps_) :
Packit 209faa
    _source_root(source_root_),
Packit 209faa
    _build_root(build_root_),
Packit 209faa
    _test_programs(tps_)
Packit 209faa
{
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Destructor.
Packit 209faa
engine::kyuafile::~kyuafile(void)
Packit 209faa
{
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Parses a test suite configuration file.
Packit 209faa
///
Packit 209faa
/// \param file The file to parse.
Packit 209faa
/// \param user_build_root If not none, specifies a path to a directory
Packit 209faa
///     containing the test programs themselves.  The layout of the build root
Packit 209faa
///     must match the layout of the source root (which is just the directory
Packit 209faa
///     from which the Kyuafile is being read).
Packit 209faa
/// \param user_config User configuration holding any test suite properties
Packit 209faa
///     to be passed to the list operation.
Packit 209faa
/// \param scheduler_handle The scheduler context to use for loading the test
Packit 209faa
///     case lists.
Packit 209faa
///
Packit 209faa
/// \return High-level representation of the configuration file.
Packit 209faa
///
Packit 209faa
/// \throw load_error If there is any problem loading the file.  This includes
Packit 209faa
///     file access errors and syntax errors.
Packit 209faa
engine::kyuafile
Packit 209faa
engine::kyuafile::load(const fs::path& file,
Packit 209faa
                       const optional< fs::path > user_build_root,
Packit 209faa
                       const config::tree& user_config,
Packit 209faa
                       scheduler::scheduler_handle& scheduler_handle)
Packit 209faa
{
Packit 209faa
    const fs::path source_root_ = file.branch_path();
Packit 209faa
    const fs::path build_root_ = user_build_root ?
Packit 209faa
        user_build_root.get() : source_root_;
Packit 209faa
Packit 209faa
    // test_program.absolute_path() uses the current work directory and that
Packit 209faa
    // fails to resolve the correct path once we have used chdir to enter the
Packit 209faa
    // test work directory.  To prevent this causing issues down the road,
Packit 209faa
    // force the build root to be absolute so that absolute_path() does not
Packit 209faa
    // need to rely on the current work directory.
Packit 209faa
    const fs::path abs_build_root = build_root_.is_absolute() ?
Packit 209faa
        build_root_ : build_root_.to_absolute();
Packit 209faa
Packit 209faa
    return kyuafile(source_root_, build_root_,
Packit 209faa
                    parser(source_root_, abs_build_root,
Packit 209faa
                           fs::path(file.leaf_name()), user_config,
Packit 209faa
                           scheduler_handle).parse());
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Gets the root directory of the test suite.
Packit 209faa
///
Packit 209faa
/// \return A path.
Packit 209faa
const fs::path&
Packit 209faa
engine::kyuafile::source_root(void) const
Packit 209faa
{
Packit 209faa
    return _source_root;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Gets the root directory of the test programs.
Packit 209faa
///
Packit 209faa
/// \return A path.
Packit 209faa
const fs::path&
Packit 209faa
engine::kyuafile::build_root(void) const
Packit 209faa
{
Packit 209faa
    return _build_root;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Gets the collection of test programs that belong to this test suite.
Packit 209faa
///
Packit 209faa
/// \return Collection of test program executable names.
Packit 209faa
const model::test_programs_vector&
Packit 209faa
engine::kyuafile::test_programs(void) const
Packit 209faa
{
Packit 209faa
    return _test_programs;
Packit 209faa
}