| 1 | |||
| 2 | package UR::Namespace::Command::Test::Use; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | use Cwd; | ||
| 9 | use YAML; | ||
| 10 | |||
| 11 | class UR::Namespace::Command::Test::Use { | ||
| 12 | is => "UR::Namespace::Command::RunsOnModulesInTree", | ||
| 13 | has_optional => [ | ||
| 14 | verbose => { is => 'Boolean', doc => 'List each explicitly.' }, | ||
| 15 | summarize_externals => { is => 'Boolean', doc => 'List all modules used which are outside the namespace.' }, | ||
| 16 | exec => { is => 'Text', doc => 'Execute the specified Perl _after_ using all of the modules.' }, | ||
| 17 | ] | ||
| 18 | }; | ||
| 19 | |||
| 20 | sub help_brief { | ||
| 21 | "Tests each module for compile errors by 'use'-ing it. Also reports on any libs added to \@INC by any modules (bad!)." | ||
| 22 | } | ||
| 23 | |||
| 24 | sub help_synopsis { | ||
| 25 | return <<EOS | ||
| 26 | ur test use | ||
| 27 | |||
| 28 | ur test use Some::Module Some::Other::Module | ||
| 29 | |||
| 30 | ur test use ./Module.pm Other/Module.pm | ||
| 31 | EOS | ||
| 32 | } | ||
| 33 | |||
| 34 | sub help_detail { | ||
| 35 | my $self = shift; | ||
| 36 | my $text = <<EOS; | ||
| 37 | |||
| 38 | Tests each module by "use"-ing it. Failures are reported individually. | ||
| 39 | |||
| 40 | Successes are only reported individualy if the --verbose option is specified. | ||
| 41 | |||
| 42 | A count of total successes/failures is returned as a summary in all cases. | ||
| 43 | |||
| 44 | EOS | ||
| 45 | $text .= $self->_help_detail_footer; | ||
| 46 | return $text; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub before { | ||
| 50 | my $self = shift; | ||
| 51 | $self->{success} = 0; | ||
| 52 | $self->{failure} = 0; | ||
| 53 | $self->{used_libs} = {}; | ||
| 54 | $self->{used_mods} = {}; | ||
| 55 | $self->{failed_libs} = []; | ||
| 56 | $self->{default_print_fh} = fileno(select); | ||
| 57 | $self->SUPER::before(@_); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub for_each_module_file { | ||
| 61 | my $self = shift; | ||
| 62 | my $module_file = shift; | ||
| 63 | my $namespace_name = $self->namespace_name; | ||
| 64 | my %libs_before = map { $_ => 1 } @INC; | ||
| 65 | my %mods_before = %INC if $self->summarize_externals; | ||
| 66 | |||
| 67 | local $SIG{__DIE__}; | ||
| 68 | local $ENV{UR_DBI_MONITOR_SQL} = 1; | ||
| 69 | local $ENV{APP_DBI_MONITOR_SQL} = 1; | ||
| 70 | local *CORE::GLOBAL::exit = sub {}; | ||
| 71 | |||
| 72 | $self->debug_message("require $module_file"); | ||
| 73 | eval "require '$module_file'"; | ||
| 74 | |||
| 75 | my %new_libs = map { $_ => 1 } grep { not $libs_before{$_} } @INC; | ||
| 76 | my %new_mods = | ||
| 77 | map { $_ => $module_file } | ||
| 78 | grep { not $_ =~ /^$namespace_name\// } | ||
| 79 | grep { not $mods_before{$_} } | ||
| 80 | keys %INC; | ||
| 81 | if (%new_libs) { | ||
| 82 | $self->{used_libs}{$module_file} = \%new_libs; | ||
| 83 | } | ||
| 84 | if (%new_mods) { | ||
| 85 | for my $mod (keys %new_mods) { | ||
| 86 | $self->{used_mods}{$mod} = $module_file; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | if ($@) { | ||
| 90 | print "$module_file FAILED:\n$@\n"; | ||
| 91 | $self->{failure}++; | ||
| 92 | push @{$self->{failed_libs}}, $module_file; | ||
| 93 | } elsif (fileno(select) != $self->{default_print_fh}) { | ||
| 94 | # un-steal the default file handle back | ||
| 95 | select(STDOUT); | ||
| 96 | print "$module_file FAILED DUE TO IMPROPER FILEHANDLE USE\n"; | ||
| 97 | $self->{failure}++; | ||
| 98 | push @{$self->{failed_libs}}, $module_file; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | print "$module_file OK\n" if $self->verbose; | ||
| 102 | $self->{success}++; | ||
| 103 | } | ||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub after { | ||
| 108 | my $self = shift; | ||
| 109 | $self->status_message("SUCCESS: $self->{success}"); | ||
| 110 | $self->status_message("FAILURE: $self->{failure}"); | ||
| 111 | |||
| 112 | if ($self->{failure} > 0) { | ||
| 113 | $self->status_message("FAILED LIBS: " . YAML::Dump($self->{failed_libs})); | ||
| 114 | } | ||
| 115 | |||
| 116 | if (%{ $self->{used_libs} }) { | ||
| 117 | $self->status_message( | ||
| 118 | "ROGUE LIBS: " | ||
| 119 | . YAML::Dump($self->{used_libs}) | ||
| 120 | ) | ||
| 121 | } | ||
| 122 | if ($self->summarize_externals) { | ||
| 123 | $self->status_message( | ||
| 124 | "MODULES USED: " | ||
| 125 | . YAML::Dump($self->{used_mods}) | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | if (my $src = $self->exec) { | ||
| 129 | eval $src; | ||
| 130 | $self->error_message($@) if $@; | ||
| 131 | } | ||
| 132 | return if $self->{failure}; | ||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 |