소스 검색

Error handling when package cache has an invalid item

default_compile_flags
vector-of-bool 3 년 전
부모
커밋
198dd5e6d2
4개의 변경된 파일124개의 추가작업 그리고 14개의 파일을 삭제
  1. +15
    -0
      src/dds/error/handle.cpp
  2. +26
    -0
      src/dds/error/handle.hpp
  3. +54
    -14
      src/dds/pkg/cache.cpp
  4. +29
    -0
      tests/test_pkg_db.py

+ 15
- 0
src/dds/error/handle.cpp 파일 보기

#include "./handle.hpp"

#include <dds/util/log.hpp>

#include <boost/leaf/handle_exception.hpp>
#include <boost/leaf/result.hpp>
#include <fmt/ostream.h>

using namespace dds;

void dds::leaf_handle_unknown_void(std::string_view message,
const boost::leaf::verbose_diagnostic_info& info) {
dds_log(warn, message);
dds_log(warn, "An unhandled error occurred:\n{}", info);
}

+ 26
- 0
src/dds/error/handle.hpp 파일 보기

#pragma once

#include <neo/fwd.hpp>

#include <string_view>

namespace boost::leaf {

class verbose_diagnostic_info;

} // namespace boost::leaf

namespace dds {

void leaf_handle_unknown_void(std::string_view message,
const boost::leaf::verbose_diagnostic_info&);

template <typename T>
auto leaf_handle_unknown(std::string_view message, T&& val) {
return [val = NEO_FWD(val), message](const boost::leaf::verbose_diagnostic_info& info) {
leaf_handle_unknown_void(message, info);
return val;
};
}

} // namespace dds

+ 54
- 14
src/dds/pkg/cache.cpp 파일 보기

#include "./cache.hpp" #include "./cache.hpp"


#include <dds/error/errors.hpp> #include <dds/error/errors.hpp>
#include <dds/error/handle.hpp>
#include <dds/pkg/db.hpp> #include <dds/pkg/db.hpp>
#include <dds/sdist/dist.hpp> #include <dds/sdist/dist.hpp>
#include <dds/sdist/library/manifest.hpp>
#include <dds/solve/solve.hpp> #include <dds/solve/solve.hpp>
#include <dds/util/log.hpp> #include <dds/util/log.hpp>
#include <dds/util/paths.hpp> #include <dds/util/paths.hpp>
#include <dds/util/string.hpp> #include <dds/util/string.hpp>


#include <boost/leaf/handle_exception.hpp>
#include <fansi/styled.hpp>
#include <neo/ref.hpp> #include <neo/ref.hpp>
#include <range/v3/action/sort.hpp> #include <range/v3/action/sort.hpp>
#include <range/v3/action/unique.hpp> #include <range/v3/action/unique.hpp>
#include <range/v3/view/transform.hpp> #include <range/v3/view/transform.hpp>


using namespace dds; using namespace dds;
using namespace fansi::literals;
using namespace ranges; using namespace ranges;


