@@ -17,6 +17,7 @@ import (
1717 "go.jetify.com/devbox/internal/devbox/devopt"
1818 "go.jetify.com/devbox/internal/envir"
1919 "go.jetify.com/devbox/internal/shellgen"
20+ "go.jetify.com/devbox/internal/xdg"
2021)
2122
2223// updateFlag overwrites golden files with the new test results.
@@ -72,6 +73,10 @@ func testWriteDevboxShellrc(t *testing.T, testdirs []string) {
7273 projectDir : "/path/to/projectDir" ,
7374 userShellrcPath : test .shellrcPath ,
7475 }
76+ // Set shell name based on test name for zsh tests
77+ if strings .Contains (test .name , "zsh" ) {
78+ s .name = shZsh
79+ }
7580 gotPath , err := s .writeDevboxShellrc ()
7681 if err != nil {
7782 t .Fatal ("Got writeDevboxShellrc error:" , err )
@@ -165,3 +170,239 @@ func TestShellPath(t *testing.T) {
165170 })
166171 }
167172}
173+
174+ func TestInitShellBinaryFields (t * testing.T ) {
175+ tests := []struct {
176+ name string
177+ path string
178+ env map [string ]string
179+ expectedName name
180+ expectedRcPath string
181+ expectedRcPathBase string
182+ }{
183+ {
184+ name : "bash shell" ,
185+ path : "/usr/bin/bash" ,
186+ expectedName : shBash ,
187+ expectedRcPathBase : ".bashrc" ,
188+ },
189+ {
190+ name : "zsh shell without ZDOTDIR" ,
191+ path : "/usr/bin/zsh" ,
192+ expectedName : shZsh ,
193+ expectedRcPathBase : ".zshrc" ,
194+ },
195+ {
196+ name : "zsh shell with ZDOTDIR" ,
197+ path : "/usr/bin/zsh" ,
198+ env : map [string ]string {
199+ "ZDOTDIR" : "/custom/zsh/config" ,
200+ },
201+ expectedName : shZsh ,
202+ expectedRcPath : "/custom/zsh/config/.zshrc" ,
203+ },
204+ {
205+ name : "ksh shell" ,
206+ path : "/usr/bin/ksh" ,
207+ expectedName : shKsh ,
208+ expectedRcPathBase : ".kshrc" ,
209+ },
210+ {
211+ name : "fish shell" ,
212+ path : "/usr/bin/fish" ,
213+ expectedName : shFish ,
214+ expectedRcPath : xdg .ConfigSubpath ("fish/config.fish" ),
215+ },
216+ {
217+ name : "dash shell" ,
218+ path : "/usr/bin/dash" ,
219+ expectedName : shPosix ,
220+ expectedRcPath : ".shinit" ,
221+ },
222+ {
223+ name : "unknown shell" ,
224+ path : "/usr/bin/unknown" ,
225+ expectedName : shUnknown ,
226+ expectedRcPathBase : "" ,
227+ },
228+ }
229+
230+ for _ , test := range tests {
231+ t .Run (test .name , func (t * testing.T ) {
232+ // Set up environment variables
233+ for k , v := range test .env {
234+ t .Setenv (k , v )
235+ }
236+
237+ shell := initShellBinaryFields (test .path )
238+
239+ if shell .name != test .expectedName {
240+ t .Errorf ("Expected shell name %v, got %v" , test .expectedName , shell .name )
241+ }
242+
243+ if test .expectedRcPath != "" {
244+ if shell .userShellrcPath != test .expectedRcPath {
245+ t .Errorf ("Expected rc path %s, got %s" , test .expectedRcPath , shell .userShellrcPath )
246+ }
247+ } else if test .expectedRcPathBase != "" {
248+ // For tests that expect a path relative to home directory,
249+ // check that the path ends with the expected basename
250+ expectedBasename := test .expectedRcPathBase
251+ actualBasename := filepath .Base (shell .userShellrcPath )
252+ if actualBasename != expectedBasename {
253+ t .Errorf ("Expected rc path basename %s, got %s (full path: %s)" , expectedBasename , actualBasename , shell .userShellrcPath )
254+ }
255+ }
256+ })
257+ }
258+ }
259+
260+ func TestSetupShellStartupFiles (t * testing.T ) {
261+ tmpDir := t .TempDir ()
262+
263+ // Create a mock zsh shell
264+ shell := & DevboxShell {
265+ name : shZsh ,
266+ userShellrcPath : filepath .Join (tmpDir , ".zshrc" ),
267+ }
268+
269+ // Create some test zsh startup files
270+ startupFiles := []string {".zshenv" , ".zprofile" , ".zlogin" , ".zlogout" , ".zimrc" }
271+ for _ , filename := range startupFiles {
272+ filePath := filepath .Join (tmpDir , filename )
273+ err := os .WriteFile (filePath , []byte ("# Test content for " + filename ), 0o644 )
274+ if err != nil {
275+ t .Fatalf ("Failed to create test file %s: %v" , filename , err )
276+ }
277+ }
278+
279+ // Create a temporary directory for shell settings
280+ shellSettingsDir := t .TempDir ()
281+
282+ // Call setupShellStartupFiles
283+ shell .setupShellStartupFiles (shellSettingsDir )
284+
285+ // Check that all startup files were created in the shell settings directory
286+ for _ , filename := range startupFiles {
287+ expectedPath := filepath .Join (shellSettingsDir , filename )
288+ _ , err := os .Stat (expectedPath )
289+ if err != nil {
290+ t .Errorf ("Expected startup file %s to be created, but got error: %v" , filename , err )
291+ }
292+
293+ // Check that the file contains the expected template content
294+ content , err := os .ReadFile (expectedPath )
295+ if err != nil {
296+ t .Errorf ("Failed to read created file %s: %v" , filename , err )
297+ continue
298+ }
299+
300+ contentStr := string (content )
301+ expectedOldPath := filepath .Join (tmpDir , filename )
302+ if ! strings .Contains (contentStr , expectedOldPath ) {
303+ t .Errorf ("Expected file %s to contain path %s, but content was: %s" , filename , expectedOldPath , contentStr )
304+ }
305+
306+ if ! strings .Contains (contentStr , "DEVBOX_ZDOTDIR" ) {
307+ t .Errorf ("Expected file %s to contain ZDOTDIR handling, but content was: %s" , filename , contentStr )
308+ }
309+ }
310+ }
311+
312+ func TestWriteDevboxShellrcBash (t * testing.T ) {
313+ tmpDir := t .TempDir ()
314+
315+ // Create a test bash rc file
316+ bashrcPath := filepath .Join (tmpDir , ".bashrc" )
317+ bashrcContent := "# Test bash configuration\n export TEST_VAR=value"
318+ err := os .WriteFile (bashrcPath , []byte (bashrcContent ), 0o644 )
319+ if err != nil {
320+ t .Fatalf ("Failed to create test .bashrc: %v" , err )
321+ }
322+
323+ // Create a mock devbox
324+ devbox := & Devbox {projectDir : "/test/project" }
325+
326+ // Create a bash shell
327+ shell := & DevboxShell {
328+ devbox : devbox ,
329+ name : shBash ,
330+ userShellrcPath : bashrcPath ,
331+ projectDir : "/test/project" ,
332+ env : map [string ]string {"TEST_ENV" : "test_value" },
333+ }
334+
335+ // Write the devbox shellrc
336+ shellrcPath , err := shell .writeDevboxShellrc ()
337+ if err != nil {
338+ t .Fatalf ("Failed to write devbox shellrc: %v" , err )
339+ }
340+
341+ // Read and verify the content
342+ content , err := os .ReadFile (shellrcPath )
343+ if err != nil {
344+ t .Fatalf ("Failed to read generated shellrc: %v" , err )
345+ }
346+
347+ contentStr := string (content )
348+
349+ // Check that it does NOT contain zsh-specific ZDOTDIR handling
350+ if strings .Contains (contentStr , "DEVBOX_ZDOTDIR" ) {
351+ t .Error ("Expected shellrc to NOT contain ZDOTDIR handling for bash" )
352+ }
353+
354+ // Check that it sources the original .bashrc
355+ if ! strings .Contains (contentStr , bashrcPath ) {
356+ t .Error ("Expected shellrc to source the original .bashrc file" )
357+ }
358+ }
359+
360+ func TestWriteDevboxShellrcWithZDOTDIR (t * testing.T ) {
361+ tmpDir := t .TempDir ()
362+
363+ // Set up ZDOTDIR environment
364+ t .Setenv ("ZDOTDIR" , tmpDir )
365+
366+ // Create a test zsh rc file in the custom ZDOTDIR
367+ customZshrcPath := filepath .Join (tmpDir , ".zshrc" )
368+ zshrcContent := "# Custom zsh configuration\n export CUSTOM_VAR=value"
369+ err := os .WriteFile (customZshrcPath , []byte (zshrcContent ), 0o644 )
370+ if err != nil {
371+ t .Fatalf ("Failed to create test .zshrc: %v" , err )
372+ }
373+
374+ // Create a mock devbox
375+ devbox := & Devbox {projectDir : "/test/project" }
376+
377+ // Create a zsh shell - this should pick up the ZDOTDIR
378+ shell := initShellBinaryFields ("/usr/bin/zsh" )
379+ shell .devbox = devbox
380+ shell .projectDir = "/test/project"
381+
382+ if shell .userShellrcPath != customZshrcPath {
383+ t .Error ("Expected shellrc path to respect ZDOTDIR" )
384+ }
385+
386+ // Write the devbox shellrc
387+ shellrcPath , err := shell .writeDevboxShellrc ()
388+ if err != nil {
389+ t .Fatalf ("Failed to write devbox shellrc: %v" , err )
390+ }
391+
392+ // Read and verify the content
393+ content , err := os .ReadFile (shellrcPath )
394+ if err != nil {
395+ t .Fatalf ("Failed to read generated shellrc: %v" , err )
396+ }
397+
398+ contentStr := string (content )
399+ // Check that it contains zsh-specific ZDOTDIR handling
400+ if ! strings .Contains (contentStr , "DEVBOX_ZDOTDIR" ) {
401+ t .Error ("Expected shellrc to contain ZDOTDIR handling for zsh" )
402+ }
403+
404+ // Check that it sources the custom .zshrc
405+ if ! strings .Contains (contentStr , customZshrcPath ) {
406+ t .Error ("Expected shellrc to source the custom .zshrc file" )
407+ }
408+ }
0 commit comments