github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/headerabi/flagsparser_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  	"testing"
    20  
    21  	"github.com/bazelbuild/reclient/internal/pkg/execroot"
    22  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"github.com/google/go-cmp/cmp/cmpopts"
    26  )
    27  
    28  const (
    29  	fakeExecRoot = "fake"
    30  )
    31  
    32  func TestParseFlags(t *testing.T) {
    33  	er, cleanup := execroot.Setup(t, nil)
    34  	defer cleanup()
    35  	tests := []struct {
    36  		name       string
    37  		command    []string
    38  		workingDir string
    39  		files      map[string][]byte
    40  		want       *flags.CommandFlags
    41  	}{
    42  		{
    43  			name:       "abi-dumper command",
    44  			workingDir: ".",
    45  			command: []string{
    46  				"prebuilts/clang-tools/linux-x86/bin/header-abi-dumper",
    47  				"--root-dir",
    48  				".",
    49  				"-o",
    50  				"outfile",
    51  				"bionic/libm/upstream-freebsd/lib/msun/src/s_fmax.c",
    52  				"--",
    53  				"-Werror=non-virtual-dtor",
    54  				"-Werror=address",
    55  				"-Werror=sequence-point",
    56  				"-fPIC",
    57  				"-Ibionic/libc/system_properties/include",
    58  				"-isystem",
    59  				"bionic/libc/include",
    60  				"-include",
    61  				"fenv-access.h",
    62  				"-Isystem/core/include",
    63  			},
    64  			want: &flags.CommandFlags{
    65  				ExecutablePath: "prebuilts/clang-tools/linux-x86/bin/header-abi-dumper",
    66  				Flags: []*flags.Flag{
    67  					&flags.Flag{Key: "--root-dir", Value: "."},
    68  					&flags.Flag{Key: "-W", Value: "error=non-virtual-dtor", Joined: true},
    69  					&flags.Flag{Key: "-W", Value: "error=address", Joined: true},
    70  					&flags.Flag{Key: "-W", Value: "error=sequence-point", Joined: true},
    71  					&flags.Flag{Key: "-fPIC"},
    72  					&flags.Flag{
    73  						Key:    "-I",
    74  						Value:  "bionic/libc/system_properties/include",
    75  						Joined: true,
    76  					},
    77  					&flags.Flag{Key: "-isystem", Value: "bionic/libc/include"},
    78  					&flags.Flag{Key: "--include", Value: "fenv-access.h"},
    79  					&flags.Flag{Key: "-I", Value: "system/core/include", Joined: true},
    80  				},
    81  				IncludeDirPaths: []string{"bionic/libc/system_properties/include", "system/core/include"},
    82  				TargetFilePaths: []string{"bionic/libm/upstream-freebsd/lib/msun/src/s_fmax.c"},
    83  				OutputFilePaths: []string{
    84  					"outfile",
    85  				},
    86  				WorkingDirectory: ".",
    87  				ExecRoot:         er,
    88  			},
    89  		},
    90  		{
    91  			name:       "abi-dumper command with multiple root dirs",
    92  			workingDir: ".",
    93  			command: []string{
    94  				"prebuilts/clang-tools/linux-x86/bin/header-abi-dumper",
    95  				"--root-dir",
    96  				".",
    97  				"--root-dir",
    98  				"$OUT_DIR:out",
    99  				"-o",
   100  				"outfile",
   101  				"bionic/libm/upstream-freebsd/lib/msun/src/s_fmax.c",
   102  				"--",
   103  				"-Werror=non-virtual-dtor",
   104  				"-Werror=address",
   105  				"-Werror=sequence-point",
   106  				"-fPIC",
   107  				"-Ibionic/libc/system_properties/include",
   108  				"-isystem",
   109  				"bionic/libc/include",
   110  				"-include",
   111  				"fenv-access.h",
   112  				"-Isystem/core/include",
   113  			},
   114  			want: &flags.CommandFlags{
   115  				ExecutablePath: "prebuilts/clang-tools/linux-x86/bin/header-abi-dumper",
   116  				Flags: []*flags.Flag{
   117  					&flags.Flag{Key: "--root-dir", Value: "."},
   118  					&flags.Flag{Key: "--root-dir", Value: "$OUT_DIR:out"},
   119  					&flags.Flag{Key: "-W", Value: "error=non-virtual-dtor", Joined: true},
   120  					&flags.Flag{Key: "-W", Value: "error=address", Joined: true},
   121  					&flags.Flag{Key: "-W", Value: "error=sequence-point", Joined: true},
   122  					&flags.Flag{Key: "-fPIC"},
   123  					&flags.Flag{
   124  						Key:    "-I",
   125  						Value:  "bionic/libc/system_properties/include",
   126  						Joined: true,
   127  					},
   128  					&flags.Flag{Key: "-isystem", Value: "bionic/libc/include"},
   129  					&flags.Flag{Key: "--include", Value: "fenv-access.h"},
   130  					&flags.Flag{Key: "-I", Value: "system/core/include", Joined: true},
   131  				},
   132  				IncludeDirPaths: []string{"bionic/libc/system_properties/include", "system/core/include"},
   133  				TargetFilePaths: []string{"bionic/libm/upstream-freebsd/lib/msun/src/s_fmax.c"},
   134  				OutputFilePaths: []string{
   135  					"outfile",
   136  				},
   137  				WorkingDirectory: ".",
   138  				ExecRoot:         er,
   139  			},
   140  		},
   141  		{
   142  			name:       "abi-dumper command with no root-dir",
   143  			workingDir: ".",
   144  			command: []string{
   145  				"prebuilts/clang-tools/linux-x86/bin/header-abi-dumper",
   146  				"-o",
   147  				"outfile",
   148  				"bionic/libm/upstream-freebsd/lib/msun/src/s_fmax.c",
   149  				"--",
   150  				"-Werror=non-virtual-dtor",
   151  				"-Werror=address",
   152  				"-Werror=sequence-point",
   153  				"-fPIC",
   154  				"-Ibionic/libc/system_properties/include",
   155  				"-isystem",
   156  				"bionic/libc/include",
   157  				"-include",
   158  				"fenv-access.h",
   159  				"-Isystem/core/include",
   160  			},
   161  			want: &flags.CommandFlags{
   162  				ExecutablePath: "prebuilts/clang-tools/linux-x86/bin/header-abi-dumper",
   163  				Flags: []*flags.Flag{
   164  					&flags.Flag{Key: "-W", Value: "error=non-virtual-dtor", Joined: true},
   165  					&flags.Flag{Key: "-W", Value: "error=address", Joined: true},
   166  					&flags.Flag{Key: "-W", Value: "error=sequence-point", Joined: true},
   167  					&flags.Flag{Key: "-fPIC"},
   168  					&flags.Flag{
   169  						Key:    "-I",
   170  						Value:  "bionic/libc/system_properties/include",
   171  						Joined: true,
   172  					},
   173  					&flags.Flag{Key: "-isystem", Value: "bionic/libc/include"},
   174  					&flags.Flag{Key: "--include", Value: "fenv-access.h"},
   175  					&flags.Flag{Key: "-I", Value: "system/core/include", Joined: true},
   176  				},
   177  				IncludeDirPaths: []string{"bionic/libc/system_properties/include", "system/core/include"},
   178  				TargetFilePaths: []string{"bionic/libm/upstream-freebsd/lib/msun/src/s_fmax.c"},
   179  				OutputFilePaths: []string{
   180  					"outfile",
   181  				},
   182  				WorkingDirectory: ".",
   183  				ExecRoot:         er,
   184  			},
   185  		},
   186  	}
   187  
   188  	for _, test := range tests {
   189  		t.Run(test.name, func(t *testing.T) {
   190  			ctx := context.Background()
   191  			execroot.AddFilesWithContent(t, er, test.files)
   192  			got, err := Parser{}.ParseFlags(ctx, test.command, test.workingDir, er)
   193  			if err != nil {
   194  				t.Errorf("ParseFlags(%v,%v,%v).err = %v, want no error.", test.command, test.workingDir, er, err)
   195  			}
   196  
   197  			if diff := cmp.Diff(test.want, got, cmpopts.IgnoreUnexported(flags.Flag{})); diff != "" {
   198  				t.Errorf("Test %v, ParseFlags(%v,%v,%v) returned diff, (-want +got): %s", test.name, test.command, test.workingDir, er, diff)
   199  			}
   200  		})
   201  	}
   202  }