@@ -3,9 +3,11 @@ package login
33import (
44 "bytes"
55 "net/http"
6+ "os"
67 "regexp"
78 "testing"
89
10+ "github.com/MakeNowJust/heredoc"
911 "github.com/cli/cli/api"
1012 "github.com/cli/cli/internal/config"
1113 "github.com/cli/cli/pkg/cmd/auth/shared"
@@ -189,18 +191,23 @@ func Test_NewCmdLogin(t *testing.T) {
189191
190192func Test_loginRun_nontty (t * testing.T ) {
191193 tests := []struct {
192- name string
193- opts * LoginOptions
194- httpStubs func (* httpmock.Registry )
195- wantHosts string
196- wantErr string
194+ name string
195+ opts * LoginOptions
196+ httpStubs func (* httpmock.Registry )
197+ env map [string ]string
198+ wantHosts string
199+ wantErr string
200+ wantStderr string
197201 }{
198202 {
199203 name : "with token" ,
200204 opts : & LoginOptions {
201205 Hostname : "github.com" ,
202206 Token : "abc123" ,
203207 },
208+ httpStubs : func (reg * httpmock.Registry ) {
209+ reg .Register (httpmock .REST ("GET" , "" ), httpmock .ScopesResponder ("repo,read:org" ))
210+ },
204211 wantHosts : "github.com:\n oauth_token: abc123\n " ,
205212 },
206213 {
@@ -223,7 +230,7 @@ func Test_loginRun_nontty(t *testing.T) {
223230 httpStubs : func (reg * httpmock.Registry ) {
224231 reg .Register (httpmock .REST ("GET" , "" ), httpmock .ScopesResponder ("read:org" ))
225232 },
226- wantErr : `could not validate token: missing required scope 'repo'` ,
233+ wantErr : `error validating token: missing required scope 'repo'` ,
227234 },
228235 {
229236 name : "missing read scope" ,
@@ -234,7 +241,7 @@ func Test_loginRun_nontty(t *testing.T) {
234241 httpStubs : func (reg * httpmock.Registry ) {
235242 reg .Register (httpmock .REST ("GET" , "" ), httpmock .ScopesResponder ("repo" ))
236243 },
237- wantErr : `could not validate token: missing required scope 'read:org'` ,
244+ wantErr : `error validating token: missing required scope 'read:org'` ,
238245 },
239246 {
240247 name : "has admin scope" ,
@@ -247,6 +254,36 @@ func Test_loginRun_nontty(t *testing.T) {
247254 },
248255 wantHosts : "github.com:\n oauth_token: abc456\n " ,
249256 },
257+ {
258+ name : "github.com token from environment" ,
259+ opts : & LoginOptions {
260+ Hostname : "github.com" ,
261+ Token : "abc456" ,
262+ },
263+ env : map [string ]string {
264+ "GH_TOKEN" : "value_from_env" ,
265+ },
266+ wantErr : "SilentError" ,
267+ wantStderr : heredoc .Doc (`
268+ The value of the GH_TOKEN environment variable is being used for authentication.
269+ To have GitHub CLI store credentials instead, first clear the value from the environment.
270+ ` ),
271+ },
272+ {
273+ name : "GHE token from environment" ,
274+ opts : & LoginOptions {
275+ Hostname : "ghe.io" ,
276+ Token : "abc456" ,
277+ },
278+ env : map [string ]string {
279+ "GH_ENTERPRISE_TOKEN" : "value_from_env" ,
280+ },
281+ wantErr : "SilentError" ,
282+ wantStderr : heredoc .Doc (`
283+ The value of the GH_ENTERPRISE_TOKEN environment variable is being used for authentication.
284+ To have GitHub CLI store credentials instead, first clear the value from the environment.
285+ ` ),
286+ },
250287 }
251288
252289 for _ , tt := range tests {
@@ -256,7 +293,8 @@ func Test_loginRun_nontty(t *testing.T) {
256293 io .SetStdoutTTY (false )
257294
258295 tt .opts .Config = func () (config.Config , error ) {
259- return config .NewBlankConfig (), nil
296+ cfg := config .NewBlankConfig ()
297+ return config .InheritEnv (cfg ), nil
260298 }
261299
262300 tt .opts .IO = io
@@ -271,10 +309,23 @@ func Test_loginRun_nontty(t *testing.T) {
271309 return api .NewClientFromHTTP (httpClient ), nil
272310 }
273311
312+ old_GH_TOKEN := os .Getenv ("GH_TOKEN" )
313+ os .Setenv ("GH_TOKEN" , tt .env ["GH_TOKEN" ])
314+ old_GITHUB_TOKEN := os .Getenv ("GITHUB_TOKEN" )
315+ os .Setenv ("GITHUB_TOKEN" , tt .env ["GITHUB_TOKEN" ])
316+ old_GH_ENTERPRISE_TOKEN := os .Getenv ("GH_ENTERPRISE_TOKEN" )
317+ os .Setenv ("GH_ENTERPRISE_TOKEN" , tt .env ["GH_ENTERPRISE_TOKEN" ])
318+ old_GITHUB_ENTERPRISE_TOKEN := os .Getenv ("GITHUB_ENTERPRISE_TOKEN" )
319+ os .Setenv ("GITHUB_ENTERPRISE_TOKEN" , tt .env ["GITHUB_ENTERPRISE_TOKEN" ])
320+ defer func () {
321+ os .Setenv ("GH_TOKEN" , old_GH_TOKEN )
322+ os .Setenv ("GITHUB_TOKEN" , old_GITHUB_TOKEN )
323+ os .Setenv ("GH_ENTERPRISE_TOKEN" , old_GH_ENTERPRISE_TOKEN )
324+ os .Setenv ("GITHUB_ENTERPRISE_TOKEN" , old_GITHUB_ENTERPRISE_TOKEN )
325+ }()
326+
274327 if tt .httpStubs != nil {
275328 tt .httpStubs (reg )
276- } else {
277- reg .Register (httpmock .REST ("GET" , "" ), httpmock .ScopesResponder ("repo,read:org" ))
278329 }
279330
280331 mainBuf := bytes.Buffer {}
@@ -289,7 +340,7 @@ func Test_loginRun_nontty(t *testing.T) {
289340 }
290341
291342 assert .Equal (t , "" , stdout .String ())
292- assert .Equal (t , "" , stderr .String ())
343+ assert .Equal (t , tt . wantStderr , stderr .String ())
293344 assert .Equal (t , tt .wantHosts , hostsBuf .String ())
294345 reg .Verify (t )
295346 })
@@ -325,7 +376,7 @@ func Test_loginRun_Survey(t *testing.T) {
325376 as .StubOne (false ) // do not continue
326377 },
327378 wantHosts : "" , // nothing should have been written to hosts
328- wantErrOut : regexp . MustCompile ( "Logging into github.com" ) ,
379+ wantErrOut : nil ,
329380 },
330381 {
331382 name : "hostname set" ,
0 commit comments