App-Fetchware

 view release on metacpan or  search on metacpan

t/App-Fetchware-Util.t  view on Meta::CPAN

that fetchware tried to download were [invalidscheme://fake.url invalidscheme://fake.url].
EOS
    # Clear previous mirror.
    config_delete('mirror');


    # Setup a mirror that should succeed, but a $url that should fail.
    config(mirror => $ENV{FETCHWARE_HTTP_LOOKUP_URL});
    my $got_output = download_dirlist($url);
    ok($got_output,
        'check download_dirlist() http mirror success');

    # parse $expected_output, because I can't use eq to test if $got_output eq
    # $expected_output anymore, because www.apache.org uses round-robin DNS or
    # ANYCAST or just multiple servers that more importantly have different time
    # stamps, and these different time stamps cause the eq test to fail. Instead
    # I must use http_parse_filelist() to parse the $got_output, and
    # then use Test::Deep to see if it matches a the right regexes.

    # Parse $got_output, creating $got_filelisting to be able to check its
    # structure below using Test::Deep for corectness.
    my $got_filelisting = http_parse_filelist($got_output);
    ok(ref $got_filelisting eq 'ARRAY',
        'checked download_dirlist() parsed output.');

    # Use Test::Deep to see if $got_output's data structure matches the right regexs.
    cmp_deeply($got_filelisting, eval(expected_filename_listing()),
        'check download_dirlist() proper output');

    # Now try multiple mirrors.

    # Delete all mirrors.
    config_delete('mirror');

    # Set up mirrors that will fail, and one final mirror that should succeed.
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $ENV{FETCHWARE_HTTP_LOOKUP_URL});

    # Test multiple mirrors.
    ok($got_output = download_dirlist($url),
        'check download_dirlist() multi-mirror http success');

    # Parse $got_output, creating $got_filelisting to be able to check its
    # structure below using Test::Deep for corectness.
    $got_filelisting = http_parse_filelist($got_output);
    ok(ref $got_filelisting eq 'ARRAY',
        'checked download_dirlist() multi-mirror parsed output.');

    # Use Test::Deep to see if $got_output's data structure matches the right regexs.
    cmp_deeply($got_filelisting, eval(expected_filename_listing()),
        'check download_dirlist() multi-mirror proper output');

    # Test download_dirlist(PATH => $path) support.
    __clear_CONFIG();
###BUGALERT### Apache does not have a ftp main, author's mirror, so I cannot
#actually test this over ftp any more.
##CANTTEST##    # Set up the mirror so that it has no path.
##CANTTEST##    my ($scheme, $auth, $path, $query, $frag) =
##CANTTEST##        uri_split($ENV{FETCHWARE_FTP_LOOKUP_URL});
##CANTTEST##    config(mirror => uri_join($scheme, $auth, undef, undef, undef));
##CANTTEST##
##CANTTEST##    # Then test download_dirlist() with what would normally be the mirror's
##CANTTEST##    # path.
##CANTTEST##    ok($got_output = download_dirlist(PATH => $path),
##CANTTEST##        'checked download_dirlists(PATH) ftp success.');
##CANTTEST##    is_deeply($got_output, $expected_output,
##CANTTEST##        'check download_dirlist(PATH) ftp proper output');


    # Clean up mirrors again.
    config_delete('mirror');


    # Set up mirrors that will fail, and one final mirror that should succeed.
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $ENV{FETCHWARE_HTTP_LOOKUP_URL});

    # Test multiple mirrors.
    ok($got_output = download_dirlist($url),
        'check download_dirlist() multi-mirror http success');

    # Parse $got_output, creating $got_filelisting to be able to check its
    # structure below using Test::Deep for corectness.
    $got_filelisting = http_parse_filelist($got_output);
    ok(ref $got_filelisting eq 'ARRAY',
        'checked download_dirlist() multi-mirror parsed output.');

    # Use Test::Deep to see if $got_output's data structure matches the right regexs.
    cmp_deeply($got_filelisting, eval(expected_filename_listing()),
        'check download_dirlist() multi-mirror proper output');

    # Test download_dirlist(PATH => $path) support.
    __clear_CONFIG();
    # Set up the mirror so that it has no path.
    my ($scheme, $auth, $path, $query, $frag) =
        uri_split($ENV{FETCHWARE_HTTP_LOOKUP_URL});
    config(mirror => uri_join($scheme, $auth, undef, undef, undef));

    # Then test download_dirlist() with what would normally be the mirror's
    # path.
    ok($got_output = download_dirlist(PATH => $path),
        'checked download_dirlists(PATH) http success.');

    # Parse $got_output, creating $got_filelisting to be able to check its
    # structure below using Test::Deep for corectness.
    $got_filelisting = http_parse_filelist($got_output);
    ok(ref $got_filelisting eq 'ARRAY',
        'checked download_dirlist(PATH) parsed output.');

    # Use Test::Deep to see if $got_output's data structure matches the right regexs.
    cmp_deeply($got_filelisting, eval(expected_filename_listing()),
        'check download_dirlist(PATH) proper output');

    # Clean up after ourselves.
    __clear_CONFIG();
};


