This repository was archived by the owner on Jan 2, 2026. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed
Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -296,5 +296,12 @@ func execCommand(cmd *cobra.Command, args []string) error {
296296}
297297
298298func makeEnv (cmd * cobra.Command ) []string {
299- return append (os .Environ (), fmt .Sprintf ("SD_ALIAS=%s" , cmd .Root ().Use ))
299+ out := os .Environ ()
300+ out = append (out , fmt .Sprintf ("SD_ALIAS=%s" , cmd .Root ().Use ))
301+
302+ if debug , _ := cmd .Root ().PersistentFlags ().GetBool ("debug" ); debug {
303+ out = append (out , "DEBUG=true" )
304+ }
305+
306+ return out
300307}
Original file line number Diff line number Diff line change @@ -412,3 +412,38 @@ func TestRunCompletionsZsh(t *testing.T) {
412412 assert .NoError (t , err )
413413 assert .Contains (t , out , "#compdef sd" )
414414}
415+
416+ func TestMakeEnv (t * testing.T ) {
417+ t .Run ("sets SD_ALIAS" , func (t * testing.T ) {
418+ t .Run ("when not aliased" , func (t * testing.T ) {
419+ sd := New ("1.0" ).(* sd )
420+ env := makeEnv (sd .root )
421+ assert .Equal (t , "SD_ALIAS=sd" , env [len (env )- 1 ])
422+ })
423+
424+ t .Run ("when aliased" , func (t * testing.T ) {
425+ var restore []string
426+ copy (restore , os .Args )
427+ defer func () {
428+ copy (os .Args , restore )
429+ }()
430+
431+ os .Args = []string {"-a" , "foo" }
432+
433+ sd := New ("1.0" ).(* sd )
434+ env := makeEnv (sd .root )
435+ assert .Equal (t , "SD_ALIAS=foo" , env [len (env )- 1 ])
436+ })
437+ })
438+
439+ t .Run ("sets DEBUG" , func (t * testing.T ) {
440+ root := & cobra.Command {}
441+ root .PersistentFlags ().Bool ("debug" , false , "Toggle debugging" )
442+ root .PersistentFlags ().Set ("debug" , "true" )
443+ child := & cobra.Command {}
444+ root .AddCommand (child )
445+
446+ env := makeEnv (child )
447+ assert .Equal (t , "DEBUG=true" , env [len (env )- 1 ])
448+ })
449+ }
You can’t perform that action at this time.
0 commit comments