From 5f0f6e036f5543dc24cb14b6e796b4ff3ea08261 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 7 Jul 2017 08:39:26 +0200 Subject: [PATCH] Add option --only-data-dir (-x) Only use the dictionaries in data-dir, do not search in user and system directories This makes testing much easier --- CMakeLists.txt | 1 + src/sdcv.cpp | 14 +++++++++----- tests/t_only_data_dir | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100755 tests/t_only_data_dir diff --git a/CMakeLists.txt b/CMakeLists.txt index eaf8048..69d9705 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,6 +144,7 @@ if (BUILD_TESTS) add_sdcv_shell_test(t_list) add_sdcv_shell_test(t_use) + add_sdcv_shell_test(t_only_data_dir) add_sdcv_shell_test(t_synonyms) add_sdcv_shell_test(t_interactive) add_sdcv_shell_test(t_utf8output) diff --git a/src/sdcv.cpp b/src/sdcv.cpp index d7f08e9..ffee977 100644 --- a/src/sdcv.cpp +++ b/src/sdcv.cpp @@ -78,6 +78,7 @@ int main(int argc, char *argv[]) try { gboolean utf8_output = FALSE; gboolean utf8_input = FALSE; glib::CharStr opt_data_dir; + gboolean only_data_dir = FALSE; gboolean colorize = FALSE; const GOptionEntry entries[] = { @@ -97,6 +98,8 @@ int main(int argc, char *argv[]) try { { "data-dir", '2', 0, G_OPTION_ARG_STRING, get_addr(opt_data_dir), _("use this directory as path to stardict data directory"), _("path/to/dir") }, + { "only-data-dir", 'x', 0, G_OPTION_ARG_NONE, &only_data_dir, + _("only use the dictionaries in data-dir, do not search in user and system directories"), nullptr }, { "color", 'c', 0, G_OPTION_ARG_NONE, &colorize, _("colorize the output"), nullptr }, {}, @@ -122,10 +125,12 @@ int main(int argc, char *argv[]) try { const gchar *stardict_data_dir = g_getenv("STARDICT_DATA_DIR"); std::string data_dir; if (!opt_data_dir) { + if (!only_data_dir) { if (stardict_data_dir) data_dir = stardict_data_dir; else data_dir = "/usr/share/stardict/dic"; + } } else { data_dir = get_impl(opt_data_dir); } @@ -134,11 +139,10 @@ int main(int argc, char *argv[]) try { if (!homedir) homedir = g_get_home_dir(); - const std::list dicts_dir_list = { - std::string(homedir) + G_DIR_SEPARATOR + ".stardict" + G_DIR_SEPARATOR + "dic", - data_dir - }; - + std::list dicts_dir_list; + if(!only_data_dir) + dicts_dir_list.push_back(std::string(homedir) + G_DIR_SEPARATOR + ".stardict" + G_DIR_SEPARATOR + "dic"); + dicts_dir_list.push_back(data_dir); if (show_list_dicts) { list_dicts(dicts_dir_list); return EXIT_SUCCESS; diff --git a/tests/t_only_data_dir b/tests/t_only_data_dir new file mode 100755 index 0000000..392f056 --- /dev/null +++ b/tests/t_only_data_dir @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +SDCV="$1" +TEST_DIR="$2" + +unset SDCV_PAGER +unset STARDICT_DATA_DIR + +DICTS=$($SDCV -x -n -l --data-dir "$TEST_DIR" | tail -n +2 | wc -l) +# the expected result: +ACTUAL_DICTS=$(find "$TEST_DIR" -name "*.ifo" | wc -l) +if [ $DICTS -ne $ACTUAL_DICTS ]; then + echo "number of dictionaries in $TEST_DIR should be $ACTUAL_DICTS but was $DICTS according to sdcv" + exit 1 +fi + +exit 0