/
Mask.pm6
81 lines (59 loc) · 1.68 KB
/
Mask.pm6
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
use v6.c;
unit class CompUnit::Repository::Mask does CompUnit::Repository;
has SetHash $!mask;
submethod BUILD(:$mask) {
$!mask = $mask.SetHash;
}
method id() {
"mask"
}
method need($spec) {
if $spec.from eq "Perl6" and $!mask{$spec.short-name}:exists or not self.next-repo {
X::CompUnit::UnsatisfiedDependency.new(:specification($spec)).throw
}
self.next-repo.need($spec)
}
method mask($module) {
$!mask{$module} = True;
}
method unmask($module) {
$!mask{$module}:delete;
}
method loaded() {
Nil
}
my CompUnit::Repository::Mask $masker;
sub mask-module($module) is export(:mask-module) {
unless $masker {
CompUnit::RepositoryRegistry.use-repository(
$masker = CompUnit::Repository::Mask.new
);
}
$masker.mask($module);
}
sub unmask-module($module) is export(:unmask-module) {
unless $masker {
die "We haven't masked anything yet?";
}
$masker.unmask($module);
}
=begin pod
=head1 NAME
CompUnit::Repository::Mask - hide installed modules for testing.
=head1 SYNOPSIS
use CompUnit::Repository::Mask :mask-module, :unmask-module;
mask-module('Test');
try require Test; # now fails
unmask-module('Test');
require Test; # succeeds
=head1 DESCRIPTION
CompUnit::Repository::Mask helps testing code dealing with optional
dependencies. It allows for masking and unmasking installed modules, so
you can write tests for when the dependency is missing and for when it's
installed.
=head1 AUTHOR
Stefan Seifert <nine@detonation.org>
=head1 COPYRIGHT AND LICENSE
Copyright 2017 Stefan Seifert
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod