aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-11-16 18:05:57 +0200
committergramanas <anastasis.gramm2@gmail.com>2018-11-16 18:05:57 +0200
commit8eb4df24eb30353bea82268440396e50ed8b1bbc (patch)
treec885ab94952315f64bd596512cd23d0ef5509b3e
parent2919a5e2c1d33d170bffc93dfeaa6292b9fa3dca (diff)
downloadck-8eb4df24eb30353bea82268440396e50ed8b1bbc.tar.gz
ck-8eb4df24eb30353bea82268440396e50ed8b1bbc.tar.bz2
ck-8eb4df24eb30353bea82268440396e50ed8b1bbc.zip
Fix bugs in test-ck and simplify clparser
-rwxr-xr-xres/test-ck50
-rw-r--r--src/actions.c2
-rw-r--r--src/actions.h4
-rw-r--r--src/ck.c2
-rw-r--r--src/clparser.c126
-rw-r--r--src/clparser.h23
-rw-r--r--src/restore.c1
-rw-r--r--test/05_restore4
8 files changed, 68 insertions, 144 deletions
diff --git a/res/test-ck b/res/test-ck
index a7a7cf8..35837fa 100755
--- a/res/test-ck
+++ b/res/test-ck
@@ -3,7 +3,9 @@
BIN=$(realpath @CMAKE_BINARY_DIR@) # solve symlink problems
TEST_LOCATION=$(realpath @PROJECT_TESTING_GROUNDS@)
V=3
-
+FILTER=""
+REG=1
+UNIT=1
# used in regression tests
function running {
# open devnul in fd 3
@@ -52,15 +54,19 @@ function unit_tests {
}
function regression_tests {
- echo -e "Regression Tests:"
+ FL=""
+ if [ "$FILTER" != "" ]; then
+ FL=" (Using filter: *$FILTER*)"
+ fi
+ echo -e "Regression Tests:$FL"
echo -e "~~~~~~~~~~~~~~~~~"
DIR=@BIN_TEST_DIR@
COUNT=1
- TOTAL=$(ls $DIR | wc -l)
- for i in $(ls $DIR); do
- ERROR="TEST "$i" FAILED:"
- PASS="=> $i passed\n"
- source $DIR/$i
+ TOTAL=$(ls $DIR/*$FILTER* | wc -l)
+ for i in $(ls $DIR/*$FILTER*); do
+ ERROR="TEST "`basename $i`" FAILED:"
+ PASS="=> `basename $i` passed\n"
+ source $i
wait $!
done
}
@@ -72,8 +78,16 @@ function err {
}
function run {
- unit_tests
- regression_tests
+ if [ $UNIT -eq 1 ]; then
+ unit_tests
+ fi
+
+ if [ $REG -eq 1 ]; then
+ regression_tests
+ elif [ $REG -eq $UNIT ]; then
+ unit_tests
+ regression_tests
+ fi
}
function print_help {
@@ -82,6 +96,7 @@ function print_help {
echo -e "\nflags:"
echo -e " -u, --unit\t\trun only the unit tests"
echo -e " -r, --regression\trun only the regression tests"
+ echo -e " -f, --filter\trun regression test matching the argument"
echo -e " -c, --clear\t\tremove test files"
echo -e " \t\t use if the tests crush unexpectedly"
echo -e " -v, --verbose\t\tverbose test output"
@@ -89,10 +104,6 @@ function print_help {
exit
}
-if [[ $# -gt 1 ]]; then
- print_help
-fi
-
while [[ $# -gt 0 ]]
do
key="$1"
@@ -102,12 +113,17 @@ do
exit
;;
-u | --unit)
- unit_tests
- exit
+ REG=0;
+ shift
;;
+ -f | --filter)
+ FILTER=$2;
+ shift
+ shift
+ ;;
-r | --regression)
- regression_tests
- exit
+ UNIT=0;
+ shift
;;
-v | --verbose)
set_verbose
diff --git a/src/actions.c b/src/actions.c
index c977aee..3293e53 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -292,7 +292,7 @@ int run_HELP(UserOpt *opt, Conf *conf) {
}
switch(parser_get_action(list_get(opt->args), NULL)) {
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
case CKA_##ACTION: \
HELP("%s", get_possible_action_strings(tmp, CKA_##ACTION)); \
print_##ACTION##_help(); \
diff --git a/src/actions.h b/src/actions.h
index 2015538..4550d4c 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -20,7 +20,7 @@
#include "clparser.h"
#include "confparser.h"
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
int run_##ACTION(UserOpt *, Conf *);
CK_ACTIONS
#undef X
@@ -84,7 +84,7 @@ int restore_make_links(cklist *from, cklist *to);
/************************/
/* PRINT RESULTS & HELP */
/************************/
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
void print_##ACTION##_help(void);
CK_ACTIONS
#undef X
diff --git a/src/ck.c b/src/ck.c
index 1be7298..4126e6d 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -65,7 +65,7 @@ int main(int argc, const char **argv) {
/* Run action and print results */
switch(opt.action) {
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
case CKA_##ACTION: \
rc = run_##ACTION(&opt, &conf); \
break;
diff --git a/src/clparser.c b/src/clparser.c
index ba73db0..081c981 100644
--- a/src/clparser.c
+++ b/src/clparser.c
@@ -68,113 +68,19 @@ static void fill_args_list(int arg_num, UserOpt *opt) {
}
}
-/* When starting to parse the action,
- * `pos` should be at 2
- * like so "ck ACTION ..."
- * ^ */
-static int parse_INIT(UserOpt *opt) {
- /* INIT expects 2 arguments
- * starting from 0 */
- int arg_num = 2;
- if (optNum != pos /* already consumed */ + arg_num) {
- opt->err = PERR_INIT_WRONG;
- return -1;
- }
-
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_ADD(UserOpt *opt) {
- /* ADD expects 2 to 4 arguments */
- if (optNum < pos + 2
- || optNum > pos + 4) {
- opt->err = PERR_ADD_WRONG;
- return -1;
- }
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_DEL(UserOpt *opt) {
- /* DEL expects 1 to 2 arguments */
- if (optNum < pos + 1
- || optNum > pos + 2) {
- opt->err = PERR_DEL_WRONG;
- return -1;
- }
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_EDIT(UserOpt *opt) {
- /* EDIT expects 1 to 2 arguments */
- if (optNum < pos + 1
- || optNum > pos + 2) {
- opt->err = PERR_EDIT_WRONG;
- return -1;
+#define X(ACTION, MIN, MAX) \
+ static int parse_ ##ACTION(UserOpt *opt) { \
+ if (optNum < pos + MIN \
+ || optNum > pos + MAX) { \
+ opt->err = PERR_ ##ACTION## _WRONG; \
+ return -1; \
+ } \
+ int arg_num = optNum - pos; \
+ fill_args_list(arg_num, opt); \
+ return 0; \
}
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_LIST(UserOpt *opt) {
- /* List expects 1 to 6 arguments */
- if (optNum < pos + 1
- || optNum > pos + 6) {
- opt->err = PERR_LIST_WRONG;
- return -1;
- }
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_SEARCH(UserOpt *opt) {
- /* Search expects a maximum of 1 argument */
- if (optNum < pos + 1
- || optNum > pos + 1) {
- opt->err = PERR_SEARCH_WRONG;
- return -1;
- }
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_HELP(UserOpt *opt) {
- /* Help expects a maximum of 1 argument */
- if (optNum < pos + 1
- || optNum > pos + 1) {
- opt->err = PERR_HELP_WRONG;
- return -1;
- }
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
-
-static int parse_RESTORE(UserOpt *opt) {
- /* Restore expects 1 to 2 arguments */
- if (optNum < pos + 1
- || optNum > pos + 2) {
- opt->err = PERR_RESTORE_WRONG;
- return -1;
- }
-
- int arg_num = optNum - pos;
- fill_args_list(arg_num, opt);
- return 0;
-}
+CK_ACTIONS
+#undef X
static void determine_action(UserOpt *opt) {
/* get action */
@@ -200,7 +106,7 @@ static int parse_vals(UserOpt *opt) {
}
switch (opt->action) {
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
case CKA_##ACTION: \
return parse_##ACTION(opt);
CK_ACTIONS
@@ -212,7 +118,7 @@ static int parse_vals(UserOpt *opt) {
CkAction parser_get_action(const char *name, char *actionName) {
int i;
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \
if (strcmp(name, str##ACTION[i]) == 0) { \
if (actionName) { \
@@ -320,7 +226,7 @@ static void verbose() {
char * get_possible_action_strings(char *dest, CkAction ckAction) {
char buf[STR_M] = "";
switch (ckAction) {
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
case CKA_##ACTION: \
strcpy(buf, "{ "); \
for (int i = 1; i < atoi(str##ACTION[0]); i++) { \
@@ -351,7 +257,7 @@ static void print_parser_error(UserOpt *opt) {
case PERR_UNKNOWN_ACTION:
ERR("Unknown action: %s", token);
return;
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
case PERR_ ##ACTION## _WRONG: \
HELP("Usage:\n%s", names); \
print_##ACTION##_help(); \
diff --git a/src/clparser.h b/src/clparser.h
index 9b22b87..f6818fa 100644
--- a/src/clparser.h
+++ b/src/clparser.h
@@ -20,19 +20,20 @@
#include "cklist.h"
-#define CK_ACTIONS \
- X(INIT) \
- X(ADD) \
- X(DEL) \
- X(EDIT) \
- X(LIST) \
- X(SEARCH) \
- X(RESTORE) \
- X(HELP)
+/* NAME | MIN ACCEPTED ARGS | MAX ACCEPTED ARGS */
+#define CK_ACTIONS \
+ X(INIT, 2 , 2) \
+ X(ADD, 2, 4) \
+ X(DEL, 1, 2) \
+ X(EDIT, 1, 2) \
+ X(LIST, 1, 6) \
+ X(SEARCH, 1, 1) \
+ X(RESTORE, 1, 2) \
+ X(HELP, 1, 1)
enum ParseErrors {
PERR_NOERR = 0,
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
PERR_##ACTION##_WRONG,
CK_ACTIONS
#undef X
@@ -42,7 +43,7 @@ typedef enum ParseErrors ParseError;
enum CkActions {
CK_WRONG_ACTION,
-#define X(ACTION) \
+#define X(ACTION, MIN, MAX) \
CKA_##ACTION,
CK_ACTIONS
#undef X
diff --git a/src/restore.c b/src/restore.c
index c90cb82..cb8e6c9 100644
--- a/src/restore.c
+++ b/src/restore.c
@@ -14,6 +14,7 @@ int restore_make_links(cklist *from, cklist *to) {
&& list_size(to) > 0
&& list_size(from) == list_size(to)) {
do {
+ HELP("If %d OR %d then file exists.", util_file_exists(list_get(to), NULL), !util_is_file_link(list_get(to)));
if (util_file_exists(list_get(to), NULL)
|| !util_is_file_link(list_get(to))) {
ERR("File %s already exists.", list_get(to));
diff --git a/test/05_restore b/test/05_restore
index 013e252..2d60cb9 100644
--- a/test/05_restore
+++ b/test/05_restore
@@ -15,7 +15,7 @@ add_config prog2 $path3
add_config prog2 $path4
# delete prog1 links
-rm $path1 $path2
+rm "$path1" "$path2"
# restore them
exec $BIN/ck -c $BIN restore -p prog1 >&${V} &
@@ -29,7 +29,7 @@ for i in $($BIN/ck -c $BIN list -p prog1); do
done
## delete all links
-rm $path1 $path2 $path3 $path4
+rm "$path1" "$path2" "$path3" "$path4"
# restore all
exec $BIN/ck -c $BIN restore all >&${V} &