Repository: cli/cli
File: pkg/cmd/gist/view/view_test.go (Go)

Code before:
			gist: &shared.Gist{
				Files: map[string]*shared.GistFile{
					"cicada.txt": {
						Content: "bwhiizzzbwhuiiizzzz",
						Type:    "text/plain",
					},
				},
			},
			wantOut: "bwhiizzzbwhuiiizzzz\n",
		},
		{
			name: "one file, no ID supplied",
			opts: &ViewOptions{
				Selector:  "",
				ListFiles: false,
			},
			mockGistList: true,
			gist: &shared.Gist{
				Files: map[string]*shared.GistFile{
					"cicada.txt": {
						Content: "test interactive mode",
						Type:    "text/plain",
					},
				},
			},
			wantErr: true,
		},
		{
			name: "filename selected",
			opts: &ViewOptions{
				Selector:  "1234",
				Filename:  "cicada.txt",
				ListFiles: false,
			},
			gist: &shared.Gist{
				Files: map[string]*shared.GistFile{
					"cicada.txt": {
						Content: "bwhiizzzbwhuiiizzzz",
						Type:    "text/plain",
					},
					"foo.md": {
						Content: "# foo",
						Type:    "application/markdown",
					},
				},
			},
			wantOut: "bwhiizzzbwhuiiizzzz\n",
		},
		{
			name: "filename selected, raw",
			opts: &ViewOptions{

Review comment (performance):
Thank you for fixing these tests @mateusmarquezini ✨ 

I think this test should be fixed in a different way 🤔  This test was originally testing that interactivity works, and I think we should preserve that meaning instead of checking that it errors now. I could see value in an _additional_ test that checks that we get the new error message when no argument is provided and no TTY is available as expected.

Here's an example test from `gh release create` 

https://github.com/cli/cli/blob/4c2ba5681d4777dc364bad989eda6a68189ca6a5/pkg/cmd/release/create/create_test.go#L68-L74

To do this, we would need to copy this logic by adding an `isTTY` value to the table test struct definition ([example](https://github.com/cli/cli/blob/4c2ba5681d4777dc364bad989eda6a68189ca6a5/pkg/cmd/release/create/create_test.go#L42-L49)):

https://github.com/cli/cli/blob/4c2ba5681d4777dc364bad989eda6a68189ca6a5/pkg/cmd/release/create/create_test.go#L42-L49

Then initialize I/O based on that in the test logic ([example](https://github.com/cli/cli/blob/4c2ba5681d4777dc364bad989eda6a68189ca6a5/pkg/cmd/release/create/create_test.go#L353-L366)):

https://github.com/cli/cli/blob/4c2ba5681d4777dc364bad989eda6a68189ca6a5/pkg/cmd/release/create/create_test.go#L353-L366

I think we should add this test to the `gh gist edit` changes as well 🙏 

----

To summarize, I'd like both `gh gist edit` and `gh gist view` tests to contain at least one test that checks that interactivity works when a TTY is available. I think it would also be good if both commands had one test to check that we receive an error message when TTY is _not_ available.

`gh release view` seems to be a good command to look into for code inspiration as the same patterns exist there. A code search for `required when not running interactively` in all `_test.go` files also appears to be helpful for finding additional inspiration.

I recognize this increases the complexity of this PR a bit; please let me know if you have any questions @mateusmarquezini ❤️ 

Code after:
			opts: &ViewOptions{
				Selector:  "1234",
				ListFiles: false,
			},
			mockGist: &shared.Gist{
				Files: map[string]*shared.GistFile{
					"cicada.txt": {
						Content: "bwhiizzzbwhuiiizzzz",
						Type:    "text/plain",
					},
				},
			},
			wantOut: "bwhiizzzbwhuiiizzzz\n",
		},
		{
			name:  "one file, no ID supplied",
			isTTY: true,
			opts: &ViewOptions{
				Selector:  "",
				ListFiles: false,
			},
			mockGistList: true,
			mockGist: &shared.Gist{
				Files: map[string]*shared.GistFile{
					"cicada.txt": {
						Content: "test interactive mode",
						Type:    "text/plain",
					},
				},
			},
			wantOut: "test interactive mode\n",
		},
		{
			name:    "no arguments notty",
			isTTY:   false,
			wantErr: "gist ID or URL required when not running interactively",
		},
		{
			name:  "filename selected",
			isTTY: true,
			opts: &ViewOptions{
				Selector:  "1234",
				Filename:  "cicada.txt",
				ListFiles: false,
			},
			mockGist: &shared.Gist{
				Files: map[string]*shared.GistFile{
					"cicada.txt": {
						Content: "bwhiizzzbwhuiiizzzz",
						Type:    "text/plain",
					},