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