App-Fetchware

 view release on metacpan or  search on metacpan

t/bin-fetchware-util.t  view on Meta::CPAN

    eval_ok(sub {parse_fetchwarefile(\$api_subs_exported)},
        qr/fetchware: The App::Fetchware module you choose in your fetchwarefile does not/,
        'checked parse_fetchwarefile() failed to export api subs.');
};


subtest 'test create_fetchware_package()' => sub {
    ###BUGALERT### Must add tests for adding the gpg generated files to the
    #fetchware package, so that gpg doesn't have to download the keys again.
    #Also, I must actually add code for this in bin/fetchware.

    my $fetchwarefile = '# Fake Fetchwarefile for testing';

    # Create a hopefully successful fetchware package using the current working
    # directory (my Fetchware git checkout) and the fake Fetchwarefile I created
    # above.
    my $cwd = dir(cwd());
    my $cwd_parent = $cwd->parent();
    my $cwd_lastdir = $cwd->dir_list(-1, 1);
    is(create_fetchware_package(\$fetchwarefile, cwd(), $cwd_parent),
        catfile($cwd_parent, "$cwd_lastdir.fpkg"),
        'checked create_fetchware_package() success');

    is(cwd(), $cwd,
        'checked create_fetchware_package() chdir back to base directory');

    # Delete generated files.
    ok(unlink(catfile($cwd_parent,"$cwd_lastdir.fpkg")) == 1,
        'checked create_fetchware_package() delete generated files');

##CANNOTTEST## Can't test anymore, because the doesntexist.ever-anywhere file
#will fail the unless conditional and skip the cp() call, so I can't test for
#this specifically anymore.
##CANNOTTEST##    eval_ok(sub {create_fetchware_package('doesntexist.ever-anywhere', cwd())},
##CANNOTTEST##        <<EOE, 'checked create_fetchware_package() cp failure');
##CANNOTTEST##fetchware: run-time error. Fetchware failed to copy the Fetchwarefile you
##CANNOTTEST##specified [doesntexist.ever-anywhere] on the command line or was contained in the
##CANNOTTEST##fetchware package you specified to the newly created fetchware package. Please
##CANNOTTEST##see perldoc App::Fetchware. OS error [No such file or directory].
##CANNOTTEST##EOE

    ok(chdir($cwd), 'checked create_fetchware_package() chdir back to base directory');

};


subtest 'check fetchware_database_path()' => sub {
    # $ENV{FETCHWARE_DATABASE_PATH} has been set to a temporary directory at the
    # top of this file, so it applies to all of these tests too; however, the
    # tests below do not read or write to the actual FETCHWARE_DATABASE_PATH;
    # instead, they just return what it should be and test that the correct
    # things are being returned. Because fetchware_database_path()'s normal
    # behavior is needed to properly test this function even as root, we should
    # local delete $ENV{FETCHWARE_DATABASE_PATH} just for this one function,
    # fetchware_database_path(), because it needs "normal" behavior for proper
    # testing, and such proper testing has no side effects like messing witht he
    # filesystem.
    local $ENV{FETCHWARE_DATABASE_PATH};
    delete $ENV{FETCHWARE_DATABASE_PATH};

    if (is_os_type('Unix', $^O)) {
        # If we're effectively root use a "system" directory.
        if ($> == 0) {
            is(fetchware_database_path(), '/var/log/fetchware',
                'checked fetchware_database_path() as root');
        # else use a "user" directory.
        } else {
            like(fetchware_database_path(),
                # Add a generic "fetchware-test", because ~/.local and /tmp are
                # not the only possibilities especially among CPAN Testers, who
                # often have tempdirs set to cwd(), or other weird paths ending
                # up being used in the test suite. This should fix this.
                # Specific CPAN Tester Report this fixes:
                # http://www.cpantesters.org/cpan/report/3ac3bd5a-4f45-11e5-bcc7-9db5dfbfc7aa
                qr!Perl/dist/fetchware$|/tmp/fetchware-test|fetchware-test!i,
                'checked fetchware_database_path() as user');
        }
    } elsif ($^O eq "MSWin32") {
        # Load main Windows module to use to see if we're Administrator or not.
        BEGIN {
            if ($^O eq "MSWin32")
            {
                require Win32;
                Module->import();  # assuming you would not be passing arguments to "use Module"
            }
        }
        if (Win32::IsAdminUser()) {
            is(fetchware_database_path(), 'C:\fetchware',
                'checked fetchware_database_path() as Administrator on Win32');
        } else {
            ###BUGALERT### Add support for this test on Windows!
            fail('Must add support for non-admin on Windows!!!');
        }
    # Fall back on File::HomeDir's recommendation if not "Unix" or windows.
    } else {
            ###BUGALERT### Add support for everything else too!!!
            fail('Must add support for your OS!!!');
    }

    # Test fetchware_database_path() when the fetchware_database_path
    # configuration option has been specified.
    config(fetchware_db_path => cwd());
    is(fetchware_database_path(), cwd(),
        'check fetchware_database_path() config option success.');
    config_delete('fetchware_db_path');

    # Test FETCHWARE_DATABASE_PATH too.
    local $ENV{FETCHWARE_DATABASE_PATH} = cwd();
    is(fetchware_database_path(), cwd(),
        'check fetchware_database_path() ENV option success.');

    # Now test both of them together.
    config(fetchware_db_path => dir(cwd())->parent());
    is(fetchware_database_path(), dir(cwd())->parent(),
        'check fetchware_database_path() both options success.');

    # Clean up after ourselves.
    delete $ENV{FETCHWARE_DATABASE_PATH};
    config_delete('fetchware_db_path');
};


subtest 'check determine_fetchware_package_path()' => sub {

    # Write some test files to my fetchware_database_path() to test determining
    # if they're there or not.
    my $fetchware_db_path = fetchware_database_path();
    my @test_files = qw(fake-apache fake-apache2 fake-mariadb fake-qmail fake-nginx);
    for my $file (@test_files) {
        ok(open( my $fh, '>', catfile($fetchware_db_path, $file)),
            "check determine_fetchware_package_path() test file creation [$file]");
        print $fh "# Meaningless test Fetchwarefile $file";
        close $fh;
    }

    # Now test multiple results with one query.
    eval_ok(sub {determine_fetchware_package_path('apache')},
        <<EOE, 'checked determine_fetchware_package_path() multiple values');
Choose which package from the list above you want to upgrade, and rerun
fetchware upgrade using it as the argument for the package you want to upgrade.
EOE

    # Remove both apache's from further tests, because it will return 2 instead
    # of a single scalar like the test assumes.
    my @apacheless_test_files = grep { $_ !~ /apache/ } @test_files;

    for my $file (@apacheless_test_files) {
        like(determine_fetchware_package_path($file), qr/$file/,
            "checked determine_fetchware_package_path() [$file] success");
    }
    
    ok( ( map { unlink catfile($fetchware_db_path, $_) } @test_files ) == 5,
        'checked determine_fetchware_package_path() delete test files');
};



( run in 1.333 second using v1.01-cache-2.11-cpan-64ef6c95b5d )