diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..b6603f4 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,28 @@ +name: CI + +on: [push, pull_request] + +jobs: + check: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.15 + + - name: Test + run: | + ./test.sh + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + args: build --snapshot + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 777d93c..bb434fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ +# ignore built binaries /terraform-demux + +# ignore test output +test-log diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..5062e9b --- /dev/null +++ b/test.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +set -e + +GIT_ROOT="$(git rev-parse --show-toplevel)" +TMP_DIR="$(mktemp -d)" + +if ! [[ $TMP_DIR ]]; then + panic "could not create temporary directory" +else + # shellcheck disable=SC2064 + trap "rm -r $TMP_DIR" EXIT + + export XDG_CACHE_HOME="$TMP_DIR" +fi + +function terraform_demux_version() { + go run "$GIT_ROOT/cmd/..." -- -version >test-log 2>&1 +} + +function expect_log() { + (grep -q "$1" test-log && pass "Found '$1' in test-log") || fail "Expected to find '$1' in test-log" +} + +function pass() { + echo "PASS:" "$@" >&2 +} + +function fail() { + echo "FAIL:" "$@" >&2 + exit 1 +} + +function panic() { + echo "PANIC:" "$@" >&2 + exit 1 +} + +function terraform_demux_version_test() { + local dir="$1" + local expected_version="$2" + + pushd "$dir" >/dev/null + + terraform_demux_version + + (grep -q "$expected_version" test-log && pass "Found '$expected_version' in $dir/test-log") \ + || fail "Expected to find '$expected_version' in $dir/test-log" + + popd >/dev/null +} + +function test_pre_0.12_exact() { + terraform_demux_version_test "testdata/terraform-pre-0.12-exact" "Terraform v0.11.15" +} + +function test_post_0.12_exact() { + terraform_demux_version_test "testdata/terraform-post-0.12-exact" "Terraform v1.0.3" +} + +function test_pessimistic_constraint() { + terraform_demux_version_test "testdata/terraform-pessimistic" "Terraform v0.14." +} + +test_pre_0.12_exact + +test_post_0.12_exact + +test_pessimistic_constraint diff --git a/testdata/terraform-pessimistic/terraform.tf b/testdata/terraform-pessimistic/terraform.tf new file mode 100644 index 0000000..b3093ec --- /dev/null +++ b/testdata/terraform-pessimistic/terraform.tf @@ -0,0 +1,3 @@ +terraform { + required_version = "~> 0.14.0" +} diff --git a/testdata/terraform-post-0.12-exact/terraform.tf b/testdata/terraform-post-0.12-exact/terraform.tf new file mode 100644 index 0000000..7cfd361 --- /dev/null +++ b/testdata/terraform-post-0.12-exact/terraform.tf @@ -0,0 +1,3 @@ +terraform { + required_version = "1.0.3" +} diff --git a/testdata/terraform-pre-0.12-exact/terraform.tf b/testdata/terraform-pre-0.12-exact/terraform.tf new file mode 100644 index 0000000..1fb51cf --- /dev/null +++ b/testdata/terraform-pre-0.12-exact/terraform.tf @@ -0,0 +1,3 @@ +terraform { + required_version = "0.11.15" +}