void pkg_cache::_log_blocking(path_ref dirpath) noexcept { void pkg_cache::_log_blocking(path_ref dirpath) noexcept {


fs::path pkg_cache::default_local_path() noexcept { return dds_data_dir() / "pkg"; } fs::path pkg_cache::default_local_path() noexcept { return dds_data_dir() / "pkg"; }


pkg_cache pkg_cache::_open_for_directory(bool writeable, path_ref dirpath) {
auto try_read_sdist = [](path_ref p) -> std::optional<sdist> {
if (starts_with(p.filename().string(), ".")) {
return std::nullopt;
}
try {
return sdist::from_directory(p);
} catch (const std::runtime_error& e) {
dds_log(error,
namespace {

std::optional<sdist> try_open_sdist_for_directory(path_ref p) noexcept {
if (starts_with(p.filename().string(), ".")) {
return std::nullopt;
}
return boost::leaf::try_catch( //
[&] { return std::make_optional(sdist::from_directory(p)); },
[&](boost::leaf::catch_<std::runtime_error> exc) {
dds_log(warn,
"Failed to load source distribution from directory '{}': {}", "Failed to load source distribution from directory '{}': {}",
p.string(), p.string(),
e.what());
exc.value().what());
return std::nullopt; return std::nullopt;
}
};
},
[&](e_package_manifest_path*,
e_library_manifest_path* lman_path,
e_pkg_namespace_str* is_namespace,
e_pkg_name_str* is_pkgname,
e_name_str bad_name,
invalid_name_reason why) {
dds_log(
warn,
"Failed to load a source distribution contained in the package cache directory");
dds_log(warn,
"The invalid source distribution is in [.bold.yellow[{}]]"_styled,
p.string());
if (is_namespace) {
dds_log(warn,
"Invalid package namespace '.bold.yellow[{}]'"_styled,
bad_name.value);
} else if (is_pkgname) {
dds_log(warn, "Invalid package name '.bold.yellow[{}]'"_styled, bad_name.value);
} else if (lman_path) {
dds_log(
warn,
"Invalid library name '.bold.yellow[{}]' (Defined in [.bold.yellow[{}]])"_styled,
bad_name.value,
lman_path->value);
}
dds_log(warn, " (.bold.yellow[{}])"_styled, invalid_name_reason_str(why));
dds_log(warn, "We will ignore this directory and not load it as an available package");
return std::nullopt;
},
leaf_handle_unknown(fmt::format("Failed to load source distribution from directory [{}]",
p.string()),
std::nullopt));
}

} // namespace


pkg_cache pkg_cache::_open_for_directory(bool writeable, path_ref dirpath) {
auto entries = auto entries =
// Get the top-level `name-version` dirs // Get the top-level `name-version` dirs
fs::directory_iterator(dirpath) // fs::directory_iterator(dirpath) //
| neo::lref // | neo::lref //
// Convert each dir into an `sdist` object // Convert each dir into an `sdist` object
| ranges::views::transform(try_read_sdist) //
| ranges::views::transform(try_open_sdist_for_directory) //
// Drop items that failed to load // Drop items that failed to load
| ranges::views::filter([](auto&& opt) { return opt.has_value(); }) // | ranges::views::filter([](auto&& opt) { return opt.has_value(); }) //
| ranges::views::transform([](auto&& opt) { return *opt; }) // | ranges::views::transform([](auto&& opt) { return *opt; }) //

+ 29
- 0
tests/test_pkg_db.py 파일 보기

import json

from dds_ci.dds import DDSWrapper from dds_ci.dds import DDSWrapper
from dds_ci.testing import Project, RepoServer, PackageJSON from dds_ci.testing import Project, RepoServer, PackageJSON
from dds_ci.testing.error import expect_error_marker from dds_ci.testing.error import expect_error_marker
dds.run(['pkg', dds.pkg_db_path_arg, 'search', 'neo-*']) dds.run(['pkg', dds.pkg_db_path_arg, 'search', 'neo-*'])
with expect_error_marker('pkg-search-no-result'): with expect_error_marker('pkg-search-no-result'):
dds.run(['pkg', dds.pkg_db_path_arg, 'search', 'nonexistent']) dds.run(['pkg', dds.pkg_db_path_arg, 'search', 'nonexistent'])


def test_pkg_cache_invalid_nofail(tmp_project: Project) -> None:
"""
Check that dds will not fail a build just because the package cache has an invalid
object within.
"""
sdist_dir = tmp_project.dds.repo_dir / 'bad@1.2.3'
sdist_dir.mkdir(parents=True)
tmp_project.build()

# Write an invalid source distribution
pkman_path = sdist_dir / 'package.json5'
pkman_path.write_text(json.dumps({}))
tmp_project.build()

pkman_path.write_text('lol') # Inavlid JSON
tmp_project.build()

pkman_path.write_text('''
{
name: 'invalid name',
namespace: 'test',
version: '1.2.3'
}
''')
tmp_project.build()

Loading…
취소
저장