diff --git a/Makefile b/Makefile index 42550ec6..bd346626 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,8 @@ bin_SRCS = src/main.cc lib_SRCS = $(filter-out $(bin_SRCS),$(SRCS)) test_SRCS = $(wildcard tests/*.cc) BINS = pkgdb -TESTS = $(filter-out tests/is_sqlite3,$(test_SRCS:.cc=)) +TEST_UTILS = tests/is_sqlite3 tests/id2attrpath +TESTS = $(filter-out $(TEST_UTILS),$(test_SRCS:.cc=)) # ---------------------------------------------------------------------------- # @@ -210,9 +211,9 @@ bin/pkgdb: src/main.o lib/$(LIBFLOXPKGDB) # ---------------------------------------------------------------------------- # -$(TESTS) tests/is_sqlite3: CXXFLAGS += $(bin_CXXFLAGS) -$(TESTS) tests/is_sqlite3: LDFLAGS += $(bin_LDFLAGS) -$(TESTS) tests/is_sqlite3: tests/%: tests/%.cc lib/$(LIBFLOXPKGDB) +$(TESTS) $(TEST_UTILS): CXXFLAGS += $(bin_CXXFLAGS) +$(TESTS) $(TEST_UTILS): LDFLAGS += $(bin_LDFLAGS) +$(TESTS) $(TEST_UTILS): tests/%: tests/%.cc lib/$(LIBFLOXPKGDB) $(CXX) $(CXXFLAGS) $(LDFLAGS) "$<" -o "$@" @@ -260,17 +261,20 @@ cc-check: $(TESTS:.cc=) done; \ exit "$$_ec" -bats-check: bin tests/is_sqlite3 +bats-check: bin $(TEST_UTILS) PKGDB="$(MAKEFILE_DIR)/bin/pkgdb" \ + ID2ATTRPATH="$(MAKEFILE_DIR)/tests/id2attrpath" \ IS_SQLITE3="$(MAKEFILE_DIR)/tests/is_sqlite3" \ bats --print-output-on-failure --verbose-run --timing \ "$(MAKEFILE_DIR)/tests" + # ---------------------------------------------------------------------------- # all: bin lib tests ignores most: bin lib ignores +test-utils: $(TEST_UTILS) # ---------------------------------------------------------------------------- # diff --git a/tests/.gitignore b/tests/.gitignore index e3a41f6b..4bbd81cf 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,3 +1,4 @@ +id2attrpath is_sqlite3 pkgdb semver diff --git a/tests/id2attrpath.bats b/tests/id2attrpath.bats new file mode 100644 index 00000000..49e63116 --- /dev/null +++ b/tests/id2attrpath.bats @@ -0,0 +1,51 @@ +#! /usr/bin/env bats +# -*- mode: bats; -*- +# ============================================================================ # +# +# `id2attrpath' executable tests. +# +# ---------------------------------------------------------------------------- # + +load setup_suite.bash; + +# bats file_tags=id2attrpath + + +# ---------------------------------------------------------------------------- # + +setup_file() { + export DBPATH="$BATS_FILE_TMPDIR/test-cli.sqlite"; + mkdir -p "$BATS_FILE_TMPDIR"; + # We don't parallelize these to avoid DB sync headaches and to recycle the + # cache between tests. + # Nonetheless this file makes an effort to avoid depending on past state in + # such a way that would make it difficult to eventually parallelize in + # the future. + export BATS_NO_PARALLELIZE_WITHIN_FILE=true; + export TEST_HARNESS_FLAKE="$TESTS_DIR/harnesses/proj0"; + # Load a database to query during testing + "$PKGDB" scrape --database "$DBPATH" "$TEST_HARNESS_FLAKE" packages "$NIX_SYSTEM"; +} + + +# ---------------------------------------------------------------------------- # + +@test "retrieves existing attrset" { + run "$ID2ATTRPATH" "$DBPATH" 2; + assert_output "packages $NIX_SYSTEM"; +} + + +# ---------------------------------------------------------------------------- # + +@test "error on nonexistent attrset" { + run "$ID2ATTRPATH" "$DBPATH" 3; + assert_failure; +} + + +# ---------------------------------------------------------------------------- # +# +# +# +# ============================================================================ # diff --git a/tests/id2attrpath.cc b/tests/id2attrpath.cc new file mode 100644 index 00000000..a656a574 --- /dev/null +++ b/tests/id2attrpath.cc @@ -0,0 +1,80 @@ +/* ========================================================================== * + * + * @file tests/id2attrpath.cc + * + * @brief Minimal executable to return the attribute path given an attrset id. + * + * + * -------------------------------------------------------------------------- */ + +#include +#include +#include + +#include "flox/util.hh" +#include "pkgdb.hh" + + +/* -------------------------------------------------------------------------- */ + +int main(int argc, char *argv[]) +{ + if (argc < 2) + { + std::cout << "usage: id2attrpath SOURCE ID" << std::endl; + return EXIT_FAILURE; + } + + std::string source( argv[1] ); + std::string idStr( argv[2] ); + + /* Attempt to parse the id to an int */ + flox::pkgdb::row_id id; + try + { + id = std::stoull( idStr ); + } + catch( const std::exception & e ) + { + std::cerr << "Couldn't convert 'id' to 'int'" << std::endl; + std::cerr << e.what() << std::endl; + return EXIT_FAILURE; + } + + /* Act on the source (database vs. flake ref) */ + if ( flox::isSQLiteDb( source ) ) + { + flox::pkgdb::PkgDb db = flox::pkgdb::PkgDb( source ); + flox::AttrPath attrPath; + /* The user-provided id may not be in the database */ + try + { + attrPath = db.getAttrSetPath( id ); + } + catch ( const std::exception & e ) + { + std::cerr << "Failed to retrieve attrpath with id " << id << std::endl; + std::cerr << e.what() << std::endl; + return EXIT_FAILURE; + } + + for (unsigned long i = 0; i < attrPath.size() - 1; i++) + { + std::cout << attrPath[i] << " "; + } + std::cout << attrPath[attrPath.size() - 1] << std::endl; + } + else + { + std::cout << "flake references aren't a supported source yet" + << std::endl; + return EXIT_FAILURE; + } +} + + +/* -------------------------------------------------------------------------- * + * + * + * + * ========================================================================== */