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

     1  package build
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/tilt-dev/tilt/internal/testutils/tempdir"
    10  	"github.com/tilt-dev/tilt/pkg/model"
    11  )
    12  
    13  func TestFilesToPathMappings(t *testing.T) {
    14  	f := tempdir.NewTempDirFixture(t)
    15  
    16  	paths := []string{
    17  		filepath.Join("sync1", "fileA"),
    18  		filepath.Join("sync1", "child", "fileB"),
    19  		filepath.Join("sync2", "fileC"),
    20  	}
    21  	f.TouchFiles(paths)
    22  
    23  	absPaths := make([]string, len(paths))
    24  	for i, p := range paths {
    25  		absPaths[i] = f.JoinPath(p)
    26  	}
    27  	// Add a file that doesn't exist on local -- but we still expect it to successfully
    28  	// map to a ContainerPath.
    29  	absPaths = append(absPaths, filepath.Join(f.Path(), "sync2", "file_deleted"))
    30  
    31  	syncs := []model.Sync{
    32  		model.Sync{
    33  			LocalPath:     f.JoinPath("sync1"),
    34  			ContainerPath: "/dest1",
    35  		},
    36  		model.Sync{
    37  			LocalPath:     f.JoinPath("sync2"),
    38  			ContainerPath: "/nested/dest2",
    39  		},
    40  	}
    41  	actual, skipped, err := FilesToPathMappings(absPaths, syncs)
    42  	if err != nil {
    43  		f.T().Fatal(err)
    44  	}
    45  
    46  	expected := []PathMapping{
    47  		PathMapping{
    48  			LocalPath:     f.JoinPath("sync1", "fileA"),
    49  			ContainerPath: "/dest1/fileA",
    50  		},
    51  		PathMapping{
    52  			LocalPath:     f.JoinPath("sync1", "child", "fileB"),
    53  			ContainerPath: "/dest1/child/fileB",
    54  		},
    55  		PathMapping{
    56  			LocalPath:     f.JoinPath("sync2", "fileC"),
    57  			ContainerPath: "/nested/dest2/fileC",
    58  		},
    59  		PathMapping{
    60  			LocalPath:     f.JoinPath("sync2", "file_deleted"),
    61  			ContainerPath: "/nested/dest2/file_deleted",
    62  		},
    63  	}
    64  
    65  	assert.ElementsMatch(t, expected, actual)
    66  	assert.Equal(t, 0, len(skipped))
    67  }
    68  
    69  func TestFileToDirectoryPathMapping(t *testing.T) {
    70  	f := tempdir.NewTempDirFixture(t)
    71  
    72  	paths := []string{
    73  		filepath.Join("sync1", "fileA"),
    74  	}
    75  	f.TouchFiles(paths)
    76  
    77  	absPaths := make([]string, len(paths))
    78  	for i, p := range paths {
    79  		absPaths[i] = f.JoinPath(p)
    80  	}
    81  
    82  	syncs := []model.Sync{
    83  		model.Sync{
    84  			LocalPath:     f.JoinPath("sync1", "fileA"),
    85  			ContainerPath: "/dest1/",
    86  		},
    87  	}
    88  
    89  	actual, skipped, err := FilesToPathMappings(absPaths, syncs)
    90  	if err != nil {
    91  		f.T().Fatal(err)
    92  	}
    93  
    94  	expected := []PathMapping{
    95  		PathMapping{
    96  			LocalPath:     filepath.Join(f.Path(), "sync1", "fileA"),
    97  			ContainerPath: "/dest1/fileA",
    98  		},
    99  	}
   100  
   101  	assert.ElementsMatch(t, expected, actual)
   102  	assert.Equal(t, 0, len(skipped))
   103  }
   104  
   105  func TestFileNotInSyncYieldsNoMapping(t *testing.T) {
   106  	f := tempdir.NewTempDirFixture(t)
   107  
   108  	files := []string{f.JoinPath("not/synced/fileA")}
   109  
   110  	syncs := []model.Sync{
   111  		model.Sync{
   112  			LocalPath:     f.JoinPath("sync1"),
   113  			ContainerPath: "/dest1",
   114  		},
   115  	}
   116  
   117  	actual, skipped, err := FilesToPathMappings(files, syncs)
   118  	if err != nil {
   119  		f.T().Fatal(err)
   120  	}
   121  	assert.Empty(t, actual, "expected no path mapping returned for a file not matching any syncs")
   122  	assert.Equal(t, files, skipped)
   123  }