subtest 'test download_dirlist(file://)' => sub {
    my $got_dirlist;
    ok($got_dirlist = download_dirlist('file://'. catfile(cwd(), 't')),
        'checked download_dirlist(file://) success');
#note explain \$got_dirlist;
};


subtest 'test download_ftp_url()' => sub {
    skip_all_unless_release_testing();

    my $filename = download_ftp_url($ENV{FETCHWARE_FTP_DOWNLOAD_URL});
    ok(-e $filename, 'checked download_ftp_url success');

    ok(unlink $filename, 'checked deleting downloaded file');


    eval_ok(sub {download_ftp_url('ftp://doesntexist.ever')},
        qr/App-Fetchware: run-time error\. fetchware failed to connect to the ftp server at
domain \[doesntexist\.ever\]\. The system error was \[Name or service not known|Net::FTP: Bad hostname 'doesntexist\.ever'\]\.
See man App::Fetchware\./,
        'checked determine_download_url() connect failure');

##HOWTOTEST## How do I test the switching to binary mode error?  Can it even
#fail?

##HOWTOTEST##    eval_ok(sub {download_ftp_url('whatftpserverdoesntsupportanonymous&ispublic?');,
##HOWTOTEST##        <<EOS, 'checked download_ftp_url() empty content failure');
##HOWTOTEST##App-Fetchware: run-time error. fetchware failed to log in to the ftp server at
##HOWTOTEST##domain [$site]. The ftp error was [@{[$ftp->message]}]. See man App::Fetchware.
##HOWTOTEST##EOS
    
    my ($scheme, $auth, $path, $query, $frag) =
        uri_split($ENV{FETCHWARE_FTP_DOWNLOAD_URL});
    eval_ok( sub {download_ftp_url("$scheme://$auth/doesnt/exist/anywhere")},
        qr!App-Fetchware: run-time error. fetchware failed to cwd\(\) to \[/doesnt/exist/a!,
        'check download_ftp_url() failed to chdir');

    eval_ok(sub {download_ftp_url("$scheme://$auth/$path/filedoesntexist")},
        qr!App-Fetchware: run-time error. fetchware failed to cwd\(\) to \[!,
        'checked download_ftp_url() cant Net::FTP->get() file');
    
##BUGALERT### Must add test for download_ftp_url() returning the $filename.

};


subtest 'test download_http_url()' => sub {
    skip_all_unless_release_testing();

###BUGALERT### the 2 lins below are copied & pasted 3 times subify them!
    my ($scheme, $auth, $path, $query, $frag) = uri_split($ENV{FETCHWARE_FTP_DOWNLOAD_URL});
    my ($volume, $directories, $filename) = splitpath($path);
    is(download_http_url($ENV{FETCHWARE_HTTP_DOWNLOAD_URL}),
        $filename, 'checked download_http_url() success.');
    ok(-e $filename, 'checked download_ftp_url success');
    ok(unlink $filename, 'checked deleting downloaded file');

    eval_ok(sub {download_http_url('http://fake.url')},
        qr/599 Internal Exception/, 'checked download_http_url bad hostname');

##HOWTOTEST## I don't think the unless length $response->{content} is easily
#testable.

##HOWTOTEST## Also, open failing isn't testable either, because any data I feed
#it will cause the other tests above to fail first.

##HOWTOTEST## How do you test close failing? I don't know if you can easily.

};


subtest 'test download_file_url' => sub {
    skip_all_unless_release_testing();

    # Create test file to download.
    my $test_dist_path = make_test_dist(file_name => 'test-dist',
        ver_num => '1.00', destination_directory => rel2abs('t'));

    my $filename = download_file_url('file://t/test-dist-1.00.fpkg');

    is($filename, 'test-dist-1.00.fpkg',
        'checked download_file_url() success.');

    # Delete useless test-dist package.
    ok(unlink $test_dist_path, 'checked download_file_url() cleanup.');

    # Delete useless copied file.
    ok(unlink $filename, 'checked download_file_url() cleanup.');
};


subtest 'test download_file()' => sub {
    skip_all_unless_release_testing();

    # Ensure we have a clean working environment.
    __clear_CONFIG();

    # Test download_file()'s parameter checking.
    eval_ok(sub {download_file()},
        <<EOE, 'checked download_file() no params exception.');
App-Fetchware-Util: You can only specify either PATH or URL never both. Only
specify one or the other when you call download_file().
EOE
    eval_ok(sub {download_file('one', 'two', 'three')},
        <<EOE, 'checked download_file() too many exception.');
App-Fetchware-Util: You can only specify either PATH or URL never both. Only
specify one or the other when you call download_file().
EOE
    eval_ok(sub {download_file('fake url who cares', PATH => 'fake/path')},
        <<EOE, 'checked download_file() wrong param exception.');
App-Fetchware-Util: You can only specify either PATH or URL never both. Only
specify one or the other when you call download_file().
EOE

    eval_ok(sub {download_file(PATH => 'fake/path')},
        <<EOE, 'checked download_file() PATH but no mirrors exception.');
App-Fetchware-Util: You only called download_file() with just a PATH parameter,
but also failed to specify any mirrors in your configuration. Without any
defined mirrors download_file() cannot determine from what host to download your
file. Please specify a mirror and try again.
EOE

    my $url = 'invalidscheme://fake.url/KEYS';
    # Setup a fake mirror to test the mirror failing too.
    config(mirror => $url);
    eval_ok(sub {download_file($url)}, <<EOS, 'checked download_file() invalid url scheme');
App-Fetchware-Util: Failed to download the specifed URL [invalidscheme://fake.url/KEYS] or path
[] using the included hostname in the url you specifed or any
mirrors. The mirrors are [invalidscheme://fake.url/KEYS]. And the urls
that fetchware tried to download were [invalidscheme://fake.url/KEYS invalidscheme://fake.url/KEYS].
EOS
    # Clear previous mirror.
    config_delete('mirror');

    # Setup a mirror that should succeed, but a $url that should fail.
    my $filename;
    config(mirror => "$ENV{FETCHWARE_HTTP_LOOKUP_URL}");
    ok($filename = download_file($url),
        'check download_file() ftp mirror success');
    ok(-e $filename, 'checked download_file() ftp filename success');
    ok(unlink $filename, 'checked deleting downloaded file');

    # Now try multiple mirrors.

    # Delete all mirrors.
    config_delete('mirror');

    # Set up mirrors that will fail, and one final mirror that should succeed.
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => $url);
    config(mirror => "$ENV{FETCHWARE_HTTP_LOOKUP_URL}");

    # Test multiple mirrors.
    ok($filename = download_file($url),
        'check download_file() ftp multi-mirror success');
    ok(-e $filename, 'checked download_file() ftp multi-mirror filename success');
    ok(unlink $filename, 'checked deleting downloaded file');

    # Test download_dirlist(PATH => $path) support.
    __clear_CONFIG();
    # Set up the mirror so that it has no path.
    my ($scheme, $auth, $path, $query, $frag) =
        uri_split($ENV{FETCHWARE_HTTP_LOOKUP_URL});
    config(mirror => uri_join($scheme, $auth, undef, undef, undef));
    # Strip $path's ending / if present.
    $path =~ s!/$!!;

    # Then test download_dirlist() with what would normally be the mirror's
    # path.
    ok($filename = download_file(PATH => "$path/KEYS"),
        'checked download_dirlists(PATH) ftp success.');
    ok(-e $filename, 'checked download_file(PATH) ftp success');
    ok(unlink $filename, 'checked deleting downloaded file');

    # Clean up mirrors again.
    config_delete('mirror');

###BUGALERT### Can't test this anymore, because apache does not provide a "main
    #author mirror that supports FTP. The main mirror only supports HTTP.
##CANTTEST##    # Set up mirrors that will fail, and one final mirror that should succeed.
##CANTTEST##    config(mirror => $url);
##CANTTEST##    config(mirror => $url);
##CANTTEST##    config(mirror => $url);
##CANTTEST##    config(mirror => $url);
##CANTTEST##    config(mirror => "$ENV{FETCHWARE_FTP_LOOKUP_URL}");
##CANTTEST##
##CANTTEST##    # Test multiple mirrors.
##CANTTEST##    ok($filename = download_file($url),
##CANTTEST##        'check download_file() ftp multi-mirror success');
##CANTTEST##    ok(-e $filename, 'checked download_file() http multi-mirror filename success');
##CANTTEST##    ok(unlink $filename, 'checked deleting downloaded file');

    # Test download_file(PATH => $path) support.
    __clear_CONFIG();
    # Set up the mirror so that it has no path.
    ($scheme, $auth, $path, $query, $frag) =
        uri_split($ENV{FETCHWARE_HTTP_LOOKUP_URL});
    config(mirror => uri_join($scheme, $auth, undef, undef, undef));
    # Strip $path's ending / if present.
    $path =~ s!/$!!;

    # Then test download_dirlist() with what would normally be the mirror's
    # path.
    ok($filename = download_file(PATH => "$path/KEYS"),
        'checked download_file(PATH) http success.');
    ok(-e $filename, 'checked download_file(PATH) http success');
    ok(unlink $filename, 'checked deleting downloaded file');

    # clean up after ourselves.
    __clear_CONFIG();
};


subtest 'test download_file(file://)' => sub {
    my $test_dist_path = make_test_dist(file_name => 'test-dist',
        ver_num => '1.00');

    # Test download_file() with a url.
    my $got_filename;
    ok($got_filename = download_file('file://' . $test_dist_path),
        'checked download_file(file://) success.');

    is(file($got_filename)->basename, file($test_dist_path)->basename,
        'checked download_file(file://) filename success.');
    # Also delete the $got_filename, because download_file() will "download"
    # (copy the file).
    ok(unlink($got_filename),
        'checked download_file(file://) cleanup copied file.');

    # Now test download_file() witha PATH instead.
    # Must specify a lookup_url(), so download_file() can detect that my
    # lookup_url is a local file:// one.
    config(lookup_url => "file://$test_dist_path");
    config(mirror => "file://$test_dist_path");
    ok($got_filename = download_file(PATH => $test_dist_path),
        'checked download_file(file://) PATH success.');
    # Also delete the $got_filename, because download_file() will "download"
    # (copy the file).
    ok(unlink($got_filename),
        'checked download_file(file://) cleanup copied file.');

    ok(unlink($test_dist_path),
        'checked download_file(file://) cleanup test file.');
};


subtest 'test no_mirror_download_file' => sub {
    skip_all_unless_release_testing();

    my $url = 'invalidscheme://fake.url';
    eval_ok(sub {no_mirror_download_file($url)}, <<EOS, 'checked no_mirror_download_file() invalid url scheme');
App-Fetchware: run-time syntax error: the url parameter your provided in
your call to download_file() [invalidscheme://fake.url] does not have a supported URL scheme (the
http:// or ftp:// part). The only supported download types, schemes, are FTP and
HTTP. See perldoc App::Fetchware.
EOS



( run in 0.866 second using v1.01-cache-2.11-cpan-b16cb0d3907 )