Skip to content

Test: Improve Temporary File Cleanup and Path Management #206

@nickna

Description

@nickna

Description

Standardize temporary file creation, cleanup, and path management across all tests to prevent test artifacts and improve reliability.

Phase

Phase 0: Test Technical Debt Cleanup

Epic

Related to #202

Acceptance Criteria

  • Audit all tests that create temporary files or directories
  • Create standardized TestFileManager utility
  • Ensure all temporary files are cleaned up in TearDown methods
  • Use unique paths to prevent test interference
  • Add failure cleanup for interrupted tests

Current Issues

  • Some tests use hardcoded temporary paths
  • Inconsistent cleanup of failed test artifacts
  • Potential file conflicts between parallel tests

Proposed Solution

public class TestFileManager : IDisposable
{
    private readonly List<string> _tempPaths = new();
    
    public string CreateTempDirectory(string prefix = "test")
    {
        var path = Path.Combine(Path.GetTempPath(), $"{prefix}_{Guid.NewGuid():N}");
        Directory.CreateDirectory(path);
        _tempPaths.Add(path);
        return path;
    }
    
    public string CreateTempFile(string extension = ".tmp")
    {
        var path = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid():N}{extension}");
        _tempPaths.Add(path);
        return path;
    }
    
    public void Dispose()
    {
        foreach (var path in _tempPaths)
        {
            try
            {
                if (Directory.Exists(path))
                    Directory.Delete(path, true);
                else if (File.Exists(path))
                    File.Delete(path);
            }
            catch { /* Ignore cleanup failures */ }
        }
    }
}

Files to Review

  • Search for Path.GetTempPath() usage
  • Tests creating database files
  • Serialization tests
  • Integration tests with file I/O

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions