(t *testing.T)
| 409 | } |
| 410 | |
| 411 | func TestCopyFileContents(t *testing.T) { |
| 412 | t.Run("returns close error after successful sync", func(t *testing.T) { |
| 413 | closeErr := errors.New("close failed") |
| 414 | out := &stubSyncWriteCloser{closeErr: closeErr} |
| 415 | |
| 416 | err := copyFileContents(strings.NewReader("hello"), out, filepath.Join(t.TempDir(), "dst.txt")) |
| 417 | |
| 418 | require.ErrorIs(t, err, closeErr) |
| 419 | assert.Equal(t, 1, out.closeCalls, "destination should be closed once") |
| 420 | assert.Equal(t, "hello", out.String(), "content should be copied before close") |
| 421 | }) |
| 422 | |
| 423 | t.Run("preserves copy error and closes destination once", func(t *testing.T) { |
| 424 | writeErr := errors.New("write failed") |
| 425 | closeErr := errors.New("close failed") |
| 426 | out := &stubSyncWriteCloser{ |
| 427 | writeErr: writeErr, |
| 428 | closeErr: closeErr, |
| 429 | } |
| 430 | |
| 431 | dst := filepath.Join(t.TempDir(), "dst.txt") |
| 432 | require.NoError(t, os.WriteFile(dst, []byte("partial"), 0600), "Should create destination placeholder") |
| 433 | |
| 434 | err := copyFileContents(strings.NewReader("hello"), out, dst) |
| 435 | |
| 436 | require.ErrorIs(t, err, writeErr) |
| 437 | assert.Equal(t, 1, out.closeCalls, "destination should be closed once during cleanup") |
| 438 | assert.NoFileExists(t, dst, "partial destination should be removed after copy failure") |
| 439 | }) |
| 440 | } |
| 441 | |
| 442 | func TestValidatePathWithinBase(t *testing.T) { |
| 443 | base := t.TempDir() |
nothing calls this directly
no test coverage detected