1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3###############################################################################
4#
5# Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24#
25# https://www.opensource.org/licenses/mit-license.php
26#
27###############################################################################
28
29import sys
30import os
31import unittest
32# Ensure python finds the local simpletap module
33sys.path.append(os.path.dirname(os.path.abspath(__file__)))
34
35from basetest import Task, TestCase
36from basetest.exceptions import CommandError
37
38
39class TestDelete(TestCase):
40    def setUp(self):
41        self.t = Task()
42
43    def test_add_delete_undo(self):
44        """Verify that add/delete/undo yields a Pending task"""
45        self.t("add one")
46        code, out, err = self.t("_get 1.uuid")
47        uuid = out.strip()
48
49        self.t("1 delete", input="y\n")
50        self.t.runError("list") # GC/handleRecurrence
51        code, out, err = self.t("_get 1.status")
52        self.assertEqual("\n", out)
53
54        code, out, err = self.t("_get %s.status" % uuid)
55        self.assertIn("deleted\n", out)
56
57        self.t("undo", input="y\n")
58        code, out, err = self.t("_get 1.status")
59        self.assertIn("pending\n", out)
60        code, out, err = self.t("_get %s.status" % uuid)
61        self.assertIn("pending\n", out)
62
63    def test_delete_en_passant(self):
64        """Verify that en-passant works with delete"""
65        self.t("add foo")
66        code, out, err = self.t("1 delete project:work", input="y\n")
67        self.assertIn("Deleted 1 task.", out)
68
69        code, out, err = self.t("all rc.verbose:nothing")
70        self.assertIn("work", out)
71
72    def test_add_done_delete(self):
73        """Verify that a completed task can be deleted"""
74        self.t("add foo")
75        code, out, err = self.t("_get 1.uuid")
76        uuid = out.strip()
77
78        code, out, err = self.t("1 done")
79        self.assertIn("Completed 1 task.", out)
80
81        self.t("all") # GC/handleRecurrence
82
83        code, out, err = self.t("%s delete" % uuid, input="y\n")
84        self.assertIn("Deleted 1 task.", out)
85
86        code, out, err = self.t("_get %s.status" % uuid)
87        self.assertIn("deleted\n", out)
88
89    def test_delete_single_prompt_loop(self):
90        """Delete prompt with closed STDIN causes infinite loop and floods stdout (single)"""
91        self.t("add foo1")
92
93        # Would expect 1 yes via input none sent
94        code, out, err = self._validate_prompt_loop(input="")
95
96        self.assertIn("Task not deleted", out)
97        self.assertIn("Deleted 0 tasks", out)
98
99        # If command was aborted, exit code should be 1
100        self.assertEqual(code, 1)
101
102    def test_delete_multiple_prompt_loop(self):
103        """Delete prompt with closed STDIN causes infinite loop and floods stdout (multiple)"""
104        self.t("add foo1")
105        self.t("add foo2")
106
107        # Would expect 2 yes via input only 1 sent
108        code, out, err = self._validate_prompt_loop(input="y\n")
109        self.assertEqual(code, 1)
110
111    def test_delete_bulk_prompt_loop(self):
112        """Delete prompt with closed STDIN causes infinite loop and floods stdout (bulk)"""
113        self.t.config("confirmation", "off")
114        # bulk is applied when confirmation is disabled
115        self.t.config("bulk", "2")
116
117        self.t("add foo1")
118        self.t("add foo2")
119        self.t("add foo3")
120
121        # Would expect 3 yes via input only 2 sent
122        code, out, err = self._validate_prompt_loop(input="y\ny\n")
123
124        self.assertEqual(code, 1)
125
126    def _validate_prompt_loop(self, input=""):
127        """Helper method to check if task flooded stream on closed STDIN"""
128        try:
129            code, out, err = self.t("/foo[1-3]/ delete", input=input, timeout=0.3)
130        except CommandError as e:
131            # If delete fails with a timeout, don't fail the test immediately
132            code, out, err = e.code, e.out, e.err
133
134        # If task fails to notice STDIN is closed it will loop and flood for
135        # confirmation until timeout
136        # 500 bytes is arbitrary. Shouldn't reach this value in normal execution
137        self.assertLessEqual(len(out), 500)
138        self.assertLessEqual(len(err), 500)
139
140        return code, out, err
141
142
143if __name__ == "__main__":
144    from simpletap import TAPTestRunner
145    unittest.main(testRunner=TAPTestRunner())
146
147# vim: ai sts=4 et sw=4 ft=python
148