1#!/usr/bin/env perl
2# vim:ts=4:sw=4:expandtab
3# © 2012 Michael Stapelberg and contributors
4# Script to create a new testcase from a template.
5#
6#     # Create (and edit) a new test for moving floating windows
7#     ./new-test floating move
8#
9#     # Create (and edit) a multi-monitor test for moving workspaces
10#     ./new-test -m move workspaces
11
12use strict;
13use warnings;
14use File::Basename qw(basename);
15use Getopt::Long;
16use v5.10;
17
18my $usage = <<'EOF';
19Script to create a new testcase from a template.
20
21    # Create (and edit) a new test for moving floating windows
22    ./new-test floating move
23
24    # Create (and edit) a multi-monitor test for moving workspaces
25    ./new-test -m move workspaces
26EOF
27
28my $multi_monitor;
29
30my $result = GetOptions(
31    'multi-monitor|mm' => \$multi_monitor
32);
33
34my $testname = join(' ', @ARGV);
35$testname =~ s/ /-/g;
36
37unless (length $testname) {
38    say $usage;
39    exit(0);
40}
41
42my $header = <<'EOF';
43#!perl
44# vim:ts=4:sw=4:expandtab
45#
46# Please read the following documents before working on tests:
47# • https://build.i3wm.org/docs/testsuite.html
48#   (or docs/testsuite)
49#
50# • https://build.i3wm.org/docs/lib-i3test.html
51#   (alternatively: perldoc ./testcases/lib/i3test.pm)
52#
53# • https://build.i3wm.org/docs/ipc.html
54#   (or docs/ipc)
55#
56# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
57#   (unless you are already familiar with Perl)
58#
59# TODO: Description of this file.
60# Ticket: #999
61# Bug still in: #GITREV#
62EOF
63
64# Figure out the next test filename.
65my @files;
66if ($multi_monitor) {
67    @files = grep { basename($_) =~ /^5/ } <t/*.t>;
68} else {
69    @files = grep { basename($_) !~ /^5/ } <t/*.t>;
70}
71my ($latest) = sort { $b cmp $a } @files;
72my ($num) = (basename($latest) =~ /^([0-9]+)/);
73my $filename = "t/" . ($num+1) . "-" . lc($testname) . ".t";
74
75# Get the current git revision.
76chomp(my $gitrev = qx(git describe --tags));
77$header =~ s/#GITREV#/$gitrev/g;
78
79# Create the file based on our template.
80open(my $fh, '>', $filename);
81print $fh $header;
82if ($multi_monitor) {
83    print $fh <<'EOF';
84use i3test i3_autostart => 0;
85
86my $config = <<EOT;
87# i3 config file (v4)
88font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
89
90fake-outputs 1024x768+0+0,1024x768+1024+0
91EOT
92
93my $pid = launch_with_config($config);
94
95
96exit_gracefully($pid);
97
98done_testing;
99EOF
100} else {
101    print $fh <<'EOF';
102use i3test;
103
104
105done_testing;
106EOF
107}
108close($fh);
109
110my $editor = $ENV{EDITOR};
111$editor ||= 'vi';
112exec $editor, $filename;
113