...

Source file src/cuelang.org/go/cue/load/import_test.go

Documentation: cuelang.org/go/cue/load

     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  	"cuelang.org/go/cue/token"
    26  )
    27  
    28  func testdata(elems ...string) string {
    29  	return filepath.Join(append([]string{"testdata"}, elems...)...)
    30  }
    31  
    32  func getInst(pkg, cwd string) (*build.Instance, error) {
    33  	// Set ModuleRoot as well; otherwise we walk the parent directories
    34  	// all the way to the root of the git repository, causing Go's test caching
    35  	// to never kick in, as the .git directory almost always changes.
    36  	// Moreover, it's extra work that isn't useful to the tests.
    37  	c, err := (&Config{ModuleRoot: ".", Dir: cwd}).complete()
    38  	if err != nil {
    39  		return nil, fmt.Errorf("unexpected error on Config.complete: %v", err)
    40  	}
    41  	l := loader{cfg: c}
    42  	inst := l.newRelInstance(token.NoPos, pkg, c.Package)
    43  	p := l.importPkg(token.NoPos, inst)[0]
    44  	return p, p.Err
    45  }
    46  
    47  func TestEmptyImport(t *testing.T) {
    48  	c, err := (&Config{
    49  		ModuleRoot: ".",
    50  	}).complete()
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	l := loader{cfg: c}
    55  	inst := l.newInstance(token.NoPos, "")
    56  	p := l.importPkg(token.NoPos, inst)[0]
    57  	if p.Err == nil {
    58  		t.Fatal(`Import("") returned nil error.`)
    59  	}
    60  	if p == nil {
    61  		t.Fatal(`Import("") returned nil package.`)
    62  	}
    63  	if p.DisplayPath != "" {
    64  		t.Fatalf("DisplayPath=%q, want %q.", p.DisplayPath, "")
    65  	}
    66  }
    67  
    68  func TestEmptyFolderImport(t *testing.T) {
    69  	path := testdata("testmod", "empty")
    70  	_, err := getInst(".", path)
    71  	if _, ok := err.(*NoFilesError); !ok {
    72  		t.Fatalf(`Import(%q) did not return NoCUEError.`, path)
    73  	}
    74  }
    75  
    76  func TestMultiplePackageImport(t *testing.T) {
    77  	path := testdata("testmod", "multi")
    78  	_, err := getInst(".", path)
    79  	mpe, ok := err.(*MultiplePackageError)
    80  	if !ok {
    81  		t.Fatalf(`Import(%q) did not return MultiplePackageError.`, path)
    82  	}
    83  	mpe.Dir = ""
    84  	want := &MultiplePackageError{
    85  		Packages: []string{"main", "test_package"},
    86  		Files:    []string{"file.cue", "file_appengine.cue"},
    87  	}
    88  	if !reflect.DeepEqual(mpe, want) {
    89  		t.Errorf("got %#v; want %#v", mpe, want)
    90  	}
    91  }
    92  
    93  func TestLocalDirectory(t *testing.T) {
    94  	cwd, err := os.Getwd()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	p, err := getInst(".", cwd)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	if p.DisplayPath != "." {
   105  		t.Fatalf("DisplayPath=%q, want %q", p.DisplayPath, ".")
   106  	}
   107  }
   108  

View as plain text