Marcel van Lohuizen | bc4d65d | 2018-12-10 15:40:02 +0100 | [diff] [blame] | 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 | "os" |
| 19 | "path/filepath" |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | |
| 23 | build "cuelang.org/go/cue/build" |
| 24 | ) |
| 25 | |
| 26 | const testdata = "./testdata/" |
| 27 | |
| 28 | func getInst(pkg, cwd string) (*build.Instance, error) { |
| 29 | c, _ := (&Config{}).complete() |
| 30 | l := loader{cfg: c} |
| 31 | p := l.importPkg(pkg, cwd) |
| 32 | return p, p.Err |
| 33 | } |
| 34 | |
| 35 | func TestDotSlashImport(t *testing.T) { |
| 36 | c, _ := (&Config{}).complete() |
| 37 | l := loader{cfg: c} |
| 38 | p := l.importPkg(".", testdata+"other") |
| 39 | err := p.Err |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if len(p.ImportPaths) != 1 || p.ImportPaths[0] != "./file" { |
| 44 | t.Fatalf("testdata/other: Imports=%v, want [./file]", p.ImportPaths) |
| 45 | } |
| 46 | |
| 47 | p1, err := getInst("./file", testdata+"other") |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if p1.PkgName != "file" { |
| 52 | t.Fatalf("./file: Name=%q, want %q", p1.PkgName, "file") |
| 53 | } |
| 54 | dir := filepath.Clean(testdata + "other/file") // Clean to use \ on Windows |
| 55 | if p1.Dir != dir { |
| 56 | t.Fatalf("./file: Dir=%q, want %q", p1.PkgName, dir) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestEmptyImport(t *testing.T) { |
| 61 | p, err := getInst("", "") |
| 62 | if err == nil { |
| 63 | t.Fatal(`Import("") returned nil error.`) |
| 64 | } |
| 65 | if p == nil { |
| 66 | t.Fatal(`Import("") returned nil package.`) |
| 67 | } |
| 68 | if p.DisplayPath != "" { |
| 69 | t.Fatalf("DisplayPath=%q, want %q.", p.DisplayPath, "") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestEmptyFolderImport(t *testing.T) { |
| 74 | _, err := getInst(".", testdata+"empty") |
| 75 | if _, ok := err.(*noCUEError); !ok { |
| 76 | t.Fatal(`Import("testdata/empty") did not return NoCUEError.`) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestIgnoredCUEFilesImport(t *testing.T) { |
| 81 | _, err := getInst(".", testdata+"ignored") |
| 82 | e, ok := err.(*noCUEError) |
| 83 | if !ok { |
| 84 | t.Fatal(`Import("testdata/ignored") did not return NoCUEError.`) |
| 85 | } |
| 86 | if !e.Ignored { |
| 87 | t.Fatal(`Import("testdata/ignored") should have ignored CUE files.`) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestMultiplePackageImport(t *testing.T) { |
| 92 | _, err := getInst(".", testdata+"multi") |
| 93 | mpe, ok := err.(*multiplePackageError) |
| 94 | if !ok { |
| 95 | t.Fatal(`Import("testdata/multi") did not return MultiplePackageError.`) |
| 96 | } |
| 97 | want := &multiplePackageError{ |
| 98 | Dir: filepath.FromSlash("testdata/multi"), |
| 99 | Packages: []string{"main", "test_package"}, |
| 100 | Files: []string{"file.cue", "file_appengine.cue"}, |
| 101 | } |
| 102 | if !reflect.DeepEqual(mpe, want) { |
| 103 | t.Errorf("got %#v; want %#v", mpe, want) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestLocalDirectory(t *testing.T) { |
| 108 | cwd, err := os.Getwd() |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | |
| 113 | p, err := getInst(".", cwd) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | |
| 118 | if p.DisplayPath != "." { |
| 119 | t.Fatalf("DisplayPath=%q, want %q", p.DisplayPath, ".") |
| 120 | } |
| 121 | } |