Skip to content

Commit 8f1a8e0

Browse files
committed
fix: only warn interactively when running as root
1 parent 40e04de commit 8f1a8e0

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

invoke/program.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .exceptions import CollectionNotFound, Exit, ParseError, UnexpectedExit
2222
from .parser import Argument, Parser, ParserContext
2323
from .terminals import pty_size
24-
from .util import debug, enable_logging, helpline
24+
from .util import debug, enable_logging, helpline, isatty
2525

2626
if TYPE_CHECKING:
2727
from .loader import Loader
@@ -430,7 +430,7 @@ def warn_if_running_as_root(self, is_testing: bool = False) -> None:
430430
"""
431431
Emit a warning when Invoke is executed as the root user.
432432
"""
433-
if is_testing or not self.running_as_root():
433+
if is_testing or not isatty(sys.stderr) or not self.running_as_root():
434434
return
435435
print(self.root_warning, file=sys.stderr)
436436

tests/program.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,26 @@ class root_warning:
110110
def prints_warning_to_tty_stderr(self):
111111
program = Program()
112112
with patch.object(program, "running_as_root", return_value=True):
113+
sys.stderr.isatty = Mock(return_value=True)
113114
program.warn_if_running_as_root(is_testing=False)
114115
assert (
115116
sys.stderr.getvalue()
116117
== "WARNING: Running Invoke as root may create root-owned files and cause later I/O or permission errors. Re-run as a non-root user.\n"
117118
)
118119

120+
@trap
121+
def does_not_warn_when_stderr_is_not_a_tty(self):
122+
program = Program()
123+
with patch.object(program, "running_as_root", return_value=True):
124+
sys.stderr.isatty = Mock(return_value=False)
125+
program.warn_if_running_as_root(is_testing=False)
126+
assert sys.stderr.getvalue() == ""
127+
119128
@trap
120129
def skips_warning_for_exit_false(self):
121130
program = Program()
122131
with patch.object(program, "running_as_root", return_value=True):
132+
sys.stderr.isatty = Mock(return_value=True)
123133
program.warn_if_running_as_root(is_testing=True)
124134
assert sys.stderr.getvalue() == ""
125135

0 commit comments

Comments
 (0)