1#!/bin/bash
2# fail out of the script if anything here fails
3set -e
4set -o pipefail
5
6# set the path to the present working directory
7export GOPATH=`pwd`
8
9function git_clone() {
10  path=$1
11  branch=$2
12  version=$3
13  if [ ! -d "src/$path" ]; then
14    mkdir -p src/$path
15    git clone https://$path.git src/$path
16  fi
17  pushd src/$path
18  git checkout "$branch"
19  git reset --hard "$version"
20  popd
21}
22
23# Remove potential previous runs
24rm -rf src test_program_bin toml-test
25
26go get github.com/pelletier/go-buffruneio
27go get github.com/davecgh/go-spew/spew
28go get gopkg.in/yaml.v2
29go get github.com/BurntSushi/toml
30
31# get code for BurntSushi TOML validation
32# pinning all to 'HEAD' for version 0.3.x work (TODO: pin to commit hash when tests stabilize)
33git_clone github.com/BurntSushi/toml master HEAD
34git_clone github.com/BurntSushi/toml-test master HEAD #was: 0.2.0 HEAD
35
36# build the BurntSushi test application
37go build -o toml-test github.com/BurntSushi/toml-test
38
39# vendorize the current lib for testing
40# NOTE: this basically mocks an install without having to go back out to github for code
41mkdir -p src/github.com/pelletier/go-toml/cmd
42mkdir -p src/github.com/pelletier/go-toml/query
43cp *.go *.toml src/github.com/pelletier/go-toml
44cp -R cmd/* src/github.com/pelletier/go-toml/cmd
45cp -R query/* src/github.com/pelletier/go-toml/query
46go build -o test_program_bin src/github.com/pelletier/go-toml/cmd/test_program.go
47
48# Run basic unit tests
49go test github.com/pelletier/go-toml -covermode=count -coverprofile=coverage.out
50go test github.com/pelletier/go-toml/cmd/tomljson
51go test github.com/pelletier/go-toml/query
52
53# run the entire BurntSushi test suite
54if [[ $# -eq 0 ]] ; then
55  echo "Running all BurntSushi tests"
56  ./toml-test ./test_program_bin | tee test_out
57else
58  # run a specific test
59  test=$1
60  test_path='src/github.com/BurntSushi/toml-test/tests'
61  valid_test="$test_path/valid/$test"
62  invalid_test="$test_path/invalid/$test"
63
64  if [ -e "$valid_test.toml" ]; then
65    echo "Valid Test TOML for $test:"
66    echo "===="
67    cat "$valid_test.toml"
68
69    echo "Valid Test JSON for $test:"
70    echo "===="
71    cat "$valid_test.json"
72
73    echo "Go-TOML Output for $test:"
74    echo "===="
75    cat "$valid_test.toml" | ./test_program_bin
76  fi
77
78  if [ -e "$invalid_test.toml" ]; then
79    echo "Invalid Test TOML for $test:"
80    echo "===="
81    cat "$invalid_test.toml"
82
83    echo "Go-TOML Output for $test:"
84    echo "===="
85    echo "go-toml Output:"
86    cat "$invalid_test.toml" | ./test_program_bin
87  fi
88fi
89