File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2121from .exceptions import CollectionNotFound , Exit , ParseError , UnexpectedExit
2222from .parser import Argument , Parser , ParserContext
2323from .terminals import pty_size
24- from .util import debug , enable_logging , helpline
24+ from .util import debug , enable_logging , helpline , isatty
2525
2626if 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments