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")
}
+52
View File
@@ -0,0 +1,52 @@
// Copyright 2018 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wuffsroot
import (
"errors"
"go/build"
"os"
"path/filepath"
"sync"
)
var cache struct {
mu sync.Mutex
value string
}
func Value() (string, error) {
cache.mu.Lock()
value := cache.value
cache.mu.Unlock()
if value != "" {
return value, nil
}
// TODO: look for a WUFFSROOT environment variable? Also check that
// wuffs-root-directory.txt exists?
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() {
cache.mu.Lock()
cache.value = p
cache.mu.Unlock()
return p, nil
}
}
return "", errors.New("could not find Wuffs root directory")
}