github.com/charlievieth/fastwalk@v1.0.3/entry_filter_windows_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fastwalk
     6  
     7  import (
     8  	"io/fs"
     9  	"os"
    10  	"path/filepath"
    11  	"strconv"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestFixLongPath(t *testing.T) {
    17  	// 248 is long enough to trigger the longer-than-248 checks in
    18  	// fixLongPath, but short enough not to make a path component
    19  	// longer than 255, which is illegal on Windows. (which
    20  	// doesn't really matter anyway, since this is purely a string
    21  	// function we're testing, and it's not actually being used to
    22  	// do a system call)
    23  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    24  	for _, test := range []struct{ in, want string }{
    25  		// Short; unchanged:
    26  		{`C:\short.txt`, `C:\short.txt`},
    27  		{`C:\`, `C:\`},
    28  		{`C:`, `C:`},
    29  		// The "long" substring is replaced by a looooooong
    30  		// string which triggers the rewriting. Except in the
    31  		// cases below where it doesn't.
    32  		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
    33  		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
    34  		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
    35  		{`\\unc\path`, `\\unc\path`},
    36  		{`long.txt`, `long.txt`},
    37  		{`C:long.txt`, `C:long.txt`},
    38  		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
    39  		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
    40  		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
    41  	} {
    42  		in := strings.ReplaceAll(test.in, "long", veryLong)
    43  		want := strings.ReplaceAll(test.want, "long", veryLong)
    44  		if got := fixLongPath(in); got != want {
    45  			got = strings.ReplaceAll(got, veryLong, "long")
    46  			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
    47  		}
    48  	}
    49  }
    50  
    51  func TestEntryFilterLongPath(t *testing.T) {
    52  	tempdir := t.TempDir()
    53  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    54  
    55  	var files []string
    56  	for i := 0; i <= 9; i++ {
    57  		dir := filepath.Join(tempdir, strconv.Itoa(i))
    58  		if err := os.Mkdir(dir, 0755); err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		name := filepath.Join(dir, veryLong)
    62  		if err := os.WriteFile(name, []byte(strconv.Itoa(i)), 0644); err != nil {
    63  			t.Fatal(err)
    64  		}
    65  		files = append(files, dir, name)
    66  	}
    67  
    68  	filter := NewEntryFilter()
    69  	for _, name := range files {
    70  		fi, err := os.Lstat(name)
    71  		if err != nil {
    72  			t.Fatal(err)
    73  		}
    74  		for _, want := range []bool{false, true} {
    75  			got := filter.Entry(name, fs.FileInfoToDirEntry(fi))
    76  			if got != want {
    77  				t.Errorf("filepath.Entry(%q) = %t want: %t", name, got, want)
    78  			}
    79  		}
    80  	}
    81  }