Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 1 | // Copyright 2019 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 protobuf |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "flag" |
| 20 | "fmt" |
| 21 | "io/ioutil" |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 22 | "os" |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 23 | "path/filepath" |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 24 | "strings" |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 25 | "testing" |
| 26 | |
Koichi Shiraishi | 30a4bee | 2019-12-26 03:06:48 +0900 | [diff] [blame] | 27 | "github.com/kr/pretty" |
| 28 | |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 29 | "cuelang.org/go/cue/ast" |
Marcel van Lohuizen | e7abb20 | 2019-10-08 11:17:17 +0200 | [diff] [blame] | 30 | "cuelang.org/go/cue/errors" |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 31 | "cuelang.org/go/cue/format" |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
Marcel van Lohuizen | fbcb339 | 2019-06-25 11:02:21 +0200 | [diff] [blame] | 34 | var update = flag.Bool("update", false, "update the test output") |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 35 | |
Marcel van Lohuizen | f94a548 | 2019-07-02 20:15:03 +0200 | [diff] [blame] | 36 | func TestExtractDefinitions(t *testing.T) { |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 37 | testCases := []string{ |
| 38 | "networking/v1alpha3/gateway.proto", |
| 39 | "mixer/v1/attributes.proto", |
| 40 | "mixer/v1/config/client/client_config.proto", |
| 41 | } |
| 42 | for _, file := range testCases { |
| 43 | t.Run(file, func(t *testing.T) { |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 44 | root := "testdata/istio.io/api" |
| 45 | filename := filepath.Join(root, filepath.FromSlash(file)) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 46 | c := &Config{ |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 47 | Paths: []string{"testdata", root}, |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | out := &bytes.Buffer{} |
| 51 | |
Marcel van Lohuizen | f94a548 | 2019-07-02 20:15:03 +0200 | [diff] [blame] | 52 | if f, err := Extract(filename, nil, c); err != nil { |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 53 | fmt.Fprintln(out, err) |
| 54 | } else { |
Marcel van Lohuizen | 4954491 | 2019-06-26 13:16:48 +0200 | [diff] [blame] | 55 | b, _ := format.Node(f, format.Simplify()) |
| 56 | out.Write(b) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | wantFile := filepath.Join("testdata", filepath.Base(file)+".out.cue") |
| 60 | if *update { |
Marcel van Lohuizen | afd1cc5 | 2019-08-07 00:28:13 +0200 | [diff] [blame] | 61 | _ = ioutil.WriteFile(wantFile, out.Bytes(), 0644) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 62 | return |
| 63 | } |
| 64 | |
| 65 | b, err := ioutil.ReadFile(wantFile) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | |
| 70 | if desc := pretty.Diff(out.String(), string(b)); len(desc) > 0 { |
| 71 | t.Errorf("files differ:\n%v", desc) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 76 | |
| 77 | func TestBuild(t *testing.T) { |
| 78 | cwd, _ := os.Getwd() |
| 79 | root := filepath.Join(cwd, "testdata/istio.io/api") |
| 80 | c := &Config{ |
| 81 | Root: root, |
| 82 | Module: "istio.io/api", |
| 83 | Paths: []string{ |
| 84 | root, |
| 85 | filepath.Join(cwd, "testdata"), |
| 86 | }, |
| 87 | } |
| 88 | |
Marcel van Lohuizen | f94a548 | 2019-07-02 20:15:03 +0200 | [diff] [blame] | 89 | b := NewExtractor(c) |
Marcel van Lohuizen | afd1cc5 | 2019-08-07 00:28:13 +0200 | [diff] [blame] | 90 | _ = b.AddFile("networking/v1alpha3/gateway.proto", nil) |
| 91 | _ = b.AddFile("mixer/v1/attributes.proto", nil) |
| 92 | _ = b.AddFile("mixer/v1/mixer.proto", nil) |
| 93 | _ = b.AddFile("mixer/v1/config/client/client_config.proto", nil) |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 94 | |
| 95 | files, err := b.Files() |
| 96 | if err != nil { |
Marcel van Lohuizen | e7abb20 | 2019-10-08 11:17:17 +0200 | [diff] [blame] | 97 | t.Fatal(errors.Details(err, nil)) |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if *update { |
| 101 | for _, f := range files { |
| 102 | b, err := format.Node(f) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | _ = os.MkdirAll(filepath.Dir(f.Filename), 0755) |
| 107 | err = ioutil.WriteFile(f.Filename, b, 0644) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | } |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | gotFiles := map[string]*ast.File{} |
| 116 | |
| 117 | for _, f := range files { |
| 118 | rel, err := filepath.Rel(cwd, f.Filename) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | gotFiles[rel] = f |
| 123 | } |
| 124 | |
Marcel van Lohuizen | 64cb20a | 2019-08-06 21:24:14 +0200 | [diff] [blame] | 125 | _ = filepath.Walk("testdata/istio.io/api", func(path string, fi os.FileInfo, err error) error { |
Marcel van Lohuizen | 93e9597 | 2019-06-27 16:47:52 +0200 | [diff] [blame] | 126 | if err != nil || fi.IsDir() || !strings.HasSuffix(path, ".cue") { |
| 127 | return err |
| 128 | } |
| 129 | |
| 130 | f := gotFiles[path] |
| 131 | if f == nil { |
| 132 | t.Errorf("did not produce file %q", path) |
| 133 | return nil |
| 134 | } |
| 135 | delete(gotFiles, path) |
| 136 | |
| 137 | got, err := format.Node(f) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | |
| 142 | want, err := ioutil.ReadFile(path) |
| 143 | if err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | |
| 147 | if !bytes.Equal(got, want) { |
| 148 | t.Errorf("%s: files differ", path) |
| 149 | } |
| 150 | return nil |
| 151 | }) |
| 152 | |
| 153 | for filename := range gotFiles { |
| 154 | t.Errorf("did not expect file %q", filename) |
| 155 | } |
| 156 | } |