Explorar o código

Fix STUPID bug in template processor, add basic test

Andy Baugh %!s(int64=5) %!d(string=hai) anos
pai
achega
1db2b6080d
Modificáronse 4 ficheiros con 52 adicións e 5 borrados
  1. 8 5
      Makefile
  2. 4 0
      lib/Troglodyne/CGI.pm
  3. 24 0
      t/Troglodyne-CGI.t
  4. 16 0
      t/lib/Cpanel/Template.pm

+ 8 - 5
Makefile

@@ -6,10 +6,10 @@ vca  = /var/cpanel/apps
 vct  = /var/cpanel/templates
 pwd  = $(shell pwd)
  
-.PHONY: all install register test uninstall rpm
+.PHONY: all install register test uninstall rpm test-depend
 all: install register
 
-install:
+install: test
 	mkdir -p $(DESTDIR)$(ulc)$(tmpl)/ui $(DESTDIR)$(ulc)$(tmpl)/config $(DESTDIR)$(ulc)$(cgi)/js $(DESTDIR)$(ulc)$(cgi)/img $(DESTDIR)$(vcp)/Troglodyne/CGI $(DESTDIR)$(vcp)/Troglodyne/API $(DESTDIR)$(vca) $(DESTDIR)$(vct)/troglodyne/config $(DESTDIR)$(ulc)/whostmgr/docroot/addon_plugins
 	install $(pwd)/templates/ui/pgupgrade.tmpl $(DESTDIR)$(ulc)$(tmpl)
 	install $(pwd)/templates/config/main.default $(DESTDIR)$(vct)/troglodyne/config
@@ -42,9 +42,12 @@ uninstall:
 	rm -rf /usr/local/cpanel/whostmgr/docroot/cgi/troglodyne
 	rm -f /usr/local/cpanel/whostmgr/docroot/addon_plugins/troglophant.png
 
-test:
-	[ ! -x /usr/local/cpanel/3rdparty/bin/prove ] || /usr/local/cpanel/3rdparty/bin/prove t/*.t
-	[ -x /usr/local/cpanel/3rdparty/bin/prove ] || prove t/*.t
+
+test-depend:
+	perl -MTest2::V0 -MTest::MockModule -MFile::Temp -MCapture::Tiny -e 'exit 0' || sudo cpan -i Test2::V0 Test::MockModule File::Temp Capture::Tiny
+
+test: test-depend
+	prove -mv t/*.t
 
 rpm:
 	rm -rf SOURCES/*

+ 4 - 0
lib/Troglodyne/CGI.pm

@@ -46,6 +46,9 @@ sub render_cached_or_process_template {
         $input_hr->{'print'} = 0;
         require Cpanel::Template;
         my ( $success, $output_sr ) = Cpanel::Template::process_template( $service, $input_hr );
+
+        # TODO: Investigate whether I can send to two filehandles simultaneously -- $output_sr and STDOUT (as we are a CGI).
+        # Then we could cache & print without the below step.
         if( $success ) {
             return if render_to_cache_and_print( $cache_dir, $output_sr );
         }
@@ -82,6 +85,7 @@ sub render_from_cache {
     my $worked = eval {
         open( my $fh, '<', "$cache_dir/$CP_SECURITY_TOKEN" ) or die "Couldn't open cache file \"$cache_dir/$CP_SECURITY_TOKEN\" for reading: $!";
         while( <$fh> ) { print $_; }
+        1;
     };
     if(my $err = $@) {
 

+ 24 - 0
t/Troglodyne-CGI.t

@@ -0,0 +1,24 @@
+use Test2::V0;
+use File::Temp;
+use FindBin;
+use Capture::Tiny qw{capture_stdout};
+
+use lib "$FindBin::Bin/lib"; # Test libraries
+use lib "$FindBin::Bin/../lib"; # Code under test
+
+use Troglodyne::CGI  ();
+use Cpanel::Template ();
+
+plan 1;
+
+subtest "render_cached_or_process_template" => sub {
+    my $tmp_obj = File::Temp->newdir();
+    local $Troglodyne::CGI::ULC = $tmp_obj->dirname();
+    my $input_hr = { 'template_file' => 'bogusbogus', 'print' => 1 };
+    my $printed = capture_stdout {
+        Troglodyne::CGI::render_cached_or_process_template( 'whostmongler', $input_hr );
+    };
+    my $test_str = "# [whostmongler] This is a test of Troglodyne::CGI. Please Ignore";
+    is( $printed, $test_str, "Got the expected output when troglodyne_do_static_render invoked" );
+    $input_hr->{'troglodyne_do_static_render'} = 1;
+};

+ 16 - 0
t/lib/Cpanel/Template.pm

@@ -0,0 +1,16 @@
+package Cpanel::Template;
+use strict;
+use warnings;
+
+use FindBin;
+
+sub process_template {
+    my ( $svc, $args_hr ) = @_;
+    my $content = "# [$svc] This is a test of Troglodyne::CGI. Please Ignore";
+    if($args_hr->{'print'}) {
+        print STDOUT $content;
+    }
+    return ( 1, \$content ); 
+};
+
+1;