cuelang.org/go@v0.10.1/cue/load/import_test.go (about) 1 // Copyright 2018 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package load 16 17 import ( 18 "fmt" 19 "os" 20 "path/filepath" 21 "reflect" 22 "testing" 23 24 "cuelang.org/go/cue/build" 25 ) 26 27 func testdata(elems ...string) string { 28 return filepath.Join(append([]string{"testdata"}, elems...)...) 29 } 30 31 var pkgRootDir, _ = os.Getwd() 32 33 func getInst(pkg, cwd string) (*build.Instance, error) { 34 insts := Instances([]string{pkg}, &Config{ 35 // Set ModuleRoot as well; otherwise we walk the parent directories 36 // all the way to the root of the git repository, causing Go's test caching 37 // to never kick in, as the .git directory almost always changes. 38 // Moreover, it's extra work that isn't useful to the tests. 39 // 40 // Note that we can't set ModuleRoot to cwd because if ModuleRoot is 41 // set, the logic will only look for a module file in that exact directory. 42 // So we set it to the module root actually used by all the callers of getInst: ./testdata/testmod. 43 ModuleRoot: filepath.Join(pkgRootDir, testdata("testmod")), 44 Dir: cwd, 45 }) 46 if len(insts) != 1 { 47 return nil, fmt.Errorf("expected one instance, got %d", len(insts)) 48 } 49 inst := insts[0] 50 return inst, inst.Err 51 } 52 53 func TestEmptyImport(t *testing.T) { 54 path := testdata("testmod", "hello") 55 p, err := getInst("", path) 56 if err == nil { 57 t.Fatal(`Import("") returned nil error.`) 58 } 59 if p == nil { 60 t.Fatal(`Import("") returned nil package.`) 61 } 62 if p.DisplayPath != "" { 63 t.Fatalf("DisplayPath=%q, want %q.", p.DisplayPath, "") 64 } 65 } 66 67 func TestEmptyFolderImport(t *testing.T) { 68 path := testdata("testmod", "empty") 69 _, err := getInst(".", path) 70 if _, ok := err.(*NoFilesError); !ok { 71 t.Fatalf(`Import(%q) did not return NoCUEError, but instead %#v (%v)`, path, err, err) 72 } 73 } 74 75 func TestMultiplePackageImport(t *testing.T) { 76 path := testdata("testmod", "multi") 77 _, err := getInst(".", path) 78 mpe, ok := err.(*MultiplePackageError) 79 if !ok { 80 t.Fatalf(`Import(%q) did not return MultiplePackageError, but instead %#v (%v)`, path, err, err) 81 } 82 mpe.Dir = "" 83 want := &MultiplePackageError{ 84 Packages: []string{"main", "test_package"}, 85 Files: []string{"file.cue", "file_appengine.cue"}, 86 } 87 if !reflect.DeepEqual(mpe, want) { 88 t.Errorf("got %#v; want %#v", mpe, want) 89 } 90 } 91 92 func TestLocalDirectory(t *testing.T) { 93 p, err := getInst(".", testdata("testmod", "hello")) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 if p.DisplayPath != "." { 99 t.Fatalf("DisplayPath=%q, want %q", p.DisplayPath, ".") 100 } 101 }