Let test binaries run from any dir in the repo

Also spin out generate.WuffsRoot into its own package.
This commit is contained in:
Nigel Tao committed 2018-11-03 08:46:31 +11:00
1 parent e2d1f8e0c2
commit 3854ce3e89
13 files changed
+208 -147

No files matched your search

+2 -32
View File
@@ -15,18 +15,16 @@
package generate
import (
"errors"
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/google/wuffs/lang/check"
"github.com/google/wuffs/lang/parse"
"github.com/google/wuffs/lang/wuffsroot"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
@@ -138,37 +136,9 @@ func ParseFiles(tm *t.Map, filenames []string, opts *parse.Options) (files []*a.
}
func resolveUse(usePath string) ([]byte, error) {
wuffsRoot, err := WuffsRoot()
wuffsRoot, err := wuffsroot.Value()
if err != nil {
return nil, err
}
return ioutil.ReadFile(filepath.Join(wuffsRoot, "gen", "wuffs", filepath.FromSlash(usePath)))
}
var cachedWuffsRoot struct {
mu sync.Mutex
value string
}
func WuffsRoot() (string, error) {
cachedWuffsRoot.mu.Lock()
value := cachedWuffsRoot.value
cachedWuffsRoot.mu.Unlock()
if value != "" {
return value, nil
}
// TODO: look for a WUFFSROOT environment variable?
for _, p := range filepath.SplitList(build.Default.GOPATH) {
p = filepath.Join(p, "src", "github.com", "google", "wuffs")
if o, err := os.Stat(p); err == nil && o.IsDir() {
cachedWuffsRoot.mu.Lock()
cachedWuffsRoot.value = p
cachedWuffsRoot.mu.Unlock()
return p, nil
}
}
return "", errors.New("could not find Wuffs root directory")
}