test_publish.sh (7237B)
1 #!/bin/sh 2 3 # Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com) 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 set -ex 19 20 die() 21 { 22 rm -rf "${tmpdir}" # cleanup temporary files 23 return "${1:-1}" # return status code (default is 1) 24 } 25 26 # Configure signal processing 27 trap 'die $?' EXIT 28 trap 'die 1' TERM INT 29 30 cookie='D3CAFBAD' 31 name="repo" 32 33 # Working directory 34 tmpdir="$(mktemp -d "${TMPDIR:-/tmp}"/git_publish_test_XXXXXX)" 35 repo="${tmpdir}/${name}" 36 git="${tmpdir}/git/${name}.git" 37 srv="${tmpdir}/srv" 38 39 # Use the local git-publish 40 export PATH="${PWD}:${PATH}" 41 42 ######################################################################## 43 # Helper functions 44 ######################################################################## 45 check_git_content() # repo-url 46 { 47 git clone "$1" "${tmpdir}/${name}" 48 49 [ -f "${tmpdir}/${name}/README" ] 50 [ -f "${tmpdir}/${name}/cookie.txt" ] 51 52 str="$(cat "${tmpdir}/${name}/cookie.txt")" 53 [ "${str}" = "${cookie}" ] 54 55 rm -rf "${tmpdir:?}/${name}" 56 } 57 58 check_www_content() 59 { 60 [ -f "${srv}/www/index.html" ] 61 [ -d "${srv}/www/${name}" ] 62 [ -f "${srv}/www/${name}/index.html" ] 63 } 64 65 ######################################################################## 66 # Setup repository 67 ######################################################################## 68 # Create bare repository 69 mkdir -p "${git}" 70 git init --initial-branch=master --bare "${git}" 71 72 # Create the repository content 73 mkdir -p "${repo}" 74 printf 'Repository to be published\n' > "${repo}/README" 75 printf '%s' "${cookie}" > "${repo}/cookie.txt" 76 77 # Configure the directory as a git working directory, commit its 78 # content and push it to the remote repository 79 cd "${repo}" 80 git init --initial-branch=master 81 git config --local user.name "Mr Untel" 82 git config --local user.email "mr@untel.fr" 83 git add README cookie.txt 84 git commit -m "Test content for publishing a repository" 85 git remote add origin "file://${git}" 86 git push origin master 87 88 cd "${OLDPWD}" 89 rm -rf "${repo}" # Remove temporary directory 90 91 ######################################################################## 92 # Basic tests 93 ######################################################################## 94 # Publish the repository 95 mkdir -p "${srv}/git" "${srv}/www" 96 git publish \ 97 -g "${srv}/git" \ 98 -w "${srv}/www" \ 99 -u "http://mr.untel.fr/" \ 100 "${git}" 101 102 [ -L "${srv}/git/${name}.git" ] # The published repository is a symlink 103 104 check_git_content "file://${srv}/git/${name}.git" 105 check_git_content "file://${git}" 106 check_www_content 107 108 # Try to publish an already published repository 109 git publish \ 110 -g "${srv}/git" \ 111 -w "${srv}/www" \ 112 -u "http://mr.untel.fr/" \ 113 "${git}" 114 115 # Unpublish 116 GIT_PUBLISH_DIR_GIT="${srv}/git" \ 117 GIT_PUBLISH_DIR_WWW="${srv}/www" \ 118 git publish -d "${git}" 119 120 # Attempting to unpublish a non-published repository results in an error 121 ! git publish -d "${git}" -g "${srv}/git" -w "${srv}/www" || die 1 122 123 # The published content should be removed 124 [ ! -e "${srv}/git/${name}.git" ] 125 [ ! -e "${srv}/www/${name}" ] 126 [ ! -e "${srv}/www/index.html" ] # No more index since no more repo 127 128 check_git_content "file://${git}" # Original repository should be OK 129 130 ######################################################################## 131 # Test the publication by moving the source repository 132 ######################################################################## 133 export GIT_PUBLISH_DIR_GIT="${srv}/git" 134 export GIT_PUBLISH_DIR_WWW="${srv}/www" 135 export GIT_PUBLISH_BASE_URL="http://mr.untel.fr" 136 137 # Publish the repository by moving it to the publicly accessible 138 # directory and converting the original repository into a symbolic link 139 # to the public repository 140 git publish -m "${git}" 141 142 # The public repository is a directory, not a symbolic link 143 [ -d "${srv}/git/${name}.git" ] && [ ! -L "${srv}/git/${name}.git" ] 144 145 # The original repository is a symbolic link to the public repository 146 [ -L "${git}" ] 147 148 check_git_content "file://${srv}/git/${name}.git" 149 check_git_content "file://${git}" 150 check_www_content 151 152 # Republish the same repo but without the -m option. 153 # Check that the public repo is thus a symbolic link to the original 154 # repository 155 git publish "${git}" 156 [ -L "${srv}/git/${name}.git" ] 157 [ -d "${git}" ] && [ ! -L "${git}" ] 158 159 # Republish the same repo with the -m option. 160 # Check that the original path is a symbolic link to the public 161 # repository 162 git publish -m "${git}" 163 [ -d "${srv}/git/${name}.git" ] && [ ! -L "${srv}/git/${name}.git" ] 164 [ -L "${git}" ] 165 166 # Unpublish the repository 167 git publish -d "${git}" 168 169 # The published content should be removed 170 [ ! -e "${srv}/git/${name}.git" ] 171 [ ! -e "${srv}/www/${name}" ] 172 [ ! -e "${srv}/www/index.html" ] # No more index since no more repo 173 174 # And the original repository should be a directory, not a symbolic 175 [ -d "${git}" ] && [ ! -L "${git}" ] 176 177 check_git_content "file://${git}" 178 179 ######################################################################## 180 # The repository cannot be [un]published 181 ######################################################################## 182 # Prevent the publication of the repository 183 touch "${git}/publication_ban" 184 185 git publish "${git}" 186 [ ! -e "${srv}/git/${name}.git" ] 187 [ ! -e "${srv}/www/${name}" ] 188 189 git publish -m "${git}" 190 [ ! -e "${srv}/git/${name}.git" ] 191 [ ! -e "${srv}/www/${name}" ] 192 193 # Make the repository publishable 194 rm "${git}/publication_ban" 195 196 # The repository cannot be published if the public path exists and it is 197 # not the result of an earlier publication of the reposite. It cannot 198 # unpublised too. 199 touch "${srv}/git/${name}.git" 200 ! git publish "${git}" || die 1 201 ! git publish -d "${git}" || die 1 202 rm "${srv}/git/${name}.git" 203 mkdir "${srv}/git/${name}.git" 204 ! git publish "${git}" || die 1 205 ! git publish -d "${git}" || die 1 206 rmdir "${srv}/git/${name}.git" 207 208 ######################################################################## 209 # Switch from the original repository to the published repository 210 ######################################################################## 211 # Publish the repository and try to unpublish the original repository. 212 # The original repository has been deleted! But the one that was 213 # published should still be valid as it was considered as the original 214 # one . 215 git publish -g "${srv}/git/" "${git}" 216 git publish -g "$(dirname "${git}")" -d "${srv}/git/${name}.git" 217 [ ! -e "${git}" ] 218 check_git_content "file://${srv}/git/${name}.git" 219 mv "${srv}/git/${name}.git" "${git}" 220 check_git_content "file://${git}" 221 222 # Same as above but the published repository is the original one moved 223 # to the public directory 224 git publish -g "${srv}/git/" -m "${git}" 225 git publish -g "$(dirname "${git}")" -d "${srv}/git/${name}.git" 226 [ ! -e "${git}" ] 227 check_git_content "file://${srv}/git/${name}.git" 228 229 die 0