Devel::Maypole - support utilities for developing the Maypole stack
# ================================================= # In a test script: use Test::More tests => 42; use Devel::Maypole qw/ :test /; my ( $database, $application ); BEGIN { $ENV{MAYPOLE_CONFIG} = 'config/beerdb.simple.yaml'; $ENV{MAYPOLE_TEMPLATES} = 't/templates'; $database = database( ddl => 'sql/ddl/beerdb.simple.sql', data => 'sql/data/beerdb.simple.sql', ); $application = application( plugins => [ qw( Config::YAML AutoUntaint Relationship ) ], ); } use Test::WWW::Mechanize::Maypole $application, $database; # ----- BEGIN TESTING ----- # frontpage { my $mech = Test::WWW::Mechanize::Maypole->new; $mech->get_ok("http://localhost/beerdb/"); $mech->content_contains( 'This is the frontpage' ); is($mech->ct, "text/html"); is($mech->status, 200); } # ================================================= # In an installation script: use Maypole::Devel qw/ :install /; # optionally suppress interactive questions: # $ENV{MAYPOLE_RESOURCES_INSTALL_PREFIX} = '/usr/local/maypole'; install_templates( 'Maypole::Plugin::Foo', 'distribution-templates-dir/set1', 'set1' ); install_ddl( 'Maypole::Plugin::Foo', 'distribution-sql-dir/ddl/basic', 'basic' ); install_ddl( 'Maypole::Plugin::Foo', 'distribution-sql-dir/ddl/advanced', 'advanced' ); # ================================================= # Somewhere else: package My::App; use Devel::Maypole qw/ :find /; my $templates = find_templates( 'Maypole::Plugin::Foo', 'set1' ); my $ddl_sql_dir = find_ddl( 'Maypole::Plugin::Foo', 'advanced' );
Builds a database and a simple application driver, ready to use in test scripts for Maypole plugins and components. Provides functions for installing resources (SQL files, configurations, templates), and for discovering the directories these resources were installed to at a later date.
Nothing is exported by default. You can import individual functions, or groups of functions using these tags:
tag functions ----------------------------------------------------------- :test database application run_standard_tests :install install install_templates install_yaml_config install_ddl install_data :find find find_templates find_yaml_config find_ddl find_data
Builds and populates an SQLite database in a temporary file, and returns a DBI connection string to the database.
Suitable SQL files will be (ASAP) included in the distribution to build a reasonably complex version of the beer database.
Returns a DBI connection string.
Options:
Path to the SQL DDL (schema) file to use. A couple of suitable files are installed by this distribution - beerdb.default.sql and beerdb.simple.sql.
beerdb.default.sql
beerdb.simple.sql
Path to the SQL data file to use. A couple of suitable files are installed by this distribution - beerdb.default.sql and beerdb.simple.sql.
Set false to not unlink the generated database file. Default true (unlink when script exits).
Builds a simple Maypole driver in a temporary file in the current directory.
Returns the package name of the application, which will be MaypoleTestApp_XXXXX where XXXXX are random characters.
MaypoleTestApp_XXXXX
XXXXX
Arrayref of plugin names, just as you would supply to use Maypole::Application.
use Maypole::Application
See Custom driver code below.
Custom driver code
Hashref of Maypole options, or a Maypole::Config object. See Configuration below.
Configuration
Set false to not unlink the generated application file. Default true (unlink when script exits).
A canned set of tests that should be runnable against most plugins.
NOT YET IMPLEMENTED.
You can build up configuration data in your test script, either as a hashref or as a Maypole::Config object, and supply that as the config parameter to application().
config
application()
Alternatively, include Maypole::Plugin::Config::YAML in the list of plugins, and set $ENV{MAYPOLE_CONFIG} to the path to a config file. See the Maypole::Plugin::Config::YAML docs for details.
$ENV{MAYPOLE_CONFIG}
This distribution includes a couple of canned config files, in the config subdirectory.
$ENV{MAYPOLE_CONFIG} = 'path/to/config/beerdb.simple.yaml'; $ENV{MAYPOLE_CONFIG} = 'path/to/config/beerdb.default.yaml';
You can considerably simplify your config by including Maypole::Plugin::AutoUntaint in the list of plugins - the supplied config files assume this.
The supplied configs also assume Maypole::Plugin::Relationship is included in the plugins.
If you need to add custom code to the application, you could put the code in a plugin, and supply the name of the plugin to plugins.
plugins
Alternatively, eval code in your test script, e.g. from 01.simple.t:
eval
01.simple.t
# For testing classmetadata eval <<CODE; sub $application\::Beer::classdata: Exported {}; sub $application\::Beer::list_columns { return qw/score name price style brewery url/}; CODE die $@ if $@;
Import these functions with the :install tag.
:install
These functions are intended to be called from a command line script, preferably a Build.PL script. They will interactively ask the user to confirm install locations, unless $ENV{MAYPOLE_RESOURCES_INSTALL_PREFIX} is defined, in which case that location will be used as the root of the install location.
Build.PL
$ENV{MAYPOLE_RESOURCES_INSTALL_PREFIX}
Returns the path in which the material was installed. You should store this in the config package for the module being installed, so that other packages can find your templates. In Build.PL, you can say
use Devel::Maypole qw/:install/; my $cfg = $builder->config_data( 'templates' ) || {}; $cfg->{set1} = install_templates( 'Maypole::Plugin::Foo', 'dist-templates-dir', 'set1' ); $builder->config_data( templates => $cfg ); # similarly for config files, databases etc.
The call to config_data will cause Module::Build to create and install the config package for you.
config_data
You can retrieve the install location directly from Maypole::Plugin::Foo::ConfigData, or you can use the find_* functions (see below), which will use Maypole::Plugin::Foo::ConfigData under the hood.
Maypole::Plugin::Foo::ConfigData
find_*
On Unix-like systems, the default install root is /usr/local/maypole/$what, where $what identifies the type of material (i.e. templates, yaml_config, ddl or data).
/usr/local/maypole/$what
$what
On Windows, the default is C:/Program Files/Maypole/$what.
C:/Program Files/Maypole/$what
$for should be the name of a package. This will be converted into a subdirectory, e.g. Maypole::Plugin::Foo becomes Maypole/Plugin/Foo.
$for
Maypole::Plugin::Foo
Maypole/Plugin/Foo
$from is the relative path to the resource directory in your distribution, e.g. templates or sql/ddl.
$from
templates
sql/ddl
$set is an optional subdirectory to install to. So if you say
$set
install_templates( 'Maypole::Plugin::Foo', 'templates/set1', 'set1' )
the templates will be installed in /usr/local/maypole/templates/Maypole/Plugin/Foo/set1.
/usr/local/maypole/templates/Maypole/Plugin/Foo/set1
$set defaults to 'default'.
The install_* functions all delegate to install, passing $what as the first parameter. Use this function to install other types of material (e.g. install( 'images', 'Maypole::Plugin::Pretty', 'img/themes/sunny', 'sunny' )), or for cycling through several types.
install_*
install
install( 'images', 'Maypole::Plugin::Pretty', 'img/themes/sunny', 'sunny' )
Import these functions with the :find tag.
:find
Each of these methods loads the appropriate *::ConfigData package for the package $for, and queries it for the location of the $set set of material.
*::ConfigData
use Devel::Maypole qw/ :find /; my $templates_dir = find_templates( $for, $set ); my $ddl_dir = find_ddl( $for, $set );
It is up to you to extract the list of files in the directory.
The find_* functions all delegate to find, passing $what as the first parameter. Use this function to locate other types of material (see install()), or for cycling through several types.
find
install()
These are installed under the resource root selected during installation.
templates/default/*
ddl/default/beerdb.default.sql ddl/default/beerdb.simple.sql
data/default/beerdb.default.sql data/default/beerdb.simple.sql
yaml_config/default/beerdb.simple.yaml
This is how you might start a test script in the distribution of a Maypole add-on.
Include Devel::Maypole in the requirements for your module. During its installation, the Devel::Maypole resource kit(s), as well as persistent config data recording the installation locations of these resources, will be set up.
# in a test script for Maypole::Plugin::Foo use Test::More tests => 42; use Devel::Maypole qw/ :test :find /; my ( $application, $database ); BEGIN { my $mp_cfg = find_yaml_config( 'Devel::Maypole', 'default' ); my $templates = find_templates ( 'Devel::Maypole', 'default' ); my $ddl = find_ddl ( 'Devel::Maypole', 'default' ); my $data = find_data ( 'Devel::Maypole', 'default' ); $ENV{MAYPOLE_CONFIG} = "$mp_cfg/beerdb.simple.yaml"; $ENV{MAYPOLE_TEMPLATES} = $templates; $database = database( ddl => "$ddl/beerdb.simple.sql", data => "$data/beerdb.simple.sql", ); $application = application( plugins => [ qw( Foo Config::YAML AutoUntaint Relationship ) ], ); } use Test::WWW::Mechanize::Maypole $application, $database; # run some canned tests from Devel::Maypole - not implemented yet... run_standard_tests( $application ); # now run some custom tests!
In time, more resource kits will be added (in addition to 'default' ), and/or more files will be added to the default resource kit, and canned sets of tests will be added.
Canned tests
Complex schema, with sufficient data for paging.
More template sets.
Support for other RDBMS's (easy enough to implement, patches welcome).
David Baird, <cpan@riverside-cms.co.uk>
<cpan@riverside-cms.co.uk>
Please report any bugs or feature requests to bug-devel-maypole@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-Maypole. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-devel-maypole@rt.cpan.org
Copyright 2005 David Baird, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Devel::Maypole, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Devel::Maypole
CPAN shell
perl -MCPAN -e shell install Devel::Maypole
For more information on module installation, please visit the detailed CPAN module installation guide.