Skip to content

Commit 6ecc0d6

Browse files
author
Ismar Iljazovic
committed
test: improve no-input mode test coverage (from PR ankitpokhrel#905)
1 parent a780d55 commit 6ecc0d6

File tree

3 files changed

+232
-3
lines changed

3 files changed

+232
-3
lines changed

internal/cmd/issue/edit/edit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func edit(cmd *cobra.Command, args []string) {
101101

102102
// Validate that at least one field was provided in no-input mode
103103
if cmdutil.IsNoInputMode() {
104-
if hasNoChanges(params, originalBody) {
104+
if params.hasNoChanges() {
105105
cmdutil.Failed("No editable fields provided. Use flags like -s, -b, -l, -C, -y, etc. to specify changes")
106106
}
107107
}
@@ -253,8 +253,8 @@ func handleUserAssign(project, key, assignee string, client *jira.Client) {
253253
}
254254
}
255255

256-
// hasNoChanges checks if any editable fields were provided in the params.
257-
func hasNoChanges(params *editParams, originalBody string) bool {
256+
// hasNoChanges checks if any editable fields were provided.
257+
func (params *editParams) hasNoChanges() bool {
258258
return params.summary == "" &&
259259
params.body == "" &&
260260
params.priority == "" &&

internal/cmdutil/utils_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/mitchellh/go-homedir"
9+
"github.com/spf13/viper"
910
"github.com/stretchr/testify/assert"
1011

1112
"github.com/ankitpokhrel/jira-cli/pkg/jira"
@@ -264,3 +265,144 @@ func TestGetSubtaskHandle(t *testing.T) {
264265
})
265266
}
266267
}
268+
269+
func TestIsNoInputMode(t *testing.T) {
270+
cases := []struct {
271+
name string
272+
setup func()
273+
expected bool
274+
}{
275+
{
276+
name: "returns false when no_input is not set",
277+
setup: func() {
278+
viper.Reset()
279+
},
280+
expected: false,
281+
},
282+
{
283+
name: "returns true when no_input is set to true",
284+
setup: func() {
285+
viper.Reset()
286+
viper.Set("no_input", true)
287+
},
288+
expected: true,
289+
},
290+
{
291+
name: "returns false when no_input is set to false",
292+
setup: func() {
293+
viper.Reset()
294+
viper.Set("no_input", false)
295+
},
296+
expected: false,
297+
},
298+
}
299+
300+
for _, tc := range cases {
301+
t.Run(tc.name, func(t *testing.T) {
302+
tc.setup()
303+
assert.Equal(t, tc.expected, IsNoInputMode())
304+
})
305+
}
306+
}
307+
308+
func TestShouldPrompt(t *testing.T) {
309+
cases := []struct {
310+
name string
311+
localNoInput bool
312+
globalNoInput bool
313+
expected bool
314+
}{
315+
{
316+
name: "should prompt when both local and global are false",
317+
localNoInput: false,
318+
globalNoInput: false,
319+
expected: true,
320+
},
321+
{
322+
name: "should not prompt when local is true and global is false",
323+
localNoInput: true,
324+
globalNoInput: false,
325+
expected: false,
326+
},
327+
{
328+
name: "should not prompt when local is false and global is true",
329+
localNoInput: false,
330+
globalNoInput: true,
331+
expected: false,
332+
},
333+
{
334+
name: "should not prompt when both local and global are true",
335+
localNoInput: true,
336+
globalNoInput: true,
337+
expected: false,
338+
},
339+
}
340+
341+
for _, tc := range cases {
342+
t.Run(tc.name, func(t *testing.T) {
343+
viper.Reset()
344+
if tc.globalNoInput {
345+
viper.Set("no_input", true)
346+
}
347+
348+
assert.Equal(t, tc.expected, ShouldPrompt(tc.localNoInput))
349+
})
350+
}
351+
}
352+
353+
func TestNoInputConfigurationMethods(t *testing.T) {
354+
cases := []struct {
355+
name string
356+
setup func(t *testing.T)
357+
expected bool
358+
}{
359+
{
360+
name: "reads no_input from viper.Set (config file simulation)",
361+
setup: func(t *testing.T) {
362+
viper.Reset()
363+
viper.Set("no_input", true)
364+
},
365+
expected: true,
366+
},
367+
{
368+
name: "reads no_input from environment variable",
369+
setup: func(t *testing.T) {
370+
viper.Reset()
371+
viper.AutomaticEnv()
372+
viper.SetEnvPrefix("jira")
373+
t.Setenv("JIRA_NO_INPUT", "true")
374+
},
375+
expected: true,
376+
},
377+
{
378+
name: "environment variable false is respected",
379+
setup: func(t *testing.T) {
380+
viper.Reset()
381+
viper.AutomaticEnv()
382+
viper.SetEnvPrefix("jira")
383+
t.Setenv("JIRA_NO_INPUT", "false")
384+
},
385+
expected: false,
386+
},
387+
{
388+
name: "prefers viper.Set over unset environment variable",
389+
setup: func(t *testing.T) {
390+
viper.Reset()
391+
viper.AutomaticEnv()
392+
viper.SetEnvPrefix("jira")
393+
t.Setenv("JIRA_NO_INPUT", "")
394+
viper.Set("no_input", true)
395+
},
396+
expected: true,
397+
},
398+
}
399+
400+
for _, tc := range cases {
401+
t.Run(tc.name, func(t *testing.T) {
402+
tc.setup(t)
403+
404+
result := IsNoInputMode()
405+
assert.Equal(t, tc.expected, result)
406+
})
407+
}
408+
}

internal/config/generator_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"testing"
77

8+
"github.com/spf13/viper"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -69,3 +70,89 @@ func TestCreate(t *testing.T) {
6970
assert.NoError(t, os.Remove(file+".bkp"))
7071
assert.NoError(t, os.Remove(filepath.Dir(file)))
7172
}
73+
74+
func TestShallOverwrite(t *testing.T) {
75+
cases := []struct {
76+
name string
77+
setup func()
78+
expected bool
79+
}{
80+
{
81+
name: "returns false when no_input mode is enabled",
82+
setup: func() {
83+
viper.Reset()
84+
viper.Set("no_input", true)
85+
},
86+
expected: false,
87+
},
88+
{
89+
name: "returns false when no_input mode is disabled or unset",
90+
setup: func() {
91+
viper.Reset()
92+
viper.Set("no_input", false)
93+
},
94+
expected: false,
95+
},
96+
}
97+
98+
for _, tc := range cases {
99+
t.Run(tc.name, func(t *testing.T) {
100+
tc.setup()
101+
defer viper.Reset()
102+
103+
result := shallOverwrite()
104+
assert.Equal(t, tc.expected, result)
105+
})
106+
}
107+
}
108+
109+
func TestConfigureInstallationTypeNoInput(t *testing.T) {
110+
cases := []struct {
111+
name string
112+
setup func()
113+
expectError bool
114+
expectedErrorMsg string
115+
}{
116+
{
117+
name: "returns error when no_input mode is enabled and installation type not provided",
118+
setup: func() {
119+
viper.Reset()
120+
viper.Set("no_input", true)
121+
},
122+
expectError: true,
123+
expectedErrorMsg: "installation type required in non-interactive mode",
124+
},
125+
{
126+
name: "succeeds when installation type is provided in no_input mode",
127+
setup: func() {
128+
viper.Reset()
129+
viper.Set("no_input", true)
130+
},
131+
expectError: false,
132+
},
133+
}
134+
135+
for _, tc := range cases {
136+
t.Run(tc.name, func(t *testing.T) {
137+
tc.setup()
138+
defer viper.Reset()
139+
140+
cfg := &JiraCLIConfig{
141+
Installation: "cloud",
142+
}
143+
if tc.expectError {
144+
cfg.Installation = ""
145+
}
146+
147+
gen := NewJiraCLIConfigGenerator(cfg)
148+
err := gen.configureInstallationType()
149+
150+
if tc.expectError {
151+
assert.Error(t, err)
152+
assert.Contains(t, err.Error(), tc.expectedErrorMsg)
153+
} else {
154+
assert.NoError(t, err)
155+
}
156+
})
157+
}
158+
}

0 commit comments

Comments
 (0)