This commit is contained in:
parent
aae75197e9
commit
b802d878f3
5 changed files with 55 additions and 31 deletions
27
src/global/findLibrary.go
Normal file
27
src/global/findLibrary.go
Normal 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)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
18
src/global/init_test.go
Normal file
18
src/global/init_test.go
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue