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