File: FauxFetchCommand.pm

package info (click to toggle)
libalien-build-perl 2.84-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,116 kB
  • sloc: perl: 10,350; ansic: 134; sh: 66; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 2,715 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package MyTest::FauxFetchCommand;

use strict;
use warnings;
use MyTest::System;
use Test2::API qw( context );
use Path::Tiny qw( path );
use Capture::Tiny qw( tee );
use JSON::PP qw( encode_json decode_json );
use Exporter qw( import );

our @EXPORT = qw( test_config );

my($test_name) = $0 =~ m{[/\\](.*)\.t$};
my $command_name = $test_name =~ /curlcommand/ ? 'curl' : 'wget';

my %record = %{ decode_json path("corpus/$test_name/record/old.json")->slurp };

sub real_cmd
{
  my(@args) = @_;

  my %old = map { $_->basename => 1 } path('.')->children;

  my($stdout, $stderr, $exit) = tee {
    CORE::system $command_name, @args;
    $? >> 8;
  };

  my $key = "@args";

  for($key, $stdout, $stderr)
  {
    s{http://localhost.*?/corpus}{http://localhost/corpus}g;
    s{ftp://[a-z]+:[a-z]+\@localhost:[0-9]+/.*?/corpus}{ftp://localhost/corpus}g;
  }

  my %files;

  if(! -d ".git")
  {
    foreach my $child (path('.')->children)
    {
      next if $old{$child->basename};
      $files{$child->basename} = $child->slurp;
    }
  }

  $record{$key} = {
    stdout => $stdout,
    stderr => $stderr,
    exit   => $exit,
    files  => \%files,
  };

  $exit;
}

sub faux_cmd
{
  my(@args) = @_;

  my $key = "@args";

  unless($record{$key})
  {
    my $ctx = context();
    $ctx->bail("do not have a record for $command_name $key");
  }

  my $run = $record{$key};

  print STDOUT $run->{stdout};
  print STDERR $run->{stderr};

  foreach my $filename (keys %{ $run->{files} })
  {
    path($filename)->spew($run->{files}->{$filename});
  }

  $run->{exit};
}

sub test_config ($)
{
  my($name) = @_;
  my $path = path("t/bin/$name.json");

  if(-f $path)
  {
    my $config = JSON::PP::decode_json(scalar $path->slurp);

    my $guard = system_fake;

    $guard->add($command_name        => \&real_cmd);
    $guard->add("/bin/$command_name" => \&real_cmd);

    $config->{url} =~ s{dist/?$}{$test_name/dir};
    $config->{guard} = $guard;

    my $ctx = context();
    $ctx->note("testing against real $command_name and real $name @{[ $config->{url} ]}");
    $ctx->release;

    return $config;
  }
  eles
  {
    my %config;
    my $guard = system_fake;

    $guard->add($command_name        => \&faux_cmd);
    $guard->add("/bin/$command_name" => \&faux_cmd);

    $config{guard} = $guard;
    $config{url}   = $name eq 'httpd'
      ? "http://localhost/corpus/$test_name/dir"
      : "ftp://localhost/corpus/$test_name/dir";

    return \%config;
  }
}

delete $ENV{CURL};
delete $ENV{WGET};

END {
  path("corpus/$test_name/record/new.json")->spew(encode_json( \%record ));
  if(eval { require YAML; 1 })
  {
    YAML::DumpFile(path("corpus/$test_name/record/new.yml")->stringify, \%record );
  }
}

1;