Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 14, 2023
1 parent 3b1d509 commit 1a15318
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ user.bazelrc
/.cache

# other
/test
*.tar.gz
/test/reg
/test/module
3 changes: 1 addition & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions bzlmod/init_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -37,6 +38,8 @@ auto bzlmod::init_module( //
return 1;
}

fs::create_directories(dir, ec);

{
auto module_file_stream = std::ofstream{module_file};

Expand Down
5 changes: 3 additions & 2 deletions bzlreg/add_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
);
Expand Down
17 changes: 14 additions & 3 deletions bzlreg/bzlreg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ Bazel registry CLI utility
Usage:
bzlreg init [<registry-dir>]
bzlreg add-module <archive-url> [--strip-prefix=<strip-prefix>] [--registry-dir=<registry-dir>]
bzlreg add-module <archive-url> [--strip-prefix=<str>] [--registry=<path>]
Options:
--registry=<path> Registry directory. Defaults to current working directory.
--strip-prefix=<str> Prefix stripped from archive and set in source.json.
)docopt";

auto main(int argc, char* argv[]) -> int {
Expand All @@ -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("<archive-url>").asString();
exit_code = bzlreg::add_module({
.registry_dir = registry_dir,
Expand Down
8 changes: 5 additions & 3 deletions bzlreg/module_bazel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions test/test.bat
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1a15318

Please sign in to comment.