Repository navigation
Expand file tree
/
Copy pathgfile_z_test.go
More file actions
670 lines (528 loc) · 13 KB
/
Copy pathgfile_z_test.go
File metadata and controls
670 lines (528 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
8000
div>
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gfile_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
)
func Test_IsDir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
paths := "/testfile"
createDir(paths)
defer delTestFiles(paths)
t.Assert(gfile.IsDir(testpath()+paths), true)
t.Assert(gfile.IsDir("./testfile2"), false)
t.Assert(gfile.IsDir("./testfile/tt.txt"), false)
t.Assert(gfile.IsDir(""), false)
})
}
func Test_IsEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
path := "/testdir_" + gconv.String(gtime.TimestampNano())
createDir(path)
defer delTestFiles(path)
t.Assert(gfile.IsEmpty(testpath()+path), true)
t.Assert(gfile.IsEmpty(testpath()+path+gfile.Separator+"test.txt"), true)
})
gtest.C(t, func(t *gtest.T) {
path := "/testfile_" + gconv.String(gtime.TimestampNano())
createTestFile(path, "")
defer delTestFiles(path)
t.Assert(gfile.IsEmpty(testpath()+path), true)
})
gtest.C(t, func(t *gtest.T) {
path := "/testfile_" + gconv.String(gtime.TimestampNano())
createTestFile(path, "1")
defer delTestFiles(path)
t.Assert(gfile.IsEmpty(testpath()+path), false)
})
}
func Test_Create(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
filepaths []string
fileobj *os.File
)
filepaths = append(filepaths, "/testfile_cc1.txt")
filepaths = append(filepaths, "/testfile_cc2.txt")
for _, v := range filepaths {
fileobj, err = gfile.Create(testpath() + v)
defer delTestFiles(v)
fileobj.Close()
t.Assert(err, nil)
}
})
}
func Test_Open(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
files []string
flags []bool
fileobj *os.File
)
file1 := "/testfile_nc1.txt"
createTestFile(file1, "")
defer delTestFiles(file1)
files = append(files, file1)
flags = append(flags, true)
files = append(files, "./testfile/file1/c1.txt")
flags = append(flags, false)
for k, v := range files {
fileobj, err = gfile.Open(testpath() + v)
fileobj.Close()
if flags[k] {
t.Assert(err, nil)
} else {
t.AssertNE(err, nil)
}
}
})
}
func Test_OpenFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
files []string
flags []bool
fileobj *os.File
)
files = append(files, "./testfile/file1/nc1.txt")
flags = append(flags, false)
f1 := "/testfile_tt.txt"
createTestFile(f1, "")
defer delTestFiles(f1)
files = append(files, f1)
flags = append(flags, true)
for k, v := range files {
fileobj, err = gfile.OpenFile(testpath()+v, os.O_RDWR, 0666)
fileobj.Close()
if flags[k] {
t.Assert(err, nil)
} else {
t.AssertNE(err, nil)
}
}
})
}
func Test_OpenWithFlag(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
files []string
flags []bool
fileobj *os.File
)
file1 := "/testfile_t1.txt"
createTestFile(file1, "")
defer delTestFiles(file1)
files = append(files, file1)
flags = append(flags, true)
files = append(files, "/testfiless/dirfiles/t1_no.txt")
flags = append(flags, false)
for k, v := range files {
fileobj, err = gfile.OpenWithFlag(testpath()+v, os.O_RDWR)
fileobj.Close()
if flags[k] {
t.Assert(err, nil)
} else {
t.AssertNE(err, nil)
}
}
})
}
func Test_OpenWithFlagPerm(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
files []string
flags []bool
fileobj *os.File
)
file1 := "/testfile_nc1.txt"
createTestFile(file1, "")
defer delTestFiles(file1)
files = append(files, file1)
flags = append(flags, true)
files = append(files, "/testfileyy/tt.txt")
flags = append(flags, false)
for k, v := range files {
fileobj, err = gfile.OpenWithFlagPerm(testpath()+v, os.O_RDWR, 666)
fileobj.Close()
if flags[k] {
t.Assert(err, nil)
} else {
t.AssertNE(err, nil)
}
}
})
}
func Test_Exists(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
flag bool
files []string
flags []bool
)
file1 := "/testfile_GetContents.txt"
createTestFile(file1, "")
defer delTestFiles(file1)
files = append(files, file1)
flags = append(flags, true)
files = append(files, "./testfile/havefile1/tt_no.txt")
flags = append(flags, false)
for k, v := range files {
flag = gfile.Exists(testpath() + v)
if flags[k] {
t.Assert(flag, true)
} else {
t.Assert(flag, false)
}
}
})
}
func Test_Pwd(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
paths, err := os.Getwd()
t.Assert(err, nil)
t.Assert(gfile.Pwd(), paths)
})
}
func Test_IsFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
flag bool
files []string
flags []bool
)
file1 := "/testfile_tt.txt"
createTestFile(file1, "")
defer delTestFiles(file1)
files = append(files, file1)
flags = append(flags, true)
dir1 := "/testfiless"
createDir(dir1)
defer delTestFiles(dir1)
files = append(files, dir1)
flags = append(flags, false)
files = append(files, "./testfiledd/tt1.txt")
flags = append(flags, false)
for k, v := range files {
flag = gfile.IsFile(testpath() + v)
if flags[k] {
t.Assert(flag, true)
} else {
t.Assert(flag, false)
}
}
})
}
func Test_Info(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
paths string = "/testfile_t1.txt"
files os.FileInfo
files2 os.FileInfo
)
createTestFile(paths, "")
defer delTestFiles(paths)
files, err = gfile.Info(testpath() + paths)
t.Assert(err, nil)
files2, err = os.Stat(testpath() + paths)
t.Assert(err, nil)
t.Assert(files, files2)
})
}
func Test_Move(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths string = "/ovetest"
filepaths string = "/testfile_ttn1.txt"
topath string = "/testfile_ttn2.txt"
)
createDir("/ovetest")
createTestFile(paths+filepaths, "a")
defer delTestFiles(paths)
yfile := testpath() + paths + filepaths
tofile := testpath() + paths + topath
t.Assert(gfile.Move(yfile, tofile), nil)
// 检查移动后的文件是否真实存在
_, err := os.Stat(tofile)
t.Assert(os.IsNotExist(err), false)
})
}
func Test_Rename(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths string = "/testfiles"
ypath string = "/testfil
8000
ettm1.txt"
topath string = "/testfilettm2.txt"
)
createDir(paths)
createTestFile(paths+ypath, "a")
defer delTestFiles(paths)
ypath = testpath() + paths + ypath
topath = testpath() + paths + topath
t.Assert(gfile.Rename(ypath, topath), nil)
t.Assert(gfile.IsFile(topath), true)
t.AssertNE(gfile.Rename("", ""), nil)
})
}
func Test_DirNames(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths string = "/testdirs"
err error
readlist []string
)
havelist := []string{
"t1.txt",
"t2.txt",
}
// 创建测试文件
createDir(paths)
for _, v := range havelist {
createTestFile(paths+"/"+v, "")
}
defer delTestFiles(paths)
readlist, err = gfile.DirNames(testpath() + paths)
t.Assert(err, nil)
t.AssertIN(readlist, havelist)
_, err = gfile.DirNames("")
t.AssertNE(err, nil)
})
}
func Test_Glob(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths string = "/testfiles/*.txt"
dirpath string = "/testfiles"
err error
resultlist []string
)
havelist1 := []string{
"t1.txt",
"t2.txt",
}
havelist2 := []string{
testpath() + "/testfiles/t1.txt",
testpath() + "/testfiles/t2.txt",
}
//===============================构建测试文件
createDir(dirpath)
for _, v := range havelist1 {
createTestFile(dirpath+"/"+v, "")
}
defer delTestFiles(dirpath)
resultlist, err = gfile.Glob(testpath()+paths, true)
t.Assert(err, nil)
t.Assert(resultlist, havelist1)
resultlist, err = gfile.Glob(testpath()+paths, false)
t.Assert(err, nil)
t.Assert(formatpaths(resultlist), formatpaths(havelist2))
_, err = gfile.Glob("", true)
t.Assert(err, nil)
})
}
func Test_Remove(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths string = "/testfile_t1.txt"
)
createTestFile(paths, "")
t.Assert(gfile.Remove(testpath()+paths), nil)
t.Assert(gfile.Remove(""), nil)
defer delTestFiles(paths)
})
}
func Test_IsReadable(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfile_GetContents.txt"
paths2 string = "./testfile_GetContents_no.txt"
)
createTestFile(paths1, "")
defer delTestFiles(paths1)
t.Assert(gfile.IsReadable(testpath()+paths1), true)
t.Assert(gfile.IsReadable(paths2), false)
})
}
func Test_IsWritable(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfile_GetContents.txt"
paths2 string = "./testfile_GetContents_no.txt"
)
createTestFile(paths1, "")
defer delTestFiles(paths1)
t.Assert(gfile.IsWritable(testpath()+paths1), true)
t.Assert(gfile.IsWritable(paths2), false)
})
}
func Test_Chmod(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfile_GetContents.txt"
paths2 string = "./testfile_GetContents_no.txt"
)
createTestFile(paths1, "")
defer delTestFiles(paths1)
t.Assert(gfile.Chmod(testpath()+paths1, 0777), nil)
t.AssertNE(gfile.Chmod(paths2, 0777), nil)
})
}
// 获取绝对目录地址
func Test_RealPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfile_files"
readlPath string
tempstr string
)
createDir(paths1)
defer delTestFiles(paths1)
readlPath = gfile.RealPath("./")
tempstr, _ = filepath.Abs("./")
t.Assert(readlPath, tempstr)
t.Assert(gfile.RealPath("./nodirs"), "")
})
}
// 获取当前执行文件的目录
func Test_SelfPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string
readlPath string
tempstr string
)
readlPath = gfile.SelfPath()
readlPath = filepath.ToSlash(readlPath)
tempstr, _ = filepath.Abs(os.Args[0])
paths1 = filepath.ToSlash(tempstr)
paths1 = strings.Replace(paths1, "./", "/", 1)
t.Assert(readlPath, paths1)
})
}
func Test_SelfDir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string
readlPath string
tempstr string
)
readlPath = gfile.SelfDir()
tempstr, _ = filepath.Abs(os.Args[0])
paths1 = filepath.Dir(tempstr)
t.Assert(readlPath, paths1)
})
}
func Test_Basename(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfilerr_GetContents.txt"
readlPath string
)
createTestFile(paths1, "")
defer delTestFiles(paths1)
readlPath = gfile.Basename(testpath() + paths1)
t.Assert(readlPath, "testfilerr_GetContents.txt")
})
}
func Test_Dir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfiless"
readlPath string
)
createDir(paths1)
defer delTestFiles(paths1)
readlPath = gfile.Dir(testpath() + paths1)
t.Assert(readlPath, testpath())
})
}
func Test_Ext(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
paths1 string = "/testfile_GetContents.txt"
dirpath1 = "/testdirs"
)
createTestFile(paths1, "")
defer delTestFiles(paths1)
createDir(dirpath1)
defer delTestFiles(dirpath1)
t.Assert(gfile.Ext(testpath()+paths1), ".txt")
t.Assert(gfile.Ext(testpath()+dirpath1), "")
})
gtest.C(t, func(t *gtest.T) {
t.Assert(gfile.Ext("/var/www/test.js"), ".js")
t.Assert(gfile.Ext("/var/www/test.min.js"), ".js")
t.Assert(gfile.Ext("/var/www/test.js?1"), ".js")
t.Assert(gfile.Ext("/var/www/test.min.js?v1"), ".js")
})
}
func Test_ExtName(t *testing.T) {
t.Assert(gfile.ExtName("/var/www/test.js"), "js")
t.Assert(gfile.ExtName("/var/www/test.min.js"), "js")
t.Assert(gfile.ExtName("/var/www/test.js?v=1"), "js")
t.Assert(gfile.ExtName("/var/www/test.min.js?v=1"), "js")
})
}
func Test_TempDir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
if gfile.Separator != "/" || !gfile.Exists("/tmp") {
t.Assert(gfile.TempDir(), os.TempDir())
} else {
t.Assert(gfile.TempDir(), "/tmp")
}
})
}
func Test_Mkdir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
tpath string = "/testfile/createdir"
err error
)
defer delTestFiles("/testfile")
err = gfile.Mkdir(testpath() + tpath)
t.Assert(err, nil)
err = gfile.Mkdir("")
t.AssertNE(err, nil)
err = gfile.Mkdir(testpath() + tpath + "2/t1")
t.Assert(err, nil)
})
}
func Test_Stat(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
tpath1 = "/testfile_t1.txt"
tpath2 = "./testfile_t1_no.txt"
err error
fileiofo os.FileInfo
)
createTestFile(tpath1, "a")
defer delTestFiles(tpath1)
fileiofo, err = gfile.Stat(testpath() + tpath1)
t.Assert(err, nil)
t.Assert(fileiofo.Size(), 1)
_, err = gfile.Stat(tpath2)
t.AssertNE(err, nil)
})
}
func Test_MainPkgPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
reads := gfile.MainPkgPath()
t.Assert(reads, "")
})
}