lib/App/Yath/Command/ui.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 159 CC 6 SCAR 23.9
CC
6
Cov
0%
run
42.0
summary
2.0
group
2.0
CRAP 10.9
1 package App::Yath::Command::ui;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.000150';
6  
7 use Test2::Util qw/pkg_to_file/;
8  
9 use Test2::Harness::UI::Util qw/share_dir share_file dbd_driver qdb_driver/;
10  
11 use Test2::Harness::UI::Config;
12 use Test2::Harness::UI::Importer;
13 use Test2::Harness::UI;
14  
15 use Test2::Harness::UI::UUID qw/gen_uuid/;
16  
17 use DBIx::QuickDB;
18 use Plack::Builder;
19 use Plack::App::Directory;
20 use Plack::App::File;
21 use Plack::Runner;
22  
23 use parent 'App::Yath::Command';
24 use Test2::Harness::Util::HashBase qw/<log_file/;
25  
26 use App::Yath::Options;
27  
28 include_options(
29 'App::Yath::Options::PreCommand',
30 );
31  
32 option_group {prefix => 'ui', category => "UI Options"} => sub {
33 option schema => (
34 type => 's',
35 default => 'PostgreSQL',
36 long_examples => [' PostgreSQL', ' MySQL'],
37 description => "What type of DB/schema to use",
38 );
39  
40 option port => (
41 type => 's',
42 long_examples => [' 8080'],
43 description => 'Port to use',
44 );
45  
46 option port_command => (
47 type => 's',
48 long_examples => [' get_port.sh', ' get_port.sh --pid $$'],
49 description => 'Use a command to get a port number. "$$" will be replaced with the PID of the yath process',
50 );
51 };
52  
53 sub summary { "Launch a standalone Test2-Harness-UI server for a log file" }
54  
55 sub group { 'log' }
56  
57 sub cli_args { "[--] event_log.jsonl[.gz|.bz2]" }
58  
59 sub description {
60 return <<" EOT";
61 EOT
62 }
63  
64 sub run {
65 my $self = shift;
66  
67 my $args = $self->args;
68 my $settings = $self->settings;
69  
70 my $schema = $settings->ui->schema;
71 require(pkg_to_file("Test2::Harness::UI::Schema::$schema"));
72  
73 shift @$args if @$args && $args->[0] eq '--';
74  
75 $self->{+LOG_FILE} = shift @$args or die "You must specify a log file";
76 die "'$self->{+LOG_FILE}' is not a valid log file" unless -f $self->{+LOG_FILE};
77 die "'$self->{+LOG_FILE}' does not look like a log file" unless $self->{+LOG_FILE} =~ m/\.jsonl(\.(gz|bz2))?$/;
78  
79 my $db = DBIx::QuickDB->build_db(harness_ui => {driver => qdb_driver($schema), dbd_driver => dbd_driver($schema)});
80  
81 my $dbh = $db->connect('quickdb', AutoCommit => 1, RaiseError => 1);
82 $dbh->do('CREATE DATABASE harness_ui') or die "Could not create db " . $dbh->errstr;
83 $db->load_sql(harness_ui => share_file("schema/$schema.sql"));
84 my $dsn = $db->connect_string('harness_ui');
85 $dbh = undef;
86  
87 $ENV{HARNESS_UI_DSN} = $dsn;
88  
89 print "DSN: $dsn\n";
90 my $config = Test2::Harness::UI::Config->new(
91 dbi_dsn => $dsn,
92 dbi_user => '',
93 dbi_pass => '',
94 single_user => 1,
95 single_run => 1,
96 );
97  
98 my $user = $config->schema->resultset('User')->create({username => 'root', password => 'root', realname => 'root', user_id => gen_uuid()});
99 my $proj = $config->schema->resultset('Project')->create({name => 'default', project_id => gen_uuid()});
100  
101 $config->schema->resultset('Run')->create({
102 run_id => gen_uuid(),
103 user_id => $user->user_id,
104 mode => 'complete',
105 status => 'pending',
106 project_id => $proj->project_id,
107  
108 log_file => {
109 log_file_id => gen_uuid(),
110 name => $self->{+LOG_FILE},
111 local_file => $self->{+LOG_FILE},
112 },
113 });
114  
115 Test2::Harness::UI::Importer->new(config => $config)->run(1);
116  
117 my $app = builder {
118 mount '/js' => Plack::App::Directory->new({root => share_dir('js')})->to_app;
119 mount '/css' => Plack::App::Directory->new({root => share_dir('css')})->to_app;
120 mount '/favicon.ico' => Plack::App::File->new({file => share_dir('img') . '/favicon.ico'})->to_app;
121 mount '/img' => Plack::App::Directory->new({root => share_dir('img')})->to_app;
122  
123 mount '/' => sub {
124 Test2::Harness::UI->new(config => $config)->to_app->(@_);
125 };
126 };
127  
128 my $port = $settings->ui->port;
129 if (my $cmd = $settings->ui->port_command) {
130 $cmd =~ s/\$\$/$$/;
131 chomp($port = `$cmd`);
132 }
133  
134 my $r = Plack::Runner->new;
135 my @options = ("--server", "Starman");
136  
137 push @options => ('--listen' => ":$port") if $port;
138  
139 $r->parse_options(@options);
140 $r->run($app);
141  
142 return 0;
143 }
144  
145 1;