@@ -253,6 +253,9 @@ func TestGetSecretKeys(t *testing.T) {
253253 t .Run ("AllKeys" , func (t * testing.T ) {
254254 keys := GetSecretKeys (vault , []string {})
255255 assert .Equal (t , 3 , len (keys ))
256+ for i := 1 ; i < len (keys ); i ++ {
257+ assert .Assert (t , keys [i - 1 ] < keys [i ], "keys should be sorted alphabetically" )
258+ }
256259 })
257260
258261 t .Run ("SpecificKeys" , func (t * testing.T ) {
@@ -503,3 +506,116 @@ func TestProcessEnvSecret(t *testing.T) {
503506 assert .Equal (t , 1 , len (lines ))
504507 })
505508}
509+
510+ func TestDeterministicOrdering (t * testing.T ) {
511+ t .Run ("GetSecretKeysReturnsSorted" , func (t * testing.T ) {
512+ vault := & Vault {
513+ Secrets : map [string ]VaultSecret {
514+ "zebra" : {},
515+ "alpha" : {},
516+ "charlie" : {},
517+ "bravo" : {},
518+ },
519+ }
520+
521+ keys := GetSecretKeys (vault , []string {})
522+ assert .Equal (t , 4 , len (keys ))
523+ assert .Equal (t , "alpha" , keys [0 ])
524+ assert .Equal (t , "bravo" , keys [1 ])
525+ assert .Equal (t , "charlie" , keys [2 ])
526+ assert .Equal (t , "zebra" , keys [3 ])
527+ })
528+
529+ t .Run ("MultipleRunsProduceSameOrder" , func (t * testing.T ) {
530+ vault := & Vault {
531+ Secrets : map [string ]VaultSecret {
532+ "secret3" : {},
533+ "secret1" : {},
534+ "secret2" : {},
535+ },
536+ }
537+
538+ firstRun := GetSecretKeys (vault , []string {})
539+ secondRun := GetSecretKeys (vault , []string {})
540+ thirdRun := GetSecretKeys (vault , []string {})
541+
542+ assert .DeepEqual (t , firstRun , secondRun )
543+ assert .DeepEqual (t , secondRun , thirdRun )
544+ })
545+
546+ t .Run ("RequestedKeysPreserveOrder" , func (t * testing.T ) {
547+ vault := & Vault {
548+ Secrets : map [string ]VaultSecret {
549+ "zebra" : {},
550+ "alpha" : {},
551+ "charlie" : {},
552+ },
553+ }
554+
555+ requested := []string {"zebra" , "alpha" }
556+ keys := GetSecretKeys (vault , requested )
557+ assert .DeepEqual (t , requested , keys )
558+ })
559+ }
560+
561+ func TestPerSecretForce (t * testing.T ) {
562+ t .Run ("SecretForceOverwritesEnv" , func (t * testing.T ) {
563+ tmpDir := t .TempDir ()
564+ envFile := filepath .Join (tmpDir , ".zshenv" )
565+ t .Setenv ("HOME" , tmpDir )
566+
567+ existingContent := `export TEST_VAR="old_value"
568+ `
569+ err := os .WriteFile (envFile , []byte (existingContent ), 0644 )
570+ assert .NilError (t , err )
571+
572+ err = ProcessEnvSecret ("TEST_VAR" , []byte ("new_value" ), true )
573+ assert .NilError (t , err )
574+
575+ content , err := os .ReadFile (envFile )
576+ assert .NilError (t , err )
577+
578+ contentStr := string (content )
579+ assert .Assert (t , strings .Contains (contentStr , `export TEST_VAR="new_value"` ))
580+ assert .Assert (t , ! strings .Contains (contentStr , "old_value" ))
581+ })
582+
583+ t .Run ("SecretWithoutForceFailsOnExisting" , func (t * testing.T ) {
584+ tmpDir := t .TempDir ()
585+ envFile := filepath .Join (tmpDir , ".zshenv" )
586+ t .Setenv ("HOME" , tmpDir )
587+
588+ existingContent := `export TEST_VAR="old_value"
589+ `
590+ err := os .WriteFile (envFile , []byte (existingContent ), 0644 )
591+ assert .NilError (t , err )
592+
593+ err = ProcessEnvSecret ("TEST_VAR" , []byte ("new_value" ), false )
594+ assert .ErrorContains (t , err , `environment variable "TEST_VAR" already exists, use --force to overwrite` )
595+ })
596+
597+ t .Run ("MixedForceInVault" , func (t * testing.T ) {
598+ tmpDir := t .TempDir ()
599+ envFile := filepath .Join (tmpDir , ".zshenv" )
600+ t .Setenv ("HOME" , tmpDir )
601+
602+ existingContent := `export VAR1="old1"
603+ export VAR2="old2"
604+ `
605+ err := os .WriteFile (envFile , []byte (existingContent ), 0644 )
606+ assert .NilError (t , err )
607+
608+ err = ProcessEnvSecret ("VAR1" , []byte ("new1" ), true )
609+ assert .NilError (t , err )
610+
611+ err = ProcessEnvSecret ("VAR2" , []byte ("new2" ), false )
612+ assert .ErrorContains (t , err , `environment variable "VAR2" already exists, use --force to overwrite` )
613+
614+ content , err := os .ReadFile (envFile )
615+ assert .NilError (t , err )
616+
617+ contentStr := string (content )
618+ assert .Assert (t , strings .Contains (contentStr , `export VAR1="new1"` ))
619+ assert .Assert (t , strings .Contains (contentStr , `export VAR2="old2"` ))
620+ })
621+ }
0 commit comments