test: llgo clean, llgo cmptest, llgo cmptest -gen
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -9,84 +10,67 @@ import (
|
|||||||
"github.com/goplus/llgo/compiler/internal/mockable"
|
"github.com/goplus/llgo/compiler/internal/mockable"
|
||||||
)
|
)
|
||||||
|
|
||||||
var origWd string
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
origWd, err := os.Getwd()
|
||||||
origWd, err = os.Getwd()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
type testContext struct {
|
// Set LLGO_ROOT to project root
|
||||||
origLLGORoot string
|
|
||||||
tmpDir string
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupTest(t *testing.T) *testContext {
|
|
||||||
ctx := &testContext{}
|
|
||||||
|
|
||||||
// Save original state
|
|
||||||
ctx.origLLGORoot = os.Getenv("LLGO_ROOT")
|
|
||||||
|
|
||||||
// Create temporary LLGO_ROOT
|
|
||||||
tmpDir, err := os.MkdirTemp("", "llgo-root-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
ctx.tmpDir = tmpDir
|
|
||||||
|
|
||||||
// Set LLGO_ROOT
|
|
||||||
llgoRoot := filepath.Join(origWd, "../../..")
|
llgoRoot := filepath.Join(origWd, "../../..")
|
||||||
if err := os.Setenv("LLGO_ROOT", llgoRoot); err != nil {
|
if err := os.Setenv("LLGO_ROOT", llgoRoot); err != nil {
|
||||||
os.RemoveAll(tmpDir)
|
panic(fmt.Sprintf("Failed to set LLGO_ROOT: %v", err))
|
||||||
t.Fatalf("Failed to set LLGO_ROOT: %v", err)
|
|
||||||
}
|
|
||||||
t.Logf("LLGO_ROOT set to: %s", llgoRoot)
|
|
||||||
mockable.EnableMock()
|
|
||||||
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
|
|
||||||
func teardownTest(ctx *testContext) {
|
|
||||||
os.Chdir(origWd)
|
|
||||||
os.Setenv("LLGO_ROOT", ctx.origLLGORoot)
|
|
||||||
if ctx.tmpDir != "" {
|
|
||||||
os.RemoveAll(ctx.tmpDir)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupTestProject(t *testing.T) string {
|
func setupTestProject(t *testing.T) string {
|
||||||
// Create a temporary directory for the test project
|
tmpDir := t.TempDir()
|
||||||
tmpDir, err := os.MkdirTemp("", "llgo-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a simple Go program
|
// Create main.go
|
||||||
mainFile := filepath.Join(tmpDir, "main.go")
|
mainGo := filepath.Join(tmpDir, "main.go")
|
||||||
err = os.WriteFile(mainFile, []byte(`package main
|
err := os.WriteFile(mainGo, []byte(`package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
import "os"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Hello, LLGO!")
|
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)
|
`), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.RemoveAll(tmpDir)
|
t.Fatalf("Failed to create main.go: %v", err)
|
||||||
t.Fatalf("Failed to write 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
|
// Create a go.mod file
|
||||||
goMod := filepath.Join(tmpDir, "go.mod")
|
err = os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module testproject
|
||||||
err = os.WriteFile(goMod, []byte(`module testproject
|
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
`), 0644)
|
`), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.RemoveAll(tmpDir)
|
|
||||||
t.Fatalf("Failed to write go.mod: %v", err)
|
t.Fatalf("Failed to write go.mod: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,22 +78,11 @@ go 1.20
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProjectCommands(t *testing.T) {
|
func TestProjectCommands(t *testing.T) {
|
||||||
// Setup test project
|
|
||||||
tmpDir := setupTestProject(t)
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
ctx := setupTest(t)
|
|
||||||
defer teardownTest(ctx)
|
|
||||||
|
|
||||||
// Change to test project directory
|
|
||||||
if err := os.Chdir(tmpDir); err != nil {
|
|
||||||
t.Fatalf("Failed to change to test directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
|
setup func(dir string) error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "build command",
|
name: "build command",
|
||||||
@@ -127,29 +100,147 @@ func TestProjectCommands(t *testing.T) {
|
|||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "run command",
|
name: "run command with file",
|
||||||
args: []string{"llgo", "run", "main.go"},
|
args: []string{"llgo", "run", "main.go"},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "run command",
|
name: "run command verbose",
|
||||||
args: []string{"llgo", "run", "-v", "."},
|
args: []string{"llgo", "run", "-v", "."},
|
||||||
wantErr: false,
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// 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 {
|
||||||
|
t.Fatalf("Failed to change directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.setup != nil {
|
||||||
|
if err := tt.setup(tmpDir); err != nil {
|
||||||
|
t.Fatalf("Failed to setup test: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mockable.EnableMock()
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
if r != "exit" {
|
if r != "exit" {
|
||||||
|
if !tt.wantErr {
|
||||||
t.Errorf("unexpected panic: %v", r)
|
t.Errorf("unexpected panic: %v", r)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
exitCode := mockable.ExitCode()
|
exitCode := mockable.ExitCode()
|
||||||
if (exitCode != 0) != tt.wantErr {
|
if (exitCode != 0) != tt.wantErr {
|
||||||
t.Errorf("got exit code %d, wantErr %v", exitCode, tt.wantErr)
|
t.Errorf("got exit code %d, wantErr %v", exitCode, tt.wantErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
os.Args = tt.args
|
os.Args = tt.args
|
||||||
@@ -158,8 +249,24 @@ func TestProjectCommands(t *testing.T) {
|
|||||||
// For build/install commands, check if binary was created
|
// For build/install commands, check if binary was created
|
||||||
if strings.HasPrefix(tt.name, "build") || strings.HasPrefix(tt.name, "install") {
|
if strings.HasPrefix(tt.name, "build") || strings.HasPrefix(tt.name, "install") {
|
||||||
binName := "testproject"
|
binName := "testproject"
|
||||||
if _, err := os.Stat(binName); os.IsNotExist(err) {
|
var binPath string
|
||||||
t.Errorf("Binary %s was not created", binName)
|
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) {
|
||||||
|
t.Errorf("Binary %s was not created at %s", binName, binPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -167,9 +274,6 @@ func TestProjectCommands(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandHandling(t *testing.T) {
|
func TestCommandHandling(t *testing.T) {
|
||||||
ctx := setupTest(t)
|
|
||||||
defer teardownTest(ctx)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
@@ -213,9 +317,6 @@ func TestCommandHandling(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHelpCommand(t *testing.T) {
|
func TestHelpCommand(t *testing.T) {
|
||||||
ctx := setupTest(t)
|
|
||||||
defer teardownTest(ctx)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
@@ -236,6 +337,14 @@ func TestHelpCommand(t *testing.T) {
|
|||||||
name: "help version",
|
name: "help version",
|
||||||
args: []string{"llgo", "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 {
|
for _, tt := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user