| 1 | use v6; |
|---|
| 2 | |
|---|
| 3 | use Test; |
|---|
| 4 | |
|---|
| 5 | =begin pod |
|---|
| 6 | |
|---|
| 7 | Tests to see whether precompiled modules are used correctly: |
|---|
| 8 | 1. They should observe @*INC |
|---|
| 9 | 2. They should only be used if there's a corresponding source |
|---|
| 10 | module and it is older than the precompiled module |
|---|
| 11 | |
|---|
| 12 | =end pod |
|---|
| 13 | |
|---|
| 14 | # XXX - needs porting, only works on Unixen today |
|---|
| 15 | BEGIN { |
|---|
| 16 | |
|---|
| 17 | plan 14; |
|---|
| 18 | |
|---|
| 19 | if $*OS eq any <MSWin32 mingw msys cygwin browser> { |
|---|
| 20 | skip_rest "tests need to be ported to work on $*OS"; |
|---|
| 21 | exit; |
|---|
| 22 | }; |
|---|
| 23 | unless (try { eval("1", :lang<perl5>) }) { |
|---|
| 24 | skip_rest "tests require Perl 5 support"; |
|---|
| 25 | exit; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | use File::Spec; |
|---|
| 31 | |
|---|
| 32 | # XXX - This should be replaced with something Perl 6-native |
|---|
| 33 | use File::Temp:from<perl5> <tempdir>; |
|---|
| 34 | |
|---|
| 35 | # XXX - Also, CLEANUP does not seem to work, so tempfiles will |
|---|
| 36 | # remain after test if we do: |
|---|
| 37 | # our &tempdir := File::Temp.can('tempdir').assuming(:CLEANUP(1)); |
|---|
| 38 | # So we laboriously work around: |
|---|
| 39 | my (@files_created, @dirs_created); |
|---|
| 40 | |
|---|
| 41 | sub mktempdir () { |
|---|
| 42 | my $dir = tempdir() |
|---|
| 43 | orelse fail; |
|---|
| 44 | @dirs_created.push($dir); |
|---|
| 45 | return $dir; |
|---|
| 46 | } |
|---|
| 47 | sub open_new (Str $filename) { |
|---|
| 48 | my $fh = open($filename, :w) |
|---|
| 49 | orelse fail; |
|---|
| 50 | @files_created.push($filename); |
|---|
| 51 | return $fh; |
|---|
| 52 | } |
|---|
| 53 | # XXX - See END block below, remove it and calls to open_new() below if removing |
|---|
| 54 | |
|---|
| 55 | sub precompile (Str $pmfile, Str $destdir) { |
|---|
| 56 | die "No such file or directory" unless $pmfile ~~ :e && $destdir ~~ :d; |
|---|
| 57 | # XXX - correct for win32? |
|---|
| 58 | my $out = catpath('', $destdir, (splitpath($pmfile))[2] ~ ".yml"); |
|---|
| 59 | @files_created.push($out); |
|---|
| 60 | # XXX - does this work under win32? |
|---|
| 61 | run($*EXECUTABLE_NAME ~ " -CParse-YAML $pmfile > $out"); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | sub generate_class (Str $classname, $value) { |
|---|
| 65 | # Must use a sub, not a method, for yaml parsing to work |
|---|
| 66 | return "class $classname \{\n sub value \{ $value.perl() \}\n\}\n"; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | sub write_class ($destdir, Str $classname, Num $value, Bool :$precompile = 0) { |
|---|
| 70 | my $filename = catpath('', $destdir, "{$classname}.pm"); |
|---|
| 71 | my $fh = open_new($filename) |
|---|
| 72 | orelse die "Couldn't open $filename: $!"; |
|---|
| 73 | $fh.say(generate_class(:$classname, :$value)); |
|---|
| 74 | $fh.close |
|---|
| 75 | orelse die "Couldn't close $filename: $!"; |
|---|
| 76 | if $precompile { |
|---|
| 77 | precompile($filename, $destdir); |
|---|
| 78 | } |
|---|
| 79 | return $filename; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | sub make_old (Str $filename) { |
|---|
| 83 | $filename ~~ :e orelse fail; |
|---|
| 84 | # XXX - not portable, please fix for win32 |
|---|
| 85 | run(«touch -t 200001010000 $filename»); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | # XXX - Wrapping in try so we can cleanup; this can go once File::Temp |
|---|
| 89 | # is native. |
|---|
| 90 | try { |
|---|
| 91 | my $lib1 = mktempdir(); |
|---|
| 92 | my $lib2 = mktempdir(); |
|---|
| 93 | diag "Created tempdirs {($lib1, $lib2)}"; |
|---|
| 94 | |
|---|
| 95 | my @libdirs = ($lib1, $lib2); |
|---|
| 96 | |
|---|
| 97 | die "# @libdirs[]: Missing directory(/ies) required by test" |
|---|
| 98 | unless all(@libdirs) ~~ :d; |
|---|
| 99 | |
|---|
| 100 | @*INC.unshift($lib1, $lib2); |
|---|
| 101 | |
|---|
| 102 | # sanity -- can we write and then use? |
|---|
| 103 | { |
|---|
| 104 | write_class($lib1, 'PMSanity', 42); |
|---|
| 105 | use_ok 'PMSanity'; |
|---|
| 106 | is PMSanity::value, 42, 'Sanity check -- can get a value'; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | # sanity -- are same-named .pm's earlier in @*INC preferred? |
|---|
| 110 | { |
|---|
| 111 | write_class($lib1, 'PMSanity2', "earlier"); |
|---|
| 112 | write_class($lib2, 'PMSanity2', "later"); |
|---|
| 113 | use_ok 'PMSanity2'; |
|---|
| 114 | is PMSanity2::value, "earlier", |
|---|
| 115 | q"Sanity check -- .pm's earlier in @*INC path are preferred"; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | # sanity -- can we use a .yml precompile? |
|---|
| 119 | { |
|---|
| 120 | my $pmfile = write_class($lib1, 'PMSanityYML', "yaml", |
|---|
| 121 | :precompile); |
|---|
| 122 | write_class($lib1, 'PMSanityYML', "pmfile"); |
|---|
| 123 | make_old($pmfile); |
|---|
| 124 | use_ok 'PMSanityYML'; |
|---|
| 125 | is PMSanityYML::value, "yaml", 'Sanity check -- can use .yml'; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | # End of sanity tests, real tests start here |
|---|
| 129 | |
|---|
| 130 | # are new .pm's preferred over old .yml's? |
|---|
| 131 | { |
|---|
| 132 | write_class($lib1, 'YAMLbyAge', "old", :precompile); |
|---|
| 133 | sleep 2; |
|---|
| 134 | write_class($lib1, 'YAMLbyAge', "new"); |
|---|
| 135 | use_ok 'YAMLbyAge'; |
|---|
| 136 | is YAMLbyAge::value, "new", "New .pm's are preferred to old .yml's"; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | # are .pm's earlier in @*INC preferred to .yml's later? |
|---|
| 140 | { |
|---|
| 141 | write_class($lib1, 'YAMLorPMbyINC', "earlier"); |
|---|
| 142 | write_class($lib2, 'YAMLorPMbyINC', "later", :precompile); |
|---|
| 143 | use_ok 'YAMLorPMbyINC'; |
|---|
| 144 | is YAMLorPMbyINC::value, "earlier", |
|---|
| 145 | q".pm's earlier in @*INC are preferred to .yml's later"; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | # are .yml's earlier in @*INC preferred to .yml's later? |
|---|
| 149 | { |
|---|
| 150 | write_class($lib1, 'YAMLbyINC', "earlier", :precompile); |
|---|
| 151 | my $pmfile = write_class($lib2, 'YAMLbyINC', "later", :precompile); |
|---|
| 152 | use_ok 'YAMLbyINC'; |
|---|
| 153 | is YAMLbyINC::value, "earlier", |
|---|
| 154 | q".yml's earlier in @*INC are preferred to .yml's later"; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | # are .yml's with no matching .pm skipped? |
|---|
| 158 | { |
|---|
| 159 | my $pmfile = write_class($lib1, 'MissingYAML', "wrong"); |
|---|
| 160 | my $ymlfile = precompile(:$pmfile, :destdir($lib1)); |
|---|
| 161 | write_class($lib2, 'MissingYAML', "right"); |
|---|
| 162 | $pmfile.unlink; |
|---|
| 163 | use_ok 'MissingYAML'; |
|---|
| 164 | is MissingYAML::value, "right", |
|---|
| 165 | q".yml's with no matching .pm are skipped"; |
|---|
| 166 | } |
|---|
| 167 | } # try |
|---|
| 168 | |
|---|
| 169 | diag "Error: $!" if $!; |
|---|
| 170 | |
|---|
| 171 | # XXX - More tempdir workaround |
|---|
| 172 | for @files_created { .unlink orelse diag "Couldn't unlink $_" } |
|---|
| 173 | for @dirs_created { .rmdir orelse diag "Couldn't rmdir $_" } |
|---|