summaryrefslogtreecommitdiff
path: root/t/cli/generate.t
blob: de8d2c3394823faa409cc62488156c5f3f45c583 (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
#!/usr/bin/perl
#
# Tests for the App::DocKnot command dispatch for generate.
#
# Copyright 2018-2019 Russ Allbery <rra@cpan.org>
#
# SPDX-License-Identifier: MIT

use 5.024;
use autodie;
use warnings;

use lib 't/lib';

use Cwd qw(getcwd);
use File::Temp;
use File::Spec;
use Perl6::Slurp;
use Test::RRA qw(is_file_contents);

use Test::More tests => 7;

# Load the module.
BEGIN { use_ok('App::DocKnot::Command') }

# Create the command-line parser.
my $docknot = App::DocKnot::Command->new();
isa_ok($docknot, 'App::DocKnot::Command');

# Generate the package README file to a temporary file, read it into memory,
# and compare it to the actual README file.  This duplicates part of the
# generate/self.t test, but via the command-line parser.
#
# Always apply a CRLF conversion layer (which should be harmless on UNIX) to
# ensure correct behavior on Windows, where we should have automatically
# converted newlines to CRLF when writing.
my $tempdir     = File::Temp->newdir();
my $output_path = File::Temp->new(DIR => $tempdir);
$docknot->run('generate', 'readme', $output_path);
my $output = slurp('<:crlf', $output_path);
is_file_contents($output, 'README', 'Generated README from argument list');
unlink($output_path);

# Do the same thing again, but using arguments from @ARGV.  Be sure to
# stringify $output_path, or slurp() will try to read from the file descriptor
# instead of the path and just get end of file.
local @ARGV = ('generate', 'readme-md', $output_path);
$docknot->run();
$output = slurp('<:crlf', "$output_path");
is_file_contents($output, 'README.md', 'Generated README.md from ARGV');

# Save the paths to various files in the source directory.
my $readme_path    = File::Spec->catfile(getcwd(), 'README');
my $readme_md_path = File::Spec->catfile(getcwd(), 'README.md');
my $metadata_path  = File::Spec->catfile(getcwd(), 'docs', 'metadata');

# Generate all of the files using generate-all in a new temporary directory.
my $tmpdir = File::Temp->newdir();
chdir($tmpdir);
$docknot->run('generate-all', '-m', $metadata_path);
$output = slurp('<:crlf', 'README');
is_file_contents($output, $readme_path, 'README from generate_all');
$output = slurp('<:crlf', 'README.md');
is_file_contents($output, $readme_md_path, 'README.md from generate_all');

# Ensure that generate works with a default argument.
$docknot->run('generate', '-m', $metadata_path, 'readme');
$output = slurp('<:crlf', 'README');
is_file_contents($output, $readme_path, 'README from generate default args');

# Allow cleanup to delete our temporary directory.
chdir(File::Spec->rootdir());