@@ -2,6 +2,7 @@ package checkout
22
33import (
44 "bytes"
5+ "errors"
56 "io/ioutil"
67 "net/http"
78 "strings"
@@ -59,6 +60,99 @@ func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) {
5960 }
6061}
6162
63+ func Test_checkoutRun (t * testing.T ) {
64+ tests := []struct {
65+ name string
66+ opts * CheckoutOptions
67+ httpStubs func (* httpmock.Registry )
68+ runStubs func (* run.CommandStubber )
69+ remotes map [string ]string
70+ wantStdout string
71+ wantStderr string
72+ wantErr bool
73+ }{
74+ {
75+ name : "fork repo was deleted" ,
76+ opts : & CheckoutOptions {
77+ SelectorArg : "123" ,
78+ Finder : func () shared.PRFinder {
79+ baseRepo , pr := stubPR ("OWNER/REPO:master" , "hubot/REPO:feature" )
80+ pr .MaintainerCanModify = true
81+ pr .HeadRepository = nil
82+ finder := shared .NewMockFinder ("123" , pr , baseRepo )
83+ return finder
84+ }(),
85+ Config : func () (config.Config , error ) {
86+ return config .NewBlankConfig (), nil
87+ },
88+ Branch : func () (string , error ) {
89+ return "main" , nil
90+ },
91+ },
92+ remotes : map [string ]string {
93+ "origin" : "OWNER/REPO" ,
94+ },
95+ runStubs : func (cs * run.CommandStubber ) {
96+ cs .Register (`git fetch origin refs/pull/123/head:feature` , 0 , "" )
97+ cs .Register (`git config branch\.feature\.merge` , 1 , "" )
98+ cs .Register (`git checkout feature` , 0 , "" )
99+ cs .Register (`git config branch\.feature\.remote origin` , 0 , "" )
100+ cs .Register (`git config branch\.feature\.merge refs/pull/123/head` , 0 , "" )
101+ },
102+ },
103+ }
104+ for _ , tt := range tests {
105+ t .Run (tt .name , func (t * testing.T ) {
106+ opts := tt .opts
107+
108+ io , _ , stdout , stderr := iostreams .Test ()
109+ opts .IO = io
110+
111+ httpReg := & httpmock.Registry {}
112+ defer httpReg .Verify (t )
113+ if tt .httpStubs != nil {
114+ tt .httpStubs (httpReg )
115+ }
116+ opts .HttpClient = func () (* http.Client , error ) {
117+ return & http.Client {Transport : httpReg }, nil
118+ }
119+
120+ cmdStubs , cmdTeardown := run .Stub ()
121+ defer cmdTeardown (t )
122+ if tt .runStubs != nil {
123+ tt .runStubs (cmdStubs )
124+ }
125+
126+ opts .Remotes = func () (context.Remotes , error ) {
127+ if len (tt .remotes ) == 0 {
128+ return nil , errors .New ("no remotes" )
129+ }
130+ var remotes context.Remotes
131+ for name , repo := range tt .remotes {
132+ r , err := ghrepo .FromFullName (repo )
133+ if err != nil {
134+ return remotes , err
135+ }
136+ remotes = append (remotes , & context.Remote {
137+ Remote : & git.Remote {Name : name },
138+ Repo : r ,
139+ })
140+ }
141+ return remotes , nil
142+ }
143+
144+ err := checkoutRun (opts )
145+ if (err != nil ) != tt .wantErr {
146+ t .Errorf ("want error: %v, got: %v" , tt .wantErr , err )
147+ }
148+ assert .Equal (t , tt .wantStdout , stdout .String ())
149+ assert .Equal (t , tt .wantStderr , stderr .String ())
150+ })
151+ }
152+ }
153+
154+ /** LEGACY TESTS **/
155+
62156func runCommand (rt http.RoundTripper , remotes context.Remotes , branch string , cli string ) (* test.CmdOut , error ) {
63157 io , _ , stdout , stderr := iostreams .Test ()
64158
0 commit comments