github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/headerabi/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 headerabi 16 17 import ( 18 "context" 19 "os" 20 "path/filepath" 21 "testing" 22 23 spb "github.com/bazelbuild/reclient/api/scandeps" 24 "github.com/bazelbuild/reclient/internal/pkg/inputprocessor" 25 "github.com/bazelbuild/reclient/internal/pkg/inputprocessor/action/cppcompile" 26 27 "github.com/bazelbuild/remote-apis-sdks/go/pkg/command" 28 "github.com/google/go-cmp/cmp" 29 ) 30 31 const executablePath = "abi-header-dumper" 32 33 func TestSpec(t *testing.T) { 34 pwd, err := os.Getwd() 35 if err != nil { 36 t.Fatalf("Unable to get current working directory: %v", err) 37 } 38 s := &stubCPPDepScanner{res: []string{"foo.h"}} 39 p := &Preprocessor{ 40 Preprocessor: &cppcompile.Preprocessor{ 41 BasePreprocessor: &inputprocessor.BasePreprocessor{Ctx: context.Background()}, 42 CPPDepScanner: s, 43 }, 44 } 45 p.Options = inputprocessor.Options{ 46 ExecRoot: pwd, 47 Cmd: []string{executablePath, "--", "-Ifoo", "-I", "bar", "-o", "test.sdump", "test.cpp"}, 48 } 49 50 if err := p.ParseFlags(); err != nil { 51 t.Fatalf("ParseFlags() failed: %v", err) 52 } 53 p.ComputeSpec() 54 got, err := p.Spec() 55 if err != nil { 56 t.Fatalf("Compute() failed: %v", err) 57 } 58 want := &command.InputSpec{ 59 Inputs: []string{ 60 "test.cpp", 61 "foo.h", 62 }, 63 VirtualInputs: []*command.VirtualInput{ 64 &command.VirtualInput{Path: filepath.Clean("foo"), IsEmptyDirectory: true}, 65 &command.VirtualInput{Path: filepath.Clean("bar"), IsEmptyDirectory: true}, 66 }, 67 } 68 opt := cmp.Transformer("filterOutExecutablePath", func(in []string) []string { 69 out := []string{} 70 for _, val := range in { 71 if val != executablePath { 72 out = append(out, val) 73 } 74 } 75 return out 76 }) 77 if diff := cmp.Diff(want, got.InputSpec, opt); diff != "" { 78 t.Errorf("Spec() returned diff (-want +got): %v", diff) 79 } 80 wantCmd := []string{ 81 "-Ifoo", 82 "-I", 83 "bar", 84 "-Qunused-arguments", 85 "-o", 86 "test.sdump", 87 filepath.Join(pwd, "test.cpp"), 88 } 89 if diff := cmp.Diff(wantCmd, s.gotCmd[1:]); diff != "" { 90 t.Errorf("CPP command from ABI header command %v had diff (-want +got): %s", p.Flags, diff) 91 } 92 } 93 94 type stubCPPDepScanner struct { 95 gotCmd []string 96 gotFileName string 97 gotDirectory string 98 99 res []string 100 err error 101 } 102 103 func (s *stubCPPDepScanner) ProcessInputs(_ context.Context, _ string, command []string, filename, directory string, _ []string) ([]string, bool, error) { 104 s.gotCmd = command 105 s.gotFileName = filename 106 s.gotDirectory = directory 107 108 return s.res, false, s.err 109 } 110 111 func (s *stubCPPDepScanner) Capabilities() *spb.CapabilitiesResponse { 112 return nil 113 }