From mboxrd@z Thu Jan 1 00:00:00 1970 From: Simon Cathebras Subject: [PATCH 2/3] Test environment of git-remote-mw Date: Wed, 30 May 2012 19:04:51 +0200 Message-ID: <1338397492-13360-2-git-send-email-simon.cathebras@ensimag.imag.fr> References: <4FC64B0C.6070507@ensimag.imag.fr> <1338397492-13360-1-git-send-email-simon.cathebras@ensimag.imag.fr> Cc: Matthieu.Moy@imag.fr, simon.cathebras@ensimag.imag.fr, charles.roussel@ensimag.imag.fr, Guillaume.Sasdy@ensimag.imag.fr, Julien.Khayat@ensimag.imag.fr, Simon.Perrat@ensimag.imag.fr, Charles Roussel , Julien Khayat , Simon Perrat , Guillaume Sasdy To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Wed May 30 19:05:11 2012 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SZmKY-0005Tg-NJ for gcvg-git-2@plane.gmane.org; Wed, 30 May 2012 19:05:07 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753260Ab2E3RFA (ORCPT ); Wed, 30 May 2012 13:05:00 -0400 Received: from mx1.imag.fr ([129.88.30.5]:39727 "EHLO shiva.imag.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751194Ab2E3RE7 (ORCPT ); Wed, 30 May 2012 13:04:59 -0400 Received: from ensimag.imag.fr (ensimag.imag.fr [195.221.228.12]) by shiva.imag.fr (8.13.8/8.13.8) with ESMTP id q4UGuaCG004663 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 30 May 2012 18:56:36 +0200 Received: from Hithlum.grenet.fr (ensibm [195.221.228.8]) by ensimag.imag.fr (8.13.8/8.13.8/ImagV2.1.r_ens) with ESMTP id q4UH4qWi014805; Wed, 30 May 2012 19:04:54 +0200 X-Mailer: git-send-email 1.7.10.2.552.gaa3bb87 In-Reply-To: <1338397492-13360-1-git-send-email-simon.cathebras@ensimag.imag.fr> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0.1 (shiva.imag.fr [129.88.30.5]); Wed, 30 May 2012 18:56:36 +0200 (CEST) X-IMAG-MailScanner-Information: Please contact MI2S MIM for more information X-MailScanner-ID: q4UGuaCG004663 X-IMAG-MailScanner: Found to be clean X-IMAG-MailScanner-SpamCheck: X-IMAG-MailScanner-From: simon.cathebras@ensimag.imag.fr MailScanner-NULL-Check: 1339001796.38282@sBv3nxCsp3oHWi3GCL/bsA Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: From: Charles Roussel In order to test git-remote-mediawiki, we need a package of functions to manage a MediaWiki: edit a page, remove a page, fetch a page, fetch all pages on a given wiki. We also need functions to compare the content of directories. Signed-off-by: Simon Cathebras Signed-off-by: Charles Roussel Signed-off-by: Julien Khayat Signed-off-by: Simon Perrat Signed-off-by: Guillaume Sasdy --- t/test-gitmw-lib.sh | 125 ++++++++++++++++++++++++++++++++++++--- t/test-gitmw.pl | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 281 insertions(+), 8 deletions(-) create mode 100755 t/test-gitmw.pl diff --git a/t/test-gitmw-lib.sh b/t/test-gitmw-lib.sh index 2deead7..8698625 100755 --- a/t/test-gitmw-lib.sh +++ b/t/test-gitmw-lib.sh @@ -1,27 +1,136 @@ +# Copyright (C) 2012 +# Charles Roussel +# Simon Cathebras +# Julien Khayat +# Guillaume Sasdy +# Simon Perrat +# Matthieu Moy +# License: GPL v2 or later + # # CONFIGURATION VARIABLES -# You might want to change those ones ... +# You might want to change these ones # WIKI_DIR_NAME="wiki" # Name of the wiki's directory WIKI_DIR_INST="/var/www" # Directory of the web server TMP="/tmp" # Temporary directory for downloads - # Absolute address needed! + # Absolute path required! SERVER_ADDR="localhost" # Web server's address -# # CONFIGURATION -# You should not change those ones unless you know what you to +# You should not change these ones unless you know what you do # -# Do not change the variables below MW_VERSION="mediawiki-1.19.0" DB_FILE="wikidb.sqlite" FILES_FOLDER="install-wiki" WIKI_ADMIN="WikiAdmin" WIKI_PASSW="AdminPass" -fail () { - echo "$1" - exit 1 +# wiki_getpage +# +# Fetch a page from the wiki and copy its content +# into directory . +wiki_getpage () { + ../test-gitmw.pl "get_page" -p "$1" "$2" +} + +# wiki_delete_page +# +# Delete the page on the wiki. +wiki_delete_page () { + ../test-gitmw.pl "delete_page" -p "$1" +} + +# wiki_edit_page +# +# Edit a page on the wiki with content +# If == true the content will be appended +# If the page doesn't exist, it is created. +wiki_editpage () { + ../test-gitmw.pl "edit_page" -p "$1" "$2" "$3" +} + +# git_content +# +# Compare the contents of directories and with diff +# and exits with error 1 if they do not match. The program will +# not look into .git in the process. +git_content () { + result=$(diff -r -B -w --exclude=".git" "$1" "$2") + + if echo $result | grep -q ">" ; then + echo "test failed: files $1 and $2 do not match" + exit 1 + fi +} + +# git_exist +# +# Check the existence of into the git repository +# and exits with error 1 if it is absent. +git_exist () { + result=$(find "$1" -type f -name "$2") + + if ! echo $result | grep -q "$2" ; then + echo "test failed: file $2 does not exist in $1" + exit 1 + fi +} + +# wiki_page_content +# +# Compares the contents of the file and the wiki page +# and exits with error 1 if they do not match. +wiki_page_content () { + wiki_getpage "$2" . + + if test -f "$2".mw ; then + git_content "$1" "$2".mw + rm "$2".mw + else + die "ERROR: file $2 not found on wiki" + fi +} + +# wiki_page_exist +# +# Check the existence of the page on the wiki and exits +# with error if it is absent from it. +wiki_page_exist () { + wiki_getpage "$1" . + + if test -f "$1".mw ; then + echo "test failed: file $1 not found on wiki" + exit 1 + else + rm "$1".mw + fi +} + +# wiki_getallpagename +# +# Fetch the name of each page on the wiki. +wiki_getallpagename () { + ../test-gitmw.pl "getallpagename" -p +} + +# wiki_getallpage +# +# Fetch all the pages from the wiki and place them in the directory +# . +wiki_getallpage () { + wiki_getallpagename + mkdir $1 + while read -r line; do + wiki_getpage "$line" $1; + done < all.txt + #rm all.txt +} + +fail() +{ + echo $1 + exit 1 } diff --git a/t/test-gitmw.pl b/t/test-gitmw.pl new file mode 100755 index 0000000..e9c50f9 --- /dev/null +++ b/t/test-gitmw.pl @@ -0,0 +1,164 @@ +#!/usr/bin/perl -w +# Copyright (C) 2012 +# Charles Roussel +# Simon Cathebras +# Julien Khayat +# Guillaume Sasdy +# Simon Perrat +# Matthieu Moy +# License: GPL v2 or later + +# Usage: +# ./test-gitmw.pl [argument]* +# Execute in terminal using the name of the function to call as first +# parameter, and the function's arguments as following parameters +# +# Example: +# ./test-gitmw.pl "get_page" foo . +# will call with arguments and <.> +# +# Available functions are: +# "get_page" +# "delete_page" +# "edit_page" +# "getallpagename" + +use MediaWiki::API; +use Switch; +# URL of the wiki used for the tests +my $wiki_url="http://localhost/wiki/api.php"; +my $wiki_admin='WikiAdmin'; +my $wiki_admin_pass='AdminPass'; +my $mw = MediaWiki::API->new; +$mw->{config}->{api_url} = $wiki_url; + +# wiki_login +# +# Logs the user with and in the global variable +# of the mediawiki $mw +sub wiki_login { + $mw->login( { lgname => "$_[0]",lgpassword => "$_[1]" } ) + || die "getpage: login failed"; +} + +# wiki_getpage +# +# fetch a page from the wiki referenced in the global variable +# $mw and copies its content in directory dest_path +sub wiki_getpage { + my $pagename = $_[0]; + my $destdir = $_[1]; + + my $page = $mw->get_page( { title => $pagename } ); + if (!defined($page)) { + die "getpage: wiki does not exist"; + } + + my $content = $page->{'*'}; + if (!defined($content)) { + die "getpage: page does not exist"; + } + + # Replace spaces by underscore in the page name + $pagename=~s/\ /_/; + open(my $file, ">$destdir/$pagename.mw"); + print $file "$content"; + close ($file); +} + +# wiki_delete_page +# +# delete the page with name from the wiki referenced +# in the global variable $mw +sub wiki_delete_page { + my $pagename = $_[0]; + + my $exist=$mw->get_page({title => $pagename}); + + if (defined($exist->{'*'})){ + $mw->edit({ action => 'delete', + title => $pagename}) + || die $mw->{error}->{code} . ": " . $mw->{error}->{details}; + } + else{ + die "no page with such name found: $pagename\n"; + } +} + +# wiki_editpage +# +# Edit a page named with content on the wiki +# referenced with the global variable $mw +# If == true : append at the end of the actual +# content of the page +# If doesn't exist, that page is created with the +sub wiki_editpage { + my $wiki_page = $_[0]; + my $wiki_content = $_[1]; + my $wiki_append = $_[2]; + + my $append = 0; + if (defined($wiki_append) && $wiki_append eq 'true') { + $append=1; + } + + my $previous_text =""; + + if ($append) { + my $ref = $mw->get_page( { title => $wiki_page } ); + $previous_text = $ref->{'*'}; + } + + my $text = $wiki_content; + if (defined($previous_text)) { + $text="$previous_text\n\n$text"; + } + $mw->edit( { action => 'edit', title => $wiki_page, text => "$text"} ); +} + +# wiki_getallpagename +# +# Fetch all pages of the wiki referenced by the global variable $mw +# and print the names of each one in the file all.txt with a new line +# ("\n") between these. +sub wiki_getallpagename { + # fetch the pages of the wiki + $mw->list ( { action => 'query', + list => 'allpages', + #cmtitle => "Category:Surnames", + #cmnamespace => 0, + cmlimit=> 500 }, + { max => 4, hook => \&cat_names } ) + || die $mw->{error}->{code}.": ".$mw->{error}->{details}; + + # print the name of each page + sub cat_names { + my ($ref) = @_; + + open(my $file, ">all.txt"); + foreach (@$ref) { + print $file "$_->{title}\n"; + } + close ($file); + } +} + +# Main part of this script: parse the command line arguments +# and select which function to execute +my $fct_to_call = shift; +my $login = $ARGV[0]; + +if ($login eq "-p") +{ + &wiki_login($wiki_admin,$wiki_admin_pass); + shift; +} + +switch ($fct_to_call) { + case "get_page" { &wiki_getpage($ARGV[0], $ARGV[1])} + case "delete_page" { &wiki_delete_page($ARGV[0])} + case "edit_page" { &wiki_editpage($ARGV[0], $ARGV[1], $ARGV[2])} + case "getallpagename" { &wiki_getallpagename()} + else{ die("test-gitmw.pl ERROR: wrong argument")} +} + -- 1.7.10.2.552.gaa3bb87