gitpages

A collection of scripts to securely webhost and publish git repositories.
git clone https://scm.kuandu.systems/git-raw/gitpages.git
Log | Files | Refs | README | LICENSE

test-gitpages-generate.sh (2865B)


      1 #!/bin/sh
      2 
      3 set -eu
      4 
      5 SCRIPT_DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
      6 SCRIPT="$SCRIPT_DIR/gitpages.sh"
      7 
      8 TEST_ROOT=$(mktemp -d)
      9 trap 'rm -rf "$TEST_ROOT"' EXIT
     10 
     11 WEB_ROOT="$TEST_ROOT/web"
     12 GIT_SRC="$TEST_ROOT/src"
     13 GIT_RAW="$WEB_ROOT/raw"
     14 GIT_HTML="$WEB_ROOT/git"
     15 ASSETS_DIR="$TEST_ROOT/assets"
     16 GITPAGES_VAR_DIR="$TEST_ROOT/var"
     17 CONFIG="$TEST_ROOT/gitpages.conf"
     18 
     19 export GITPAGES_VAR_DIR
     20 
     21 mkdir -p \
     22   "$GIT_SRC" \
     23   "$GIT_RAW" \
     24   "$GIT_HTML" \
     25   "$ASSETS_DIR" \
     26   "$GITPAGES_VAR_DIR"
     27 
     28 #
     29 # Mock stagit and stagit-index.
     30 #
     31 mkdir -p "$TEST_ROOT/bin"
     32 export PATH="$TEST_ROOT/bin:$PATH"
     33 
     34 cat > "$TEST_ROOT/bin/stagit" << 'EOF'
     35 #!/bin/sh
     36 set -eu
     37 
     38 # generate_repo_html() has already cd'ed into the staging directory.
     39 touch index.html
     40 EOF
     41 
     42 cat > "$TEST_ROOT/bin/stagit-index" << 'EOF'
     43 #!/bin/sh
     44 set -eu
     45 
     46 printf '%s\n' '<html><body>Repositories</body></html>'
     47 EOF
     48 
     49 chmod +x \
     50   "$TEST_ROOT/bin/stagit" \
     51   "$TEST_ROOT/bin/stagit-index"
     52 
     53 #
     54 # Configuration.
     55 #
     56 cat > "$CONFIG" << EOF
     57 WEB_ROOT=$WEB_ROOT
     58 GIT_SRC=$GIT_SRC
     59 GIT_RAW=raw
     60 GIT_HTML=git
     61 ASSETS_DIR=$ASSETS_DIR
     62 EOF
     63 
     64 fail() {
     65   printf 'FAIL: %s\n' "$1" >&2
     66   exit 1
     67 }
     68 
     69 assert_dir() {
     70   [ -d "$1" ] || fail "$2"
     71 }
     72 
     73 assert_file() {
     74   [ -f "$1" ] || fail "$2"
     75 }
     76 
     77 #
     78 # Create a real bare repository in GIT_SRC.
     79 #
     80 create_repo() {
     81   name="$1"
     82   work="$TEST_ROOT/work-$name"
     83 
     84   mkdir -p "$work"
     85 
     86   (
     87     cd "$work"
     88 
     89     git init > /dev/null
     90     git config user.name "Test User"
     91     git config user.email "test@example.com"
     92 
     93     printf 'test\n' > README
     94     git add README
     95     git commit -m "Initial commit" > /dev/null
     96 
     97     git clone --bare . "$GIT_SRC/$name.git" > /dev/null
     98   )
     99 
    100   git --git-dir="$GIT_SRC/$name.git" \
    101     symbolic-ref HEAD refs/heads/main
    102 }
    103 
    104 echo "Testing $SCRIPT generate"
    105 
    106 #
    107 # Test: generate all repositories
    108 #
    109 echo "=== Test: generate all repositories ==="
    110 
    111 create_repo myrepo
    112 
    113 "$SCRIPT" generate -c "$CONFIG"
    114 
    115 assert_dir \
    116   "$GIT_RAW/myrepo.git" \
    117   "myrepo.git was mirrored"
    118 
    119 assert_dir \
    120   "$GIT_HTML/myrepo" \
    121   "HTML directory was generated"
    122 
    123 assert_file \
    124   "$GIT_HTML/myrepo/index.html" \
    125   "repository index.html was generated"
    126 
    127 assert_file \
    128   "$GIT_HTML/index.html" \
    129   "repository index was generated"
    130 
    131 echo "PASS: generate all repositories"
    132 
    133 #
    134 # Test: generate a single repository
    135 #
    136 echo "=== Test: generate single repository ==="
    137 
    138 rm -rf "$GIT_HTML/myrepo"
    139 
    140 "$SCRIPT" generate -c "$CONFIG" "$GIT_RAW/myrepo.git"
    141 
    142 assert_dir \
    143   "$GIT_HTML/myrepo" \
    144   "HTML directory was generated for single repository"
    145 
    146 assert_file \
    147   "$GIT_HTML/myrepo/index.html" \
    148   "repository index.html was generated for single repository"
    149 
    150 echo "PASS: generate single repository"
    151 
    152 #
    153 # Test: unknown option
    154 #
    155 echo "=== Test: invalid arguments ==="
    156 
    157 if "$SCRIPT" generate -c "$CONFIG" -x > /dev/null 2>&1; then
    158   fail "unknown option was accepted"
    159 fi
    160 
    161 echo "PASS: invalid arguments"
    162 
    163 echo "All generate tests passed."