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