github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/fs_case_insensitive_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 windows || darwin
    16  
    17  package inputprocessor
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  )
    24  
    25  func TestNormalize(t *testing.T) {
    26  	execRoot := t.TempDir()
    27  	t.Logf("execRoot=%q", execRoot)
    28  	for _, pathname := range []string{
    29  		"TopDir/SubDir/File.h",
    30  		"other_top_dir/sub_dir/file.h",
    31  	} {
    32  		fullpath := filepath.Join(execRoot, pathname)
    33  		err := os.MkdirAll(filepath.Dir(fullpath), 0755)
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  		t.Logf("create %q", fullpath)
    38  		err = os.WriteFile(fullpath, nil, 0644)
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  	}
    43  
    44  	pn := newPathNormalizer(false)
    45  	for _, tc := range []struct {
    46  		input   string
    47  		want    string
    48  		wantErr bool
    49  	}{
    50  		{
    51  			input: filepath.Clean("TopDir/SubDir/File.h"),
    52  			want:  filepath.Clean("TopDir/SubDir/File.h"),
    53  		},
    54  		{
    55  			input: "TopDir/SubDir/File.h",
    56  			want:  filepath.Clean("TopDir/SubDir/File.h"),
    57  		},
    58  		{
    59  			input: filepath.Clean("topdir/subdir/file.h"),
    60  			want:  filepath.Clean("TopDir/SubDir/File.h"),
    61  		},
    62  		{
    63  			input: "topDir",
    64  			want:  "TopDir",
    65  		},
    66  		{
    67  			input: filepath.Clean("Other_Top_Dir/Sub_Dir/File.h"),
    68  			want:  filepath.Clean("other_top_dir/sub_dir/file.h"),
    69  		},
    70  		{
    71  			input: "//TopDir/SubDir/File.h",
    72  			want:  filepath.Clean("TopDir/SubDir/File.h"),
    73  		},
    74  		{
    75  			input:   filepath.Clean("topdir/subdir/NonExistingFile.h"),
    76  			wantErr: true,
    77  		},
    78  		{
    79  			input:   filepath.Clean("TopDir/SubDir/file2.h"),
    80  			wantErr: true,
    81  		},
    82  	} {
    83  		got, err := pn.normalize(execRoot, tc.input)
    84  		if tc.wantErr {
    85  			if err == nil {
    86  				t.Errorf("pn.normalize(execRoot, %q)=%q; want error", tc.input, got)
    87  			}
    88  			continue
    89  		}
    90  		if got != tc.want || err != nil {
    91  			t.Errorf("pn.normalize(execRoot, %q)=%q, %v; want %q, <nil>", tc.input, got, err, tc.want)
    92  		}
    93  	}
    94  
    95  	t.Logf("delete TopDir/SubDir/File.h")
    96  	err := os.Remove(filepath.Join(execRoot, "TopDir/SubDir/File.h"))
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	t.Logf("create TopDir/SubDir/File2.h")
   101  	err = os.WriteFile(filepath.Join(execRoot, "TopDir/SubDir/File2.h"), nil, 0644)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	pn = newPathNormalizer(false)
   106  
   107  	for _, tc := range []struct {
   108  		input   string
   109  		want    string
   110  		wantErr bool
   111  	}{
   112  		{
   113  			input:   filepath.Clean("TopDir/SubDir/File.h"),
   114  			wantErr: true,
   115  		},
   116  		{
   117  			input:   "TopDir/SubDir/File.h",
   118  			wantErr: true,
   119  		},
   120  		{
   121  			input:   filepath.Clean("topdir/subdir/file.h"),
   122  			wantErr: true,
   123  		},
   124  		{
   125  			input: "topDir",
   126  			want:  "TopDir",
   127  		},
   128  		{
   129  			input: filepath.Clean("Other_Top_Dir/Sub_Dir/File.h"),
   130  			want:  filepath.Clean("other_top_dir/sub_dir/file.h"),
   131  		},
   132  		{
   133  			input:   "//TopDir/SubDir/File.h",
   134  			wantErr: true,
   135  		},
   136  		{
   137  			input:   filepath.Clean("topdir/subdir/NonExistingFile.h"),
   138  			wantErr: true,
   139  		},
   140  		{
   141  			input: filepath.Clean("TopDir/SubDir/file2.h"),
   142  			want:  filepath.Clean("TopDir/SubDir/File2.h"),
   143  		},
   144  	} {
   145  		got, err := pn.normalize(execRoot, tc.input)
   146  		if tc.wantErr {
   147  			if err == nil {
   148  				t.Errorf("pn.normalize(execRoot, %q)=%q; want error", tc.input, got)
   149  			}
   150  			continue
   151  		}
   152  		if got != tc.want || err != nil {
   153  			t.Errorf("pn.normalize(execRoot, %q)=%q, %v; want %q, <nil>", tc.input, got, err, tc.want)
   154  		}
   155  	}
   156  
   157  }