@@ -60,6 +60,68 @@ def __init__(self, command, text=True):
6060 assert exc_info .value .exit_code == 1
6161
6262
63+ def test_run_and_monitor_uses_offline_tracker_when_offline_mode (monkeypatch ):
64+ captured = {}
65+
66+ class FakeOfflineTracker (FakeTracker ):
67+ def __init__ (self , ** kwargs ):
68+ captured ["kwargs" ] = kwargs
69+ super ().__init__ ()
70+
71+ class FakePopen :
72+ def __init__ (self , command , text = True ):
73+ pass
74+
75+ def wait (self ):
76+ return 0
77+
78+ monkeypatch .setattr (monitor_module , "OfflineEmissionsTracker" , FakeOfflineTracker )
79+ monkeypatch .setattr (monitor_module , "EmissionsTracker" , FakeTracker )
80+ monkeypatch .setattr (monitor_module .subprocess , "Popen" , FakePopen )
81+ monkeypatch .setattr (monitor_module , "print" , lambda * args , ** kwargs : None )
82+
83+ with pytest .raises (typer .Exit ) as exc_info :
84+ monitor_module .run_and_monitor (
85+ SimpleNamespace (args = ["echo" , "hi" ]),
86+ offline = True ,
87+ country_iso_code = "FRA" ,
88+ )
89+
90+ assert exc_info .value .exit_code == 0
91+ assert captured ["kwargs" ]["country_iso_code" ] == "FRA"
92+
93+
94+ def test_run_and_monitor_uses_online_tracker_by_default (monkeypatch ):
95+ captured = {}
96+
97+ class FakeOnlineTracker (FakeTracker ):
98+ def __init__ (self , ** kwargs ):
99+ captured ["kwargs" ] = kwargs
100+ super ().__init__ ()
101+
102+ class FakePopen :
103+ def __init__ (self , command , text = True ):
104+ pass
105+
106+ def wait (self ):
107+ return 0
108+
109+ monkeypatch .setattr (monitor_module , "EmissionsTracker" , FakeOnlineTracker )
110+ monkeypatch .setattr (monitor_module , "OfflineEmissionsTracker" , FakeTracker )
111+ monkeypatch .setattr (monitor_module .subprocess , "Popen" , FakePopen )
112+ monkeypatch .setattr (monitor_module , "print" , lambda * args , ** kwargs : None )
113+
114+ with pytest .raises (typer .Exit ) as exc_info :
115+ monitor_module .run_and_monitor (
116+ SimpleNamespace (args = ["echo" , "hi" ]),
117+ save_to_api = True ,
118+ )
119+
120+ assert exc_info .value .exit_code == 0
121+ assert captured ["kwargs" ]["tracking_mode" ] == "process"
122+ assert captured ["kwargs" ]["save_to_api" ] is True
123+
124+
63125def test_run_and_monitor_handles_keyboard_interrupt (monkeypatch ):
64126 process_info = {"terminated" : 0 , "killed" : 0 }
65127
0 commit comments