shutter / t /Shutter /App /13_plugins.t
runitx's picture
Claude Opus 5.5
Extract plugin discovery into Shutter::App::PluginRegistry
bc44c86 unverified
Raw History Blame Contribute Delete
6.54 kB
#!/usr/bin/perl
use 5.010;
use utf8;
use strict;
use warnings;
use Test::More;
use Glib qw/ TRUE FALSE /;
use File::Temp qw/ tempdir /;
use File::Path qw/ make_path /;
require_ok("Shutter::App::PluginRegistry");
can_ok("Shutter::App::PluginRegistry", qw(check_installed_plugins plugin_get_info check_installed_upload_plugins upload_plugin_get_info));
# Write an executable shell script that answers "<script> <key>" with the
# value from %answers (empty for unknown keys).
sub write_plugin {
my ($path, %answers) = @_;
open my $fh, '>:encoding(UTF-8)', $path or die "cannot write $path: $!";
print {$fh} "#!/bin/sh\ncase \"\$1\" in\n";
for my $key (sort keys %answers) {
print {$fh} " $key) printf '%s\\n' '$answers{$key}' ;;\n";
}
print {$fh} "esac\n";
close $fh;
chmod 0755, $path or die "chmod $path: $!";
return $path;
}
# Minimal stand-in for the Shutter objects the registry needs ($sc, $d,
# $shf, $lp): a single object blessed into main implements all of them.
sub new_stub { return bless {}, __PACKAGE__ }
sub get_debug { return 0 }
sub get { return $_[1] }
sub file_exists { my ($self, $f) = @_; return defined $f && -f $f }
sub file_executable { my ($self, $f) = @_; return -x $f }
sub icon_size { return (16, 16) }
sub load { my ($self, $f) = @_; return "pixbuf:$f" }
my $root = tempdir(CLEANUP => 1);
local $ENV{HOME} = tempdir(CLEANUP => 1);
subtest "plugin_get_info / upload_plugin_get_info" => sub {
my $bin = write_plugin("$root/info", name => 'Bläh', sort => 'effect');
my $name = Shutter::App::PluginRegistry::plugin_get_info($bin, 'name');
is($name, "Bläh\n", "output is returned verbatim (incl. newline)");
ok(utf8::is_utf8($name), "output is decoded as UTF-8");
is(Shutter::App::PluginRegistry::plugin_get_info($bin, 'sort'), "effect\n", "other key");
is(Shutter::App::PluginRegistry::plugin_get_info($bin, 'unknown'), "", "unknown key gives empty string");
is(Shutter::App::PluginRegistry::upload_plugin_get_info($bin, 'name'), "Bläh\n", "upload variant behaves the same");
};
SKIP: {
skip "no display available", 2 unless Gtk3::init_check();
subtest "check_installed_plugins" => sub {
my $dir = "$root/share/shutter/resources/system/plugins/shell/myplug";
make_path($dir);
write_plugin("$dir/myplug", name => 'My Plugin', sort => 'effect', tip => 'Does things');
my $user_dir = "$ENV{HOME}/.shutter/plugins/perl/userplug";
make_path($user_dir);
write_plugin("$user_dir/userplug", name => 'User Plugin', sort => 'misc', tip => 'Tip');
open my $fh, '>', "$user_dir/userplug.png" or die;
close $fh;
# directory without the plugin binary: stale entries are removed
my $broken = "$root/share/shutter/resources/system/plugins/shell/broken";
make_path($broken);
my %plugins = ($broken => {binary => "$broken/broken"});
my $lp = new_stub();
my $gui = 0;
my $ret = Shutter::App::PluginRegistry::check_installed_plugins(new_stub(), new_stub(), $lp, new_stub(), undef, $root, \%plugins, sub { $gui++ });
is($ret, TRUE, "returns TRUE");
ok(!exists $plugins{$broken}, "plugin without binary removed");
is_deeply(
$plugins{$dir},
{
binary => "$dir/myplug",
name => 'My Plugin',
category => 'effect',
tooltip => 'Does things',
lang => 'shell',
pixbuf => "$root/share/shutter/resources/icons/executable.svg",
pixbuf_object => "pixbuf:$root/share/shutter/resources/icons/executable.svg",
},
"system plugin registered with fallback icon"
);
is($plugins{$user_dir}{lang}, 'perl', "language taken from parent dir");
is($plugins{$user_dir}{pixbuf}, "$user_dir/userplug.png", "custom icon used");
is($plugins{$user_dir}{pixbuf_object}, "pixbuf:$user_dir/userplug.png", "custom icon loaded");
is($gui, 2, "gui refreshed once per new plugin");
# already known plugins are not queried again
$plugins{$dir}{name} = "Cached\n";
Shutter::App::PluginRegistry::check_installed_plugins(new_stub(), new_stub(), $lp, new_stub(), undef, $root, \%plugins, sub { $gui++ });
is($plugins{$dir}{name}, 'Cached', "cached info kept (and chomped)");
is($gui, 2, "no gui refresh without new plugins");
};
subtest "check_installed_upload_plugins" => sub {
my $dir = "$root/share/shutter/resources/system/upload_plugins/upload";
make_path($dir);
my %features = (
supports_anonymous_upload => 1,
supports_authorized_upload => 0,
supports_oauth_upload => 0
);
write_plugin("$dir/Foo.pm", module => 'Foo', url => 'https://foo.example', description => 'Foo host', registration => 'Sign up', %features);
write_plugin("$dir/Imgur.pm", module => 'Imgur', %features);
write_plugin("$dir/Bad.pm", %features);
my %accounts = (Foo => {username => 'me'});
my $gui = 0;
my $ret = Shutter::App::PluginRegistry::check_installed_upload_plugins(new_stub(), new_stub(), undef, $root, FALSE, \%accounts, sub { $gui++ });
is($ret, TRUE, "returns TRUE");
ok(!exists $accounts{Imgur}, "Imgur skipped without Path::Class");
ok(!exists $accounts{Bad}, "plugin without module removed");
is($accounts{Foo}{module}, "Foo\n", "module");
is($accounts{Foo}{host}, "https://foo.example\n", "host");
is($accounts{Foo}{description}, "Foo host\n", "description");
is($accounts{Foo}{register_text}, "Sign up\n", "registration text");
is($accounts{Foo}{register_color}, "blue", "register colour");
is($accounts{Foo}{username}, 'me', "existing username kept");
is($accounts{Foo}{password}, '', "password defaulted");
is($accounts{Foo}{path}, "$dir/Foo.pm", "path");
is($accounts{Foo}{folder}, "$dir/", "folder");
is($accounts{Foo}{supports_anonymous_upload}, "1\n", "feature flag");
is($gui, 2, "gui refreshed for each new upload plugin");
Shutter::App::PluginRegistry::check_installed_upload_plugins(new_stub(), new_stub(), undef, $root, TRUE, \%accounts, sub { $gui++ });
is($accounts{Imgur}{module}, "Imgur\n", "Imgur registered with Path::Class");
};
}
done_testing();