llgo.next => llgo
This commit is contained in:
26
cmd/llgo/build_cmd.gox
Normal file
26
cmd/llgo/build_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
self "github.com/goplus/llgo/cmd/internal/build"
|
||||
)
|
||||
|
||||
short "Compile packages and dependencies"
|
||||
|
||||
flagOff
|
||||
|
||||
run args => {
|
||||
self.Cmd.Run self.Cmd, args
|
||||
}
|
||||
26
cmd/llgo/clean_cmd.gox
Normal file
26
cmd/llgo/clean_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
self "github.com/goplus/llgo/cmd/internal/clean"
|
||||
)
|
||||
|
||||
short "Remove object files and cached files"
|
||||
|
||||
flagOff
|
||||
|
||||
run args => {
|
||||
self.Cmd.Run self.Cmd, args
|
||||
}
|
||||
26
cmd/llgo/cmptest_cmd.gox
Normal file
26
cmd/llgo/cmptest_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
self "github.com/goplus/llgo/cmd/internal/run"
|
||||
)
|
||||
|
||||
short "Compile and run with llgo, compare result (stdout/stderr/exitcode) with go or llgo.expect; generate llgo.expect file if -gen is specified"
|
||||
|
||||
flagOff
|
||||
|
||||
run args => {
|
||||
self.CmpTestCmd.Run self.CmpTestCmd, args
|
||||
}
|
||||
20
cmd/llgo/get_cmd.gox
Normal file
20
cmd/llgo/get_cmd.gox
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
short "Add dependencies to current module and install them"
|
||||
|
||||
run args => {
|
||||
panic "todo"
|
||||
}
|
||||
194
cmd/llgo/gop_autogen.go
Normal file
194
cmd/llgo/gop_autogen.go
Normal file
@@ -0,0 +1,194 @@
|
||||
// Code generated by gop (Go+); DO NOT EDIT.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/goplus/cobra/xcmd"
|
||||
build1 "github.com/goplus/llgo/cmd/internal/build"
|
||||
clean1 "github.com/goplus/llgo/cmd/internal/clean"
|
||||
install1 "github.com/goplus/llgo/cmd/internal/install"
|
||||
run1 "github.com/goplus/llgo/cmd/internal/run"
|
||||
test1 "github.com/goplus/llgo/cmd/internal/test"
|
||||
"github.com/goplus/llgo/internal/env"
|
||||
"github.com/qiniu/x/stringutil"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const _ = true
|
||||
|
||||
type build struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type clean struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type cmptest struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type get struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type install struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type run struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type test struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type version struct {
|
||||
xcmd.Command
|
||||
*App
|
||||
}
|
||||
type App struct {
|
||||
xcmd.App
|
||||
}
|
||||
|
||||
func (this *App) Main() {
|
||||
_gop_obj0 := &build{App: this}
|
||||
_gop_obj1 := &clean{App: this}
|
||||
_gop_obj2 := &cmptest{App: this}
|
||||
_gop_obj3 := &get{App: this}
|
||||
_gop_obj4 := &install{App: this}
|
||||
_gop_obj5 := &run{App: this}
|
||||
_gop_obj6 := &test{App: this}
|
||||
_gop_obj7 := &version{App: this}
|
||||
xcmd.Gopt_App_Main(this, _gop_obj0, _gop_obj1, _gop_obj2, _gop_obj3, _gop_obj4, _gop_obj5, _gop_obj6, _gop_obj7)
|
||||
}
|
||||
//line cmd/llgo/build_cmd.gox:20
|
||||
func (this *build) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/build_cmd.gox:20:1
|
||||
this.Short("Compile packages and dependencies")
|
||||
//line cmd/llgo/build_cmd.gox:22:1
|
||||
this.FlagOff()
|
||||
//line cmd/llgo/build_cmd.gox:24:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/build_cmd.gox:25:1
|
||||
build1.Cmd.Run(build1.Cmd, args)
|
||||
})
|
||||
}
|
||||
func (this *build) Classfname() string {
|
||||
return "build"
|
||||
}
|
||||
//line cmd/llgo/clean_cmd.gox:20
|
||||
func (this *clean) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/clean_cmd.gox:20:1
|
||||
this.Short("Remove object files and cached files")
|
||||
//line cmd/llgo/clean_cmd.gox:22:1
|
||||
this.FlagOff()
|
||||
//line cmd/llgo/clean_cmd.gox:24:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/clean_cmd.gox:25:1
|
||||
clean1.Cmd.Run(clean1.Cmd, args)
|
||||
})
|
||||
}
|
||||
func (this *clean) Classfname() string {
|
||||
return "clean"
|
||||
}
|
||||
//line cmd/llgo/cmptest_cmd.gox:20
|
||||
func (this *cmptest) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/cmptest_cmd.gox:20:1
|
||||
this.Short("Compile and run with llgo, compare result (stdout/stderr/exitcode) with go or llgo.expect; generate llgo.expect file if -gen is specified")
|
||||
//line cmd/llgo/cmptest_cmd.gox:22:1
|
||||
this.FlagOff()
|
||||
//line cmd/llgo/cmptest_cmd.gox:24:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/cmptest_cmd.gox:25:1
|
||||
run1.CmpTestCmd.Run(run1.CmpTestCmd, args)
|
||||
})
|
||||
}
|
||||
func (this *cmptest) Classfname() string {
|
||||
return "cmptest"
|
||||
}
|
||||
//line cmd/llgo/get_cmd.gox:16
|
||||
func (this *get) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/get_cmd.gox:16:1
|
||||
this.Short("Add dependencies to current module and install them")
|
||||
//line cmd/llgo/get_cmd.gox:18:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/get_cmd.gox:19:1
|
||||
panic("todo")
|
||||
})
|
||||
}
|
||||
func (this *get) Classfname() string {
|
||||
return "get"
|
||||
}
|
||||
//line cmd/llgo/install_cmd.gox:20
|
||||
func (this *install) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/install_cmd.gox:20:1
|
||||
this.Short("Compile and install packages and dependencies")
|
||||
//line cmd/llgo/install_cmd.gox:22:1
|
||||
this.FlagOff()
|
||||
//line cmd/llgo/install_cmd.gox:24:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/install_cmd.gox:25:1
|
||||
install1.Cmd.Run(install1.Cmd, args)
|
||||
})
|
||||
}
|
||||
func (this *install) Classfname() string {
|
||||
return "install"
|
||||
}
|
||||
//line cmd/llgo/run_cmd.gox:20
|
||||
func (this *run) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/run_cmd.gox:20:1
|
||||
this.Short("Compile and run Go program")
|
||||
//line cmd/llgo/run_cmd.gox:22:1
|
||||
this.FlagOff()
|
||||
//line cmd/llgo/run_cmd.gox:24:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/run_cmd.gox:25:1
|
||||
run1.Cmd.Run(run1.Cmd, args)
|
||||
})
|
||||
}
|
||||
func (this *run) Classfname() string {
|
||||
return "run"
|
||||
}
|
||||
//line cmd/llgo/test_cmd.gox:20
|
||||
func (this *test) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/test_cmd.gox:20:1
|
||||
this.Short("Compile and run Go test")
|
||||
//line cmd/llgo/test_cmd.gox:22:1
|
||||
this.FlagOff()
|
||||
//line cmd/llgo/test_cmd.gox:24:1
|
||||
this.Run__1(func(args []string) {
|
||||
//line cmd/llgo/test_cmd.gox:25:1
|
||||
test1.Cmd.Run(test1.Cmd, args)
|
||||
})
|
||||
}
|
||||
func (this *test) Classfname() string {
|
||||
return "test"
|
||||
}
|
||||
//line cmd/llgo/version_cmd.gox:22
|
||||
func (this *version) Main(_gop_arg0 string) {
|
||||
this.Command.Main(_gop_arg0)
|
||||
//line cmd/llgo/version_cmd.gox:22:1
|
||||
this.Short("Print LLGo version")
|
||||
//line cmd/llgo/version_cmd.gox:24:1
|
||||
this.Run__0(func() {
|
||||
//line cmd/llgo/version_cmd.gox:25:1
|
||||
fmt.Println(stringutil.Concat("llgo ", env.Version(), " ", runtime.GOOS, "/", runtime.GOARCH))
|
||||
})
|
||||
}
|
||||
func (this *version) Classfname() string {
|
||||
return "version"
|
||||
}
|
||||
func main() {
|
||||
//line cmd/llgo/version_cmd.gox:24:1
|
||||
new(App).Main()
|
||||
}
|
||||
26
cmd/llgo/install_cmd.gox
Normal file
26
cmd/llgo/install_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
self "github.com/goplus/llgo/cmd/internal/install"
|
||||
)
|
||||
|
||||
short "Compile and install packages and dependencies"
|
||||
|
||||
flagOff
|
||||
|
||||
run args => {
|
||||
self.Cmd.Run self.Cmd, args
|
||||
}
|
||||
105
cmd/llgo/llgo.go
105
cmd/llgo/llgo.go
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/qiniu/x/log"
|
||||
|
||||
"github.com/goplus/llgo/cmd/internal/base"
|
||||
"github.com/goplus/llgo/cmd/internal/build"
|
||||
"github.com/goplus/llgo/cmd/internal/clean"
|
||||
"github.com/goplus/llgo/cmd/internal/get"
|
||||
"github.com/goplus/llgo/cmd/internal/help"
|
||||
"github.com/goplus/llgo/cmd/internal/install"
|
||||
"github.com/goplus/llgo/cmd/internal/run"
|
||||
"github.com/goplus/llgo/cmd/internal/test"
|
||||
"github.com/goplus/llgo/cmd/internal/version"
|
||||
"github.com/goplus/llgo/internal/mockable"
|
||||
)
|
||||
|
||||
func mainUsage() {
|
||||
help.PrintUsage(os.Stderr, base.Llgo)
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.Usage = mainUsage
|
||||
base.Llgo.Commands = []*base.Command{
|
||||
build.Cmd,
|
||||
install.Cmd,
|
||||
get.Cmd,
|
||||
run.Cmd,
|
||||
run.CmpTestCmd,
|
||||
test.Cmd,
|
||||
clean.Cmd,
|
||||
version.Cmd,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
if len(args) < 1 {
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
log.SetFlags(log.Ldefault &^ log.LstdFlags)
|
||||
|
||||
base.CmdName = args[0] // for error messages
|
||||
if args[0] == "help" {
|
||||
help.Help(os.Stderr, args[1:])
|
||||
return
|
||||
}
|
||||
|
||||
BigCmdLoop:
|
||||
for bigCmd := base.Llgo; ; {
|
||||
for _, cmd := range bigCmd.Commands {
|
||||
if cmd.Name() != args[0] {
|
||||
continue
|
||||
}
|
||||
args = args[1:]
|
||||
if len(cmd.Commands) > 0 {
|
||||
bigCmd = cmd
|
||||
if len(args) == 0 {
|
||||
help.PrintUsage(os.Stderr, bigCmd)
|
||||
mockable.Exit(2)
|
||||
}
|
||||
if args[0] == "help" {
|
||||
help.Help(os.Stderr, append(strings.Split(base.CmdName, " "), args[1:]...))
|
||||
return
|
||||
}
|
||||
base.CmdName += " " + args[0]
|
||||
continue BigCmdLoop
|
||||
}
|
||||
if !cmd.Runnable() {
|
||||
continue
|
||||
}
|
||||
cmd.Run(cmd, args)
|
||||
return
|
||||
}
|
||||
helpArg := ""
|
||||
if i := strings.LastIndex(base.CmdName, " "); i >= 0 {
|
||||
helpArg = " " + base.CmdName[:i]
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "llgo %s: unknown command\nRun 'llgo help%s' for usage.\n", base.CmdName, helpArg)
|
||||
mockable.Exit(2)
|
||||
}
|
||||
}
|
||||
@@ -1,396 +0,0 @@
|
||||
//go:build !llgo
|
||||
// +build !llgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/goplus/llgo/internal/mockable"
|
||||
)
|
||||
|
||||
func init() {
|
||||
origWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Set LLGO_ROOT to project root
|
||||
llgoRoot := filepath.Join(origWd, "../..")
|
||||
if err := os.Setenv("LLGO_ROOT", llgoRoot); err != nil {
|
||||
panic(fmt.Sprintf("Failed to set LLGO_ROOT: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
func setupTestProject(t *testing.T) string {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create main.go
|
||||
mainGo := filepath.Join(tmpDir, "main.go")
|
||||
err := os.WriteFile(mainGo, []byte(`package main
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
|
||||
func main() {
|
||||
var arg string = "LLGO"
|
||||
if len(os.Args) > 1 {
|
||||
arg = os.Args[1]
|
||||
}
|
||||
switch arg {
|
||||
case "stderr":
|
||||
fmt.Fprintln(os.Stderr, "Hello, World!")
|
||||
case "exit":
|
||||
os.Exit(1)
|
||||
default:
|
||||
fmt.Println("Hello, " + arg + "!")
|
||||
}
|
||||
}
|
||||
`), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create main.go: %v", err)
|
||||
}
|
||||
|
||||
// Create llgo.expect for cmptest
|
||||
expectFile := filepath.Join(tmpDir, "llgo.expect")
|
||||
err = os.WriteFile(expectFile, []byte(`#stdout
|
||||
Hello, LLGO!
|
||||
|
||||
#stderr
|
||||
|
||||
#exit 0
|
||||
`), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create llgo.expect: %v", err)
|
||||
}
|
||||
|
||||
// Create a go.mod file
|
||||
err = os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module testproject
|
||||
|
||||
go 1.22.0
|
||||
`), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to write go.mod: %v", err)
|
||||
}
|
||||
|
||||
return tmpDir
|
||||
}
|
||||
|
||||
func TestProjectCommands(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
wantErr bool
|
||||
setup func(dir string) error
|
||||
}{
|
||||
{
|
||||
name: "build command",
|
||||
args: []string{"llgo", "build", "-o", "testproject", "."},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "install command",
|
||||
args: []string{"llgo", "install", "."},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "run command",
|
||||
args: []string{"llgo", "run", "."},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "run command with file",
|
||||
args: []string{"llgo", "run", "main.go"},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "run command verbose",
|
||||
args: []string{"llgo", "run", "-v", "."},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "clean command",
|
||||
args: []string{"llgo", "clean"},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "cmptest command",
|
||||
args: []string{"llgo", "cmptest", "."},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "cmptest command with gen",
|
||||
args: []string{"llgo", "cmptest", "-gen", "."},
|
||||
wantErr: false,
|
||||
setup: func(dir string) error {
|
||||
return os.Remove(filepath.Join(dir, "llgo.expect"))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cmptest command with args",
|
||||
args: []string{"llgo", "cmptest", ".", "World"},
|
||||
wantErr: true,
|
||||
setup: func(dir string) error {
|
||||
return os.WriteFile(filepath.Join(dir, "llgo.expect"), []byte(`#stdout
|
||||
Hello, World!
|
||||
|
||||
#stderr
|
||||
|
||||
#exit 0
|
||||
`), 0644)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cmptest command with different stderr",
|
||||
args: []string{"llgo", "cmptest", ".", "stderr"},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "cmptest command with different exit code",
|
||||
args: []string{"llgo", "cmptest", ".", "exit"},
|
||||
wantErr: true,
|
||||
setup: func(dir string) error {
|
||||
// Create llgo.expect with different exit code
|
||||
return os.WriteFile(filepath.Join(dir, "llgo.expect"), []byte(`#stdout
|
||||
Hello, LLGO!
|
||||
|
||||
#stderr
|
||||
|
||||
#exit 1
|
||||
`), 0644)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cmptest command without llgo.expect to compare with go run",
|
||||
args: []string{"llgo", "cmptest", "."},
|
||||
wantErr: false,
|
||||
setup: func(dir string) error {
|
||||
return os.Remove(filepath.Join(dir, "llgo.expect"))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cmptest command with different go run output",
|
||||
args: []string{"llgo", "cmptest", "."},
|
||||
wantErr: true,
|
||||
setup: func(dir string) error {
|
||||
// Remove llgo.expect file
|
||||
if err := os.Remove(filepath.Join(dir, "llgo.expect")); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create main_llgo.go for llgo
|
||||
if err := os.WriteFile(filepath.Join(dir, "main_llgo.go"), []byte(`//go:build llgo
|
||||
// +build llgo
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, LLGO!")
|
||||
}
|
||||
`), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create main_go.go for go
|
||||
return os.WriteFile(filepath.Join(dir, "main.go"), []byte(`//go:build !llgo
|
||||
// +build !llgo
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, Go!")
|
||||
}
|
||||
`), 0644)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var testErr error
|
||||
const maxAttempts = 4
|
||||
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
||||
testErr = func() error {
|
||||
// Create a new test directory for each test case
|
||||
tmpDir := setupTestProject(t)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// Change to test project directory
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
return fmt.Errorf("Failed to change directory: %v", err)
|
||||
}
|
||||
|
||||
if tt.setup != nil {
|
||||
if err := tt.setup(tmpDir); err != nil {
|
||||
return fmt.Errorf("Failed to setup test: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
mockable.EnableMock()
|
||||
var exitErr error
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if r != "exit" {
|
||||
if !tt.wantErr {
|
||||
exitErr = fmt.Errorf("unexpected panic: %v", r)
|
||||
}
|
||||
} else {
|
||||
exitCode := mockable.ExitCode()
|
||||
if (exitCode != 0) != tt.wantErr {
|
||||
exitErr = fmt.Errorf("got exit code %d, wantErr %v", exitCode, tt.wantErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
os.Args = tt.args
|
||||
main()
|
||||
}()
|
||||
if exitErr != nil {
|
||||
return exitErr
|
||||
}
|
||||
|
||||
// For build/install commands, check if binary was created
|
||||
if strings.HasPrefix(tt.name, "build") || strings.HasPrefix(tt.name, "install") {
|
||||
binName := "testproject"
|
||||
var binPath string
|
||||
if strings.HasPrefix(tt.name, "install") {
|
||||
// For install command, binary should be in GOBIN or GOPATH/bin
|
||||
gobin := os.Getenv("GOBIN")
|
||||
if gobin == "" {
|
||||
gopath := os.Getenv("GOPATH")
|
||||
if gopath == "" {
|
||||
gopath = filepath.Join(os.Getenv("HOME"), "go")
|
||||
}
|
||||
gobin = filepath.Join(gopath, "bin")
|
||||
}
|
||||
binPath = filepath.Join(gobin, binName)
|
||||
} else {
|
||||
// For build command, binary should be in current directory
|
||||
binPath = filepath.Join(tmpDir, binName)
|
||||
}
|
||||
if _, err := os.Stat(binPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("Binary %s was not created at %s", binName, binPath)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
if testErr == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if attempt < maxAttempts {
|
||||
t.Logf("Test failed on attempt %v/%v: %v. Retrying...", testErr, attempt, maxAttempts)
|
||||
}
|
||||
}
|
||||
|
||||
if testErr != nil {
|
||||
t.Error(testErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandHandling(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "version command",
|
||||
args: []string{"llgo", "version"},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "help command",
|
||||
args: []string{"llgo", "help"},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid command",
|
||||
args: []string{"llgo", "invalid"},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if r != "exit" {
|
||||
t.Errorf("unexpected panic: %v", r)
|
||||
}
|
||||
exitCode := mockable.ExitCode()
|
||||
if (exitCode != 0) != tt.wantErr {
|
||||
t.Errorf("got exit code %d, wantErr %v", exitCode, tt.wantErr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
os.Args = tt.args
|
||||
main()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
}{
|
||||
{
|
||||
name: "help build",
|
||||
args: []string{"llgo", "help", "build"},
|
||||
},
|
||||
{
|
||||
name: "help install",
|
||||
args: []string{"llgo", "help", "install"},
|
||||
},
|
||||
{
|
||||
name: "help run",
|
||||
args: []string{"llgo", "help", "run"},
|
||||
},
|
||||
{
|
||||
name: "help version",
|
||||
args: []string{"llgo", "help", "version"},
|
||||
},
|
||||
{
|
||||
name: "help clean",
|
||||
args: []string{"llgo", "help", "clean"},
|
||||
},
|
||||
{
|
||||
name: "help cmptest",
|
||||
args: []string{"llgo", "help", "cmptest"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if r != "exit" {
|
||||
t.Errorf("unexpected panic: %v", r)
|
||||
}
|
||||
exitCode := mockable.ExitCode()
|
||||
if exitCode != 0 {
|
||||
t.Errorf("got exit code %d, want 0", exitCode)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
os.Args = tt.args
|
||||
main()
|
||||
})
|
||||
}
|
||||
}
|
||||
26
cmd/llgo/run_cmd.gox
Normal file
26
cmd/llgo/run_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
self "github.com/goplus/llgo/cmd/internal/run"
|
||||
)
|
||||
|
||||
short "Compile and run Go program"
|
||||
|
||||
flagOff
|
||||
|
||||
run args => {
|
||||
self.Cmd.Run self.Cmd, args
|
||||
}
|
||||
26
cmd/llgo/test_cmd.gox
Normal file
26
cmd/llgo/test_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
self "github.com/goplus/llgo/cmd/internal/test"
|
||||
)
|
||||
|
||||
short "Compile and run Go test"
|
||||
|
||||
flagOff
|
||||
|
||||
run args => {
|
||||
self.Cmd.Run self.Cmd, args
|
||||
}
|
||||
26
cmd/llgo/version_cmd.gox
Normal file
26
cmd/llgo/version_cmd.gox
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/goplus/llgo/internal/env"
|
||||
)
|
||||
|
||||
short "Print LLGo version"
|
||||
|
||||
run => {
|
||||
echo "llgo ${env.version} ${runtime.GOOS}/${runtime.GOARCH}"
|
||||
}
|
||||
Reference in New Issue
Block a user