Repository: cli/cli
File: pkg/cmd/agent-task/create/create_test.go (Go)

Code before:
			wantOpts:    &CreateOptions{ProblemStatement: "task from stdin"},
		},
		{
			name:        "mutually exclusive arg and file",
			args:        []string{"Some task inline", "-F", "{{FILE}}"},
			fileContent: "Some task",
			expectedErr: "only one of -F or arg can be provided",
		},
		{
			name:        "missing file path",
			args:        []string{"-F", "does-not-exist.md"},
			expectedErr: "could not read task description file: open does-not-exist.md: no such file or directory",
		},
		{
			name:        "empty file",
			args:        []string{"-F", "{{FILE}}"},
			fileContent: "   \n\n",
			expectedErr: "task description file is empty",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ios, stdinBuf, _, _ := iostreams.Test()

			// Provide file content either via stdin ( -F - ) or by creating a temp file
			if tt.fileContent != "" {
				isStdin := len(tt.args) == 2 && tt.args[0] == "-F" && tt.args[1] == "-"
				hasFileToken := slices.Contains(tt.args, "{{FILE}}")

				switch {
				case isStdin:
					stdinBuf.WriteString(tt.fileContent)
				case hasFileToken:
					dir := t.TempDir()
					path := filepath.Join(dir, "task.md")
					if err := os.WriteFile(path, []byte(tt.fileContent), 0o600); err != nil {
						t.Fatalf("failed to write temp file: %v", err)
					}
					for i, a := range tt.args {
						if a == "{{FILE}}" {
							tt.args[i] = path
						}
					}
				}
			}

			f := &cmdutil.Factory{IOStreams: ios}
			var gotOpts *CreateOptions
			cmd := NewCmdCreate(f, func(o *CreateOptions) error {
				gotOpts = o

Review comment (refactor):
I think we can just simplify this by always writing the content of `tt.stdin` to the `stdinBuf`. Technically, there can be a test case where something's written on the stdin but it's not a `--from-file` thing, right?

Code after:
		{
			name:        "mutually exclusive arg and file",
			args:        []string{"Some task inline", "-F", "{{FILE}}"},
			fileContent: "Some task",
			expectedErr: "only one of -F or arg can be provided",
		},
		{
			name:        "missing file path",
			args:        []string{"-F", "does-not-exist.md"},
			expectedErr: "could not read task description file: open does-not-exist.md: no such file or directory",
		},
		{
			name:        "empty file",
			args:        []string{"-F", "{{FILE}}"},
			fileContent: "   \n\n",
			expectedErr: "task description file is empty",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ios, stdinBuf, _, _ := iostreams.Test()

			// Provide file content either via stdin ( -F - ) or by creating a temp file
			if tt.fileContent != "" {
				isStdin := len(tt.args) == 2 && tt.args[0] == "-F" && tt.args[1] == "-"
				hasFileToken := slices.Contains(tt.args, "{{FILE}}")

				switch {
				case isStdin:
					stdinBuf.WriteString(tt.fileContent)
				case hasFileToken:
					dir := t.TempDir()
					path := filepath.Join(dir, "task.md")
					if err := os.WriteFile(path, []byte(tt.fileContent), 0o600); err != nil {
						t.Fatalf("failed to write temp file: %v", err)
					}
					for i, a := range tt.args {
						if a == "{{FILE}}" {
							tt.args[i] = path
						}
					}
				}
			}

			f := &cmdutil.Factory{IOStreams: ios}
			var gotOpts *CreateOptions
			cmd := NewCmdCreate(f, func(o *CreateOptions) error {
				gotOpts = o
				return nil
			})