Skip to content

Commit 66adf32

Browse files
committed
Add test for color hint
1 parent 05763fd commit 66adf32

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

test/color_hint.t

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python3
2+
3+
###############################################################################
4+
#
5+
# Copyright 2025, Gothenburg Bit Factory.
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://opensource.org/license/mit
26+
#
27+
###############################################################################
28+
29+
import os
30+
import sys
31+
import unittest
32+
33+
# Ensure python finds the local simpletap module
34+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
35+
36+
from basetest import Timew, TestCase
37+
38+
39+
class TestColorHint(TestCase):
40+
def setUp(self):
41+
"""Executed before each test in the class"""
42+
self.t = Timew()
43+
44+
def test_color_hint_overrides_config_and_works_when_piped(self):
45+
""":color hint should override color=off config and work with piped output"""
46+
# Set color to off in config file
47+
self.t.config("color", "off")
48+
49+
# Track some time
50+
self.t("start 1h ago foo")
51+
self.t("stop")
52+
53+
# Test with day command - output is naturally piped in test framework
54+
code, out, err = self.t(":color day")
55+
self.assertIn("\033[", out, "Expected colors with :color hint for day command")
56+
57+
# Test with summary command
58+
code, out, err = self.t(":color summary")
59+
self.assertIn("\033[", out, "Expected colors with :color hint for summary command")
60+
61+
# Test with tags command
62+
code, out, err = self.t(":color tags")
63+
self.assertIn("\033[", out, "Expected colors with :color hint for tags command")
64+
65+
def test_nocolor_hint_disables_color(self):
66+
""":nocolor hint should disable color output"""
67+
# Set color to on in config file
68+
self.t.config("color", "on")
69+
70+
# Track some time
71+
self.t("start 1h ago bar")
72+
self.t("stop")
73+
74+
# Use :nocolor hint
75+
code, out, err = self.t(":nocolor day")
76+
77+
# Check that there are no ANSI color codes
78+
self.assertNotIn("\033[", out, "Expected no ANSI color codes when using :nocolor hint")
79+
80+
def test_no_color_by_default_when_piped(self):
81+
"""By default, no color should be used when output is piped"""
82+
# Don't set any color config (should default to off for pipes)
83+
84+
# Track some time
85+
self.t("start 2h ago qux")
86+
self.t("stop 1h ago")
87+
88+
# Without :color hint, output should not have colors when piped
89+
# (test framework pipes output by default)
90+
code, out, err = self.t("day")
91+
self.assertNotIn("\033[", out, "Expected no colors by default when piped")
92+
93+
code, out, err = self.t("summary")
94+
self.assertNotIn("\033[", out, "Expected no colors in summary by default when piped")
95+
96+
97+
if __name__ == "__main__":
98+
from simpletap import TAPTestRunner
99+
unittest.main(testRunner=TAPTestRunner())

0 commit comments

Comments
 (0)