App-TimeTracker

 view release on metacpan or  search on metacpan

t/Command/core.t  view on Meta::CPAN

use 5.010;
use strict;
use warnings;
use lib qw(t);

use Test::Most;
use Test::Trap;
use Test::File;
use testlib::FakeHomeDir;
use App::TimeTracker::Proto;
local $ENV{TZ} = 'UTC';

my $tmp  = testlib::Fixtures::setup_tempdir;
my $home = $tmp->subdir('.TimeTracker');
$tmp->subdir('some_project')->mkpath;
$tmp->subdir('other_project')->mkpath;
$tmp->subdir('project_name_auto')->mkpath;
$tmp->subdir('project_name_custom')->mkpath;
my $p   = App::TimeTracker::Proto->new( home => $home );
my $now = DateTime->now;
$now->set_time_zone('local');
my $basetf      = $now->ymd('') . '-';
my $tracker_dir = $home->subdir( $now->year, sprintf( "%02d", $now->month ) );

{    # init
    @ARGV = ('init');
    my $class = $p->setup_class( {} );

    file_exists_ok( $home->file('projects.json') );
    file_exists_ok( $home->file('tracker.json') );
    file_not_exists_ok( $tmp->file( 'some_project',  '.tracker.json' ) );
    file_not_exists_ok( $tmp->file( 'other_project', '.tracker.json' ) );

    $p->_build_home($home);

    my $t = $class->name->new( home => $home, config => {}, _current_project => 'some_project' );
    trap { $t->cmd_init( $tmp->subdir('some_project') ) };
    is( $trap->stdout, "Set up this directory for time-tracking via file .tracker.json\n",
        'init: output' );

    file_exists_ok( $home->file('projects.json') );
    file_exists_ok( $home->file('tracker.json') );
    file_exists_ok( $tmp->file( 'some_project', '.tracker.json' ) );

}

{    # init (setting project name)
    file_not_exists_ok( $tmp->file( 'project_name_auto',  '.tracker.json' ) );
    file_not_exists_ok( $tmp->file( 'project_name_custom', '.tracker.json' ) );

    @ARGV = ('init');
    my $class = $p->setup_class( {} );

    {
        my $p   = App::TimeTracker::Proto->new( home => $home );
        # _current_project not set
        my $t = $class->name->new( home => $home, config => {} );
        trap { $t->cmd_init( $tmp->subdir('project_name_auto') ) };

        file_exists_ok( $tmp->file( 'project_name_auto', '.tracker.json' ) );
        my $config = $p->load_config( $tmp->subdir(qw(project_name_auto)) );
        is $config->{project}, 'project_name_auto', 'automatic project name';
        is $p->project, 'project_name_auto', 'project attribute set correctly';
    }

    {
        my $p   = App::TimeTracker::Proto->new( home => $home );
        # _current_project is set
        my $t = $class->name->new( home => $home, config => {}, _current_project => 'my-custom-project' );
        trap { $t->cmd_init( $tmp->subdir('project_name_custom') ) };

        file_exists_ok( $tmp->file( 'project_name_custom', '.tracker.json' ) );
        my $config = $p->load_config( $tmp->subdir(qw(project_name_custom)) );
        is $config->{project}, 'my-custom-project', 'custom project name';
        is $p->project, 'my-custom-project', 'project attribute set from file';
    }

}

{    # init (setting up project tree)



( run in 1.036 second using v1.01-cache-2.11-cpan-39bf76dae61 )