github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/cppcompile/preprocessor_darwin_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  //go:build darwin
    16  
    17  package cppcompile
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/bazelbuild/reclient/internal/pkg/execroot"
    25  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor"
    26  
    27  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/command"
    28  	"github.com/google/go-cmp/cmp"
    29  )
    30  
    31  func TestComputeSpecWithXCodeSDK(t *testing.T) {
    32  	ctx := context.Background()
    33  	s := &stubCPPDepScanner{
    34  		res: []string{"include/foo.h"},
    35  		err: nil,
    36  	}
    37  	c := &Preprocessor{CPPDepScanner: s, BasePreprocessor: &inputprocessor.BasePreprocessor{Ctx: ctx}}
    38  
    39  	existingFiles := []string{
    40  		filepath.Clean("bin/clang++"),
    41  		filepath.Clean("src/test.cpp"),
    42  		filepath.Clean("include/foo.h"),
    43  		filepath.Clean("out/dummy"),
    44  		filepath.Clean("sdk/SDKSettings.json"),
    45  	}
    46  	er, cleanup := execroot.Setup(t, existingFiles)
    47  	defer cleanup()
    48  	execroot.AddDirs(t, er, []string{
    49  		"include/foo",
    50  		"include/bar",
    51  	})
    52  	opts := inputprocessor.Options{
    53  		Cmd:        []string{"../bin/clang++", "-o", "test.o", "-MF", "test.d", "-I../include/foo", "-I", "../include/bar", "-c", "../src/test.cpp", "-isysroot", "../sdk"},
    54  		WorkingDir: "out",
    55  		ExecRoot:   er,
    56  		Labels:     map[string]string{"type": "compile", "compiler": "clang", "lang": "cpp"},
    57  	}
    58  	got, err := inputprocessor.Compute(c, opts)
    59  	if err != nil {
    60  		t.Errorf("Spec() failed: %v", err)
    61  	}
    62  	want := &command.InputSpec{
    63  		Inputs: []string{
    64  			filepath.Clean("include/foo.h"),
    65  			filepath.Clean("src/test.cpp"),
    66  			filepath.Clean("bin/clang++"),
    67  			filepath.Clean("sdk/SDKSettings.json"),
    68  		},
    69  		VirtualInputs: []*command.VirtualInput{
    70  			{Path: filepath.Clean("include/foo"), IsEmptyDirectory: true},
    71  			{Path: filepath.Clean("include/bar"), IsEmptyDirectory: true},
    72  			{Path: filepath.Clean("sdk"), IsEmptyDirectory: true},
    73  		},
    74  	}
    75  	if diff := cmp.Diff(want, got.InputSpec, strSliceCmp); diff != "" {
    76  		t.Errorf("Compute() returned diff (-want +got): %v", diff)
    77  	}
    78  }