Test for unusual task data (#3770)

* Test for unusual task data

A task might have any combination of keys and values, but Taskwarrior
often assumes that only valid values can occur, and crashes otherwise.
This is highly inconvenient, as it's often impossible to do anything
with the invalid task -- Taskwarrior just fails without modifying it.

So, this is the beginning of some testing for such invalid tasks, with
the goal of making Taskwarrior due something reasonable. In general, an
invalid attribute value is treated as if it was not set. This is not
exhaustive, and there are likely still bugs of this sort, but as we find
them we can fix and add regression tests to this script.

This introduces a new test-only binary that creates a "bare" task using
TaskChampion, avoiding Taskwarrior's efforts to not create "unusual"
tasks.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Dustin J. Mitchell 2025-02-05 08:20:35 -05:00 committed by GitHub
parent e1fc283da5
commit fdb7e5e020
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 368 additions and 41 deletions

View file

@ -188,6 +188,7 @@ set (pythonTests
undo.test.py
unicode.test.py
unique.test.py
unusual_task.test.py
upgrade.test.py
urgency.test.py
urgency_inherit.test.py
@ -207,6 +208,15 @@ foreach (python_Test ${pythonTests})
)
endforeach(python_Test)
# Create a `make_tc_task` binary, used for unusual_task.test.py. In order to build this
# for the tests, it's added as a dependency of `test_runner`.
add_executable (make_tc_task make_tc_task.cpp)
target_link_libraries (make_tc_task task commands columns libshared task commands columns libshared task commands columns libshared ${TASK_LIBRARIES})
if (DARWIN)
target_link_libraries (make_tc_task "-framework CoreFoundation -framework Security -framework SystemConfiguration")
endif (DARWIN)
add_dependencies(test_runner make_tc_task)
# -- Shell tests
set (shell_SRCS

View file

@ -28,8 +28,11 @@ ON_POSIX = "posix" in sys.builtin_module_names
# Directory relative to basetest module location
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
# From the CMAKE value of the same name. This is substituted at configure.
CMAKE_BINARY_DIR = os.path.abspath("${CMAKE_BINARY_DIR}")
# Location of binary files (usually the src/ folder)
BIN_PREFIX = os.path.abspath(os.path.join("${CMAKE_BINARY_DIR}", "src"))
BIN_PREFIX = os.path.abspath(os.path.join(CMAKE_BINARY_DIR, "src"))
# Default location of test hooks
DEFAULT_HOOK_PATH = os.path.abspath(

80
test/make_tc_task.cpp Normal file
View file

@ -0,0 +1,80 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2025, Dustin J. Mitchell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
// cmake.h include header must come first
#include <CmdInfo.h>
#include <main.h>
#include <stdlib.h>
#include <taskchampion-cpp/lib.h>
#include <test.h>
#include <util.h>
#include <iostream>
#include <limits>
#include "format.h"
namespace {
////////////////////////////////////////////////////////////////////////////////
int usage() {
std::cerr << "USAGE: make_tc_task DATADIR KEY=VALUE ..\n";
return 1;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv) {
if (!--argc) {
return usage();
}
char *datadir = *++argv;
auto replica = tc::new_replica_on_disk(datadir, true);
auto uuid = tc::uuid_v4();
auto operations = tc::new_operations();
auto task = tc::create_task(uuid, operations);
while (--argc) {
std::string arg = *++argv;
size_t eq_idx = arg.find('=');
if (eq_idx == std::string::npos) {
return usage();
}
std::string property = arg.substr(0, eq_idx);
std::string value = arg.substr(eq_idx + 1);
task->update(property, value, operations);
}
replica->commit_operations(std::move(operations));
std::cout << static_cast<std::string>(uuid.to_string()) << "\n";
return 0;
}
////////////////////////////////////////////////////////////////////////////////

202
test/unusual_task.test.py Executable file
View file

@ -0,0 +1,202 @@
#!/usr/bin/env python3
###############################################################################
#
# Copyright 2025 Dustin J. Mitchell
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# https://www.opensource.org/licenses/mit-license.php
#
###############################################################################
import sys
import os
import re
import time
import unittest
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from basetest import Task, TestCase
from basetest.utils import run_cmd_wait, CMAKE_BINARY_DIR
class TestUnusualTasks(TestCase):
def setUp(self):
"""Executed before each test in the class"""
self.t = Task()
self.t.config(
"report.custom-report.columns",
"id,description,entry,start,end,due,scheduled,modified,until",
)
self.t.config("verbose", "nothing")
def make_task(self, **props):
make_tc_task = os.path.abspath(
os.path.join(CMAKE_BINARY_DIR, "test", "make_tc_task")
)
cmd = [make_tc_task, self.t.datadir]
for p, v in props.items():
cmd.append(f"{p}={v}")
_, out, _ = run_cmd_wait(cmd)
return out.strip()
def test_empty_task_info(self):
uuid = self.make_task()
_, out, _ = self.t(f"{uuid} info")
self.assertNotIn("Entered", out)
self.assertNotIn("Waiting", out)
self.assertNotIn("Last modified", out)
self.assertNotIn("Start", out)
self.assertNotIn("End", out)
self.assertNotIn("Due", out)
self.assertNotIn("Until", out)
self.assertRegex(out, r"Status\s+Pending")
def test_modify_empty_task(self):
uuid = self.make_task()
self.t(f"{uuid} modify a description +taggy due:tomorrow")
_, out, _ = self.t(f"{uuid} info")
self.assertRegex(out, r"Description\s+a description")
self.assertRegex(out, r"Tags\s+taggy")
def test_empty_task_recurring(self):
uuid = self.make_task(status="recurring")
_, out, _ = self.t(f"{uuid} info")
self.assertRegex(out, r"Status\s+Recurring")
_, out, _ = self.t(f"{uuid} custom-report")
def test_recurring_invalid_rtype(self):
uuid = self.make_task(
status="recurring", due=str(int(time.time())), rtype="occasional"
)
_, out, _ = self.t(f"{uuid} info")
self.assertRegex(out, r"Status\s+Recurring")
self.assertRegex(out, r"Recurrence type\s+occasional")
_, out, _ = self.t(f"{uuid} custom-report")
def test_recurring_invalid_recur(self):
uuid = self.make_task(
status="recurring",
due=str(int(time.time())),
rtype="periodic",
recur="xxxxx",
)
_, out, _ = self.t(f"{uuid} info")
self.assertRegex(out, r"Status\s+Recurring")
self.assertRegex(out, r"Recurrence type\s+periodic")
_, out, _ = self.t(f"{uuid} custom-report")
def test_recurring_bad_quarters_rtype(self):
uuid = self.make_task(
status="recurring", due=str(int(time.time())), rtype="periodic", recur="9aq"
)
_, out, _ = self.t(f"{uuid} custom-report")
def test_invalid_entry_info(self):
uuid = self.make_task(entry="abcdef")
_, out, _ = self.t(f"{uuid} info")
self.assertNotIn("Entered", out)
def test_invalid_modified_info(self):
uuid = self.make_task(modified="abcdef")
_, out, _ = self.t(f"{uuid} info")
self.assertNotIn(r"Last modified", out)
def test_invalid_start_info(self):
uuid = self.make_task(start="abcdef")
_, out, _ = self.t(f"{uuid} info")
def test_invalid_dates_report(self):
uuid = self.make_task(
wait="wait",
scheduled="scheduled",
start="start",
due="due",
end="end",
until="until",
modified="modified",
)
_, out, _ = self.t(f"{uuid} custom-report")
def test_invalid_dates_stop(self):
uuid = self.make_task(
wait="wait",
scheduled="scheduled",
start="start",
due="due",
end="end",
until="until",
modified="modified",
)
_, out, _ = self.t(f"{uuid} stop")
def test_invalid_dates_modify(self):
uuid = self.make_task(
wait="wait",
scheduled="scheduled",
start="start",
due="due",
end="end",
until="until",
modified="modified",
)
_, out, _ = self.t(f"{uuid} mod a description +tag")
def test_invalid_dates_info(self):
uuid = self.make_task(
wait="wait",
scheduled="scheduled",
start="start",
due="due",
end="end",
until="until",
modified="modified",
)
_, out, _ = self.t(f"{uuid} info")
self.assertNotRegex("^Entered\s+", out)
self.assertNotRegex("^Start\s+", out)
self.assertIn(r"Wait set to 'wait'", out)
self.assertIn(r"Scheduled set to 'scheduled'", out)
self.assertIn(r"Start set to 'start'", out)
self.assertIn(r"Due set to 'due'", out)
self.assertIn(r"End set to 'end'", out)
self.assertIn(r"Until set to 'until'", out)
# (note that 'modified' is not shown in the journal)
def test_invalid_dates_export(self):
uuid = self.make_task(
wait="wait",
scheduled="scheduled",
start="start",
due="due",
end="end",
until="until",
modified="modified",
)
_, out, _ = self.t(f"{uuid} export")
if __name__ == "__main__":
from simpletap import TAPTestRunner
unittest.main(testRunner=TAPTestRunner())
# vim: ai sts=4 et sw=4 ft=python