Ancient

 view release on metacpan or  search on metacpan

t/8006-file-platform.t  view on Meta::CPAN

# ============================================

subtest 'path separators' => sub {
    # Forward slashes work everywhere
    my $path1 = file::join('a', 'b', 'c');
    ok($path1 =~ m{a.b.c}, 'join produces path-like string');

    # Test dirname/basename with forward slashes
    is(file::basename('/path/to/file.txt'), 'file.txt', 'basename with forward slashes');
    is(file::dirname('/path/to/file.txt'), '/path/to', 'dirname with forward slashes');

    SKIP: {
        skip "Windows path tests", 2 unless $is_windows;

        # Windows should also handle backslashes
        is(file::basename('C:\\path\\to\\file.txt'), 'file.txt', 'basename with backslashes');
        is(file::dirname('C:\\path\\to\\file.txt'), 'C:\\path\\to', 'dirname with backslashes');
    }
};

# ============================================
# Symlink tests (Unix only)
# ============================================

SKIP: {
    skip "Symlink tests require Unix", 6 unless $is_unix;

    subtest 'symlinks' => sub {
        my $target = "$tmpdir/symlink_target.txt";
        my $link = "$tmpdir/symlink_link.txt";

        file::spew($target, "target content");

        SKIP: {
            skip "symlink not available", 5 unless eval { symlink($target, $link) };

            ok(file::exists($link), 'symlink exists');
            ok(file::is_link($link), 'is_link returns true for symlink');
            ok(!file::is_link($target), 'is_link returns false for regular file');

            # Reading through symlink should work
            is(file::slurp($link), "target content", 'can read through symlink');

            # File tests on symlink
            ok(file::is_file($link), 'symlink to file is_file');
        }
    };
}

# ============================================
# Permission tests (Unix only)
# ============================================

SKIP: {
    skip "Permission tests require Unix", 1 unless $is_unix;

    subtest 'unix permissions' => sub {
        my $file = "$tmpdir/perm_test.txt";
        file::spew($file, "permission test");

        # Test chmod
        ok(file::chmod($file, 0644), 'chmod 0644');
        my $mode = file::mode($file);
        is($mode & 0777, 0644, 'mode is 0644');

        ok(file::chmod($file, 0755), 'chmod 0755');
        $mode = file::mode($file);
        is($mode & 0777, 0755, 'mode is 0755');

        # Test is_executable
        ok(file::is_executable($file), 'is_executable after chmod 0755');

        file::chmod($file, 0644);
        ok(!file::is_executable($file), 'not executable after chmod 0644');
    };
}

# ============================================
# Case sensitivity
# ============================================

subtest 'case sensitivity' => sub {
    my $lower = "$tmpdir/casefile.txt";
    my $upper = "$tmpdir/CaseFile.txt";

    file::spew($lower, "lowercase");

    SKIP: {
        # On case-insensitive systems (macOS default, Windows), these are same file
        skip "Case-insensitive filesystem", 2 if file::exists($upper) && !$is_unix;

        # On case-sensitive systems, these are different files
        if (!file::exists($upper)) {
            file::spew($upper, "UPPERCASE");
            ok(file::exists($lower), 'lowercase file exists');
            ok(file::exists($upper), 'uppercase file exists');
            isnt(file::slurp($lower), file::slurp($upper), 'different content');
        }
    }

    pass('case sensitivity test completed');
};

# ============================================
# Line ending handling
# ============================================

subtest 'line endings' => sub {
    my $unix_file = "$tmpdir/unix_lines.txt";
    my $win_file = "$tmpdir/win_lines.txt";
    my $mac_file = "$tmpdir/mac_lines.txt";

    # Unix: \n
    file::spew($unix_file, "line1\nline2\nline3");
    my $unix_lines = file::lines($unix_file);
    is(scalar(@$unix_lines), 3, 'unix line count');
    is($unix_lines->[0], 'line1', 'unix line 1');

    # Windows: \r\n (note: file module splits on \n only)
    file::spew($win_file, "line1\r\nline2\r\nline3");
    my $win_lines = file::lines($win_file);
    is(scalar(@$win_lines), 3, 'windows line count');
    # Lines will have \r at end
    like($win_lines->[0], qr/^line1/, 'windows line 1 starts correctly');

    # Old Mac: \r only
    file::spew($mac_file, "line1\rline2\rline3");
    my $mac_lines = file::lines($mac_file);
    is(scalar(@$mac_lines), 1, 'mac lines (no \\n) is one line');
};

# ============================================
# Binary mode consistency
# ============================================



( run in 1.319 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )