github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/ospath/ospath_test.go (about)

     1  package ospath
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/tilt-dev/tilt/internal/testutils/tempdir"
    10  )
    11  
    12  func TestChild(t *testing.T) {
    13  	f := NewOspathFixture(t)
    14  
    15  	paths := []string{
    16  		filepath.Join("parent", "fileA"),
    17  		filepath.Join("parent", "child", "fileB"),
    18  		filepath.Join("parent", "child", "grandchild", "fileC"),
    19  		filepath.Join("sibling", "fileD"),
    20  	}
    21  	f.TouchFiles(paths)
    22  
    23  	f.assertChild("parent", filepath.Join("sibling", "fileD"), "")
    24  	f.assertChild(filepath.Join("parent", "child"), filepath.Join("parent", "fileA"), "")
    25  	f.assertChild("parent", filepath.Join("parent", "fileA"), "fileA")
    26  	f.assertChild("parent", filepath.Join("parent", "child", "fileB"), filepath.Join("child", "fileB"))
    27  	f.assertChild("parent", filepath.Join("parent", "child", "grandchild", "fileC"), filepath.Join("child", "grandchild", "fileC"))
    28  
    29  	f.assertChild(filepath.Join("parent", "child"), filepath.Join("parent", "child", "fileB"), "fileB")
    30  
    31  	f.assertChild("parent", "parent", ".")
    32  }
    33  
    34  func TestCaseInsensitiveFileSystem(t *testing.T) {
    35  	f := NewOspathFixture(t)
    36  
    37  	fileA := filepath.Join("parent", "fileA")
    38  	f.TouchFiles([]string{fileA})
    39  
    40  	// Assume that macOS and Windows are case-insensitive, and other operating
    41  	// systems are not. This isn't strictly accurate, but is good enough for
    42  	// testing.
    43  	if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
    44  		f.assertChild("Parent", fileA, "fileA")
    45  	} else {
    46  		f.assertChild("Parent", fileA, "")
    47  	}
    48  }
    49  
    50  func TestIsBrokenSymlink(t *testing.T) {
    51  	if runtime.GOOS == "windows" {
    52  		t.Skip("windows does not support user-land symlinks")
    53  	}
    54  	f := NewOspathFixture(t)
    55  
    56  	f.TouchFiles([]string{
    57  		"fileA",
    58  		"child/fileB",
    59  		"child/grandchild/fileC",
    60  	})
    61  
    62  	f.symlink("fileA", "symlinkFileA")
    63  	f.symlink("fileB", "symlinkFileB")
    64  
    65  	f.assertBrokenSymlink("fileA", false)
    66  	f.assertBrokenSymlink("fileB", false)
    67  	f.assertBrokenSymlink("child/fileB", false)
    68  	f.assertBrokenSymlink("child/grandchild/fileC", false)
    69  	f.assertBrokenSymlink("symlinkFileA", false)
    70  	f.assertBrokenSymlink("symlinkFileB", true)
    71  }
    72  
    73  func TestInvalidDir(t *testing.T) {
    74  	f := NewOspathFixture(t)
    75  
    76  	// Passing "" as dir used to make Child hang forever. Let's make sure it doesn't do that.
    77  	_, isChild := Child("", "random")
    78  	if isChild {
    79  		f.t.Fatalf("Expected file 'random' to NOT be a child of empty dir")
    80  	}
    81  }
    82  
    83  func TestDirTrailingSlash(t *testing.T) {
    84  	f := NewOspathFixture(t)
    85  
    86  	f.TouchFiles([]string{filepath.Join("some", "dir", "file")})
    87  
    88  	// Should work regardless of whether directory has trailing slash
    89  	f.assertChild(filepath.Join("some", "dir"),
    90  		filepath.Join("some", "dir", "file"), "file")
    91  	f.assertChild(filepath.Join("some", "dir")+string(filepath.Separator),
    92  		filepath.Join("some", "dir", "file"), "file")
    93  }
    94  
    95  func TestTryAsCwdChildren(t *testing.T) {
    96  	f := NewOspathFixture(t)
    97  	f.Chdir()
    98  
    99  	results := TryAsCwdChildren([]string{f.Path()})
   100  
   101  	if len(results) == 0 {
   102  		t.Fatal("Expected 1 result, got 0")
   103  	}
   104  
   105  	r := results[0]
   106  
   107  	if r != "." {
   108  		t.Errorf("Expected %s to equal \".\"", r)
   109  	}
   110  }
   111  
   112  type OspathFixture struct {
   113  	*tempdir.TempDirFixture
   114  	t *testing.T
   115  }
   116  
   117  func NewOspathFixture(t *testing.T) *OspathFixture {
   118  	ret := &OspathFixture{
   119  		TempDirFixture: tempdir.NewTempDirFixture(t),
   120  		t:              t,
   121  	}
   122  	return ret
   123  }
   124  
   125  // pass `expectedRelative` = "" to indicate that `file` is NOT a child of `dir`
   126  func (f *OspathFixture) assertChild(dir, file, expectedRel string) {
   127  	dir = f.JoinPath(dir)
   128  	file = f.JoinPath(file)
   129  	rel, isChild := Child(dir, file)
   130  	if expectedRel == "" {
   131  		if isChild {
   132  			f.t.Fatalf("Expected file '%s' to NOT be a child of dir '%s'", file, dir)
   133  		}
   134  		return
   135  	}
   136  
   137  	if !isChild {
   138  		f.t.Fatalf("Expected file '%s' to be a child of dir '%s', but got !isChild", file, dir)
   139  	}
   140  
   141  	if rel != expectedRel {
   142  		f.t.Fatalf("Expected relative path of '%s' to dir '%s' to be: '%s'. Actual: '%s'.", file, dir, expectedRel, rel)
   143  	}
   144  }
   145  
   146  func (f *OspathFixture) symlink(oldPath, newPath string) {
   147  	oldPath = filepath.Join(f.Path(), oldPath)
   148  	newPath = filepath.Join(f.Path(), newPath)
   149  	err := os.Symlink(oldPath, newPath)
   150  	if err != nil {
   151  		f.t.Fatal(err)
   152  	}
   153  }
   154  
   155  func (f *OspathFixture) assertBrokenSymlink(file string, expected bool) {
   156  	broken, err := IsBrokenSymlink(filepath.Join(f.Path(), file))
   157  	if err != nil {
   158  		f.t.Fatal(err)
   159  	}
   160  
   161  	if broken != expected {
   162  		if broken {
   163  			f.t.Fatalf("Expected a regular file or working symlink: %s", file)
   164  		} else {
   165  			f.t.Fatalf("Expected a broken symlink: %s", file)
   166  		}
   167  	}
   168  }