github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/archive/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 archive
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/bazelbuild/reclient/internal/pkg/execroot"
    23  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor"
    24  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	"github.com/google/go-cmp/cmp/cmpopts"
    28  )
    29  
    30  func TestArchiveParser(t *testing.T) {
    31  	root, cleanup := execroot.Setup(t, nil)
    32  	defer cleanup()
    33  	test := []struct {
    34  		name          string
    35  		command       []string
    36  		existingFiles map[string][]byte
    37  		want          *flags.CommandFlags
    38  	}{
    39  		{
    40  			name:    "regular",
    41  			command: []string{"ar", "-T", "-r", "-c", "-s", "-D", "foo.a", "bar.so"},
    42  			want: &flags.CommandFlags{
    43  				ExecutablePath: "ar",
    44  				Flags: []*flags.Flag{
    45  					&flags.Flag{Value: "-T"},
    46  					&flags.Flag{Value: "-r"},
    47  					&flags.Flag{Value: "-c"},
    48  					&flags.Flag{Value: "-s"},
    49  					&flags.Flag{Value: "-D"},
    50  				},
    51  				Dependencies: []string{
    52  					"bar.so",
    53  				},
    54  				ExecRoot:        root,
    55  				OutputFilePaths: []string{"foo.a"},
    56  			},
    57  		},
    58  		{
    59  			name:          "using rsp",
    60  			command:       []string{"ar", "-T", "-r", "-c", "-s", "-D", "foo.a", "bar.so", "@foo.a.rsp"},
    61  			existingFiles: map[string][]byte{"foo.a.rsp": []byte("baz.so bas.so")},
    62  			want: &flags.CommandFlags{
    63  				ExecutablePath: "ar",
    64  				Flags: []*flags.Flag{
    65  					&flags.Flag{Value: "-T"},
    66  					&flags.Flag{Value: "-r"},
    67  					&flags.Flag{Value: "-c"},
    68  					&flags.Flag{Value: "-s"},
    69  					&flags.Flag{Value: "-D"},
    70  				},
    71  				Dependencies: []string{
    72  					"bar.so",
    73  					"foo.a.rsp",
    74  					"baz.so",
    75  					"bas.so",
    76  				},
    77  				ExecRoot:        root,
    78  				OutputFilePaths: []string{"foo.a"},
    79  			},
    80  		},
    81  		{
    82  			name:    "print",
    83  			command: []string{"ar", "-t", "foo.a"},
    84  			want: &flags.CommandFlags{
    85  				ExecutablePath: "ar",
    86  				Flags: []*flags.Flag{
    87  					&flags.Flag{Value: "-t"},
    88  				},
    89  				Dependencies: []string{"foo.a"},
    90  				ExecRoot:     root,
    91  			},
    92  		},
    93  		{
    94  			name:    "delete",
    95  			command: []string{"ar", "-d", "foo.a", "foo.so"},
    96  			want: &flags.CommandFlags{
    97  				ExecutablePath: "ar",
    98  				Flags: []*flags.Flag{
    99  					&flags.Flag{Value: "-d"},
   100  				},
   101  				Dependencies:    []string{"foo.a"},
   102  				OutputFilePaths: []string{"foo.a"},
   103  				ExecRoot:        root,
   104  			},
   105  		},
   106  	}
   107  
   108  	for _, test := range test {
   109  		t.Run(test.name, func(t *testing.T) {
   110  			execroot.AddFilesWithContent(t, root, test.existingFiles)
   111  			defer func() {
   112  				for f := range test.existingFiles {
   113  					if err := os.Remove(filepath.Join(root, f)); err != nil {
   114  						// Fatal because they can affect other tests.
   115  						t.Fatalf("Failed to clean test file: %v", err)
   116  					}
   117  				}
   118  			}()
   119  
   120  			p := &Preprocessor{&inputprocessor.BasePreprocessor{
   121  				Options: inputprocessor.Options{
   122  					Cmd:      test.command,
   123  					ExecRoot: root,
   124  				},
   125  			}}
   126  
   127  			if err := p.ParseFlags(); err != nil {
   128  				t.Errorf("ParseFlags() returned error: %v", err)
   129  			}
   130  			if diff := cmp.Diff(test.want, p.Flags, cmpopts.IgnoreUnexported(flags.Flag{})); diff != "" {
   131  				t.Errorf("ParseFlags() returned diff, (-want +got): %s", diff)
   132  			}
   133  		})
   134  	}
   135  }