Improved build tests
All checks were successful
/ test (push) Successful in 17s

This commit is contained in:
Eduard Urbach 2025-06-20 13:16:44 +02:00
parent c35f8ff05c
commit aae75197e9
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
6 changed files with 87 additions and 90 deletions

View file

@ -1,45 +1,9 @@
package build package build
import (
"path/filepath"
"strings"
)
// Build describes the parameters for the "build" command. // Build describes the parameters for the "build" command.
type Build struct { type Build struct {
Files []string Files []string
Arch Arch Arch Arch
OS OS OS OS
Dry bool 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"))
} }

View file

@ -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")
}

37
src/build/Executable.go Normal file
View file

@ -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))
}

View file

@ -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
}

View file

@ -1,6 +1,8 @@
package build package build
import "runtime" import (
"git.urbach.dev/cli/q/src/global"
)
// New creates a new build. // New creates a new build.
func New(files ...string) *Build { func New(files ...string) *Build {
@ -8,14 +10,14 @@ func New(files ...string) *Build {
Files: files, Files: files,
} }
switch runtime.GOARCH { switch global.HostArch {
case "amd64": case "amd64":
b.Arch = X86 b.Arch = X86
case "arm64": case "arm64":
b.Arch = ARM b.Arch = ARM
} }
switch runtime.GOOS { switch global.HostOS {
case "linux": case "linux":
b.OS = Linux b.OS = Linux
case "darwin": case "darwin":

View file

@ -3,6 +3,7 @@ package global
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"runtime/debug" "runtime/debug"
) )
@ -12,12 +13,16 @@ var (
Library string Library string
Root string Root string
WorkingDirectory string WorkingDirectory string
HostOS string
HostArch string
) )
// init is the very first thing that's executed. // init is the very first thing that's executed.
// It disables the GC and initializes global variables. // It disables the GC and initializes global variables.
func init() { func init() {
debug.SetGCPercent(-1) debug.SetGCPercent(-1)
HostOS = runtime.GOOS
HostArch = runtime.GOARCH
var err error var err error
Executable, err = os.Executable() Executable, err = os.Executable()