From 1a15318bb6e0c130f620d3e367096d1d55cc8a21 Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Thu, 14 Sep 2023 13:39:26 -0700 Subject: [PATCH] chore: add tests --- .gitignore | 3 ++- MODULE.bazel | 3 +-- bzlmod/init_module.cc | 3 +++ bzlreg/add_module.cc | 5 +++-- bzlreg/bzlreg.cc | 17 ++++++++++++++--- bzlreg/module_bazel.cc | 8 +++++--- test/test.bat | 20 ++++++++++++++++++++ 7 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 test/test.bat diff --git a/.gitignore b/.gitignore index 6d57aeb..ed8ac1d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,6 @@ user.bazelrc /.cache # other -/test *.tar.gz +/test/reg +/test/module diff --git a/MODULE.bazel b/MODULE.bazel index 3e9493f..a9acbdf 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -4,7 +4,7 @@ module( compatibility_level = 1, ) -bazel_dep(name = "rules_cc", version = "0.0.8") +bazel_dep(name = "rules_cc", version = "0.0.4") bazel_dep(name = "bazel_skylib", version = "1.4.2") bazel_dep(name = "nlohmann_json", version = "3.11.2") bazel_dep(name = "boost.process", version = "1.83.0.bzl.2") @@ -14,7 +14,6 @@ bazel_dep(name = "libdeflate", version = "1.18") bazel_dep(name = "abseil-cpp", version = "20230802.0") bazel_dep(name = "boringssl", version = "0.0.0-20230215-5c22014") bazel_dep(name = "docopt.cpp") - git_override( module_name = "docopt.cpp", commit = "e2f9cdba36c3b70883cea848a8e4d72d9b9a3fac", diff --git a/bzlmod/init_module.cc b/bzlmod/init_module.cc index 1e808fb..1178c12 100644 --- a/bzlmod/init_module.cc +++ b/bzlmod/init_module.cc @@ -24,6 +24,7 @@ common --enable_bzlmod auto bzlmod::init_module( // fs::path dir ) -> int { + auto ec = std::error_code{}; auto module_file = dir / "MODULE.bazel"; auto workspace_file = dir / "WORKSPACE.bazel"; auto workspace_bzlmod_file = dir / "WORKSPACE.bzlmod"; @@ -37,6 +38,8 @@ auto bzlmod::init_module( // return 1; } + fs::create_directories(dir, ec); + { auto module_file_stream = std::ofstream{module_file}; diff --git a/bzlreg/add_module.cc b/bzlreg/add_module.cc index 7266d43..184ec1e 100644 --- a/bzlreg/add_module.cc +++ b/bzlreg/add_module.cc @@ -82,7 +82,7 @@ auto bzlreg::add_module(add_module_options options) -> int { if(!fs::exists(registry_dir / "bazel_registry.json")) { std::cerr << std::format( - "bazel_registry.json file is missing. Are sure {} is a bazel registry?", + "bazel_registry.json file is missing. Are sure {} is a bazel registry?\n", registry_dir.generic_string() ); return 1; @@ -125,6 +125,7 @@ auto bzlreg::add_module(add_module_options options) -> int { auto decompressed_data = bzlreg::decompress_archive(*compressed_data); auto tar_view = bzlreg::tar_view{decompressed_data}; + auto module_bzl_view = tar_view.file( strip_prefix.empty() // ? "MODULE.bazel" @@ -182,7 +183,7 @@ auto bzlreg::add_module(add_module_options options) -> int { } else { std::cerr << std::format( // "[WARN] Unable to infer repository string from {}\n" - " Please add to {} manually", + " Please add to {} manually\n", archive_url_str, metadata_config_path.generic_string() ); diff --git a/bzlreg/bzlreg.cc b/bzlreg/bzlreg.cc index 403376a..ff6304f 100644 --- a/bzlreg/bzlreg.cc +++ b/bzlreg/bzlreg.cc @@ -11,7 +11,11 @@ Bazel registry CLI utility Usage: bzlreg init [] - bzlreg add-module [--strip-prefix=] [--registry-dir=] + bzlreg add-module [--strip-prefix=] [--registry=] + +Options: + --registry= Registry directory. Defaults to current working directory. + --strip-prefix= Prefix stripped from archive and set in source.json. )docopt"; auto main(int argc, char* argv[]) -> int { @@ -33,9 +37,16 @@ auto main(int argc, char* argv[]) -> int { auto strip_prefix = args["--strip-prefix"] // ? args.at("--strip-prefix").asString() : ""; - auto registry_dir = args["--registry-dir"] // - ? fs::path{args.at("--registry-dir").asString()} + auto registry_dir = args["--registry"] // + ? fs::path{args.at("--registry").asString()} : fs::current_path(); + + if(registry_dir.empty()) { + std::cerr << "[ERROR] --registry must not be empty\n"; + std::cerr << USAGE; + return 1; + } + auto archive_url = args.at("").asString(); exit_code = bzlreg::add_module({ .registry_dir = registry_dir, diff --git a/bzlreg/module_bazel.cc b/bzlreg/module_bazel.cc index a794578..4eb4f70 100644 --- a/bzlreg/module_bazel.cc +++ b/bzlreg/module_bazel.cc @@ -42,10 +42,12 @@ static auto parse_call( // absl::SkipWhitespace() ); - auto attr_name = absl::StripAsciiWhitespace(attr_line_split[0]); - auto attr_value = absl::StripAsciiWhitespace(attr_line_split[1]); + if(attr_line_split.size() > 1) { + auto attr_name = absl::StripAsciiWhitespace(attr_line_split[0]); + auto attr_value = absl::StripAsciiWhitespace(attr_line_split[1]); - result.attrs[attr_name] = attr_value; + result.attrs[attr_name] = attr_value; + } } result.contents_after = contents.substr(paren_close + 1); diff --git a/test/test.bat b/test/test.bat new file mode 100644 index 0000000..fe8de8d --- /dev/null +++ b/test/test.bat @@ -0,0 +1,20 @@ +@echo OFF + +set BZLREG=%~dp0..\bazel-bin\bzlreg\bzlreg.exe +set BZLMOD=%~dp0..\bazel-bin\bzlmod\bzlmod.exe + +echo initializing test registry +%BZLREG% init %~dp0reg || exit /b + +echo adding rules_cc to test registry +%BZLREG% add-module https://github.com/bazelbuild/rules_cc/releases/download/0.0.8/rules_cc-0.0.8.tar.gz --strip-prefix=rules_cc-0.0.8 --registry=%~dp0reg || exit /b + +echo initializing test module +%BZLMOD% init %~dp0module || exit /b + +echo common --registry=file://%~dp0reg >> %~dp0module/.bazelrc + +cd %~dp0module +%BZLMOD% add rules_cc || exit /b + +echo done