xref: /openbsd-src/gnu/usr.bin/perl/cpan/podlators/t/man/devise-date.t (revision e068048151d29f2562a32185e21a8ba885482260)
1#!/usr/bin/perl
2#
3# In order for MakeMaker to build in the core, nothing can use Fcntl which
4# includes POSIX.  devise_date()'s use of strftime() was replaced.  This tests
5# that it's identical.  It also tests special handling of the POD_MAN_DATE
6# and SOURCE_DATE_EPOCH environment variables.
7#
8# Copyright 2009, 2014-2015, 2018-2019, 2022 Russ Allbery <rra@cpan.org>
9#
10# This program is free software; you may redistribute it and/or modify it
11# under the same terms as Perl itself.
12#
13# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
14
15use 5.008;
16use strict;
17use warnings;
18
19use Pod::Man;
20use POSIX qw(strftime);
21
22use Test::More tests => 6;
23
24# Start with environment variables affecting the date stripped.
25local $ENV{SOURCE_DATE_EPOCH} = undef;
26local $ENV{POD_MAN_DATE} = undef;
27
28# Check that the results of device_date matches strftime.  There is no input
29# file name, so this will use the current time.
30my $parser = Pod::Man->new;
31is(
32    $parser->devise_date,
33    strftime('%Y-%m-%d', gmtime()),
34    'devise_date matches strftime',
35);
36
37# Set the override environment variable and ensure that it's honored.
38local $ENV{POD_MAN_DATE} = '2014-01-01';
39is($parser->devise_date, '2014-01-01', 'devise_date honors POD_MAN_DATE');
40
41# Check that an empty environment variable is honored.
42local $ENV{POD_MAN_DATE} = q{};
43is($parser->devise_date, q{}, 'devise_date honors empty POD_MAN_DATE');
44
45# Set another environment variable and ensure that it's honored.
46local $ENV{POD_MAN_DATE} = undef;
47local $ENV{SOURCE_DATE_EPOCH} = 1439390140;
48is($parser->devise_date, '2015-08-12', 'devise_date honors SOURCE_DATE_EPOCH');
49
50# Check that POD_MAN_DATE overrides SOURCE_DATE_EPOCH
51local $ENV{POD_MAN_DATE} = '2013-01-01';
52local $ENV{SOURCE_DATE_EPOCH} = 1482676620;
53is(
54    $parser->devise_date, '2013-01-01',
55    'devise_date honors POD_MAN_DATE over SOURCE_DATE_EPOCH',
56);
57
58# Check that an invalid SOURCE_DATE_EPOCH is not accepted
59local $ENV{POD_MAN_DATE} = undef;
60local $ENV{SOURCE_DATE_EPOCH} = '1482676620B';
61is(
62    $parser->devise_date,
63    strftime('%Y-%m-%d', gmtime()),
64    'devise_date ignores invalid SOURCE_DATE_EPOCH',
65);
66