From 5f4b4d1d008671bcc5c8b280d6b21f853e639e15 Mon Sep 17 00:00:00 2001 From: eleonore12345 Date: Tue, 16 Jul 2024 14:41:30 +0200 Subject: [PATCH] test_git_update.sh le fond est fini! --- test_git_update.sh | 301 +++++++++++++++++++++++++++++---------------- 1 file changed, 194 insertions(+), 107 deletions(-) diff --git a/test_git_update.sh b/test_git_update.sh index bbff599..0d9486b 100755 --- a/test_git_update.sh +++ b/test_git_update.sh @@ -3,20 +3,24 @@ . driglibash-base echo "" -REPO_NAME=performance_testing -REMOTE=ssh://git@git.jean-cloud.net:22529/eleonore/performance_testing.git +REPO_NAME="performance_testing" +REMOTE="ssh://git@git.jean-cloud.net:22529/eleonore/performance_testing.git" WITH_SUBMODULE="true" FILENAMES=("sample0" "sample1 sample3") #we do not check for all files FILENAMES_TAG=("sample0 sample1 sample4") FILENAMES_BRANCH=("sample0 sample1 sample2") #we do not check for all files, especially not those specific to the branch. -FILE_ON_BRANCH_ONLY=sample2 -FILE_ON_TAG_ONLY=sample4 -FILE_ON_MAIN_ONLY=sample3 -FILE_TO_BE_DELETED=sample0 -FILE_TO_BE_CREATED=new_file -TAG_NAME=tagging_point -BRANCH_NAME=secondary -TEMP_CLONE_DIR="temp_local_clone" +FILE_ON_BRANCH_ONLY="sample2" +FILE_ON_TAG_ONLY="sample4" +FILE_ON_MAIN_ONLY="sample3" +FILE_TO_BE_DELETED="sample0" +FILE_TO_BE_CREATED="new_file" +FILE_TO_BE_MODIFIED=$FILE_TO_BE_CREATED +TAG_NAME="tagging_point" +TAG_FLAG="tag_flag" +BRANCH_NAME="secondary" +TMP_CLONE_DIR="tmp_local_clone" +TMP_EXISTING_DIR="tmp_existing_dir" +TMP_EXISTING_FILE="tmp_existing_file" if [ "$WITH_SUBMODULE" = "true" ]; then bash creation_repo.sh -s &> /dev/null @@ -41,9 +45,15 @@ DESCRIPTION TEST1: git cloned in an empty directory with tag TEST2: git cloned in an empty directory with branch TEST3: git cloned in an existing directory + TEST4: git updated fast-forward on main + TEST5: git updated with underlying conflict on main + TEST6: git updated, switching to another branch, deleting and adding files in the process + TEST7: git updated, switching to a tag, deleting and adding files in the process + TEST8: git updated, before and after changing a tag, deleting and adding files in the process " } +#check functions cloning_check(){ local input=("$@") local repo_name=${input[0]} @@ -110,41 +120,24 @@ branch_check(){ cd .. } -make_temporary_clone(){ - mkdir $TEMP_CLONE_DIR - cd $TEMP_CLONE_DIR - run git clone --recurse-submodules --shallow-submodules --depth 1 --config core.sshCommand="ssh" $REMOTE . &> /dev/null - cd .. -} - -modification_remote(){ - run cd $TEMP_CLONE_DIR - run touch $FILE_TO_BE_CREATED - echo "new text" > $FILE_TO_BE_CREATED - run rm $FILE_TO_BE_DELETED - run git add $FILE_TO_BE_CREATED $FILE_TO_BE_DELETED - run git commit -m "$FILE_TO_BE_CREATED created and $FILE_TO_BE_DELETED deleted" &> /dev/null - run git push &> /dev/null - run cd .. -} - -undo_modification_remote(){ - cd $TEMP_CLONE_DIR - run git revert --no-edit HEAD &> /dev/null - run git push &> /dev/null - cd .. -} - -modification_local(){ - cd $REPO_NAME - touch $FILE_TO_BE_CREATED - echo "different text" > $FILE_TO_BE_CREATED - git add $FILE_TO_BE_CREATED - git commit -m "$FILE_TO_BE_CREATED added" - cd .. +existing_check(){ + run cd $TMP_EXISTING_DIR + if [ -f $TMP_EXISTING_FILE ]; then + echo "The preexisting non-conflicting file is still here." + existing_result=0 + run cd .. + conflict_check $TMP_EXISTING_DIR + existing_result=conflict_result + else + echo "The preexisting non-conflicting file has been deleted." + run cd .. + existing_result=1 + fi } modification_check(){ + local repo_name=$1 + run cd $repo_name if [ -f $FILE_TO_BE_CREATED ]; then echo "The new file has been imported." modification_result=0 @@ -159,53 +152,129 @@ modification_check(){ else echo "$FILE_TO_BE_DELETED has been deleted." fi + run cd.. } -changing_branch_check(){ +switching_branch_check(){ + local repo_name=$1 + run cd $repo_name if [ -f $FILE_ON_BRANCH_ONLY ]; then echo "The files of the branch $BRANCH_NAME are present." - branch_changing_result=0 + branch_switching_result=0 if [ -f $FILE_ON_MAIN_ONLY ]; then echo "The files of the branch main are present." - branch_changing_result=1 + branch_switching_result=1 else echo "The files of the branch main are absent." - branch_changing_result=0 + branch_switching_result=0 fi else echo "The files of the branch $BRANCH_NAME are absent." - branch_changing_result=1 + branch_switching_result=1 fi - + cd .. } -changing_tag_check(){ +switching_tag_check(){ + local repo_name=$1 + run cd $repo_name if [ -f $FILE_ON_TAG_ONLY ]; then echo "The files of the tag $TAG_NAME are present." - tag_changing_result=0 + tag_switching_result=0 if [ -f $FILE_ON_MAIN_ONLY ]; then echo "The files of the last commit on main are present." - tag_changing_result=1 + tag_switching_result=1 else echo "The files of the last commit in main are absent." - tag_changing_result=0 + tag_switching_result=0 fi else echo "The files of the tag $TAG_NAME are absent." - tag_changing_result=1 + tag_switching_result=1 fi - + run cd .. } conflict_check(){ - if [[ $(grep "new" $FILE_TO_BE_CREATED | wc -l) > 0 && $(grep "different" $FILE_TO_BE_CREATED | wc -l) = 0 ]]; then + local repo_name=$1 + cd $repo_name + if [[ $(grep "new" $FILE_TO_BE_MODIFIED | wc -l) > 0 && $(grep "different" $FILE_TO_BE_MODIFIED | wc -l) = 0 ]]; then echo "The remote version has overwritten the local version." conflict_result=0 else echo "The remote version has not overwritten the local version." fi + cd .. } +#intermediate functions +make_temporary_existing_dir(){ + run mkdir $TMP_EXISTING_DIR + run cd $TMP_EXISTING_DIR + run touch $TMP_EXISTING_FILE + run touch $FILE_TO_BE_MODIFIED + echo "this should be overwritten" > $FILE_TO_BE_MODIFIED + cd .. +} + +make_temporary_clone(){ + mkdir $TMP_CLONE_DIR + cd $TMP_CLONE_DIR + run git clone --recurse-submodules --shallow-submodules --depth 1 --no-single-branch --config core.sshCommand="ssh" $REMOTE . &> /dev/null + cd .. +} + +modification_remote(){ + run cd $TMP_CLONE_DIR + run touch $FILE_TO_BE_CREATED + echo "new text" > $FILE_TO_BE_CREATED + run rm $FILE_TO_BE_DELETED + run git add $FILE_TO_BE_CREATED $FILE_TO_BE_DELETED &> /dev/null + run git commit -m "$FILE_TO_BE_CREATED created and $FILE_TO_BE_DELETED deleted" &> /dev/null + run git push &> /dev/null + run cd .. +} + +undo_modification_remote(){ + cd $TMP_CLONE_DIR + run git revert --no-edit HEAD &> /dev/null + run git push &> /dev/null + cd .. + rm -rf $TMP_CLONE_DIR +} + +modification_local(){ + run cd $REPO_NAME + run touch $FILE_TO_BE_CREATED + run echo "different text" > $FILE_TO_BE_CREATED + run git add $FILE_TO_BE_CREATED &> /dev/null + run git commit -m "$FILE_TO_BE_CREATED added" &> /dev/null + run cd .. +} + +changing_tag(){ + run cd $TMP_CLONE_DIR + run git checkout $TAG_NAME &> /dev/null + run git tag -f $TAG_FLAG &> /dev/null #temporary tag flag to be able to put the tag back here after the test + run git push -f origin $TAG_FLAG &> /dev/null + run git checkout $BRANCH_NAME &> /dev/null + run git tag -f $TAG_NAME &> /dev/null #moving the tag on the branch secondary + run git push -f origin $TAG_NAME &> /dev/null #push the move to the remote + cd .. +} + +undo_changing_tag(){ + run cd $TMP_CLONE_DIR + run git checkout $TAG_FLAG &> /dev/null + run git tag -f $TAG_NAME &> /dev/null #move locally + run git push -f origin $TAG_NAME &> /dev/null #push the move to the remote + run git push --delete origin $TAG_FLAG &> /dev/null #delete remotely + run git tag --delete $TAG_FLAG &> /dev/null #delete locally + run cd .. + run rm -rf $TMP_CLONE_DIR +} + + test0 (){ #CASE 0: git cloned in an empty directory section TEST0 @@ -274,34 +343,17 @@ test3(){ if [ -d $REPO_NAME ]; then run rm -rf $REPO_NAME fi - #mimic an existing directory, with two existing file, one with the same name as a to-be-cloned file - run mkdir tmp_dir - run cd tmp_dir - run touch tmp_file - run touch "${FILENAMES[0]}" - echo "this should be overwritten" > ${FILENAMES[0]} + #mimic an existing directory, with two existing file, one with the same name as a to-be-cloned file, and go inside the new dir + make_temporary_existing_dir + #make git_update.sh clone in the existing dir + run cd $TMP_EXISTING_DIR run ../git_update.sh -N $REMOTE run cd .. #checks - cloning_check "tmp_dir" ${FILENAMES[@]} - history_check $"tmp_dir" - #existing check - cd tmp_dir - if [ -f tmp_file ]; then - echo "The preexisting non-conflicting file is still here." - existing_result=0 - if [ $(grep "overwritten" ${FILENAMES[0]} | wc -l) = 0 ]; then - echo "The conflicting files have been overwitten." - else - echo "The conflicting existing files have not been overwritten." - existing_result=1 - fi - else - echo "The preexisting non-conflicting file has been deleted." - existing_result=1 - fi - run cd .. - #run rm -rf tmp_dir + cloning_check $TMP_EXISTING_DIR ${FILENAMES[@]} + history_check $TMP_EXISTING_DIR + existing_check + run rm -rf tmp_existing_dir case3=$((cloning_result+history_result+existing_result)) if [ $case3 = 0 ]; then @@ -326,9 +378,9 @@ test4(){ #make git_update.sh update the repository run cd $REPO_NAME run ../git_update.sh $REMOTE - #checks - modification_check run cd .. + #checks + modification_check $REPO_NAME history_check $REPO_NAME #cleaning undo_modification_remote @@ -357,10 +409,10 @@ test5(){ #make git_update.sh update the local repository run cd $REPO_NAME run ../git_update.sh $REMOTE - #checks - run conflict_check ${FILENAMES[@]} - #cleaning run cd .. + #checks + run conflict_check $REPO_NAME + #cleaning undo_modification_remote case5=$((conflict_result+history_result)) @@ -383,15 +435,15 @@ test6(){ #call git update with another branch cd $REPO_NAME ../git_update.sh -r $BRANCH_NAME $REMOTE - #checks - changing_branch_check cd .. + #checks + switching_branch_check $REPO_NAME history_check $REPO_NAME - case6=$((branch_changing_result+history_result)) + case6=$((branch_switching_result+history_result)) if [ "$case6" = "0" ]; then - echo "case 6, branch-changing update: OK" + echo "case 6, branch-switching update: OK" else - echo "case 6, branch-changing update: FAIL" + echo "case 6, branch-switching update: FAIL" fi } @@ -404,28 +456,63 @@ test7(){ fi #clone the repo in its last state run git clone --recurse-submodules --shallow-submodules --depth 1 --config core.sshCommand="ssh" $REMOTE &> /dev/null - #call git update with another branch + #call git_update.sh with a tag cd $REPO_NAME ../git_update.sh -r $TAG_NAME $REMOTE - #checks - changing_tag_check run cd .. + #checks + switching_tag_check $REPO_NAME history_check $REPO_NAME - case7=$((tag_changing_result+history_result)) + case7=$((tag_switching_result+history_result)) if [ "$case7" = "0" ]; then - echo "case 7, branch-changing update: OK" + echo "case 7, tag-switching update: OK" else - echo "case 7, branch-changing update: FAIL" + echo "case 7, tag-switching update: FAIL" fi } +test8(){ +#CASE 8: git updated, before and after changing a tag, deleting and adding files in the process + section TEST8 + #if it exists, delete the directory + if [ -d $REPO_NAME ]; then + run rm -rf $REPO_NAME + fi + #clone the repo in its last state + run git clone --recurse-submodules --shallow-submodules --depth 1 --config core.sshCommand="ssh" $REMOTE &> /dev/null + #call git_update.sh with a tag + cd $REPO_NAME + ../git_update.sh -r $TAG_NAME $REMOTE &> /dev/null + cd .. + #intermediate check to make sure that we are starting on the initial position of the tag + echo "first position of the tag" + switching_tag_check $REPO_NAME + #change the position of the tag from elsewhere + changing_tag + #call git_update.sh again to go to the new position of the tag + run cd $REPO_NAME + ../git_update.sh -r $TAG_NAME $REMOTE + run cd .. + #put back the remote in its initial state + undo_changing_tag + + #checks + echo "second position of the tag" + switching_branch_check $REPO_NAME #new position of the tag=last commit of the branch hence we can reuse this check + history_check $REPO_NAME + case8=$((tag_switching_result+branch_switching_result+history_result)) + if [ "$case8" = "0" ]; then + echo "case 8, tag-changing update: OK" + else + echo "case 8, tag-changing update: FAIL" + fi + +} -#not ff #ref changed - - - - +#updating on a branch/tag +#créer une option dans creation_repo pour le relier à un remote +#appeler creation tmp dir à l'intérieur while getopts ":hn:a" option; do case $option in @@ -443,6 +530,7 @@ while getopts ":hn:a" option; do done if [ "$ALL_TESTS" = "true" ]; then + make_temporary_clone test0 test1 test2 @@ -453,9 +541,10 @@ if [ "$ALL_TESTS" = "true" ]; then test7 test8 test9 - + rm -rf $TMP_CLONE_DIR elif [ -n "$TEST_NUM" ]; then - if [[ "$TEST_NUM" = 4 || "$TEST_NUM" = 5 ]];then + #in order to only create the temporary clone once if we execute all tests + if [[ "$TEST_NUM" = 4 || "$TEST_NUM" = 5 || "$TEST_NUM" = 8 ]]; then make_temporary_clone fi case $TEST_NUM in @@ -477,14 +566,12 @@ elif [ -n "$TEST_NUM" ]; then test7;; 8) test8;; - 9) - test9;; *) echo "Error: Invalid test number" die;; esac - if [ "$TEST_NUM" = 4 ];then - rm -rf $TEMP_CLONE_DIR + if [ "$TEST_NUM" = 4 || "$TEST_NUM" = 5 || "$TEST_NUM" = 8 ];then + rm -rf $TMP_CLONE_DIR fi else Help