From f1b3b3bb7204d4d57269b0dd498382ae7ecf6df4 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Fri, 5 Jan 2018 23:34:16 +0100 Subject: [PATCH] Refactor CmdDefault - restore original command behaviour:return exit code 0 if tracking is active, 1 if not - show welcome screen on first run - add/update tests --- src/commands/CmdDefault.cpp | 46 +++++++++++++++++++++---------------- test/cli.t | 16 +++++++++++-- test/clock.t | 10 ++++---- test/quiet.t | 4 ++-- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/commands/CmdDefault.cpp b/src/commands/CmdDefault.cpp index 00c1aa91..d7045f47 100644 --- a/src/commands/CmdDefault.cpp +++ b/src/commands/CmdDefault.cpp @@ -34,33 +34,39 @@ int CmdDefault (Rules& rules, Database& database) { // Load the most recent interval, summarize and display. auto interval = getLatestInterval (database); + if (interval.range.is_open ()) - { - if (rules.getBoolean ("verbose")) - std::cout << intervalSummarize (database, rules, interval); - } - else { if (rules.getBoolean ("verbose")) { - if (rules.getBoolean ("temp.shiny")) - std::cout << '\n' - << "Welcome to Timewarrior.\n" - << '\n' - << "There is built-in help:\n" - << " timew help\n" - << " timew help \n" - << " (and more)\n" - << '\n' - << "There is a fully-detailed man page:\n" - << " man timew\n" - << '\n'; - else - std::cout << "There is no active time tracking.\n"; + std::cout << intervalSummarize (database, rules, interval); } + + return 0; } - return 0; + if (rules.getBoolean ("temp.shiny")) + { + std::cout << '\n' + << "Welcome to Timewarrior.\n" + << '\n' + << "There is built-in help:\n" + << " timew help\n" + << " timew help \n" + << " (and more)\n" + << '\n' + << "There is a fully-detailed man page:\n" + << " man timew\n" + << '\n'; + return 0; + } + + if (rules.getBoolean ("verbose")) + { + std::cout << "There is no active time tracking.\n"; + } + + return 1; } //////////////////////////////////////////////////////////////////////////////// diff --git a/test/cli.t b/test/cli.t index 40fc5933..d8be80be 100755 --- a/test/cli.t +++ b/test/cli.t @@ -28,6 +28,7 @@ import sys import os +import shutil import unittest from datetime import datetime # Ensure python finds the local simpletap module @@ -57,16 +58,27 @@ class TestCLI(TestCase): """Executed before each test in the class""" self.t = Timew() + def test_initial_call_of_timew(self): + """Verify that calling 'timew' the first time returns exit code 0""" + self.t.reset_env() + shutil.rmtree(self.t.env["TIMEWARRIORDB"]) + + code, out, err = self.t.runSuccess(":yes") + self.assertIn("Welcome to Timewarrior.\n", out) + + assert os.path.isdir(self.t.env["TIMEWARRIORDB"]) + assert os.path.exists(self.t.env["TIMEWARRIORDB"]) + def test_TimeWarrior_without_command_without_active_time_tracking(self): """Call 'timew' without active time tracking""" - code, out, err = self.t() + code, out, err = self.t.runError() self.assertIn("There is no active time tracking", out) def test_TimeWarrior_without_command_with_active_time_tracking(self): """Call 'timew' with active time tracking""" self.t("start FOO") code, out, err = self.t() - self.assertIn("Tracking FOO", out) + self.assertIn("Tracking FOO\n", out) def test_TimeWarrior_with_invalid_command(self): """Call a non-existing TimeWarrior command should be an error""" diff --git a/test/clock.t b/test/clock.t index 3361e98d..69b7067f 100755 --- a/test/clock.t +++ b/test/clock.t @@ -63,7 +63,7 @@ class TestClock(TestCase): def test_start_new(self): """Verify that 'start' creates an open interval""" - code, out, err = self.t("") + code, out, err = self.t.runError("") self.assertIn("There is no active time tracking.", out) code, out, err = self.t("start tag1 tag2") @@ -76,7 +76,7 @@ class TestClock(TestCase): def test_start_stop(self): """Verify that start/stop creates and closes an interval""" - code, out, err = self.t("") + code, out, err = self.t.runError("") self.assertIn("There is no active time tracking.", out) code, out, err = self.t("start tag1 tag2") @@ -90,12 +90,12 @@ class TestClock(TestCase): self.assertIn('"end":', out) self.assertIn('"tags":["tag1","tag2"]', out) - code, out, err = self.t("") + code, out, err = self.t.runError("") self.assertIn("There is no active time tracking.", out) def test_start_additional(self): """Verify that 'start' closes an open interval and starts a new one""" - code, out, err = self.t("") + code, out, err = self.t.runError("") self.assertIn("There is no active time tracking.", out) code, out, err = self.t("start tag1 tag2") @@ -114,7 +114,7 @@ class TestClock(TestCase): self.assertIn('"tags":["tag1","tag2"]', out) self.assertIn('"tags":["tag3"]', out) - code, out, err = self.t("") + code, out, err = self.t.runError("") self.assertIn("There is no active time tracking.", out) def test_start_subtract(self): diff --git a/test/quiet.t b/test/quiet.t index 8adebd83..60adae8b 100755 --- a/test/quiet.t +++ b/test/quiet.t @@ -58,10 +58,10 @@ class TestQuietMode(TestCase): def test_default(self): """Default command should obey :quiet hint""" - code, out, err = self.t("") + code, out, err = self.t.runError("") self.assertIn('There is no active time tracking.', out) - code, out, err = self.t(":quiet") + code, out, err = self.t.runError(":quiet") self.assertNotIn('There is no active time tracking.', out) def test_stop(self):