Do not auto-create .taskrc when stdout is not a TTY (#3888)

* Do not auto-create .taskrc when stdout is not a TTY

This avoids prompting or automatically creating such a file, both of
which are unexpected when performing command-line completion.

Fixes #3751.

* Test case for taskrc creation no longer works

A taskrc is only created when stdout is a tty, which would require
allocating a pty, which is very platform-dependent and definitely not
worth the trouble for this test.
This commit is contained in:
Dustin J. Mitchell 2025-06-02 07:59:05 -04:00 committed by GitHub
parent f6824e90a1
commit 75d351afad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View file

@ -1173,6 +1173,13 @@ void Context::staticInitialization() {
void Context::createDefaultConfig() { void Context::createDefaultConfig() {
// Do we need to create a default rc? // Do we need to create a default rc?
if (rc_file._data != "" && !rc_file.exists()) { if (rc_file._data != "" && !rc_file.exists()) {
// If stdout is not a file, we are probably executing in a completion context and should not
// prompt (as the user won't see it) or modify the config (as completion functions are typically
// read-only).
if (!isatty(STDOUT_FILENO)) {
throw std::string("Cannot proceed without rc file.");
}
if (config.getBoolean("confirmation") && if (config.getBoolean("confirmation") &&
!confirm(format("A configuration file could not be found in {1}\n\nWould you like a sample " !confirm(format("A configuration file could not be found in {1}\n\nWould you like a sample "
"{2} created, so Taskwarrior can proceed?", "{2} created, so Taskwarrior can proceed?",

View file

@ -40,6 +40,7 @@ class TestTaskrc(TestCase):
"""Executed before each test in the class""" """Executed before each test in the class"""
self.t = Task() self.t = Task()
@unittest.skip("taskrc generation requires a tty - see #3751")
def test_default_taskrc(self): def test_default_taskrc(self):
"""Verify that a default .taskrc is generated""" """Verify that a default .taskrc is generated"""
os.remove(self.t.taskrc) os.remove(self.t.taskrc)