Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

6.1 KB
Raw Blame
using Filee.Core.Formats;
using Filee.Core.Presets;

namespace Filee.Core.Tests;

public class OutputPathResolverTests
{
    private static readonly DateTime Now = new(2026, 9, 30, 14, 5, 9);

    private static OutputPathResolver.Tokens Tokens(string source, int index = 1) => new(source, "PNG", index, Now);

    [Fact]
    public void Default_pattern_keeps_the_source_name()
    {
        using var dir = new TempDir();
        var source = dir.File("photo.jpg");

        var path = OutputPathResolver.Resolve(new OutputRule(), Tokens(source), "png", File.Exists);

        Assert.Equal(Path.Combine(dir.Path, "photo.png"), path);
    }

    [Fact]
    public void Two_part_extensions_are_one_extension()
    {
        using var dir = new TempDir();
        var source = dir.File("backup.tar.gz");

        var path = OutputPathResolver.Resolve(new OutputRule(), Tokens(source), "zip", File.Exists);

        Assert.Equal(Path.Combine(dir.Path, "backup.zip"), path);
        Assert.Equal("tgz", FormatRegistry.Detect(source)!.Id);
        Assert.Equal("tar.gz", FormatRegistry.ExtensionOf(source));
        Assert.Equal("gz", FormatRegistry.ExtensionOf("notes.txt.gz"));
        Assert.Equal("notes.txt", FormatRegistry.NameWithoutExtension("notes.txt.gz"));
        Assert.Equal("split", FormatRegistry.Detect("archive.7z.001")!.Id);
        Assert.Equal("7z.001", FormatRegistry.ExtensionOf("archive.7z.001"));
        Assert.Equal("archive", FormatRegistry.NameWithoutExtension("archive.7z.001"));
        Assert.Equal("split", FormatRegistry.Detect("archive.001")!.Id);
        Assert.Equal("001", FormatRegistry.ExtensionOf("archive.001"));
        Assert.Equal("archive", FormatRegistry.NameWithoutExtension("archive.001"));
        Assert.Equal("mp4", FormatRegistry.Detect("clip.MP4")!.Id);
        Assert.Null(FormatRegistry.Detect("README"));
    }

    [Fact]
    public void An_empty_extension_asks_for_a_folder()
    {
        using var dir = new TempDir();
        var source = dir.File("photos.zip");
        Directory.CreateDirectory(Path.Combine(dir.Path, "photos"));

        var path = OutputPathResolver.Resolve(new OutputRule(), Tokens(source), "", p => File.Exists(p) || Directory.Exists(p));

        Assert.Equal(Path.Combine(dir.Path, "photos (2)"), path);
    }

    [Fact]
    public void A_folder_ending_in_a_separator_is_named_and_placed_like_the_folder()
    {
        using var dir = new TempDir();
        var folder = Directory.CreateDirectory(Path.Combine(dir.Path, "Photos")).FullName;

        var path = OutputPathResolver.Resolve(new OutputRule(), Tokens(folder + Path.DirectorySeparatorChar), "zip", File.Exists);

        Assert.Equal(Path.Combine(dir.Path, "Photos.zip"), path);
    }

    [Fact]
    public void Existing_file_gets_a_number_by_default()
    {
        using var dir = new TempDir();
        var source = dir.File("photo.jpg");
        dir.File("photo.png");

        var path = OutputPathResolver.Resolve(new OutputRule(), Tokens(source), "png", File.Exists);

        Assert.Equal(Path.Combine(dir.Path, "photo (2).png"), path);
    }

    [Fact]
    public void Overwrite_and_skip_policies()
    {
        using var dir = new TempDir();
        var source = dir.File("photo.jpg");
        var existing = dir.File("photo.png");

        Assert.Equal(existing, OutputPathResolver.Resolve(new OutputRule { Conflict = ConflictPolicy.Overwrite }, Tokens(source), "png", File.Exists));
        Assert.Null(OutputPathResolver.Resolve(new OutputRule { Conflict = ConflictPolicy.Skip }, Tokens(source), "png", File.Exists));
    }

    [Theory]
    [InlineData(ConflictPolicy.Overwrite)]
    [InlineData(ConflictPolicy.Skip)]
    [InlineData(ConflictPolicy.Rename)]
    public void The_source_file_is_never_overwritten(ConflictPolicy policy)
    {
        using var dir = new TempDir();
        var source = dir.File("photo.jpg");

        var path = OutputPathResolver.Resolve(new OutputRule { Conflict = policy }, Tokens(source), "jpg", File.Exists);

        Assert.Equal(Path.Combine(dir.Path, "photo (2).jpg"), path);
    }

    [Fact]
    public void Tokens_are_expanded_and_invalid_characters_removed()
    {
        var rule = new OutputRule { FileNamePattern = "{name}_{preset}_{date}_{index}:?" };
        var path = OutputPathResolver.Resolve(rule, Tokens(Path.Combine("C:", "x", "a.jpg"), 3), "png", _ => false);

        Assert.Equal("a_PNG_20260930_3__.png", Path.GetFileName(path));
    }

    [Fact]
    public void Tokens_in_the_source_name_are_kept_as_they_are()
    {
        var rule = new OutputRule { FileNamePattern = "{name}_{index}" };
        var path = OutputPathResolver.Resolve(rule, Tokens(Path.Combine("C:", "x", "Minutes {date} {Index}.docx"), 3), "pdf", _ => false);

        Assert.Equal("Minutes {date} {Index}_3.pdf", Path.GetFileName(path));
    }

    [Fact]
    public void Subfolder_name_supports_tokens()
    {
        using var dir = new TempDir();
        var source = dir.File("report.pdf");
        var rule = new OutputRule { Location = OutputLocation.Subfolder, SubfolderName = "{name}" };

        var path = OutputPathResolver.Resolve(rule, Tokens(source), "pdf", File.Exists, "_p1");

        Assert.Equal(Path.Combine(dir.Path, "report", "report_p1.pdf"), path);
    }

    [Fact]
    public void Custom_folder_is_used_when_set()
    {
        var rule = new OutputRule { Location = OutputLocation.CustomFolder, CustomFolder = Path.Combine("D:", "out") };

        var path = OutputPathResolver.Resolve(rule, Tokens(Path.Combine("C:", "in", "a.jpg")), "png", _ => false);

        Assert.Equal(Path.Combine("D:", "out", "a.png"), path);
    }

    [Theory]
    [InlineData(ConflictPolicy.Overwrite)]
    [InlineData(ConflictPolicy.Skip)]
    [InlineData(ConflictPolicy.Rename)]
    public void A_case_alias_of_the_source_is_never_overwritten(ConflictPolicy policy)
    {
        using var dir = new TempDir();
        var source = dir.File("photo.jpg", "original");
        var rule = new OutputRule { FileNamePattern = "PHOTO", Conflict = policy };

        var path = OutputPathResolver.Resolve(rule, Tokens(source), "jpg", File.Exists);

        Assert.Equal(Path.Combine(dir.Path, "PHOTO (2).jpg"), path);
        Assert.Equal("original", File.ReadAllText(source));
    }

}