Skip to content

Commit

Permalink
Merge pull request #4988 from andydotxyz/fix/unreverttool
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz authored Jul 4, 2024
2 parents 85c2e2a + 975a6e9 commit 6aef0c3
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 771 deletions.
64 changes: 4 additions & 60 deletions cmd/fyne/internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -126,37 +125,8 @@ func (b *Builder) Build() error {
return b.build()
}

func checkVersion(output string, versionConstraint *version.ConstraintGroup) error {
split := strings.Split(output, " ")
// We are expecting something like: `go version goX.Y OS`
if len(split) != 4 || split[0] != "go" || split[1] != "version" || len(split[2]) < 5 || split[2][:2] != "go" {
return fmt.Errorf("invalid output for `go version`: `%s`", output)
}

normalized := version.Normalize(split[2][2:len(split[2])])
if !versionConstraint.Match(normalized) {
return fmt.Errorf("expected go version %v got `%v`", versionConstraint.GetConstraints(), normalized)
}

return nil
}

func isWeb(goos string) bool {
return goos == "js" || goos == "wasm"
}

func checkGoVersion(runner runner, versionConstraint *version.ConstraintGroup) error {
if versionConstraint == nil {
return nil
}

goVersion, err := runner.runOutput("version")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", string(goVersion))
return err
}

return checkVersion(string(goVersion), versionConstraint)
return goos == "js" || goos == "wasm" || goos == "web"
}

type goModEdit struct {
Expand Down Expand Up @@ -195,17 +165,11 @@ func getFyneGoModVersion(runner runner) (string, error) {
}

func (b *Builder) build() error {
var versionConstraint *version.ConstraintGroup

goos := b.os
if goos == "" {
goos = targetOS()
}

if goos == "js" && runtime.GOOS == "windows" {
return errors.New("gopherjs doesn't support Windows. Only wasm target is supported for the web output. You can also use fyne-cross to solve this")
}

fyneGoModRunner := b.updateAndGetGoExecutable(goos)

srcdir, err := b.computeSrcDir(fyneGoModRunner)
Expand Down Expand Up @@ -267,12 +231,7 @@ func (b *Builder) build() error {
tags = append(tags, "release")
}
if len(tags) > 0 {
if goos == "js" {
args = append(args, "--tags")
} else {
args = append(args, "-tags")
}
args = append(args, strings.Join(tags, ","))
args = append(args, "-tags", strings.Join(tags, ","))
}

if b.goPackage != "" {
Expand All @@ -281,20 +240,9 @@ func (b *Builder) build() error {

if goos != "ios" && goos != "android" && !isWeb(goos) {
env = append(env, "GOOS="+goos)
} else if goos == "wasm" {
versionConstraint = version.NewConstrainGroupFromString(">=1.17")
} else if goos == "web" || goos == "wasm" {
env = append(env, "GOARCH=wasm")
env = append(env, "GOOS=js")
} else if goos == "js" {
_, err := b.runner.runOutput("version")
if err != nil {
fmt.Fprintf(os.Stderr, "Can not execute `gopherjs version`. Please do `go install github.com/gopherjs/gopherjs@latest`.\n")
return err
}
}

if err := checkGoVersion(b.runner, versionConstraint); err != nil {
return err
}

b.runner.setDir(b.srcdir)
Expand Down Expand Up @@ -364,11 +312,7 @@ func (b *Builder) updateAndGetGoExecutable(goos string) runner {
fyneGoModRunner = newCommand(goBin)
b.runner = fyneGoModRunner
} else {
if goos != "js" {
b.runner = newCommand("go")
} else {
b.runner = newCommand("gopherjs")
}
b.runner = newCommand("go")
}
}
return fyneGoModRunner
Expand Down
170 changes: 0 additions & 170 deletions cmd/fyne/internal/commands/build_test.go
Original file line number Diff line number Diff line change
@@ -1,107 +1,13 @@
package commands

import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/mcuadros/go-version"
"github.com/stretchr/testify/assert"
)

func Test_CheckGoVersionNoGo(t *testing.T) {
commandNil := &testCommandRuns{runs: []mockRunner{}, t: t}
assert.Nil(t, checkGoVersion(commandNil, nil))
commandNil.verifyExpectation()

commandNotNil := &testCommandRuns{runs: []mockRunner{{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{err: fmt.Errorf("file not found")},
}}, t: t}
assert.NotNil(t, checkGoVersion(commandNotNil, version.NewConstrainGroupFromString(">=1.17")))
commandNotNil.verifyExpectation()
}

func Test_CheckGoVersionValidValue(t *testing.T) {
expected := []mockRunner{
{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version go1.17.6 windows/amd64")},
},
{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version go1.17.6 linux/amd64")},
}}

versionOk := &testCommandRuns{runs: expected, t: t}
assert.Nil(t, checkGoVersion(versionOk, version.NewConstrainGroupFromString(">=1.17")))
assert.Nil(t, checkGoVersion(versionOk, version.NewConstrainGroupFromString(">=1.17")))
versionOk.verifyExpectation()

versionNotOk := &testCommandRuns{runs: expected, t: t}
assert.NotNil(t, checkGoVersion(versionNotOk, version.NewConstrainGroupFromString("<1.17")))
assert.NotNil(t, checkGoVersion(versionNotOk, version.NewConstrainGroupFromString("<1.17")))
versionNotOk.verifyExpectation()
}

func Test_CheckGoVersionInvalidValue(t *testing.T) {
tests := map[string]struct {
runs []mockRunner
}{
"Wrong go command output": {runs: []mockRunner{{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("got noversion go1.17.6 windows/amd64")},
}}},
"Wrong go command length": {runs: []mockRunner{{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version really")},
}}},
"Wrong version string": {runs: []mockRunner{{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version 1.17.6 windows/amd64")},
}}},
"No version number": {runs: []mockRunner{{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version gowrong windows/amd64")},
}}},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
versionInvalid := &testCommandRuns{runs: tc.runs, t: t}
assert.NotNil(t, checkGoVersion(versionInvalid, version.NewConstrainGroupFromString(">=1.17")))
versionInvalid.verifyExpectation()
})
}
}

func Test_CheckVersionTableTests(t *testing.T) {
tests := map[string]struct {
goVersion string
constraint string
expectNil bool
}{
"Windows 1.17.6": {goVersion: "go version go1.17.6 windows/amd64", constraint: ">=1.17", expectNil: true},
"Linux 1.17.6": {goVersion: "go version go1.17.6 linux/amd64", constraint: ">=1.17", expectNil: true},
"Linux 1.18.0": {goVersion: "go version go1.18.0 linux/amd64", constraint: ">=1.17", expectNil: true},
"Windows 1.14.0": {goVersion: "go version go1.14.0 windows/amd64", constraint: ">=1.17", expectNil: false},
"Windows 1.15.0": {goVersion: "go version go1.15.0 windows/amd64", constraint: ">=1.17", expectNil: false},
"Windows 1.16.0": {goVersion: "go version go1.16.0 windows/amd64", constraint: ">=1.17", expectNil: false},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
if tc.expectNil {
assert.Nil(t, checkVersion(tc.goVersion, version.NewConstrainGroupFromString(tc.constraint)))
} else {
assert.NotNil(t, checkVersion(tc.goVersion, version.NewConstrainGroupFromString(tc.constraint)))
}
})
}
}

func Test_BuildWasmVersion(t *testing.T) {
expected := []mockRunner{
{
Expand All @@ -110,10 +16,6 @@ func Test_BuildWasmVersion(t *testing.T) {
ret: []byte("{ \"Module\": { \"Path\": \"fyne.io/fyne/v2\"} }"),
},
},
{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version go1.17.6 windows/amd64")},
},
{
expectedValue: expectedValue{
args: []string{"build"},
Expand All @@ -140,12 +42,6 @@ func Test_BuildWasmReleaseVersion(t *testing.T) {
ret: []byte("{ \"Module\": { \"Path\": \"fyne.io/fyne/v2\"} }"),
},
},
{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{
ret: []byte("go version go1.17.6 windows/amd64"),
},
},
{
expectedValue: expectedValue{
args: []string{"build", "-tags", "release"},
Expand All @@ -166,72 +62,6 @@ func Test_BuildWasmReleaseVersion(t *testing.T) {
wasmBuildTest.verifyExpectation()
}

func Test_BuildGopherJSReleaseVersion(t *testing.T) {
expected := []mockRunner{}

if runtime.GOOS != "windows" {
expected = []mockRunner{
{
expectedValue: expectedValue{args: []string{"mod", "edit", "-json"}},
mockReturn: mockReturn{
ret: []byte("{ \"Module\": { \"Path\": \"fyne.io/fyne/v2\"} }"),
},
},
{
expectedValue: expectedValue{
args: []string{"version"},
osEnv: true,
},
mockReturn: mockReturn{
ret: []byte(""),
err: nil,
},
},
{
expectedValue: expectedValue{
args: []string{"build", "--tags", "release"},
osEnv: true,
dir: "myTest",
},
mockReturn: mockReturn{
ret: []byte(""),
},
},
}
}

gopherJSBuildTest := &testCommandRuns{runs: expected, t: t}
b := &Builder{appData: &appData{}, os: "js", srcdir: "myTest", release: true, runner: gopherJSBuildTest}
err := b.build()
if runtime.GOOS == "windows" {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
gopherJSBuildTest.verifyExpectation()
}

func Test_BuildWasmOldVersion(t *testing.T) {
expected := []mockRunner{
{
expectedValue: expectedValue{args: []string{"mod", "edit", "-json"}},
mockReturn: mockReturn{
ret: []byte("{ \"Module\": { \"Path\": \"fyne.io/fyne/v2\"} }"),
},
},
{
expectedValue: expectedValue{args: []string{"version"}},
mockReturn: mockReturn{ret: []byte("go version go1.16.0 windows/amd64")},
},
}

wasmBuildTest := &testCommandRuns{runs: expected, t: t}
b := &Builder{appData: &appData{}, os: "wasm", srcdir: "myTest", runner: wasmBuildTest}
err := b.build()
assert.NotNil(t, err)
wasmBuildTest.verifyExpectation()
}

func Test_BuildLinuxReleaseVersion(t *testing.T) {
relativePath := "." + string(os.PathSeparator) + filepath.Join("cmd", "terminal")

Expand Down
Loading

0 comments on commit 6aef0c3

Please sign in to comment.