github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/metalava/flagsparser_test.go (about) 1 // Copyright 2023 Google LLC 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 metalava 16 17 import ( 18 "testing" 19 20 "github.com/bazelbuild/reclient/internal/pkg/execroot" 21 "github.com/bazelbuild/reclient/internal/pkg/inputprocessor" 22 "github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags" 23 24 "github.com/google/go-cmp/cmp" 25 "github.com/google/go-cmp/cmp/cmpopts" 26 ) 27 28 func TestMetalavaParser(t *testing.T) { 29 er, cleanup := execroot.Setup(t, nil) 30 defer cleanup() 31 test := []struct { 32 name string 33 options inputprocessor.Options 34 existingFiles map[string][]byte 35 want *flags.CommandFlags 36 }{ 37 { 38 name: "basic", 39 options: inputprocessor.Options{ 40 ExecRoot: er, 41 Cmd: []string{ 42 "metalava", 43 "-J-Xmx2048M", 44 "@test.rsp", 45 "@test2.rsp", 46 "-bootclasspath", 47 "e:f:g", 48 "-classpath", 49 "h,i", 50 "--api-lint", 51 "--api", 52 "j", 53 "--removed-api", 54 "k", 55 "--stubs", 56 "l", 57 "--convert-to-jdiff", 58 "m", 59 "n", 60 "--convert-new-to-jdiff", 61 "o", 62 "p", 63 "q", 64 "--android-jar-pattern", 65 "r/%/s", 66 "--baseline", 67 "t", 68 "--strict-input-files:warn", 69 "v", 70 "-sourcepath", 71 "u", 72 }, 73 }, 74 existingFiles: map[string][]byte{ 75 "test.rsp": []byte("a b"), 76 "test2.rsp": []byte("c d"), 77 }, 78 want: &flags.CommandFlags{ 79 ExecutablePath: "metalava", 80 Flags: []*flags.Flag{ 81 &flags.Flag{Value: "-J-Xmx2048M"}, 82 &flags.Flag{Value: "--api-lint"}, 83 }, 84 TargetFilePaths: []string{"test.rsp", "test2.rsp"}, 85 Dependencies: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "m", "o", "p", "r/", "t"}, 86 ExecRoot: er, 87 OutputFilePaths: []string{"j", "k", "n", "q", "t", "v"}, 88 EmittedDependencyFile: "v", 89 OutputDirPaths: []string{"l"}, 90 VirtualDirectories: []string{"u"}, 91 }, 92 }, { 93 name: "strict input warn mode", 94 options: inputprocessor.Options{ 95 ExecRoot: er, 96 Cmd: []string{ 97 "metalava", 98 "-J-Xmx2048M", 99 "@test.rsp", 100 "@test2.rsp", 101 "-bootclasspath", 102 "e:f:g", 103 "-classpath", 104 "h,i", 105 "--api-lint", 106 "--api", 107 "j", 108 "--removed-api", 109 "k", 110 "--stubs", 111 "l", 112 "--convert-to-jdiff", 113 "m", 114 "n", 115 "--convert-new-to-jdiff", 116 "o", 117 "p", 118 "q", 119 "--android-jar-pattern", 120 "r/%/s", 121 "--baseline", 122 "t", 123 "--strict-input-files:warn", 124 "v", 125 "-sourcepath", 126 "u", 127 }, 128 }, 129 existingFiles: map[string][]byte{ 130 "test.rsp": []byte("a b"), 131 "test2.rsp": []byte("c d"), 132 }, 133 want: &flags.CommandFlags{ 134 ExecutablePath: "metalava", 135 Flags: []*flags.Flag{ 136 &flags.Flag{Value: "-J-Xmx2048M"}, 137 &flags.Flag{Value: "--api-lint"}, 138 }, 139 TargetFilePaths: []string{"test.rsp", "test2.rsp"}, 140 Dependencies: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "m", "o", "p", "r/", "t"}, 141 ExecRoot: er, 142 OutputFilePaths: []string{"j", "k", "n", "q", "t", "v"}, 143 EmittedDependencyFile: "v", 144 OutputDirPaths: []string{"l"}, 145 VirtualDirectories: []string{"u"}, 146 }, 147 }, 148 } 149 for _, test := range test { 150 t.Run(test.name, func(t *testing.T) { 151 execroot.AddFilesWithContent(t, er, test.existingFiles) 152 p := &Preprocessor{&inputprocessor.BasePreprocessor{ 153 Options: test.options, 154 }} 155 if err := p.ParseFlags(); err != nil { 156 t.Errorf("ParseFlags() returned error: %v", err) 157 } 158 if diff := cmp.Diff(test.want, p.Flags, cmpopts.IgnoreUnexported(flags.Flag{})); diff != "" { 159 t.Errorf("ParseFlags() returned diff, (-want +got): %s", diff) 160 } 161 }) 162 } 163 }