Skip to content

Commit

Permalink
Improve imports (google#66)
Browse files Browse the repository at this point in the history
* Improve imports

* Nonexistent file is now a runtime error (used to be ignored)
* Permission denied is a runtime error (used to be an internal error)
* Basic tests added for imports
* Renamed tests from .input to .jsonnet
	* It's standard
	* It's compatible with C++ test suite
	* Editors enable syntax highlighting based on extension
	* Importing .input looks weird
  • Loading branch information
sbarzowski authored and sparkprime committed Sep 22, 2017
1 parent 8638a21 commit fe62ec4
Show file tree
Hide file tree
Showing 303 changed files with 36 additions and 14 deletions.
12 changes: 8 additions & 4 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,18 @@ func (cache *ImportCache) importData(key importCacheKey) *ImportCacheValue {
return &val
}

func (cache *ImportCache) ImportString(codeDir, importedPath string) (*valueString, error) {
func (cache *ImportCache) ImportString(codeDir, importedPath string, e *evaluator) (*valueString, error) {
data := cache.importData(importCacheKey{codeDir, importedPath})
if data.data.err != nil {
return nil, data.data.err
return nil, e.Error(data.data.err.Error())
}
// TODO(sbarzowski) wrap error in runtime error
return makeValueString(data.data.content), nil
}

func (cache *ImportCache) ImportCode(codeDir, importedPath string, e *evaluator) (value, error) {
cached := cache.importData(importCacheKey{codeDir, importedPath})
if cached.data.err != nil {
return nil, cached.data.err
return nil, e.Error(cached.data.err.Error())
}
if cached.asCode == nil {
node, err := snippetToAST(cached.data.foundHere, cached.data.content)
Expand Down Expand Up @@ -141,6 +140,11 @@ func (importer *FileImporter) Import(dir, importedPath string) *ImportedData {
}
}

if !found {
return &ImportedData{
err: fmt.Errorf("Couldn't open import %#v: No match locally or in the Jsonnet library paths.", importedPath),
}
}
return &ImportedData{content: string(content), foundHere: foundHere}
}

Expand Down
8 changes: 3 additions & 5 deletions interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,12 @@ func (i *interpreter) evaluate(a ast.Node, context *TraceContext) (value, error)
return nil, e.Error(fmt.Sprintf("Value non indexable: %v", reflect.TypeOf(targetValue)))

case *ast.Import:
// TODO(sbarzowski) put this information in AST instead of getting it out of tracing data...
codeDir := path.Dir(e.trace.loc.FileName)
codeDir := path.Dir(ast.Loc().FileName)
return i.importCache.ImportCode(codeDir, ast.File, e)

case *ast.ImportStr:
// TODO(sbarzowski) put this information in AST instead of getting it out of tracing data...
codeDir := path.Dir(e.trace.loc.FileName)
return i.importCache.ImportString(codeDir, ast.File)
codeDir := path.Dir(ast.Loc().FileName)
return i.importCache.ImportString(codeDir, ast.File, e)

case *ast.LiteralBoolean:
return makeValueBoolean(ast.Value), nil
Expand Down
10 changes: 5 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func removeExcessiveWhitespace(s string) string {
func TestMain(t *testing.T) {
flag.Parse()
var mainTests []mainTest
match, err := filepath.Glob("testdata/*.input")
match, err := filepath.Glob("testdata/*.jsonnet")
if err != nil {
t.Fatal(err)
}
for _, input := range match {
golden := input
name := input
if strings.HasSuffix(input, ".input") {
name = input[:len(input)-len(".input")]
if strings.HasSuffix(input, ".jsonnet") {
name = input[:len(input)-len(".jsonnet")]
golden = name + ".golden"
}
mainTests = append(mainTests, mainTest{name: name, input: input, golden: golden})
Expand Down Expand Up @@ -106,12 +106,12 @@ func TestMain(t *testing.T) {
// TODO(sbarzowski) better reporting of differences in whitespace
// missing newline issues can be very subtle now
t.Fail()
t.Errorf("Mismatch when running %s.input. Golden: %s\n", test.name, test.golden)
t.Errorf("Mismatch when running %s.jsonnet. Golden: %s\n", test.name, test.golden)
data := diff(output, string(golden))
if err != nil {
t.Errorf("computing diff: %s", err)
}
t.Errorf("diff %s jsonnet %s.input\n", test.golden, test.name)
t.Errorf("diff %s jsonnet %s.jsonnet\n", test.golden, test.name)
t.Errorf(string(data))

}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions testdata/false.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/false.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions testdata/import.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/import.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "true.jsonnet"
1 change: 1 addition & 0 deletions testdata/import2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/import2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "false.jsonnet"
1 change: 1 addition & 0 deletions testdata/import3.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"true\n"
1 change: 1 addition & 0 deletions testdata/import3.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importstr "true.jsonnet"
1 change: 1 addition & 0 deletions testdata/import4.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"false\n"
1 change: 1 addition & 0 deletions testdata/import4.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importstr "false.jsonnet"
1 change: 1 addition & 0 deletions testdata/import_failure_directory.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: read testdata: is a directory
3 changes: 3 additions & 0 deletions testdata/import_failure_directory.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Test for the case when the imported thing exists, but there
// is an error. In this case it's not even a file.
import '.'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions testdata/nonexistent_import.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: Couldn't open import "no chance a file with this name exists": No match locally or in the Jsonnet library paths.
1 change: 1 addition & 0 deletions testdata/nonexistent_import.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importstr 'no chance a file with this name exists'
1 change: 1 addition & 0 deletions testdata/nonexistent_import_crazy.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: Couldn't open import "ąęółńśćźż \\\" \\' \\n\\n\\t\\t": No match locally or in the Jsonnet library paths.
1 change: 1 addition & 0 deletions testdata/nonexistent_import_crazy.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importstr "ąęółńśćźż \" \' \n\n\t\t"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions testdata/true.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/true.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fe62ec4

Please sign in to comment.