github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/tool/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 tool
    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  var (
    31  	strSliceCmp = cmpopts.SortSlices(func(a, b string) bool { return a < b })
    32  )
    33  
    34  func TestToolPreprocessor(t *testing.T) {
    35  	er, cleanup := execroot.Setup(t, nil)
    36  	defer cleanup()
    37  	tests := []struct {
    38  		name          string
    39  		options       inputprocessor.Options
    40  		existingFiles map[string][]byte
    41  		wantSpec      *inputprocessor.ActionSpec
    42  	}{{
    43  		name: "basic",
    44  		options: inputprocessor.Options{
    45  			WorkingDir: "wd",
    46  			Cmd:        []string{filepath.Join("..", "unzip"), "foo.zip", "-o", "foo"},
    47  		},
    48  		existingFiles: map[string][]byte{"unzip": nil, filepath.Join("wd", "foo.zip"): nil},
    49  		wantSpec: &inputprocessor.ActionSpec{
    50  			InputSpec: &command.InputSpec{
    51  				Inputs: []string{"unzip"},
    52  			},
    53  		},
    54  	}}
    55  
    56  	for _, test := range tests {
    57  		t.Run(test.name, func(t *testing.T) {
    58  			ctx := context.Background()
    59  			execroot.AddFilesWithContent(t, er, test.existingFiles)
    60  			test.options.ExecRoot = er
    61  			pp := &Preprocessor{
    62  				&inputprocessor.BasePreprocessor{Ctx: ctx},
    63  			}
    64  			gotSpec, err := inputprocessor.Compute(pp, test.options)
    65  			if err != nil {
    66  				t.Errorf("Compute() returned error: %v", err)
    67  			}
    68  
    69  			if diff := cmp.Diff(test.wantSpec, gotSpec, strSliceCmp); diff != "" {
    70  				t.Errorf("Compute() returned diff in ActionSpec, (-want +got): %s", diff)
    71  			}
    72  		})
    73  	}
    74  }