Skip to content

Commit 0710ed9

Browse files
committed
Refactor to be more easily testable
1 parent 2db304e commit 0710ed9

File tree

1 file changed

+86
-35
lines changed

1 file changed

+86
-35
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -704,54 +704,75 @@ func outsideSupportedRange(version string) bool {
704704
return semver.Compare(short, "v"+minGoVersion) < 0 || semver.Compare(short, "v"+maxGoVersion) > 0
705705
}
706706

707-
func isGoInstalled() bool {
708-
_, err := exec.LookPath("go")
709-
return err == nil
707+
func checkForUnsupportedVersions(v versionInfo) (msg, version string) {
708+
if v.goDirectiveFound && outsideSupportedRange(v.goModVersion) {
709+
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
710+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ")."
711+
version = ""
712+
//TODO: emit diagnostic
713+
}
714+
715+
if v.goInstallationFound && outsideSupportedRange(v.goEnvVersion) {
716+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
717+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ")."
718+
version = ""
719+
//TODO: emit diagnostic
720+
}
721+
722+
return msg, version
710723
}
711724

712-
func getVersionToInstall() string {
713-
log.Printf("Autobuilder was built with %s, environment has %s\n", runtime.Version(), getEnvGoVersion())
714-
depMode := getDepMode()
715-
goModVersion, goDirectiveFound := tryReadGoDirective(depMode)
725+
func checkForVersionsNotFound(v versionInfo) (msg, version string) {
726+
if !v.goInstallationFound && !v.goDirectiveFound {
727+
msg = "No version of Go installed and no `go.mod` file found. Writing an " +
728+
"`environment.json` file specifying the maximum supported version of Go (" +
729+
maxGoVersion + ")."
730+
version = maxGoVersion
731+
}
716732

717-
if outsideSupportedRange(goModVersion) {
718-
log.Println("The version of Go specified in the go.mod file (" + goModVersion + ") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ").")
719-
//TODO: emit diagnostic
720-
return ""
733+
if !v.goInstallationFound && v.goDirectiveFound {
734+
msg = "No version of Go installed. Writing an `environment.json` file specifying the " +
735+
"version of Go found in the `go.mod` file (" + v.goModVersion + ")."
736+
version = v.goModVersion
721737
}
722738

723-
if !isGoInstalled() {
724-
if goDirectiveFound {
725-
log.Println("No version of Go installed. Writing an `environment.json` file specifying the version of Go from the go.mod file (" + goModVersion + ").")
726-
return goModVersion
727-
} else {
728-
log.Println("No version of Go installed and no `go.mod` file found. Writing an `environment.json` file specifying the maximum supported version of Go (" + maxGoVersion + ").")
729-
return maxGoVersion
730-
}
739+
if v.goInstallationFound && !v.goDirectiveFound {
740+
msg = "No `go.mod` file found. Version " + v.goEnvVersion + " installed in the environment."
741+
version = ""
731742
}
732743

733-
envVersion := getEnvGoVersion()[2:]
744+
return msg, version
745+
}
734746

735-
if outsideSupportedRange(envVersion) {
736-
log.Println("The version of Go installed in the environment (" + goModVersion + ") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ").")
737-
//TODO: emit diagnostic
738-
return ""
747+
func compareVersions(v versionInfo) (msg, version string) {
748+
if semver.Compare("v"+v.goModVersion, "v"+v.goEnvVersion) > 0 {
749+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
750+
") is lower than the version found in the `go.mod` file (" + v.goModVersion +
751+
").\nWriting an `environment.json` file specifying the version of Go from the " +
752+
"`go.mod` file (" + v.goModVersion + ")."
753+
version = v.goModVersion
754+
} else {
755+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
756+
") is high enough for the version found in the `go.mod` file (" + v.goModVersion + ")."
757+
version = ""
739758
}
740759

741-
if !goDirectiveFound {
742-
log.Println("No `go.mod` file found. Version " + envVersion + " installed in the environment.")
743-
return ""
760+
return msg, version
761+
}
762+
763+
func getVersionToInstall(v versionInfo) (msg, version string) {
764+
msg, version = checkForUnsupportedVersions(v)
765+
if msg != "" {
766+
return msg, version
744767
}
745768

746-
if semver.Compare("v"+goModVersion, "v"+envVersion) > 0 {
747-
log.Println(
748-
"The version of Go installed in the environment (" + envVersion + ") is lower than the version specified in the go.mod file (" + goModVersion +
749-
").\nWriting an `environment.json` file specifying the version of go from the go.mod file (" + goModVersion + ").")
750-
return goModVersion
769+
msg, version = checkForVersionsNotFound(v)
770+
if msg != "" {
771+
return msg, version
751772
}
752773

753-
// no need to install a version of Go
754-
return ""
774+
msg, version = compareVersions(v)
775+
return msg, version
755776
}
756777

757778
func writeEnvironmentFile(version string) {
@@ -782,8 +803,38 @@ func writeEnvironmentFile(version string) {
782803
}
783804
}
784805

806+
type versionInfo struct {
807+
goModVersion string
808+
goDirectiveFound bool
809+
goEnvVersion string
810+
goInstallationFound bool
811+
}
812+
813+
func (v versionInfo) String() string {
814+
return fmt.Sprintf("go.mod version: %s, go.mod directive found: %t, go env version: %s, go installation found: %t", v.goModVersion, v.goDirectiveFound, v.goEnvVersion, v.goInstallationFound)
815+
}
816+
817+
func isGoInstalled() bool {
818+
_, err := exec.LookPath("go")
819+
return err == nil
820+
}
821+
785822
func identifyEnvironment() {
786-
versionToInstall := getVersionToInstall()
823+
var v versionInfo
824+
depMode := getDepMode()
825+
v.goModVersion, v.goDirectiveFound = tryReadGoDirective(depMode)
826+
827+
v.goInstallationFound = isGoInstalled()
828+
if v.goInstallationFound {
829+
v.goEnvVersion = getEnvGoVersion()[2:]
830+
}
831+
832+
msg, versionToInstall := getVersionToInstall(v)
833+
834+
if msg != "" {
835+
log.Println(msg)
836+
}
837+
787838
writeEnvironmentFile(versionToInstall)
788839
}
789840

0 commit comments

Comments
 (0)