notcurses-tester: check validity of datadir on start #1168

This commit is contained in:
nick black 2020-12-02 14:00:30 -05:00
parent 2c85142b2a
commit 79b9a8120e
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

View File

@ -8,6 +8,7 @@
#include <iostream> #include <iostream>
#include <climits> #include <climits>
#include <termios.h> #include <termios.h>
#include <sys/stat.h>
#include <filesystem> #include <filesystem>
#include <langinfo.h> #include <langinfo.h>
@ -42,6 +43,25 @@ handle_opts(const char** argv){
} }
} }
// check that the (provided or default) data directory exists, and has at
// least one of our necessary files. otherwise, print a warning + return error.
static int
check_data_dir(){
auto p = find_data("changes.jpg");
if(!p){
std::cerr << "Coudln't find testing data! Supply directory with -p." << std::endl;
return -1;
}
struct stat s;
if(stat(p, &s)){
std::cerr << "Couldn't open " << p << ". Supply directory with -p." << std::endl;
free(p);
return -1;
}
free(p);
return 0;
}
// reset the terminal in the event of early exit (notcurses_init() presumably // reset the terminal in the event of early exit (notcurses_init() presumably
// ran, but we don't have the notcurses struct to destroy, so just do it raw). // ran, but we don't have the notcurses struct to destroy, so just do it raw).
static void static void
@ -108,6 +128,9 @@ auto main(int argc, const char **argv) -> int {
context.setOption("no-breaks", true); // don't break in the debugger when assertions fail context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
dt_removed args(argv); dt_removed args(argv);
handle_opts(argv); handle_opts(argv);
if(check_data_dir()){
return EXIT_FAILURE;
}
int res = context.run(); // run int res = context.run(); // run
if(context.shouldExit()){ // important - query flags (and --exit) rely on the user doing this if(context.shouldExit()){ // important - query flags (and --exit) rely on the user doing this
return res; // propagate the result of the tests return res; // propagate the result of the tests