summaryrefslogtreecommitdiff
path: root/lookup
blob: df0d5bbc497d654ba739e5d0685f3c749f92ef76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env bash

# shellcheck disable=1090

set -e -u -o pipefail

main() {
  lookup=$1

  # default_repo=https://github.com/yaml/yaml-test-suite
  # default_commit=origin/data
  repo=
  commit=
  env=

  if [[ $lookup == data ]]; then
    debug "Looking up test-suite data repo/commit"
  elif [[ $lookup == env ]]; then
    debug "Looking up test-suite env"
  fi

  base=$(cd ../.. && pwd)
  head=$(get-head-commit)

  if [[ ${LIBYAML_TEST_SUITE_ENV-} ]]; then
    lookup-env
  else
    [[ $env ]] || lookup-log
    [[ $env ]] || lookup-local
  fi

  if [[ $lookup == data ]]; then
    [[ $repo && $commit ]] ||
      die "Can't find yaml-test-suite env to use"
    echo "$repo $commit"
  elif [[ $lookup == env ]]; then
    [[ $env ]] ||
      die "Can't find yaml-test-suite env to use"
    echo "$env"
  fi
}

get-head-commit() {
  debug "Getting libyaml HEAD commit"

  [[ -d $base/.git ]] ||
    die "'$base' is not a git repository"

  head=$(cd "$base" && git rev-parse HEAD)

  [[ $head ]] ||
    die "Can't find HEAD commit in '$base'"

  debug "HEAD commit is '$head'"

  echo "$head"
}

lookup-env() {
  local lookup=$LIBYAML_TEST_SUITE_ENV

  debug "Trying LIBYAML_TEST_SUITE_ENV=$lookup"

  case $lookup in
    /*)
      env=$lookup
      source "$env"
      found
      ;;
    pin-*)
      env=env/$lookup
      [[ -e $env ]] ||
        die "Specified LIBYAML_TEST_SUITE_ENV=$lookup but '$env' not found"
      source "$env"
      found
      ;;
    time)
      die "LIBYAML_TEST_SUITE_ENV=time not yet implemented"
      ;;
    *)
      die "Unsupported value 'LIBYAML_TEST_SUITE_ENV=$lookup'"
      ;;
  esac
}

lookup-log() {
  debug "Looking for '*** yaml-test-suite' in most recent 'git log' message"

  found=$(
    cd "$base" || exit
    git log --format=%B -n1 |
      (grep -A999999 '^\*\*\*\ yaml-test-suite' || true) |
      tail -n+2
  )
  if [[ $found ]]; then
    env=env/tmp-$head
    echo "$found" > "$env"
    source "$env"
    found
  fi
}

lookup-local() {
  debug "Looking for local env for '$head'"

  local e=env/pin-$head

  if [[ -e $e ]]; then
    env=$e
    source "./env/pin-$head"
    found
  fi
}

found() {
  debug "Found '$env'"

  repo=$LIBYAML_TEST_SUITE_DATA_REPO
  commit=$LIBYAML_TEST_SUITE_DATA_COMMIT
}

whitelist() (:)
blacklist() (:)
die() { echo "Died: $*" >&2; exit 1; }
warn() ( echo "$*" >&2 )
debug() (
  if [[ ${LIBYAML_TEST_SUITE_DEBUG-} ]]; then
    warn "[DEBUG] $*"
  fi
)

main "$@"