git-prompt.sh: make $f local to __git_eread()
[gitweb.git] / contrib / mw-to-git / t / test-gitmw-lib.sh
index 47341f76bce9eac238f92d666230406d62c672af..3372b2af346ac23a73a7723ac6d0c81fb8c5e705 100755 (executable)
@@ -13,6 +13,7 @@
 
 . ./test.config
 
+WIKI_URL=http://"$SERVER_ADDR:$PORT/$WIKI_DIR_NAME"
 CURR_DIR=$(pwd)
 TEST_OUTPUT_DIRECTORY=$(pwd)
 TEST_DIRECTORY="$CURR_DIR"/../../../t
@@ -25,6 +26,147 @@ else
        WIKI_DIR_INST="$CURR_DIR/$WEB_WWW"
 fi
 
+wiki_upload_file () {
+       "$CURR_DIR"/test-gitmw.pl upload_file "$@"
+}
+
+wiki_getpage () {
+       "$CURR_DIR"/test-gitmw.pl get_page "$@"
+}
+
+wiki_delete_page () {
+       "$CURR_DIR"/test-gitmw.pl delete_page "$@"
+}
+
+wiki_editpage () {
+       "$CURR_DIR"/test-gitmw.pl edit_page "$@"
+}
+
+die () {
+       die_with_status 1 "$@"
+}
+
+die_with_status () {
+       status=$1
+       shift
+       echo >&2 "$*"
+       exit "$status"
+}
+
+
+# Check the preconditions to run git-remote-mediawiki's tests
+test_check_precond () {
+       if ! test_have_prereq PERL
+       then
+               skip_all='skipping gateway git-mw tests, perl not available'
+               test_done
+       fi
+
+       GIT_EXEC_PATH=$(cd "$(dirname "$0")" && cd "../.." && pwd)
+       PATH="$GIT_EXEC_PATH"'/bin-wrapper:'"$PATH"
+
+       if [ ! -d "$WIKI_DIR_INST/$WIKI_DIR_NAME" ];
+       then
+               skip_all='skipping gateway git-mw tests, no mediawiki found'
+               test_done
+       fi
+}
+
+# test_diff_directories <dir_git> <dir_wiki>
+#
+# Compare the contents of directories <dir_git> and <dir_wiki> with diff
+# and errors if they do not match. The program will
+# not look into .git in the process.
+# Warning: the first argument MUST be the directory containing the git data
+test_diff_directories () {
+       rm -rf "$1_tmp"
+       mkdir -p "$1_tmp"
+       cp "$1"/*.mw "$1_tmp"
+       diff -r -b "$1_tmp" "$2"
+}
+
+# $1=<dir>
+# $2=<N>
+#
+# Check that <dir> contains exactly <N> files
+test_contains_N_files () {
+       if test `ls -- "$1" | wc -l` -ne "$2"; then
+               echo "directory $1 should contain $2 files"
+               echo "it contains these files:"
+               ls "$1"
+               false
+       fi
+}
+
+
+# wiki_check_content <file_name> <page_name>
+#
+# Compares the contents of the file <file_name> and the wiki page
+# <page_name> and exits with error 1 if they do not match.
+wiki_check_content () {
+       mkdir -p wiki_tmp
+       wiki_getpage "$2" wiki_tmp
+       # replacement of forbidden character in file name
+       page_name=$(printf "%s\n" "$2" | sed -e "s/\//%2F/g")
+
+       diff -b "$1" wiki_tmp/"$page_name".mw
+       if test $? -ne 0
+       then
+               rm -rf wiki_tmp
+               error "ERROR: file $2 not found on wiki"
+       fi
+       rm -rf wiki_tmp
+}
+
+# wiki_page_exist <page_name>
+#
+# Check the existence of the page <page_name> on the wiki and exits
+# with error if it is absent from it.
+wiki_page_exist () {
+       mkdir -p wiki_tmp
+       wiki_getpage "$1" wiki_tmp
+       page_name=$(printf "%s\n" "$1" | sed "s/\//%2F/g")
+       if test -f wiki_tmp/"$page_name".mw ; then
+               rm -rf wiki_tmp
+       else
+               rm -rf wiki_tmp
+               error "test failed: file $1 not found on wiki"
+       fi
+}
+
+# wiki_getallpagename
+#
+# Fetch the name of each page on the wiki.
+wiki_getallpagename () {
+       "$CURR_DIR"/test-gitmw.pl getallpagename
+}
+
+# wiki_getallpagecategory <category>
+#
+# Fetch the name of each page belonging to <category> on the wiki.
+wiki_getallpagecategory () {
+       "$CURR_DIR"/test-gitmw.pl getallpagename "$@"
+}
+
+# wiki_getallpage <dest_dir> [<category>]
+#
+# Fetch all the pages from the wiki and place them in the directory
+# <dest_dir>.
+# If <category> is define, then wiki_getallpage fetch the pages included
+# in <category>.
+wiki_getallpage () {
+       if test -z "$2";
+       then
+               wiki_getallpagename
+       else
+               wiki_getallpagecategory "$2"
+       fi
+       mkdir -p "$1"
+       while read -r line; do
+               wiki_getpage "$line" $1;
+       done < all.txt
+}
+
 # ================= Install part =================
 
 error () {
@@ -190,20 +332,21 @@ wiki_install () {
        fi
 
        # Fetch MediaWiki's archive if not already present in the TMP directory
+       MW_FILENAME="mediawiki-$MW_VERSION_MAJOR.$MW_VERSION_MINOR.tar.gz"
        cd "$TMP"
-       if [ ! -f "$MW_VERSION.tar.gz" ] ; then
-               echo "Downloading $MW_VERSION sources ..."
-               wget "http://download.wikimedia.org/mediawiki/1.19/mediawiki-1.19.0.tar.gz" ||
+       if [ ! -f $MW_FILENAME ] ; then
+               echo "Downloading $MW_VERSION_MAJOR.$MW_VERSION_MINOR sources ..."
+               wget "http://download.wikimedia.org/mediawiki/$MW_VERSION_MAJOR/$MW_FILENAME" ||
                        error "Unable to download "\
-                       "http://download.wikimedia.org/mediawiki/1.19/"\
-                       "mediawiki-1.19.0.tar.gz. "\
+                       "http://download.wikimedia.org/mediawiki/$MW_VERSION_MAJOR/"\
+                       "$MW_FILENAME. "\
                        "Please fix your connection and launch the script again."
-               echo "$MW_VERSION.tar.gz downloaded in `pwd`. "\
+               echo "$MW_FILENAME downloaded in `pwd`. "\
                        "You can delete it later if you want."
        else
-               echo "Reusing existing $MW_VERSION.tar.gz downloaded in `pwd`."
+               echo "Reusing existing $MW_FILENAME downloaded in `pwd`."
        fi
-       archive_abs_path=$(pwd)/"$MW_VERSION.tar.gz"
+       archive_abs_path=$(pwd)/$MW_FILENAME
        cd "$WIKI_DIR_INST/$WIKI_DIR_NAME/" ||
                error "can't cd to $WIKI_DIR_INST/$WIKI_DIR_NAME/"
        tar xzf "$archive_abs_path" --strip-components=1 ||
@@ -285,5 +428,5 @@ wiki_delete () {
        # Delete the wiki's SQLite database
        rm -f "$TMP/$DB_FILE" || error "Database $TMP/$DB_FILE could not be deleted."
        rm -f "$FILES_FOLDER/$DB_FILE"
-       rm -rf "$TMP/$MW_VERSION"
+       rm -rf "$TMP/mediawiki-$MW_VERSION_MAJOR.$MW_VERSION_MINOR.tar.gz"
 }