@@ -490,3 +490,177 @@ func TestResolver_ResolveGitea_TeaConfig(t *testing.T) {
490490 t .Errorf ("Source = %v, want %v" , token .Source , TokenSourceConfig )
491491 }
492492}
493+
494+ func TestResolver_ResolveGitHub_NoToken (t * testing.T ) {
495+ // Clear all env vars
496+ os .Unsetenv ("GITHUB_TOKEN" )
497+ os .Unsetenv ("GH_TOKEN" )
498+
499+ // Set PATH to empty to ensure gh CLI is not available
500+ oldPath := os .Getenv ("PATH" )
501+ defer os .Setenv ("PATH" , oldPath )
502+ os .Setenv ("PATH" , "" )
503+
504+ resolver := NewResolver ()
505+ _ , err := resolver .ResolveGitHub (context .Background ())
506+ if err == nil {
507+ t .Error ("Expected error when no GitHub token available" )
508+ }
509+ }
510+
511+ func TestResolver_ResolveGitLab_NoToken (t * testing.T ) {
512+ // Clear all env vars
513+ os .Unsetenv ("GITLAB_TOKEN" )
514+ os .Unsetenv ("GL_TOKEN" )
515+
516+ // Set PATH to empty to ensure glab CLI is not available
517+ oldPath := os .Getenv ("PATH" )
518+ defer os .Setenv ("PATH" , oldPath )
519+ os .Setenv ("PATH" , "" )
520+
521+ resolver := NewResolver ()
522+ _ , err := resolver .ResolveGitLab (context .Background (), "" )
523+ if err == nil {
524+ t .Error ("Expected error when no GitLab token available" )
525+ }
526+ }
527+
528+ func TestResolver_ResolveGitHub_EmptyEnvVar (t * testing.T ) {
529+ // Set env var to empty string
530+ t .Setenv ("GITHUB_TOKEN" , "" )
531+ t .Setenv ("GH_TOKEN" , "" )
532+
533+ // Set PATH to empty to ensure gh CLI is not available
534+ oldPath := os .Getenv ("PATH" )
535+ defer os .Setenv ("PATH" , oldPath )
536+ os .Setenv ("PATH" , "" )
537+
538+ resolver := NewResolver ()
539+ _ , err := resolver .ResolveGitHub (context .Background ())
540+ if err == nil {
541+ t .Error ("Expected error when GitHub token env var is empty" )
542+ }
543+ }
544+
545+ func TestResolver_ResolveGitLab_EmptyEnvVar (t * testing.T ) {
546+ // Set env var to empty string
547+ t .Setenv ("GITLAB_TOKEN" , "" )
548+ t .Setenv ("GL_TOKEN" , "" )
549+
550+ // Set PATH to empty to ensure glab CLI is not available
551+ oldPath := os .Getenv ("PATH" )
552+ defer os .Setenv ("PATH" , oldPath )
553+ os .Setenv ("PATH" , "" )
554+
555+ resolver := NewResolver ()
556+ _ , err := resolver .ResolveGitLab (context .Background (), "gitlab.com" )
557+ if err == nil {
558+ t .Error ("Expected error when GitLab token env var is empty" )
559+ }
560+ }
561+
562+ func TestResolver_ResolveGitea_EmptyEnvVar (t * testing.T ) {
563+ // Set env vars to empty strings
564+ t .Setenv ("CODEBERG_TOKEN" , "" )
565+ t .Setenv ("GITEA_TOKEN" , "" )
566+
567+ resolver := NewResolver ()
568+ _ , err := resolver .ResolveGitea (context .Background (), "codeberg.org" )
569+ if err == nil {
570+ t .Error ("Expected error when Gitea token env var is empty" )
571+ }
572+ }
573+
574+ func TestResolver_ResolveGitLab_CLISuccess (t * testing.T ) {
575+ // Clear env vars to force CLI path
576+ os .Unsetenv ("GITLAB_TOKEN" )
577+ os .Unsetenv ("GL_TOKEN" )
578+
579+ // Create a mock glab command that returns a token
580+ tempDir := t .TempDir ()
581+ glabScript := filepath .Join (tempDir , "glab" )
582+ scriptContent := `#!/bin/sh
583+ echo "glpat_cli_token"
584+ `
585+ if err := os .WriteFile (glabScript , []byte (scriptContent ), 0o755 ); err != nil {
586+ t .Fatalf ("Failed to create mock glab script: %v" , err )
587+ }
588+
589+ // Prepend our temp dir to PATH
590+ oldPath := os .Getenv ("PATH" )
591+ t .Setenv ("PATH" , tempDir + ":" + oldPath )
592+
593+ resolver := NewResolver ()
594+ token , err := resolver .ResolveGitLab (context .Background (), "gitlab.com" )
595+ if err != nil {
596+ t .Errorf ("Unexpected error: %v" , err )
597+ return
598+ }
599+
600+ if token .Value != "glpat_cli_token" {
601+ t .Errorf ("Token = %q, want %q" , token .Value , "glpat_cli_token" )
602+ }
603+ if token .Source != TokenSourceCLI {
604+ t .Errorf ("Source = %v, want %v" , token .Source , TokenSourceCLI )
605+ }
606+ }
607+
608+ func TestResolver_ResolveGitHub_CLISuccess (t * testing.T ) {
609+ // Clear env vars to force CLI path
610+ os .Unsetenv ("GITHUB_TOKEN" )
611+ os .Unsetenv ("GH_TOKEN" )
612+
613+ // Create a mock gh command that returns a token
614+ tempDir := t .TempDir ()
615+ ghScript := filepath .Join (tempDir , "gh" )
616+ scriptContent := `#!/bin/sh
617+ echo "ghp_cli_token"
618+ `
619+ if err := os .WriteFile (ghScript , []byte (scriptContent ), 0o755 ); err != nil {
620+ t .Fatalf ("Failed to create mock gh script: %v" , err )
621+ }
622+
623+ // Prepend our temp dir to PATH
624+ oldPath := os .Getenv ("PATH" )
625+ t .Setenv ("PATH" , tempDir + ":" + oldPath )
626+
627+ resolver := NewResolver ()
628+ token , err := resolver .ResolveGitHub (context .Background ())
629+ if err != nil {
630+ t .Errorf ("Unexpected error: %v" , err )
631+ return
632+ }
633+
634+ if token .Value != "ghp_cli_token" {
635+ t .Errorf ("Token = %q, want %q" , token .Value , "ghp_cli_token" )
636+ }
637+ if token .Source != TokenSourceCLI {
638+ t .Errorf ("Source = %v, want %v" , token .Source , TokenSourceCLI )
639+ }
640+ }
641+
642+ func TestResolver_ResolveGitLab_CLIEmptyToken (t * testing.T ) {
643+ // Clear env vars to force CLI path
644+ os .Unsetenv ("GITLAB_TOKEN" )
645+ os .Unsetenv ("GL_TOKEN" )
646+
647+ // Create a mock glab command that returns empty
648+ tempDir := t .TempDir ()
649+ glabScript := filepath .Join (tempDir , "glab" )
650+ scriptContent := `#!/bin/sh
651+ echo ""
652+ `
653+ if err := os .WriteFile (glabScript , []byte (scriptContent ), 0o755 ); err != nil {
654+ t .Fatalf ("Failed to create mock glab script: %v" , err )
655+ }
656+
657+ // Prepend our temp dir to PATH
658+ oldPath := os .Getenv ("PATH" )
659+ t .Setenv ("PATH" , tempDir + ":" + oldPath )
660+
661+ resolver := NewResolver ()
662+ _ , err := resolver .ResolveGitLab (context .Background (), "gitlab.com" )
663+ if err == nil {
664+ t .Error ("Expected error when glab returns empty token" )
665+ }
666+ }
0 commit comments