File: TestConfig.pm

package info (click to toggle)
libwww-myspace-perl 0.82-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 728 kB
  • ctags: 170
  • sloc: perl: 4,646; makefile: 10
file content (121 lines) | stat: -rw-r--r-- 3,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Handle things that each test script will have to do.

=head1 NAME

TestConfig - Set up for WWW::Myspace dist tests

=head1 SYNOPSIS

 use lib 't';
 use TestConfig;

 $CONFIG->{'acct1'}->{'myspace'}->method;

TestConfig exports a single variable (a hashref), "$CONFIG".
$CONFIG is loaded from t/config.yaml, then for "acct1" and
"acct2", a "myspace" item is added by doing this:
 $CONFIG->{'acct1'}->{'myspace'} = new WWW::Myspace(
    $CONFIG->{'acct1'}->{'username'},
    $CONFIG->{'acct1'}->{'password'} );
 $CONFIG->{'acct2'}->{'myspace'} = new WWW::Myspace(
    $CONFIG->{'acct2'}->{'username'},
    $CONFIG->{'acct2'}->{'password'} );

See config.yaml for layout and all values of $CONFIG.

=head1 AUTHOR

Grant Grueninger, grantg <at> cpan.org

=cut

package TestConfig;

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw( $CONFIG login_myspace );

#warn "Loading WWW::Myspace\n";
use WWW::Myspace;
use YAML qw'LoadFile Dump';
use File::Spec::Functions;

# See if there's a config file so we can test login-specific features
our $CONFIG = _read_config();

# This is our exported login routine.
sub login_myspace {

	# If we're not supposed to log in, setup new objects and return true.
	if ( ! $CONFIG->{login} ) {
		$CONFIG->{acct1}->{myspace} = new WWW::Myspace( auto_login=>0 );
		$CONFIG->{acct2}->{myspace} = new WWW::Myspace( auto_login=>0 );
		return 1;
	}

#	warn "Logging into " . $CONFIG->{'acct1'}->{'username'} . "\n";

	_login( $CONFIG->{acct1} );
	_login( $CONFIG->{acct2} );
	
#	$CONFIG->{'acct1'}->{'myspace'} = new WWW::Myspace( $CONFIG->{'acct1'}->{'username'},
#	$CONFIG->{'acct1'}->{'password'} );
#	if ( $CONFIG->{'acct1'}->{'myspace'}->error ) {
#		warn $CONFIG->{'acct1'}->{'myspace'}->error
#	}
	
#	warn "Logging into " . $CONFIG->{'acct2'}->{'username'} . "\n";
#	$CONFIG->{'acct2'}->{'myspace'} = new
#		WWW::Myspace( $CONFIG->{'acct2'}->{'username'},
#					  $CONFIG->{'acct2'}->{'password'}
#					);
#	if ( $CONFIG->{'acct2'}->{'myspace'}->error ) {
#		warn $CONFIG->{'acct2'}->{'myspace'}->error
#	}

	if ( $CONFIG->{'acct1'}->{'myspace'}->{'logged_in'} &&
		 $CONFIG->{'acct2'}->{'myspace'}->{'logged_in'} ) {
		return 1;
	} else {
		return 0;
	}
}

# Set up the configuration.
# If there's a test_config file in the user's myspace cache dir, use
# it to run full tests.  Otherwise run the basic tests using the
# basic config included with the distribution.
sub _read_config {
	my $myspace = new WWW::Myspace( auto_login => 0, human => 0 );
	my $login = 1;
	my $config = "";
	my $configfile = catfile( $myspace->cache_dir, 'test_config.yaml' );

	# If there's a local config file, and we're not supposed to
	# ignore it (presence of "eu" file means run as an end-user),
	# read the local config file. Otherwise, read the generic one
	# in the distribution.
	if ( ( -f $configfile ) && ( ! -f "eu" ) ) {
		$config = LoadFile( $configfile );
		$config->{login} = 1;
	} else {
		$configfile = catfile( 't', 'config.yaml' );
		$config = LoadFile( $configfile );
		$config->{login} = 0 ;
	}

	return $config
}

# Log into the passed account
sub _login {
	my ( $acct ) = @_;

	# Log in and set the myspace object.
	$acct->{myspace} = new WWW::Myspace( $acct->{username}, $acct->{password} );

	# Spout a warning if there was a problem
	if ( $acct->{'myspace'}->error ) { warn $acct->{'myspace'}->error }

}
1;