From 26f1d48fa423ed2108da3238f12ca6b54a91b1d7 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 20 Sep 2025 06:55:38 +0200 Subject: [PATCH] test(gist): add tests for handling truncated files in view command * Added test cases for viewing truncated files with and without the raw flag. * Included scenarios for multiple files with truncation behavior. * Ensured correct output is returned for truncated files when accessed via raw URLs. --- pkg/cmd/gist/view/view_test.go | 102 +++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 706b85f10..85c2b7ad8 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -344,6 +344,96 @@ func Test_viewRun(t *testing.T) { }, wantOut: "cicada.txt\nfoo.md\n", }, + { + name: "truncated file with raw and filename", + isTTY: true, + opts: &ViewOptions{ + Selector: "1234", + Raw: true, + Filename: "large.txt", + }, + mockGist: &shared.Gist{ + Files: map[string]*shared.GistFile{ + "large.txt": { + Content: "This is truncated content...", + Type: "text/plain", + Truncated: true, + RawURL: "https://gist.githubusercontent.com/user/1234/raw/large.txt", + }, + }, + }, + wantOut: "This is the full content of the large file retrieved from raw URL\n", + }, + { + name: "truncated file without raw flag", + isTTY: true, + opts: &ViewOptions{ + Selector: "1234", + Raw: false, + Filename: "large.txt", + }, + mockGist: &shared.Gist{ + Files: map[string]*shared.GistFile{ + "large.txt": { + Content: "This is truncated content...", + Type: "text/plain", + Truncated: true, + RawURL: "https://gist.githubusercontent.com/user/1234/raw/large.txt", + }, + }, + }, + wantOut: "This is the full content of the large file retrieved from raw URL\n", + }, + { + name: "multiple files with one truncated", + isTTY: true, + opts: &ViewOptions{ + Selector: "1234", + Raw: true, + }, + mockGist: &shared.Gist{ + Description: "Mixed files", + Files: map[string]*shared.GistFile{ + "normal.txt": { + Content: "normal content", + Type: "text/plain", + }, + "large.txt": { + Content: "This is truncated content...", + Type: "text/plain", + Truncated: true, + RawURL: "https://gist.githubusercontent.com/user/1234/raw/large.txt", + }, + }, + }, + wantOut: "Mixed files\n\nlarge.txt\n\nThis is the full content of the large file retrieved from raw URL\n\nnormal.txt\n\nnormal content\n", + }, + { + name: "multiple files with subsequent files truncated as empty", + isTTY: true, + opts: &ViewOptions{ + Selector: "1234", + Raw: true, + }, + mockGist: &shared.Gist{ + Description: "Large gist with multiple files", + Files: map[string]*shared.GistFile{ + "large.txt": { + Content: "This is truncated content...", + Type: "text/plain", + Truncated: true, + RawURL: "https://gist.githubusercontent.com/user/1234/raw/large.txt", + }, + "also-truncated.txt": { + Type: "text/plain", + Content: "", // Empty because GitHub truncates subsequent files + Truncated: true, // Subsequent files are also marked as truncated + RawURL: "https://gist.githubusercontent.com/user/1234/raw/also-truncated.txt", + }, + }, + }, + wantOut: "Large gist with multiple files\n\nalso-truncated.txt\n\nThis is the full content of the also-truncated file retrieved from raw URL\n\nlarge.txt\n\nThis is the full content of the large file retrieved from raw URL\n", + }, } for _, tt := range tests { @@ -354,6 +444,18 @@ func Test_viewRun(t *testing.T) { } else { reg.Register(httpmock.REST("GET", "gists/1234"), httpmock.JSONResponse(tt.mockGist)) + + for filename, file := range tt.mockGist.Files { + if file.Truncated && file.RawURL != "" { + if filename == "large.txt" { + reg.Register(httpmock.REST("GET", "user/1234/raw/large.txt"), + httpmock.StringResponse("This is the full content of the large file retrieved from raw URL")) + } else if filename == "also-truncated.txt" { + reg.Register(httpmock.REST("GET", "user/1234/raw/also-truncated.txt"), + httpmock.StringResponse("This is the full content of the also-truncated file retrieved from raw URL")) + } + } + } } if tt.opts == nil {