bonus: logout error if GITHUB_TOKEN is set

This commit is contained in:
vilmibm 2020-08-10 13:01:20 -05:00
parent e6ae0a122b
commit f7ee39dfeb
2 changed files with 18 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
@ -62,7 +63,10 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co
}
func logoutRun(opts *LogoutOptions) error {
// TODO check for GITHUB_TOKEN and error if found
if os.Getenv("GITHUB_TOKEN") != "" {
return errors.New("GITHUB_TOKEN is set in your environment. If you no longer want to use it with gh, please unset it.")
}
isTTY := opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY()
hostname := opts.Hostname

View file

@ -3,6 +3,7 @@ package logout
import (
"bytes"
"net/http"
"os"
"regexp"
"testing"
@ -183,6 +184,7 @@ func Test_logoutRun_nontty(t *testing.T) {
cfgHosts []string
wantHosts string
wantErr *regexp.Regexp
ghtoken string
}{
{
name: "no arguments",
@ -211,10 +213,21 @@ func Test_logoutRun_nontty(t *testing.T) {
},
wantErr: regexp.MustCompile(`not logged in to any hosts`),
},
{
name: "gh token is set",
opts: &LogoutOptions{},
ghtoken: "abc123",
wantErr: regexp.MustCompile(`GITHUB_TOKEN is set in your environment`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ghtoken := os.Getenv("GITHUB_TOKEN")
defer func() {
os.Setenv("GITHUB_TOKEN", ghtoken)
}()
os.Setenv("GITHUB_TOKEN", tt.ghtoken)
io, _, _, stderr := iostreams.Test()
io.SetStdinTTY(false)