diff --git a/src/build/Executable_test.go b/src/build/Executable_test.go index 5ce972c..6c905d6 100644 --- a/src/build/Executable_test.go +++ b/src/build/Executable_test.go @@ -20,8 +20,8 @@ func TestExecutable(t *testing.T) { 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 + global.OS = os + global.Arch = arch b := build.New(file) exe := filepath.Base(b.Executable()) @@ -35,6 +35,6 @@ func TestExecutable(t *testing.T) { } } - global.HostOS = runtime.GOOS - global.HostArch = runtime.GOARCH + global.OS = runtime.GOOS + global.Arch = runtime.GOARCH } \ No newline at end of file diff --git a/src/build/New.go b/src/build/New.go index 1564eb9..d7ed351 100644 --- a/src/build/New.go +++ b/src/build/New.go @@ -10,14 +10,14 @@ func New(files ...string) *Build { Files: files, } - switch global.HostArch { + switch global.Arch { case "amd64": b.Arch = X86 case "arm64": b.Arch = ARM } - switch global.HostOS { + switch global.OS { case "linux": b.OS = Linux case "darwin": diff --git a/src/global/findLibrary.go b/src/global/findLibrary.go new file mode 100644 index 0000000..dedcf01 --- /dev/null +++ b/src/global/findLibrary.go @@ -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) + } +} \ No newline at end of file diff --git a/src/global/global.go b/src/global/init.go similarity index 59% rename from src/global/global.go rename to src/global/init.go index 9b89814..b3add9c 100644 --- a/src/global/global.go +++ b/src/global/init.go @@ -9,20 +9,20 @@ import ( // Global variables that are useful in all packages. var ( + Arch string Executable string Library string + OS 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 + OS = runtime.GOOS + Arch = runtime.GOARCH var err error Executable, err = os.Executable() @@ -50,25 +50,4 @@ func init() { if err != nil || !stat.IsDir() { 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) - } } \ No newline at end of file diff --git a/src/global/init_test.go b/src/global/init_test.go new file mode 100644 index 0000000..09b906e --- /dev/null +++ b/src/global/init_test.go @@ -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) +} \ No newline at end of file