# We use a simple Go program to report many Go versions.
# The program also errors on any command other than "go version",
# which saves us having to rebuild main.go many times.
go build -o .bin/go$exe ./fakego
env PATH=${WORK}/.bin${:}${PATH}

# An empty go version.
env GOVERSION=''
! garble build
stderr 'Go version is too old; please upgrade to Go 1.17.x or a newer devel version'

# We should error on a devel version that's too old.
# Note that they lacked the "goN.M-" prefix.
env GOVERSION='devel +afb5fca Sun Aug 07 00:00:00 2020 +0000'
! garble build
stderr 'Go version is too old; please upgrade to Go 1.17.x or a newer devel version'

# Another form of old version; with an old "goN.M-" prefix.
env GOVERSION='devel go1.15-afb5fca Sun Aug 07 00:00:00 2020 +0000'
! garble build
stderr 'Go version "go1.15" is too old; please upgrade to Go 1.17.x'

# A current devel version should be fine
env GOVERSION='devel go1.18-ad97d204f0 Sun Sep 12 16:46:58 2021 +0000'
! garble build
stderr 'mocking the real build'

# We should error on a stable version that's too old.
env GOVERSION='go1.14'
! garble build
stderr 'Go version "go1.14" is too old; please upgrade to Go 1.17.x'
! stderr 'or a newer devel version'

# We should accept a future stable version.
env GOVERSION='go1.18.2'
! garble build
stderr 'mocking the real build'

# We should accept custom devel strings.
env GOVERSION='devel go1.18-somecustomversion'
! garble build
stderr 'mocking the real build'

-- go.mod --
module test/main

go 1.17
-- main.go --
package main

func main() {}

-- fakego/main.go --
package main

import (
	"encoding/json"
	"fmt"
	"os"
)

func main() {
	if len(os.Args) > 0 && os.Args[1] == "env" {
		enc, _ := json.Marshal(struct{
			GOVERSION string
		} {
			GOVERSION: os.Getenv("GOVERSION"),
		})
		fmt.Printf("%s\n", enc)
		return
	}
	fmt.Fprintln(os.Stderr, "mocking the real build")
	os.Exit(1)
}
