File: has_same_bytes.t

package info (click to toggle)
libpath-tiny-perl 0.148-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: perl: 1,300; makefile: 2; sh: 1
file content (72 lines) | stat: -rw-r--r-- 2,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use 5.008001;
use strict;
use warnings;
use Test::More 0.96;

use lib 't/lib';
use TestUtils qw/exception has_symlinks/;

use Path::Tiny;

my $dir = Path::Tiny->tempdir;

# identical contents in two files
my $file1a = $dir->child("file1b.txt");
my $file1b = $dir->child("file1a.txt");
for my $f ( $file1a, $file1b ) {
    $f->spew("hello world");
}

# different contents
my $file2 = $dir->child("file2.txt");
$file2->spew("goodbye world");

# a directory, instead of a file
my $subdir = $dir->child("subdir");
$subdir->mkdir;

subtest "only files" => sub {
    ok( $file1a->has_same_bytes($file1a), "same file" );
    ok( $file1a->has_same_bytes($file1b), "different files, same contents" );
    ok( !$file1a->has_same_bytes($file2), "different files, different contents" );
};

subtest "symlinks" => sub {
    plan skip_all => "No symlink support"
      unless has_symlinks();

    my $file1c = $dir->child("file1c.txt");
    symlink "$file1a" => "$file1c";

    ok( $file1a->has_same_bytes($file1c), "file compared to self symlink" );
    ok( $file1c->has_same_bytes($file1a), "self symlink compared to file" );
};

subtest "exception" => sub {
    my $doesnt_exist = $dir->child("doesnt_exist.txt");

    # Different OSes return different errors, so we just check for any error.
    ok( exception { $file1a->has_same_bytes($doesnt_exist) },
        "file->has_same_bytes(doesnt_exist)" );
    ok( exception { $doesnt_exist->has_same_bytes($file1a) },
        "doesnt_exist->has_same_bytes(file)" );
    ok( exception { $file1a->has_same_bytes($subdir) },
        "file->has_same_bytes(dir)" );
    ok( exception { $subdir->has_same_bytes($file1a) },
        "dir->has_same_bytes(file)" );
    ok( exception { $subdir->has_same_bytes($subdir) },
        "dir->has_same_bytes(dir)" );
    ok( exception { $subdir->has_same_bytes($dir) },
        "dir->has_same_bytes(different_dir)" );
};

done_testing;
#
# This file is part of Path-Tiny
#
# This software is Copyright (c) 2014 by David Golden.
#
# This is free software, licensed under:
#
#   The Apache License, Version 2.0, January 2004
#