github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/metalava/preprocessor_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 "context" 19 "path/filepath" 20 "testing" 21 22 "github.com/bazelbuild/reclient/internal/pkg/execroot" 23 "github.com/bazelbuild/reclient/internal/pkg/inputprocessor" 24 25 "github.com/bazelbuild/remote-apis-sdks/go/pkg/command" 26 "github.com/google/go-cmp/cmp" 27 "github.com/google/go-cmp/cmp/cmpopts" 28 ) 29 30 const ( 31 wd = "wd" 32 ) 33 34 var ( 35 strSliceCmp = cmpopts.SortSlices(func(a, b string) bool { return a < b }) 36 ) 37 38 func TestMetalavaPreprocessor(t *testing.T) { 39 existingFiles := []string{ 40 filepath.Clean("wd/foo.java"), 41 filepath.Clean("wd/bar/bar.java"), 42 filepath.Clean("metalava.jar"), 43 } 44 er, cleanup := execroot.Setup(t, append(existingFiles, filepath.Join(wd, "remote_toolchain_inputs"))) 45 defer cleanup() 46 execroot.AddFileWithContent(t, filepath.Join(er, wd, "foo.rsp"), []byte("foo.java")) 47 execroot.AddDirs(t, er, []string{"wd/src"}) 48 cmd := []string{"metalava", "-classpath", "bar", "--api", "api.txt", "@foo.rsp", "-sourcepath", "src"} 49 ctx := context.Background() 50 i := &command.InputSpec{ 51 Inputs: []string{filepath.Clean("metalava.jar")}, 52 EnvironmentVariables: map[string]string{"FOO": filepath.Join(er, "foo")}, 53 } 54 pp := &Preprocessor{ 55 &inputprocessor.BasePreprocessor{ 56 Ctx: ctx, 57 Executor: &execStub{stdout: "Metalava: 1.3.0"}, 58 }, 59 } 60 gotSpec, err := inputprocessor.Compute(pp, inputprocessor.Options{ 61 Cmd: cmd, 62 WorkingDir: wd, 63 ExecRoot: er, 64 Inputs: i, 65 }) 66 if err != nil { 67 t.Errorf("Compute() returned error: %v", err) 68 } 69 70 wantSpec := &inputprocessor.ActionSpec{ 71 InputSpec: &command.InputSpec{ 72 Inputs: []string{filepath.Clean("wd/foo.rsp"), filepath.Clean("wd/bar"), filepath.Clean("wd/foo.java"), filepath.Clean("metalava.jar")}, 73 VirtualInputs: []*command.VirtualInput{ 74 &command.VirtualInput{Path: filepath.Clean("wd/src"), IsEmptyDirectory: true}, 75 }, 76 EnvironmentVariables: map[string]string{ 77 "FOO": filepath.Join("..", "foo"), 78 }, 79 }, 80 OutputFiles: []string{filepath.Clean("wd/api.txt")}, 81 } 82 if diff := cmp.Diff(wantSpec, gotSpec, strSliceCmp); diff != "" { 83 t.Errorf("Compute() returned diff in ActionSpec, (-want +got): %s", diff) 84 } 85 } 86 87 type execStub struct { 88 stdout string 89 stderr string 90 err error 91 } 92 93 func (e *execStub) Execute(ctx context.Context, cmd *command.Command) (string, string, error) { 94 return e.stdout, e.stderr, e.err 95 }