|
1 | 1 | package git_commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
4 | 6 | "strings" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/go-errors/errors" |
| 10 | + "github.com/jesseduffield/lazygit/pkg/commands/models" |
8 | 11 | "github.com/jesseduffield/lazygit/pkg/commands/oscommands" |
9 | 12 | "github.com/jesseduffield/lazygit/pkg/env" |
10 | 13 | "github.com/samber/lo" |
11 | 14 | "github.com/stretchr/testify/assert" |
12 | 15 | ) |
13 | 16 |
|
| 17 | +func TestParseGitmodules(t *testing.T) { |
| 18 | + scenarios := []struct { |
| 19 | + testName string |
| 20 | + lines []string |
| 21 | + expected []*models.SubmoduleConfig |
| 22 | + }{ |
| 23 | + { |
| 24 | + testName: "two submodules", |
| 25 | + lines: []string{ |
| 26 | + `[submodule "one"]`, |
| 27 | + "\tpath = path/to/one", |
| 28 | + "\turl = https://example.com/one.git", |
| 29 | + `[submodule "two"]`, |
| 30 | + "\tpath = two", |
| 31 | + "\turl = ../two", |
| 32 | + }, |
| 33 | + expected: []*models.SubmoduleConfig{ |
| 34 | + {Name: "one", Path: "path/to/one", Url: "https://example.com/one.git"}, |
| 35 | + {Name: "two", Path: "two", Url: "../two"}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + testName: "url that contains path=", |
| 40 | + lines: []string{ |
| 41 | + `[submodule "sub"]`, |
| 42 | + "\tpath = sub", |
| 43 | + "\turl = https://example.com/repo?path=other", |
| 44 | + }, |
| 45 | + expected: []*models.SubmoduleConfig{ |
| 46 | + {Name: "sub", Path: "sub", Url: "https://example.com/repo?path=other"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + testName: "commented-out section", |
| 51 | + lines: []string{ |
| 52 | + `[submodule "sub"]`, |
| 53 | + "\tpath = sub", |
| 54 | + "\turl = ../sub", |
| 55 | + `#[submodule "old"]`, |
| 56 | + "#\tpath = old", |
| 57 | + "#\turl = ../old", |
| 58 | + }, |
| 59 | + expected: []*models.SubmoduleConfig{ |
| 60 | + {Name: "sub", Path: "sub", Url: "../sub"}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, s := range scenarios { |
| 66 | + t.Run(s.testName, func(t *testing.T) { |
| 67 | + configs, err := parseGitmodules(strings.NewReader(strings.Join(s.lines, "\n")), nil) |
| 68 | + assert.NoError(t, err) |
| 69 | + assert.Equal(t, s.expected, configs) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestIsSuspiciousSubmoduleName(t *testing.T) { |
| 75 | + scenarios := []struct { |
| 76 | + name string |
| 77 | + suspicious bool |
| 78 | + }{ |
| 79 | + {name: "sub", suspicious: false}, |
| 80 | + {name: "path/to/sub", suspicious: false}, |
| 81 | + {name: "path\\to\\sub", suspicious: false}, |
| 82 | + {name: ".", suspicious: false}, |
| 83 | + {name: "...", suspicious: false}, |
| 84 | + {name: "..sub", suspicious: false}, |
| 85 | + {name: "sub..", suspicious: false}, |
| 86 | + {name: "a..b/c", suspicious: false}, |
| 87 | + {name: "", suspicious: true}, |
| 88 | + {name: "..", suspicious: true}, |
| 89 | + {name: "../victim", suspicious: true}, |
| 90 | + {name: "../../../victim", suspicious: true}, |
| 91 | + {name: "sub/..", suspicious: true}, |
| 92 | + {name: "sub/../../victim", suspicious: true}, |
| 93 | + {name: "..\\victim", suspicious: true}, |
| 94 | + {name: "sub\\..\\..\\victim", suspicious: true}, |
| 95 | + {name: "sub/..\\..\\victim", suspicious: true}, |
| 96 | + } |
| 97 | + |
| 98 | + for _, s := range scenarios { |
| 99 | + t.Run(s.name, func(t *testing.T) { |
| 100 | + assert.Equal(t, s.suspicious, isSuspiciousSubmoduleName(s.name)) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestIsSuspiciousSubmodulePath(t *testing.T) { |
| 106 | + dir := t.TempDir() |
| 107 | + parentDir := filepath.Join(dir, "repo") |
| 108 | + assert.NoError(t, os.MkdirAll(filepath.Join(parentDir, "sub"), 0o755)) |
| 109 | + assert.NoError(t, os.MkdirAll(filepath.Join(parentDir, "path", "to", "sub"), 0o755)) |
| 110 | + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "other"), 0o755)) |
| 111 | + assert.NoError(t, os.WriteFile(filepath.Join(parentDir, "file"), []byte("content"), 0o644)) |
| 112 | + |
| 113 | + scenarios := []struct { |
| 114 | + path string |
| 115 | + suspicious bool |
| 116 | + }{ |
| 117 | + {path: "sub", suspicious: false}, |
| 118 | + {path: "sub/", suspicious: false}, |
| 119 | + {path: "path/to/sub", suspicious: false}, |
| 120 | + {path: "not_checked_out", suspicious: false}, |
| 121 | + {path: "path/not_checked_out/sub", suspicious: false}, |
| 122 | + {path: "", suspicious: true}, |
| 123 | + {path: ".", suspicious: true}, |
| 124 | + {path: "./", suspicious: true}, |
| 125 | + {path: "sub/..", suspicious: true}, |
| 126 | + {path: "..", suspicious: true}, |
| 127 | + {path: "../other", suspicious: true}, |
| 128 | + {path: "sub/../../other", suspicious: true}, |
| 129 | + {path: filepath.Join(dir, "other"), suspicious: true}, |
| 130 | + {path: "file", suspicious: true}, |
| 131 | + {path: "file/sub", suspicious: true}, |
| 132 | + } |
| 133 | + |
| 134 | + for _, s := range scenarios { |
| 135 | + t.Run(s.path, func(t *testing.T) { |
| 136 | + assert.Equal(t, s.suspicious, isSuspiciousSubmodulePath(parentDir, s.path)) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestIsSuspiciousSubmodulePathWithSymlinks(t *testing.T) { |
| 142 | + dir := t.TempDir() |
| 143 | + parentDir := filepath.Join(dir, "repo") |
| 144 | + assert.NoError(t, os.MkdirAll(filepath.Join(parentDir, "sub"), 0o755)) |
| 145 | + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "other", "sub"), 0o755)) |
| 146 | + if err := os.Symlink(filepath.Join("..", "other"), filepath.Join(parentDir, "link_outside")); err != nil { |
| 147 | + t.Skipf("Can't create symlinks: %v", err) |
| 148 | + } |
| 149 | + assert.NoError(t, os.Symlink("sub", filepath.Join(parentDir, "link_inside"))) |
| 150 | + |
| 151 | + scenarios := []struct { |
| 152 | + path string |
| 153 | + suspicious bool |
| 154 | + }{ |
| 155 | + {path: "sub", suspicious: false}, |
| 156 | + {path: "link_outside", suspicious: true}, |
| 157 | + {path: "link_outside/sub", suspicious: true}, |
| 158 | + {path: "link_inside", suspicious: true}, |
| 159 | + } |
| 160 | + |
| 161 | + for _, s := range scenarios { |
| 162 | + t.Run(s.path, func(t *testing.T) { |
| 163 | + assert.Equal(t, s.suspicious, isSuspiciousSubmodulePath(parentDir, s.path)) |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestSubmoduleDeleteOutsideModulesDir(t *testing.T) { |
| 169 | + scenarios := []struct { |
| 170 | + testName string |
| 171 | + name string |
| 172 | + // relative to the directory that contains the repo |
| 173 | + victimPath string |
| 174 | + }{ |
| 175 | + { |
| 176 | + testName: "modules dir itself", |
| 177 | + name: ".", |
| 178 | + victimPath: "repo/.git/modules/other_submodule", |
| 179 | + }, |
| 180 | + { |
| 181 | + testName: "outside the repo", |
| 182 | + name: "../../../victim", |
| 183 | + victimPath: "victim", |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + for _, s := range scenarios { |
| 188 | + t.Run(s.testName, func(t *testing.T) { |
| 189 | + dir := t.TempDir() |
| 190 | + victimFile := filepath.Join(dir, s.victimPath, "file") |
| 191 | + assert.NoError(t, os.MkdirAll(filepath.Dir(victimFile), 0o755)) |
| 192 | + assert.NoError(t, os.WriteFile(victimFile, []byte("content"), 0o644)) |
| 193 | + |
| 194 | + runner := oscommands.NewFakeRunner(t) |
| 195 | + instance := buildSubmoduleCommands(commonDeps{ |
| 196 | + runner: runner, |
| 197 | + repoPaths: MockRepoPaths(filepath.Join(dir, "repo")), |
| 198 | + }) |
| 199 | + |
| 200 | + err := instance.Delete(&models.SubmoduleConfig{Name: s.name, Path: "sub"}) |
| 201 | + assert.Error(t, err) |
| 202 | + assert.FileExists(t, victimFile) |
| 203 | + runner.CheckForMissingCalls() |
| 204 | + }) |
| 205 | + } |
| 206 | +} |
| 207 | + |
14 | 208 | func TestSubmoduleGetConflictCommits(t *testing.T) { |
15 | 209 | type scenario struct { |
16 | 210 | testName string |
|
0 commit comments