aboutsummaryrefslogtreecommitdiffstats
path: root/t/fix_eol.t
blob: 9363769ecf13c1db7254fe4f9b6a97b67519b3f8 (plain)
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
73
74
75
76
77
#!/usr/bin/perl
# $Id: gprintify 257533 2009-05-23 12:45:15Z guillomovitch $

use strict;
use warnings;
use Test::More;
use File::Temp qw/tempdir/;
use File::Path qw/make_path/;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";
use Digest::MD5;

plan tests => 2;

# test the script itself
my ($buildroot, $test, $before, $after);

($buildroot, $test) = setup(<<EOF);
foo\r
EOF
$before = get_md5($test);
run($buildroot);
$after = get_md5($test);

isnt(
    $before,
    $after,
    'script should be modified'
);

($buildroot, $test) = setup(<<EOF);
foo\r
EOF
$before = get_md5($test);
$ENV{EXCLUDE_FROM_EOL_CONVERSION} = 'test';
run($buildroot);
$after = get_md5($test);

is(
    $before,
    $after,
    'EXCLUDE_FROM_EOL_CONVERSION should prevent end-of-line conversion'
);

sub setup {
    my ($content) = @_;

    my $buildroot = tempdir(CLEANUP => ($ENV{TEST_DEBUG} ? 0 : 1));

    my $initrddir = $buildroot . '/etc/rc.d/init.d';
    my $datadir   = $buildroot . '/usr/share';
    my $test = $datadir . '/test';

    make_path($datadir);
    open(my $out, '>', $test) or die "can't write to $test: $!";
    print $out $content;
    close($out);

    return ($buildroot, $test);
}

sub run {
    my ($buildroot) = @_;

    $ENV{RPM_BUILD_ROOT} = $buildroot;
    system("$Bin/../fix_eol");
}

sub get_md5 {
    my ($file) = @_;
    open(my $in, '<', $file) or die "can't read $file: $!";
    binmode($in);
    my $md5 = Digest::MD5->new();
    $md5->addfile($in);
    close($in);
    return $md5->hexdigest();
}