Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Add attr path lookup helper
Browse files Browse the repository at this point in the history
  • Loading branch information
zmitchell committed Aug 11, 2023
1 parent 9d7205b commit 0af6603
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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=))


# ---------------------------------------------------------------------------- #
Expand Down Expand Up @@ -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 "$@"


Expand Down Expand Up @@ -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)


# ---------------------------------------------------------------------------- #
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id2attrpath
is_sqlite3
pkgdb
semver
51 changes: 51 additions & 0 deletions tests/id2attrpath.bats
Original file line number Diff line number Diff line change
@@ -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;
}


# ---------------------------------------------------------------------------- #
#
#
#
# ============================================================================ #
80 changes: 80 additions & 0 deletions tests/id2attrpath.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* ========================================================================== *
*
* @file tests/id2attrpath.cc
*
* @brief Minimal executable to return the attribute path given an attrset id.
*
*
* -------------------------------------------------------------------------- */

#include <iostream>
#include <string>
#include <cstdlib>

#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;
}
}


/* -------------------------------------------------------------------------- *
*
*
*
* ========================================================================== */

0 comments on commit 0af6603

Please sign in to comment.