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 | "bytes" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | "testing" |
| 25 | |
| 26 | build "cuelang.org/go/cue/build" |
| 27 | "cuelang.org/go/internal/str" |
| 28 | ) |
| 29 | |
| 30 | // TestLoad is an end-to-end test. |
| 31 | func TestLoad(t *testing.T) { |
| 32 | cwd, err := os.Getwd() |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | args := str.StringList |
| 37 | testCases := []struct { |
| 38 | args []string |
| 39 | want string |
| 40 | err string |
| 41 | }{{ |
| 42 | args: nil, |
| 43 | want: "test: test.cue (1 files)", |
| 44 | }, { |
| 45 | args: args("."), |
| 46 | want: "test: test.cue (1 files)", |
| 47 | }, { |
| 48 | args: args("./other/..."), |
| 49 | want: ` |
| 50 | main: other/main.cue (1 files) |
| 51 | file: other/file/file.cue (1 files);main: other/main.cue (1 files) |
| 52 | file: other/file/file.cue (1 files)`, |
| 53 | }, { |
| 54 | args: args("./anon"), |
| 55 | want: ": (0 files)", |
| 56 | err: "build constraints exclude all CUE files", |
| 57 | }, { |
| 58 | args: args("./other"), |
| 59 | want: ` |
| 60 | main: other/main.cue (1 files) |
| 61 | file: other/file/file.cue (1 files)`, |
| 62 | }, { |
| 63 | args: args("./hello"), |
| 64 | want: "test: test.cue hello/test.cue (2 files)", |
| 65 | }, { |
| 66 | args: args("./anon.cue", "./other/anon.cue"), |
| 67 | want: ": ./anon.cue ./other/anon.cue (2 files)", |
| 68 | }, { |
| 69 | // Absolute file is normalized. |
| 70 | args: args(filepath.Join(cwd, "testdata", "anon.cue")), |
| 71 | want: ": ./anon.cue (1 files)", |
| 72 | }, { |
| 73 | args: args("non-existing"), |
| 74 | want: ": (0 files)", |
| 75 | err: `cannot find package "non-existing"`, |
| 76 | }, { |
| 77 | args: args("./empty"), |
| 78 | want: ": (0 files)", |
| 79 | err: `no CUE files in ./empty`, |
Marcel van Lohuizen | 9ccf273 | 2019-02-23 14:32:03 +0100 | [diff] [blame] | 80 | }, { |
| 81 | args: args("./imports"), |
| 82 | want: ` |
| 83 | imports: imports/imports.cue (1 files) |
| 84 | catch: pkg/acme.com/catch/catch.cue (1 files)`, |
| 85 | err: ``, |
Marcel van Lohuizen | bc4d65d | 2018-12-10 15:40:02 +0100 | [diff] [blame] | 86 | }} |
| 87 | for i, tc := range testCases { |
| 88 | t.Run(strconv.Itoa(i)+"/"+strings.Join(tc.args, ":"), func(t *testing.T) { |
| 89 | c := &Config{Dir: filepath.Join(cwd, testdata)} |
| 90 | pkgs := Instances(tc.args, c) |
| 91 | |
| 92 | var errs, data []string |
| 93 | for _, p := range pkgs { |
| 94 | if p.Err != nil { |
| 95 | errs = append(errs, p.Err.Error()) |
| 96 | } |
| 97 | got := strings.TrimSpace(pkgInfo(pkgs[0])) |
| 98 | data = append(data, got) |
| 99 | } |
| 100 | |
| 101 | if err := strings.Join(errs, ";"); err == "" != (tc.err == "") || |
| 102 | err != "" && !strings.Contains(err, tc.err) { |
| 103 | t.Errorf("error:\n got: %v\nwant: %v", err, tc.err) |
| 104 | } |
| 105 | got := strings.Join(data, ";") |
| 106 | want := strings.TrimSpace(tc.want) |
| 107 | if got != want { |
| 108 | t.Errorf("got:\n%v\nwant:\n%v", got, want) |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func pkgInfo(p *build.Instance) string { |
| 115 | b := &bytes.Buffer{} |
| 116 | fmt.Fprintf(b, "%s: %s (%d files)\n", |
| 117 | p.PkgName, strings.Join(p.CUEFiles, " "), len(p.Files)) |
| 118 | for _, p := range p.Imports { |
| 119 | fmt.Fprintf(b, "\t%s\n", pkgInfo(p)) |
| 120 | } |
| 121 | return b.String() |
| 122 | } |