1#!/bin/bash
2# fail out of the script if anything here fails
3set -e
4
5# set the path to the present working directory
6export GOPATH=`pwd`
7
8function git_clone() {
9  path=$1
10  branch=$2
11  version=$3
12  if [ ! -d "src/$path" ]; then
13    mkdir -p src/$path
14    git clone https://$path.git src/$path
15  fi
16  pushd src/$path
17  git checkout "$branch"
18  git reset --hard "$version"
19  popd
20}
21
22# Remove potential previous runs
23rm -rf src test_program_bin toml-test
24
25# Run go vet
26go vet ./...
27
28go get github.com/pelletier/go-buffruneio
29go get github.com/davecgh/go-spew/spew
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