github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/fs_unix_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  		"foo",
    30  		"dir/bar",
    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  	pn := newPathNormalizer(false)
    44  	for _, tc := range []struct {
    45  		input   string
    46  		want    string
    47  		wantErr bool
    48  	}{
    49  		{
    50  			input: "foo",
    51  			want:  "foo",
    52  		},
    53  		{
    54  			input:   "Foo",
    55  			wantErr: true,
    56  		},
    57  		{
    58  			input: "/foo",
    59  			want:  "foo",
    60  		},
    61  		{
    62  			input: "//foo",
    63  			want:  "foo",
    64  		},
    65  		{
    66  			input: "dir/bar",
    67  			want:  "dir/bar",
    68  		},
    69  		{
    70  			input: "dir//bar",
    71  			want:  "dir/bar",
    72  		},
    73  		{
    74  			input:   "foo/bar",
    75  			wantErr: true,
    76  		},
    77  	} {
    78  		got, err := pn.normalize(execRoot, tc.input)
    79  		if tc.wantErr {
    80  			if err == nil {
    81  				t.Errorf("pn.normalize(execRoot, %q)=%q; want error", tc.input, got)
    82  			}
    83  			continue
    84  		}
    85  		if got != tc.want || err != nil {
    86  			t.Errorf("pn.normalize(execRoot, %q)=%q, %v; want=%q, <nil>", tc.input, got, err, tc.want)
    87  		}
    88  	}
    89  }