Skip to content

Commit

Permalink
Item13077: improvement of Save unit tests. Hopefully this will be eno…
Browse files Browse the repository at this point in the history
…ugh to provide a framework for testing in the event of new problems
  • Loading branch information
Comment committed Dec 4, 2014
1 parent 29c436b commit 2eab50e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 38 deletions.
Expand Up @@ -96,4 +96,10 @@ $Foswiki::cfg{UnitTestContrib}{Configure}{DEP_PERL} = {
'hash' => { 'hash' => 'xxx$Foswiki::cfg{UnitTestContrib}{Configure}{H}xxx' },
'array' => [ '$Foswiki::cfg{UnitTestContrib}{Configure}{H}' ]
};
# **PERL**
# Default: { a => 1, b => 2 }
$Foswiki::cfg{UnitTestContrib}{Configure}{PERL_HASH} = { a => 1, b => 2 };
# **PERL**
# Default: [ 1, 2 ]
$Foswiki::cfg{UnitTestContrib}{Configure}{PERL_ARRAY} = [ 1, 2 ];
1;
74 changes: 61 additions & 13 deletions UnitTestContrib/test/unit/ConfigureSaveTests.pm
Expand Up @@ -49,7 +49,8 @@ sub tear_down {
$Foswiki::cfg{WorkingDir} = $this->{test_work_dir};

$this->SUPER::tear_down();
print STDERR "Tearing down $this->{lscpath}\n";

#print STDERR "Tearing down $this->{lscpath}\n";
if ( $this->{safe_lsc} ) {
open( F, '>', $this->{lscpath} );
print F $this->{safe_lsc};
Expand All @@ -60,6 +61,7 @@ sub tear_down {
}
}

# TODO: this needs to test that backups are correctly made
sub test_changecfg {
my $this = shift;
my $params = {
Expand All @@ -71,11 +73,14 @@ sub test_changecfg {
'{"Test-Key"}' => 'newtestkey',

# Specced items
'{Sessions}{ExpireAfter}' => '99',
'{UnitTestContrib}{Configure}{NUMBER}' => '99',

# Some PERL items, array and hash
'{AccessibleCFG}' => '[]',
'{Log}{Action}' => '{ pootle=>1 }',
'{UnitTestContrib}{Configure}{PERL_ARRAY}' => '[ 3, 4 ]',
'{UnitTestContrib}{Configure}{PERL_HASH}' => '{ pootle=>1 }',

# REGEX item
'{UnitTestContrib}{Configure}{REGEX}' => '(black|white)+',

# Undeffable
'{TempfileDir}' => '',
Expand All @@ -86,14 +91,56 @@ sub test_changecfg {
$wizard->save($reporter);

# Check report
my $expected = [
{
text => "| {OS} | undef | \'$Foswiki::cfg{OS}\' |",
level => 'notes'
},
{
level => 'notes',
text => '| {\'Test-Key\'} | undef | \'newtestkey\' |'
},
{
'level' => 'notes',
'text' => '| {TestA} | undef | \'Shingle\' |'
},
{
'level' => 'notes',
'text' => '| {TestB}{Ruin} | undef | \'Ribbed\' |'
},
{
level => 'notes',
text => '| {UnitTestContrib}{Configure}{NUMBER} | (666) | \'99\' |',
},
{
level => 'notes',
text =>
'| {UnitTestContrib}{Configure}{PERL_ARRAY} | ([1,2]) | [3,4] |',
},
{
level => 'notes',
text =>
'| {UnitTestContrib}{Configure}{PERL_HASH} | ({\'a\' => 1,\'b\' => 2}) | {\'pootle\' => 1} |',
},
{
level => 'notes',
text =>
q<| {UnitTestContrib}{Configure}{REGEX} | (^regex$) | qr/(black&#124;white)+/ |>,
}
];
my $ms = $reporter->messages();
$this->assert_matches( qr/^Previous/, $ms->[0]->{text} );
$this->assert_matches( qr/^New/, $ms->[1]->{text} );
$this->assert_matches(
qr/AccessibleCFG.*\[.+ \[\]/,
$ms->[3]->{text},
Data::Dumper->Dump( [$ms] )
);
my $r = shift(@$ms);
$this->assert_matches( qr/^Previous/, $r->{text} );
$this->assert_str_equals( 'notes', $r->{level} );
$r = shift(@$ms);
$this->assert_matches( qr/^New/, $r->{text} );
$this->assert_str_equals( 'notes', $r->{level} );
$r = shift(@$ms);
$this->assert_matches( qr/^\| \*Key/, $r->{text} );
$this->assert_str_equals( 'notes', $r->{level} );

#print STDERR Data::Dumper->Dump([$ms]);
$this->assert_deep_equals( $ms, $expected );

# Check it was written correctly
open( F, '<',
Expand All @@ -110,11 +157,12 @@ sub test_changecfg {
eval $c;
%Foswiki::cfg = (); #{ConfigurationFinished} = 0;
Foswiki::Configure::Load::readConfig( 1, 1 );

#print STDERR Data::Dumper->Dump([\%Foswiki::cfg]);
delete $Foswiki::cfg{ConfigurationFinished};

$this->assert_num_equals( 0, scalar @{ $blah{AccessibleCFG} } );
$this->assert_null( $blah{TempfileDir} );
$this->assert_num_equals( 99, $blah{Sessions}{ExpireAfter} );
$this->assert_num_equals( 99, $blah{UnitTestContrib}{Configure}{NUMBER} );
$this->assert_str_equals( 'newtestkey', $blah{'Test-Key'} );
$this->assert_str_equals( 'Shingle', $blah{'TestA'} );
$this->assert_str_equals( 'Ribbed', $blah{'TestB'}{'Ruin'} );
Expand Down
63 changes: 38 additions & 25 deletions core/lib/Foswiki/Configure/Wizards/Save.pm
Expand Up @@ -280,25 +280,38 @@ sub _compareConfigs {

my $session = $Foswiki::Plugins::SESSION;

return 1 unless ( defined $o || defined $n );

my $old = Foswiki::Configure::Reporter::uneval($o);
my $new = Foswiki::Configure::Reporter::uneval($n);

# Intermediates on the road to a value will return undef here.
my $vs = $spec->getValueObject($keypath);
if ( $vs && $vs->{typename} eq 'PASSWORD' ) {
$old = '_[redacted]_';
$new = '_[redacted]_';

if ($vs) {

#print STDERR "REPORT ON $vs->{keys} $old $new\n";
if ( $vs->{typename} eq 'PASSWORD' ) {
$old = '_[redacted]_';
$new = '_[redacted]_';
}
if ( $old ne $new ) {
$old = "($vs->{default})" if $old eq 'undef' && $vs->{default};
_logAndReport( $reporter, $session, $keypath, $old, $new );
return 0;
}
return 1;
}

if ( ref($o) ne ref($n) ) {
#print STDERR "$keypath is not in spec\n";
if ( $o && $n && ref($o) ne ref($n) ) {

# Both set, but different types. Stop the recursion here.
_logAndReport( $reporter, $session, $keypath, $old, $new );
return 0;
}

# We know they are the same type
if ( ref($o) eq 'HASH' ) {
# We know they are the same type (or one is undef)
if ( ref($o) eq 'HASH' || ref($n) eq 'HASH' ) {
$o = {} unless defined $o;
$n = {} unless defined $n;
my %keys = map { $_ => 1 } ( keys %$o, keys %$n );
my $ok = 1;
foreach my $k ( sort keys %keys ) {
Expand All @@ -319,27 +332,28 @@ sub _compareConfigs {
return $ok;
}

if ( ref($o) eq 'ARRAY' ) {
if ( ref($o) eq 'ARRAY' || ref($n) eq 'ARRAY' ) {
$o = [] unless defined $o;
$n = [] unless defined $n;
if ( scalar(@$o) != scalar(@$n) ) {
_logAndReport( $reporter, $session, $keypath, $old, $new );
return 0;
}
else {
for ( my $i = 0 ; $i < scalar(@$o) ; $i++ ) {
unless (
_compareConfigs(
$spec, $o->[$i], $n->[$i],
$reporter, "$keypath\[$i\]"
)
)
{
_logAndReport( $reporter, $session, $keypath, $old, $new );
return 0;
}
for ( my $i = 0 ; $i < scalar(@$o) ; $i++ ) {
unless (
_compareConfigs(
$spec, $o->[$i], $n->[$i], $reporter, "$keypath\[$i\]"
)
)
{
_logAndReport( $reporter, $session, $keypath, $old, $new );
return 0;
}
}
return 1;
}
elsif (( !defined $o && defined $n )

if ( ( !defined $o && defined $n )
|| ( defined $o && !defined $n )
|| $o ne $n )
{
Expand Down Expand Up @@ -384,8 +398,7 @@ sub _generateLSC {

my $vs = $spec->getValueObject($keys);
if ($vs) {
my $d = $vs->encodeValue($datum);
$d = "''" unless defined $d;
my $d = Foswiki::Configure::Reporter::uneval($datum);
push( @dump, "\$Foswiki::cfg$keys = $d;\n" );
}
elsif ( ref($datum) eq 'HASH' ) {
Expand Down

0 comments on commit 2eab50e

Please sign in to comment.