Added global tests
All checks were successful
/ test (push) Successful in 17s

This commit is contained in:
Eduard Urbach 2025-06-20 14:40:00 +02:00
parent aae75197e9
commit b802d878f3
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
5 changed files with 55 additions and 31 deletions

View file

@ -20,8 +20,8 @@ func TestExecutable(t *testing.T) {
for _, arch := range archList { for _, arch := range archList {
t.Run(fmt.Sprintf("%s-%s", os, arch), func(t *testing.T) { t.Run(fmt.Sprintf("%s-%s", os, arch), func(t *testing.T) {
for _, file := range fileList { for _, file := range fileList {
global.HostOS = os global.OS = os
global.HostArch = arch global.Arch = arch
b := build.New(file) b := build.New(file)
exe := filepath.Base(b.Executable()) exe := filepath.Base(b.Executable())
@ -35,6 +35,6 @@ func TestExecutable(t *testing.T) {
} }
} }
global.HostOS = runtime.GOOS global.OS = runtime.GOOS
global.HostArch = runtime.GOARCH global.Arch = runtime.GOARCH
} }

View file

@ -10,14 +10,14 @@ func New(files ...string) *Build {
Files: files, Files: files,
} }
switch global.HostArch { switch global.Arch {
case "amd64": case "amd64":
b.Arch = X86 b.Arch = X86
case "arm64": case "arm64":
b.Arch = ARM b.Arch = ARM
} }
switch global.HostOS { switch global.OS {
case "linux": case "linux":
b.OS = Linux b.OS = Linux
case "darwin": case "darwin":

27
src/global/findLibrary.go Normal file
View file

@ -0,0 +1,27 @@
package global
import (
"os"
"path/filepath"
)
// findLibrary tries to go up each directory from the working directory and check for the existence of a "lib" directory.
// This is needed for tests to work correctly.
func findLibrary() {
dir := WorkingDirectory
for {
Library = filepath.Join(dir, "lib")
stat, err := os.Stat(Library)
if err == nil && stat.IsDir() {
return
}
if dir == "/" {
panic("standard library not found")
}
dir = filepath.Dir(dir)
}
}

View file

@ -9,20 +9,20 @@ import (
// Global variables that are useful in all packages. // Global variables that are useful in all packages.
var ( var (
Arch string
Executable string Executable string
Library string Library string
OS 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 OS = runtime.GOOS
HostArch = runtime.GOARCH Arch = runtime.GOARCH
var err error var err error
Executable, err = os.Executable() Executable, err = os.Executable()
@ -51,24 +51,3 @@ func init() {
findLibrary() findLibrary()
} }
} }
// findLibrary tries to go up each directory from the working directory and check for the existence of a "lib" directory.
// This is needed for tests to work correctly.
func findLibrary() {
dir := WorkingDirectory
for {
Library = filepath.Join(dir, "lib")
stat, err := os.Stat(Library)
if err == nil && stat.IsDir() {
return
}
if dir == "/" {
panic("standard library not found")
}
dir = filepath.Dir(dir)
}
}

18
src/global/init_test.go Normal file
View file

@ -0,0 +1,18 @@
package global_test
import (
"runtime"
"testing"
"git.urbach.dev/cli/q/src/global"
"git.urbach.dev/go/assert"
)
func TestInit(t *testing.T) {
assert.Equal(t, global.Arch, runtime.GOARCH)
assert.True(t, len(global.Executable) > 0)
assert.True(t, len(global.Library) > 0)
assert.Equal(t, global.OS, runtime.GOOS)
assert.True(t, len(global.Root) > 0)
assert.True(t, len(global.WorkingDirectory) > 0)
}