From aae75197e9de04244a5c3bf470db1b851bf1f393 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 20 Jun 2025 13:16:44 +0200 Subject: [PATCH] Improved build tests --- src/build/Build.go | 36 ------------------------- src/build/Build_test.go | 51 ------------------------------------ src/build/Executable.go | 37 ++++++++++++++++++++++++++ src/build/Executable_test.go | 40 ++++++++++++++++++++++++++++ src/build/New.go | 8 +++--- src/global/global.go | 5 ++++ 6 files changed, 87 insertions(+), 90 deletions(-) delete mode 100644 src/build/Build_test.go create mode 100644 src/build/Executable.go create mode 100644 src/build/Executable_test.go diff --git a/src/build/Build.go b/src/build/Build.go index 21e2e14..cae03d4 100644 --- a/src/build/Build.go +++ b/src/build/Build.go @@ -1,45 +1,9 @@ package build -import ( - "path/filepath" - "strings" -) - // Build describes the parameters for the "build" command. type Build struct { Files []string Arch Arch OS OS Dry bool -} - -// Executable returns the path to the executable. -func (build *Build) Executable() string { - path, err := filepath.Abs(build.Files[0]) - - if err != nil { - panic(err) - } - - if strings.HasSuffix(path, ".q") { - path = fromFileName(path) - } else { - path = fromDirectoryName(path) - } - - if build.OS == Windows { - path += ".exe" - } - - return path -} - -// fromDirectoryName returns the executable path based on the directory name. -func fromDirectoryName(path string) string { - return filepath.Join(path, filepath.Base(path)) -} - -// fromFileName returns the executable path based on the file name. -func fromFileName(path string) string { - return filepath.Join(filepath.Dir(path), strings.TrimSuffix(filepath.Base(path), ".q")) } \ No newline at end of file diff --git a/src/build/Build_test.go b/src/build/Build_test.go deleted file mode 100644 index 516114b..0000000 --- a/src/build/Build_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package build_test - -import ( - "path/filepath" - "testing" - - "git.urbach.dev/cli/q/src/build" - "git.urbach.dev/go/assert" -) - -func TestExecutableFromDirectoryLinux(t *testing.T) { - b := build.New("../../examples/hello") - b.OS = build.Linux - exe := filepath.Base(b.Executable()) - assert.Equal(t, exe, "hello") -} - -func TestExecutableFromFileLinux(t *testing.T) { - b := build.New("../../examples/hello/hello.q") - b.OS = build.Linux - exe := filepath.Base(b.Executable()) - assert.Equal(t, exe, "hello") -} - -func TestExecutableFromDirectoryMac(t *testing.T) { - b := build.New("../../examples/hello") - b.OS = build.Mac - exe := filepath.Base(b.Executable()) - assert.Equal(t, exe, "hello") -} - -func TestExecutableFromFileMac(t *testing.T) { - b := build.New("../../examples/hello/hello.q") - b.OS = build.Mac - exe := filepath.Base(b.Executable()) - assert.Equal(t, exe, "hello") -} - -func TestExecutableFromDirectoryWindows(t *testing.T) { - b := build.New("../../examples/hello") - b.OS = build.Windows - exe := filepath.Base(b.Executable()) - assert.Equal(t, exe, "hello.exe") -} - -func TestExecutableFromFileWindows(t *testing.T) { - b := build.New("../../examples/hello/hello.q") - b.OS = build.Windows - exe := filepath.Base(b.Executable()) - assert.Equal(t, exe, "hello.exe") -} \ No newline at end of file diff --git a/src/build/Executable.go b/src/build/Executable.go new file mode 100644 index 0000000..33f9c71 --- /dev/null +++ b/src/build/Executable.go @@ -0,0 +1,37 @@ +package build + +import ( + "path/filepath" + "strings" +) + +// Executable returns the path to the executable. +func (build *Build) Executable() string { + path, err := filepath.Abs(build.Files[0]) + + if err != nil { + panic(err) + } + + if strings.HasSuffix(path, ".q") { + path = fromFileName(path) + } else { + path = fromDirectoryName(path) + } + + if build.OS == Windows { + path += ".exe" + } + + return path +} + +// fromFileName returns the executable path based on the file name. +func fromFileName(path string) string { + return filepath.Join(filepath.Dir(path), strings.TrimSuffix(filepath.Base(path), ".q")) +} + +// fromDirectoryName returns the executable path based on the directory name. +func fromDirectoryName(path string) string { + return filepath.Join(path, filepath.Base(path)) +} \ No newline at end of file diff --git a/src/build/Executable_test.go b/src/build/Executable_test.go new file mode 100644 index 0000000..5ce972c --- /dev/null +++ b/src/build/Executable_test.go @@ -0,0 +1,40 @@ +package build_test + +import ( + "fmt" + "path/filepath" + "runtime" + "testing" + + "git.urbach.dev/cli/q/src/build" + "git.urbach.dev/cli/q/src/global" + "git.urbach.dev/go/assert" +) + +func TestExecutable(t *testing.T) { + osList := []string{"linux", "darwin", "windows"} + archList := []string{"amd64", "arm64"} + fileList := []string{"../../examples/hello", "../../examples/hello/hello.q"} + + for _, os := range osList { + for _, arch := range archList { + t.Run(fmt.Sprintf("%s-%s", os, arch), func(t *testing.T) { + for _, file := range fileList { + global.HostOS = os + global.HostArch = arch + b := build.New(file) + exe := filepath.Base(b.Executable()) + + if os == "windows" { + assert.Equal(t, exe, "hello.exe") + } else { + assert.Equal(t, exe, "hello") + } + } + }) + } + } + + global.HostOS = runtime.GOOS + global.HostArch = runtime.GOARCH +} \ No newline at end of file diff --git a/src/build/New.go b/src/build/New.go index 6013623..1564eb9 100644 --- a/src/build/New.go +++ b/src/build/New.go @@ -1,6 +1,8 @@ package build -import "runtime" +import ( + "git.urbach.dev/cli/q/src/global" +) // New creates a new build. func New(files ...string) *Build { @@ -8,14 +10,14 @@ func New(files ...string) *Build { Files: files, } - switch runtime.GOARCH { + switch global.HostArch { case "amd64": b.Arch = X86 case "arm64": b.Arch = ARM } - switch runtime.GOOS { + switch global.HostOS { case "linux": b.OS = Linux case "darwin": diff --git a/src/global/global.go b/src/global/global.go index bbdcdef..9b89814 100644 --- a/src/global/global.go +++ b/src/global/global.go @@ -3,6 +3,7 @@ package global import ( "os" "path/filepath" + "runtime" "runtime/debug" ) @@ -12,12 +13,16 @@ var ( Library string Root string WorkingDirectory string + HostOS string + HostArch string ) // init is the very first thing that's executed. // It disables the GC and initializes global variables. func init() { debug.SetGCPercent(-1) + HostOS = runtime.GOOS + HostArch = runtime.GOARCH var err error Executable, err = os.Executable()