@@ -2,7 +2,10 @@ package cmd
22
33import (
44 "context"
5+ "errors"
56 cotesting "github.com/linuxsuren/cobra-extension/pkg/testing"
7+ "github.com/linuxsuren/http-downloader/pkg/exec"
8+ "github.com/spf13/cobra"
69 "github.com/stretchr/testify/assert"
710 "testing"
811)
@@ -51,3 +54,102 @@ func Test_newInstallCmd(t *testing.T) {
5154 }}
5255 test .Valid (t , cmd .Flags ())
5356}
57+
58+ func TestInstallPreRunE (t * testing.T ) {
59+ type args struct {
60+ cmd * cobra.Command
61+ args []string
62+ }
63+ for i , tt := range []struct {
64+ name string
65+ opt * installOption
66+ args args
67+ expectErr bool
68+ }{{
69+ name : "tool and category are empty" ,
70+ opt : & installOption {},
71+ expectErr : true ,
72+ }, {
73+ name : "a fake tool that have an invalid path, no category" ,
74+ opt : & installOption {
75+ downloadOption : downloadOption {searchOption : searchOption {Fetch : false }},
76+ },
77+ args : args {
78+ args : []string {"xx@xx@xx" },
79+ },
80+ expectErr : true ,
81+ }, {
82+ name : "have category" ,
83+ opt : & installOption {
84+ downloadOption : downloadOption {
85+ searchOption : searchOption {Fetch : false },
86+ Category : "tool" ,
87+ },
88+ },
89+ args : args {
90+ args : []string {"xx@xx@xx" },
91+ },
92+ expectErr : false ,
93+ }} {
94+ t .Run (tt .name , func (t * testing.T ) {
95+ err := tt .opt .preRunE (tt .args .cmd , tt .args .args )
96+ if tt .expectErr {
97+ assert .NotNil (t , err , "failed with [%d] - case [%s]" , i , tt .name )
98+ } else {
99+ assert .Nil (t , err , "failed with [%d] - case [%s]" , i , tt .name )
100+ }
101+ })
102+ }
103+ }
104+
105+ func TestShouldInstall (t * testing.T ) {
106+ opt := & installOption {
107+ execer : & exec.FakeExecer {},
108+ tool : "fake" ,
109+ }
110+ should , exist := opt .shouldInstall ()
111+ assert .False (t , should )
112+ assert .True (t , exist )
113+
114+ // force to install
115+ opt .force = true
116+ should , exist = opt .shouldInstall ()
117+ assert .True (t , should )
118+ assert .True (t , exist )
119+
120+ // not exist
121+ opt .execer = & exec.FakeExecer {ExpectError : errors .New ("fake" )}
122+ should , exist = opt .shouldInstall ()
123+ assert .True (t , should )
124+ assert .False (t , exist )
125+ }
126+
127+ func TestInstall (t * testing.T ) {
128+ type args struct {
129+ cmd * cobra.Command
130+ args []string
131+ }
132+ for i , tt := range []struct {
133+ name string
134+ opt * installOption
135+ args args
136+ expectErr bool
137+ }{{
138+ name : "is a nativePackage, but it's exist" ,
139+ opt : & installOption {
140+ nativePackage : true ,
141+ execer : exec.FakeExecer {},
142+ },
143+ args : args {cmd : & cobra.Command {}},
144+ expectErr : false ,
145+ }} {
146+ t .Run (tt .name , func (t * testing.T ) {
147+ err := tt .opt .install (tt .args .cmd , tt .args .args )
148+ if tt .expectErr {
149+ assert .NotNil (t , err , "failed with [%d] - case [%s]" , i , tt .name )
150+ } else {
151+ assert .Nil (t , err , "failed with [%d] - case [%s]" , i , tt .name )
152+ }
153+ })
154+ }
155+ }
0 commit comments