@@ -76,27 +76,153 @@ func TestRunningCommand(t *testing.T) {
7676 assert .Error (t , err , "Expected an error when executing a command without a spinner initialized" )
7777 })
7878
79- t .Run ("bash env extraction " , func (t * testing.T ) {
79+ t .Run ("bash env export preserves original environment " , func (t * testing.T ) {
8080 tmpDirs := make (map [string ]string )
81- cfg := & config.Config {MinutesToTimeout : 1 }
81+
82+ // Set up initial environment with some original variables
83+ originalEnv := []string {
84+ "ORIGINAL_VAR=original_value" ,
85+ "PATH=/usr/bin:/bin" ,
86+ "HOME=/home/test" ,
87+ "WORKING_DIR=/test/dir" ,
88+ }
89+
90+ cfg := & config.Config {MinutesToTimeout : 1 , Env : originalEnv }
8291 ui := view .NewView ("mock" )
92+
93+ // Export a new variable using the real bash chunk approach
8394 chunk := ExecutableChunk {
8495 Runtime : "bash" ,
96+ Content : []string {"export NEW_VAR=new_value" },
8597 Context : & runnercontext.Context {
8698 Cfg : cfg ,
8799 RView : ui ,
88100 },
89101 }
90- cmdStr := "bash -c 'echo \" ### ENV ###\" && echo \" TEST_ENV_VAR=test_value\" '"
91- command , err := chunk .AddCommandToExecute (cmdStr , tmpDirs )
92- assert .NoError (t , err , "Expected no error when adding command to execute" )
93- command .IsBash = true
94- err = command .Execute ()
95- assert .NoError (t , err , "Expected no error when executing bash command" )
96- found := slices .ContainsFunc (command .Ctx .Cfg .Env , func (env string ) bool {
97- return env == "TEST_ENV_VAR=test_value"
102+ err := chunk .PrepareForExecution (tmpDirs )
103+ assert .NoError (t , err , "Expected no error when preparing chunk for execution" )
104+
105+ err = chunk .ExecuteSequential ()
106+ assert .NoError (t , err , "Expected no error when executing bash chunk" )
107+
108+ // Verify original environment variables are preserved
109+ foundOriginal := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
110+ return env == "ORIGINAL_VAR=original_value"
111+ })
112+ assert .True (t , foundOriginal , "Expected ORIGINAL_VAR to be preserved" )
113+
114+ foundPath := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
115+ return strings .HasPrefix (env , "PATH=" )
116+ })
117+ assert .True (t , foundPath , "Expected PATH to be preserved" )
118+
119+ foundHome := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
120+ return env == "HOME=/home/test"
121+ })
122+ assert .True (t , foundHome , "Expected HOME to be preserved" )
123+
124+ foundWorkingDir := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
125+ return env == "WORKING_DIR=/test/dir"
126+ })
127+ assert .True (t , foundWorkingDir , "Expected WORKING_DIR to be preserved" )
128+
129+ // Verify new variable was added
130+ foundNew := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
131+ return strings .HasPrefix (env , "NEW_VAR=" )
132+ })
133+ assert .True (t , foundNew , "Expected NEW_VAR to be added" )
134+ })
135+
136+ t .Run ("bash env unset removes variables selectively" , func (t * testing.T ) {
137+ tmpDirs := make (map [string ]string )
138+
139+ // Set up environment with variables to test unset behavior
140+ initialEnv := []string {
141+ "KEEP_VAR=keep_this" ,
142+ "REMOVE_VAR=remove_this" ,
143+ "PATH=/usr/bin:/bin" ,
144+ }
145+
146+ cfg := & config.Config {MinutesToTimeout : 1 , Env : initialEnv }
147+ ui := view .NewView ("mock" )
148+
149+ // Unset one variable while keeping others
150+ chunk := ExecutableChunk {
151+ Runtime : "bash" ,
152+ Content : []string {"unset REMOVE_VAR" },
153+ Context : & runnercontext.Context {
154+ Cfg : cfg ,
155+ RView : ui ,
156+ },
157+ }
158+ err := chunk .PrepareForExecution (tmpDirs )
159+ assert .NoError (t , err , "Expected no error when preparing chunk for execution" )
160+
161+ err = chunk .ExecuteSequential ()
162+ assert .NoError (t , err , "Expected no error when executing bash chunk" )
163+
164+ // Verify the targeted variable was unset
165+ foundRemoved := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
166+ return strings .HasPrefix (env , "REMOVE_VAR=" )
167+ })
168+ assert .False (t , foundRemoved , "Expected REMOVE_VAR to be removed after unset" )
169+
170+ // Verify other variables are still preserved
171+ foundKeep := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
172+ return env == "KEEP_VAR=keep_this"
173+ })
174+ assert .True (t , foundKeep , "Expected KEEP_VAR to be preserved after unset" )
175+
176+ foundPath := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
177+ return strings .HasPrefix (env , "PATH=" )
178+ })
179+ assert .True (t , foundPath , "Expected PATH to be preserved after unset" )
180+ })
181+
182+ t .Run ("bash env unset critical variables" , func (t * testing.T ) {
183+ tmpDirs := make (map [string ]string )
184+
185+ // Set up environment with critical variables
186+ initialEnv := []string {
187+ "HOME=/home/test" ,
188+ "PATH=/usr/bin:/bin" ,
189+ "KEEP_VAR=keep_this" ,
190+ }
191+
192+ cfg := & config.Config {MinutesToTimeout : 1 , Env : initialEnv }
193+ ui := view .NewView ("mock" )
194+
195+ // Unset HOME to verify critical environment variables can be unset
196+ chunk := ExecutableChunk {
197+ Runtime : "bash" ,
198+ Content : []string {"unset HOME" },
199+ Context : & runnercontext.Context {
200+ Cfg : cfg ,
201+ RView : ui ,
202+ },
203+ }
204+ err := chunk .PrepareForExecution (tmpDirs )
205+ assert .NoError (t , err , "Expected no error when preparing chunk for execution" )
206+
207+ err = chunk .ExecuteSequential ()
208+ assert .NoError (t , err , "Expected no error when executing bash chunk" )
209+
210+ // Verify HOME was unset
211+ foundHome := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
212+ return strings .HasPrefix (env , "HOME=" )
213+ })
214+ assert .False (t , foundHome , "Expected HOME to be removed after unset" )
215+
216+ // Verify other variables are still preserved
217+ foundKeep := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
218+ return env == "KEEP_VAR=keep_this"
219+ })
220+ assert .True (t , foundKeep , "Expected KEEP_VAR to be preserved after unsetting HOME" )
221+
222+ foundPath := slices .ContainsFunc (chunk .Context .Cfg .Env , func (env string ) bool {
223+ return strings .HasPrefix (env , "PATH=" )
98224 })
99- assert .True (t , found , "Expected to find TEST_ENV_VAR in the environment variables " )
225+ assert .True (t , foundPath , "Expected PATH to be preserved after unsetting HOME " )
100226 })
101227
102228 t .Run ("kill" , func (t * testing.T ) {
0 commit comments