@@ -230,10 +230,33 @@ func Test_editRun(t *testing.T) {
230230 wantLastRequestParameters : map [string ]interface {}{
231231 "description" : "catbug" ,
232232 "files" : map [string ]interface {}{
233- "cicada.txt " : map [string ]interface {}{
234- "content" : "bwhiizzzbwhuiiizzzz " ,
235- "filename" : "cicada.txt " ,
233+ "unix.md " : map [string ]interface {}{
234+ "content" : "new file content " ,
235+ "filename" : "unix.md " ,
236236 },
237+ },
238+ },
239+ },
240+ {
241+ name : "single file edit flag sends only edited file" ,
242+ opts : & EditOptions {
243+ Selector : "1234" ,
244+ EditFilename : "unix.md" ,
245+ },
246+ mockGist : & shared.Gist {
247+ ID : "1234" ,
248+ Files : map [string ]* shared.GistFile {
249+ "cicada.txt" : {Filename : "cicada.txt" , Content : "bwhiizzzbwhuiiizzzz" , Type : "text/plain" },
250+ "unix.md" : {Filename : "unix.md" , Content : "meow" , Type : "text/markdown" },
251+ },
252+ Owner : & shared.GistOwner {Login : "octocat" },
253+ },
254+ httpStubs : func (reg * httpmock.Registry ) {
255+ reg .Register (httpmock .REST ("POST" , "gists/1234" ), httpmock .StatusStringResponse (201 , "{}" ))
256+ },
257+ wantLastRequestParameters : map [string ]interface {}{
258+ "description" : "" ,
259+ "files" : map [string ]interface {}{
237260 "unix.md" : map [string ]interface {}{
238261 "content" : "new file content" ,
239262 "filename" : "unix.md" ,
@@ -478,10 +501,6 @@ func Test_editRun(t *testing.T) {
478501 wantLastRequestParameters : map [string ]interface {}{
479502 "description" : "" ,
480503 "files" : map [string ]interface {}{
481- "sample.txt" : map [string ]interface {}{
482- "filename" : "sample.txt" ,
483- "content" : "bwhiizzzbwhuiiizzzz" ,
484- },
485504 "sample2.txt" : nil ,
486505 },
487506 },
@@ -581,6 +600,120 @@ func Test_editRun(t *testing.T) {
581600 },
582601 wantErr : "no file in the gist" ,
583602 },
603+ {
604+ name : "edit gist with truncated file" ,
605+ opts : & EditOptions {
606+ Selector : "1234" ,
607+ },
608+ mockGist : & shared.Gist {
609+ ID : "1234" ,
610+ Files : map [string ]* shared.GistFile {
611+ "large.txt" : {
612+ Filename : "large.txt" ,
613+ Content : "This is truncated content..." ,
614+ Type : "text/plain" ,
615+ Truncated : true ,
616+ RawURL : "https://gist.githubusercontent.com/user/1234/raw/large.txt" ,
617+ },
618+ },
619+ Owner : & shared.GistOwner {Login : "octocat" },
620+ },
621+ httpStubs : func (reg * httpmock.Registry ) {
622+ reg .Register (httpmock .REST ("POST" , "gists/1234" ),
623+ httpmock .StatusStringResponse (201 , "{}" ))
624+ },
625+ wantLastRequestParameters : map [string ]interface {}{
626+ "description" : "" ,
627+ "files" : map [string ]interface {}{
628+ "large.txt" : map [string ]interface {}{
629+ "content" : "new file content" ,
630+ "filename" : "large.txt" ,
631+ },
632+ },
633+ },
634+ },
635+ {
636+ name : "edit specific truncated file in gist with multiple truncated files" ,
637+ opts : & EditOptions {
638+ Selector : "1234" ,
639+ EditFilename : "large.txt" ,
640+ },
641+ mockGist : & shared.Gist {
642+ ID : "1234" ,
643+ Files : map [string ]* shared.GistFile {
644+ "large.txt" : {
645+ Filename : "large.txt" ,
646+ Content : "This is truncated content..." ,
647+ Type : "text/plain" ,
648+ Truncated : true ,
649+ RawURL : "https://gist.githubusercontent.com/user/1234/raw/large.txt" ,
650+ },
651+ "also-truncated.txt" : {
652+ Filename : "also-truncated.txt" ,
653+ Content : "" , // Empty because GitHub truncates subsequent files
654+ Type : "text/plain" ,
655+ Truncated : true , // Subsequent files are also marked as truncated
656+ RawURL : "https://gist.githubusercontent.com/user/1234/raw/also-truncated.txt" ,
657+ },
658+ },
659+ Owner : & shared.GistOwner {Login : "octocat" },
660+ },
661+ httpStubs : func (reg * httpmock.Registry ) {
662+ reg .Register (httpmock .REST ("POST" , "gists/1234" ),
663+ httpmock .StatusStringResponse (201 , "{}" ))
664+ },
665+ wantLastRequestParameters : map [string ]interface {}{
666+ "description" : "" ,
667+ "files" : map [string ]interface {}{
668+ "large.txt" : map [string ]interface {}{
669+ "content" : "new file content" ,
670+ "filename" : "large.txt" ,
671+ },
672+ },
673+ },
674+ },
675+ {
676+ name : "interactive truncated multi-file gist fetches only selected file raw content the first time" ,
677+ isTTY : true ,
678+ opts : & EditOptions {Selector : "1234" },
679+ prompterStubs : func (pm * prompter.MockPrompter ) {
680+ pm .RegisterSelect ("Edit which file?" , []string {"also-truncated.txt" , "large.txt" }, func (_ , _ string , opts []string ) (int , error ) {
681+ return prompter .IndexFor (opts , "large.txt" )
682+ })
683+ pm .RegisterSelect ("What next?" , editNextOptions , func (_ , _ string , opts []string ) (int , error ) {
684+ return prompter .IndexFor (opts , "Edit another file" )
685+ })
686+ // Editing large.txt twice to ensure that fetch for the raw URL happens only once
687+ pm .RegisterSelect ("Edit which file?" , []string {"also-truncated.txt" , "large.txt" }, func (_ , _ string , opts []string ) (int , error ) {
688+ return prompter .IndexFor (opts , "large.txt" )
689+ })
690+ pm .RegisterSelect ("What next?" , editNextOptions , func (_ , _ string , opts []string ) (int , error ) {
691+ return prompter .IndexFor (opts , "Submit" )
692+ })
693+ },
694+ mockGist : & shared.Gist {
695+ ID : "1234" ,
696+ Files : map [string ]* shared.GistFile {
697+ "large.txt" : {Filename : "large.txt" , Content : "This is truncated content..." , Type : "text/plain" , Truncated : true , RawURL : "https://gist.githubusercontent.com/user/1234/raw/large.txt" },
698+ "also-truncated.txt" : {Filename : "also-truncated.txt" , Content : "stuff..." , Type : "text/plain" , Truncated : true , RawURL : "https://gist.githubusercontent.com/user/1234/raw/also-truncated.txt" },
699+ },
700+ Owner : & shared.GistOwner {Login : "octocat" },
701+ },
702+ httpStubs : func (reg * httpmock.Registry ) {
703+ reg .Register (httpmock .REST ("POST" , "gists/1234" ), httpmock .StatusStringResponse (201 , "{}" ))
704+ // Explicity exclude also-truncated.txt raw URL to ensure it is not fetched since we did not select it.
705+ reg .Exclude (t , httpmock .REST ("GET" , "user/1234/raw/also-truncated.txt" ))
706+ },
707+ wantLastRequestParameters : map [string ]interface {}{
708+ "description" : "" ,
709+ "files" : map [string ]interface {}{
710+ "large.txt" : map [string ]interface {}{
711+ "content" : "new file content" ,
712+ "filename" : "large.txt" ,
713+ },
714+ },
715+ },
716+ },
584717 }
585718
586719 for _ , tt := range tests {
@@ -603,6 +736,17 @@ func Test_editRun(t *testing.T) {
603736 httpmock .JSONResponse (tt .mockGist ))
604737 reg .Register (httpmock .GraphQL (`query UserCurrent\b` ),
605738 httpmock .StringResponse (`{"data":{"viewer":{"login":"octocat"}}}` ))
739+
740+ // Register raw URL mocks for truncated files
741+ for filename , file := range tt .mockGist .Files {
742+ if file .Truncated && file .RawURL != "" {
743+ // Mock the raw URL response for GetRawGistFile calls
744+ if filename == "large.txt" {
745+ reg .Register (httpmock .REST ("GET" , "user/1234/raw/large.txt" ),
746+ httpmock .StringResponse ("This is the full content of the large file retrieved from raw URL" ))
747+ }
748+ }
749+ }
606750 }
607751 }
608752
0 commit comments