|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
4 | 8 | "testing" |
5 | 9 |
|
6 | 10 | "github.com/git-lfs/git-lfs/v3/config" |
| 11 | + "github.com/git-lfs/git-lfs/v3/lfs" |
7 | 12 | "github.com/stretchr/testify/assert" |
8 | 13 | ) |
9 | 14 |
|
@@ -184,3 +189,135 @@ func TestIsAutoTrackExcludedUserConfigEmpty(t *testing.T) { |
184 | 189 | assert.True(t, isAutoTrackExcluded(".gitlab-ci.yml")) |
185 | 190 | assert.False(t, isAutoTrackExcluded("normal.dat")) |
186 | 191 | } |
| 192 | + |
| 193 | +// setupAutoTrackTest creates a temporary directory with a .gitattributes file |
| 194 | +// and a .git/lfs/tmp directory (needed by cfg.TempDir()), then replaces the |
| 195 | +// global cfg with a config pointing to that directory. The returned function |
| 196 | +// restores the original cfg. |
| 197 | +func setupAutoTrackTest(t *testing.T, gitattributesContent string) func() { |
| 198 | + t.Helper() |
| 199 | + dir := t.TempDir() |
| 200 | + |
| 201 | + err := os.MkdirAll(filepath.Join(dir, ".git", "lfs", "tmp"), 0755) |
| 202 | + if err != nil { |
| 203 | + t.Fatal(err) |
| 204 | + } |
| 205 | + |
| 206 | + if gitattributesContent != "" { |
| 207 | + err = os.WriteFile(filepath.Join(dir, ".gitattributes"), []byte(gitattributesContent), 0644) |
| 208 | + if err != nil { |
| 209 | + t.Fatal(err) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + saved := cfg |
| 214 | + cfg = config.NewIn(dir, "") |
| 215 | + return func() { cfg = saved } |
| 216 | +} |
| 217 | + |
| 218 | +// --- Plan point 6: Memory concern with io.ReadAll — spooling in clean() --- |
| 219 | + |
| 220 | +func TestCleanAutoTrackPassThrough(t *testing.T) { |
| 221 | + defer setupAutoTrackTest(t, "* autotracksize=100")() |
| 222 | + |
| 223 | + gf := lfs.NewGitFilter(cfg) |
| 224 | + var buf bytes.Buffer |
| 225 | + data := strings.NewReader("small file content") |
| 226 | + |
| 227 | + ptr, err := clean(gf, &buf, data, "test.dat", -1) |
| 228 | + assert.Nil(t, ptr) |
| 229 | + assert.NoError(t, err) |
| 230 | + assert.Equal(t, "small file content", buf.String()) |
| 231 | +} |
| 232 | + |
| 233 | +func TestCleanAutoTrackGitCriticalExcluded(t *testing.T) { |
| 234 | + defer setupAutoTrackTest(t, "* autotracksize=10")() |
| 235 | + |
| 236 | + gf := lfs.NewGitFilter(cfg) |
| 237 | + var buf bytes.Buffer |
| 238 | + data := strings.NewReader("this is a much longer file content that exceeds") |
| 239 | + |
| 240 | + ptr, err := clean(gf, &buf, data, ".gitattributes", -1) |
| 241 | + assert.Nil(t, ptr) |
| 242 | + assert.NoError(t, err) |
| 243 | + assert.Equal(t, "this is a much longer file content that exceeds", buf.String()) |
| 244 | +} |
| 245 | + |
| 246 | +func TestCleanAutoTrackExcludedFile(t *testing.T) { |
| 247 | + defer setupAutoTrackTest(t, "* autotracksize=10")() |
| 248 | + |
| 249 | + gf := lfs.NewGitFilter(cfg) |
| 250 | + var buf bytes.Buffer |
| 251 | + data := strings.NewReader("this is a much longer file content that exceeds") |
| 252 | + |
| 253 | + ptr, err := clean(gf, &buf, data, "readme.md", -1) |
| 254 | + assert.Nil(t, ptr) |
| 255 | + assert.NoError(t, err) |
| 256 | + assert.Equal(t, "this is a much longer file content that exceeds", buf.String()) |
| 257 | +} |
| 258 | + |
| 259 | +func TestCleanAutoTrackAlreadyPointer(t *testing.T) { |
| 260 | + defer setupAutoTrackTest(t, "* autotracksize=100")() |
| 261 | + |
| 262 | + gf := lfs.NewGitFilter(cfg) |
| 263 | + var buf bytes.Buffer |
| 264 | + pointerContent := "version https://git-lfs.github.com/spec/v1\noid sha256:7cd8be1d2cd0dd22cd9d229bb6b5785009a05e8b39d405615d882caac56562b5\nsize 1024\n" |
| 265 | + data := strings.NewReader(pointerContent) |
| 266 | + |
| 267 | + ptr, err := clean(gf, &buf, data, "test.dat", -1) |
| 268 | + assert.NotNil(t, ptr) |
| 269 | + assert.NoError(t, err) |
| 270 | + assert.Equal(t, pointerContent, buf.String()) |
| 271 | +} |
| 272 | + |
| 273 | +// --- Plan point 7: Smudge pass-through check --- |
| 274 | + |
| 275 | +func TestSmudgePassThroughAutoTrack(t *testing.T) { |
| 276 | + defer setupAutoTrackTest(t, "* autotracksize=100")() |
| 277 | + |
| 278 | + // File under threshold passes through |
| 279 | + assert.True(t, smudgePassThrough("test.dat", 50)) |
| 280 | + |
| 281 | + // File over threshold does not pass through |
| 282 | + assert.False(t, smudgePassThrough("test.dat", 200)) |
| 283 | + |
| 284 | + // Excluded file passes through regardless of size |
| 285 | + assert.True(t, smudgePassThrough("readme.md", 200)) |
| 286 | + |
| 287 | + // Git-critical file passes through regardless of size |
| 288 | + assert.True(t, smudgePassThrough(".gitattributes", 200)) |
| 289 | +} |
| 290 | + |
| 291 | +// --- Plan point 8: fileSize = -1 (PR #6011 scenario) --- |
| 292 | + |
| 293 | +func TestCleanAutoTrackFileSizeNegativeOne(t *testing.T) { |
| 294 | + defer setupAutoTrackTest(t, "* autotracksize=100")() |
| 295 | + |
| 296 | + gf := lfs.NewGitFilter(cfg) |
| 297 | + var buf bytes.Buffer |
| 298 | + data := strings.NewReader("small content") |
| 299 | + |
| 300 | + // fileSize=-1 simulates behavior after PR #6011; the autotrack |
| 301 | + // pass-through must not depend on this parameter. |
| 302 | + ptr, err := clean(gf, &buf, data, "test.dat", -1) |
| 303 | + assert.Nil(t, ptr) |
| 304 | + assert.NoError(t, err) |
| 305 | + assert.Equal(t, "small content", buf.String()) |
| 306 | +} |
| 307 | + |
| 308 | +func TestCleanAutoTrackFileSizeNegativeOneOverThreshold(t *testing.T) { |
| 309 | + defer setupAutoTrackTest(t, "* autotracksize=10")() |
| 310 | + |
| 311 | + gf := lfs.NewGitFilter(cfg) |
| 312 | + var buf bytes.Buffer |
| 313 | + data := strings.NewReader("this is a much longer file content that exceeds") |
| 314 | + |
| 315 | + ptr, err := clean(gf, &buf, data, "nontracked.dat", -1) |
| 316 | + // With autotrack active, file over threshold should NOT pass through |
| 317 | + // as (nil, nil). It hits gf.Clean which needs a real git repo, so |
| 318 | + // we expect some error (ExitWithError or similar), but the key is |
| 319 | + // that it did NOT silently pass through via the old fileSize >= 0 gating. |
| 320 | + if ptr == nil && err == nil { |
| 321 | + t.Errorf("file over threshold with autotrack should not return (nil, nil); must reach gf.Clean") |
| 322 | + } |
| 323 | +} |
0 commit comments