@@ -3,9 +3,11 @@ package common
33import (
44 "fmt"
55 "os"
6+ "path"
67 "testing"
8+ "time"
79
8- "github.com/magiconair/properties /assert"
10+ "github.com/stretchr/testify /assert"
911)
1012
1113func TestGetOrDefault (t * testing.T ) {
@@ -129,3 +131,138 @@ func TestGetEnvironment(t *testing.T) {
129131 })
130132 }
131133}
134+
135+ func TestExist (t * testing.T ) {
136+ tests := []struct {
137+ name string
138+ createFilepath func (* testing.T ) string
139+ wantResult bool
140+ }{{
141+ name : "a existing regular file" ,
142+ createFilepath : func (t * testing.T ) string {
143+ var f * os.File
144+ var err error
145+ f , err = os .CreateTemp (os .TempDir (), "fake" )
146+ assert .Nil (t , err )
147+ assert .NotNil (t , f )
148+ return f .Name ()
149+ },
150+ wantResult : true ,
151+ }, {
152+ name : "a existing directory" ,
153+ createFilepath : func (t * testing.T ) string {
154+ dir , err := os .MkdirTemp (os .TempDir (), "fake" )
155+ assert .Nil (t , err )
156+ assert .NotEmpty (t , dir )
157+ return dir
158+ },
159+ wantResult : true ,
160+ }, {
161+ name : "non-exsit regular file" ,
162+ createFilepath : func (t * testing.T ) string {
163+ return path .Join (os .TempDir (), fmt .Sprintf ("%d" , time .Now ().Nanosecond ()))
164+ },
165+ wantResult : false ,
166+ }}
167+ for i , tt := range tests {
168+ t .Run (tt .name , func (t * testing.T ) {
169+ filepath := tt .createFilepath (t )
170+ if filepath != "" {
171+ defer func () {
172+ _ = os .RemoveAll (filepath )
173+ }()
174+ }
175+
176+ ok := Exist (filepath )
177+ if tt .wantResult {
178+ assert .True (t , ok , "should exist in case [%d]-[%s]" , i , tt .name )
179+ } else {
180+ assert .False (t , ok , "should not exist in case [%d]-[%s]" , i , tt .name )
181+ }
182+ })
183+ }
184+ }
185+
186+ func TestParseVersionNum (t * testing.T ) {
187+ tests := []struct {
188+ name string
189+ version string
190+ expect string
191+ }{{
192+ name : "version start with v" ,
193+ version : "v1.2.3" ,
194+ expect : "1.2.3" ,
195+ }, {
196+ name : "version has not prefix v" ,
197+ version : "1.2.3" ,
198+ expect : "1.2.3" ,
199+ }, {
200+ name : "have more prefix" ,
201+ version : "alpha-v1.2.3" ,
202+ expect : "1.2.3" ,
203+ }}
204+ for i , tt := range tests {
205+ t .Run (tt .name , func (t * testing.T ) {
206+ version := ParseVersionNum (tt .version )
207+ assert .Equal (t , tt .expect , version , "expect [%s], got [%s] in case [%d]" , tt .expect , version , i )
208+ })
209+ }
210+ }
211+
212+ func TestIsDirWriteable (t * testing.T ) {
213+ tests := []struct {
214+ name string
215+ dir string
216+ wantErr bool
217+ }{{
218+ name : "should writeable" ,
219+ dir : os .TempDir (),
220+ wantErr : false ,
221+ }, {
222+ name : "should not writable" ,
223+ dir : path .Join (os .TempDir (), "fake" , "dir" ),
224+ wantErr : true ,
225+ }}
226+ for i , tt := range tests {
227+ t .Run (tt .name , func (t * testing.T ) {
228+ err := IsDirWriteable (tt .dir )
229+ if tt .wantErr {
230+ assert .NotNil (t , err , "expect error, but not in case [%d]" , i )
231+ } else {
232+ assert .Nil (t , err , "expect not error, but have in case [%d]" , i )
233+ }
234+ })
235+ }
236+ }
237+
238+ func TestCheckDirPermission (t * testing.T ) {
239+ tests := []struct {
240+ name string
241+ dir string
242+ perm os.FileMode
243+ wantErr bool
244+ }{{
245+ name : "dir is empty" ,
246+ dir : "" ,
247+ wantErr : true ,
248+ }, {
249+ name : "non-exsiting dir" ,
250+ dir : path .Join (os .TempDir (), "fake" ),
251+ wantErr : true ,
252+ }, {
253+ name : "have permission" ,
254+ perm : os .FileMode (0777 ),
255+ dir : os .TempDir (),
256+ wantErr : false ,
257+ }}
258+ for i , tt := range tests {
259+ t .Run (tt .name , func (t * testing.T ) {
260+ err := CheckDirPermission (tt .dir , tt .perm )
261+ if tt .wantErr {
262+ assert .NotNil (t , err , "expect error, but not in case [%d]" , i )
263+ } else {
264+ assert .Nil (t , err , "expect not error, but have in case [%d]" , i )
265+ }
266+ })
267+ }
268+ }
0 commit comments