[BACK]Return to path_test.go CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / pkgsrc / pkgtools / pkglint / files

Annotation of pkgsrc/pkgtools/pkglint/files/path_test.go, Revision 1.2

1.1       rillig      1: package pkglint
                      2:
                      3: import (
                      4:        "gopkg.in/check.v1"
                      5:        "io"
                      6:        "os"
                      7:        "runtime"
                      8:        "strings"
                      9: )
                     10:
                     11: func (s *Suite) Test_NewPath(c *check.C) {
                     12:        t := s.Init(c)
                     13:
                     14:        t.CheckEquals(NewPath("filename"), NewPath("filename"))
                     15:        t.CheckEquals(NewPath("\\"), NewPath("\\"))
                     16:        c.Check(NewPath("\\"), check.Not(check.Equals), NewPath("/"))
                     17: }
                     18:
                     19: func (s *Suite) Test_NewPathSlash(c *check.C) {
                     20:        t := s.Init(c)
                     21:
                     22:        t.CheckEquals(NewPathSlash("filename"), NewPathSlash("filename"))
                     23:        t.CheckEquals(NewPathSlash("\\"), NewPathSlash("\\"))
                     24:
                     25:        t.CheckEquals(
                     26:                NewPathSlash("\\"),
                     27:                NewPathSlash(condStr(runtime.GOOS == "windows", "/", "\\")))
                     28: }
                     29:
                     30: func (s *Suite) Test_Path_String(c *check.C) {
                     31:        t := s.Init(c)
                     32:
                     33:        for _, p := range []string{"", "filename", "a/b", "c\\d"} {
                     34:                t.CheckEquals(NewPath(p).String(), p)
                     35:        }
                     36: }
                     37:
                     38: func (s *Suite) Test_Path_GoString(c *check.C) {
                     39:        t := s.Init(c)
                     40:
                     41:        test := func(p Path, s string) {
                     42:                t.CheckEquals(p.GoString(), s)
                     43:        }
                     44:
                     45:        test("", "\"\"")
                     46:        test("filename", "\"filename\"")
                     47:        test("a/b", "\"a/b\"")
                     48:        test("c\\d", "\"c\\\\d\"")
                     49: }
                     50:
                     51: func (s *Suite) Test_Path_Dir(c *check.C) {
                     52:        t := s.Init(c)
                     53:
                     54:        test := func(p, dir Path) {
                     55:                t.CheckEquals(p.Dir(), dir)
                     56:        }
                     57:
                     58:        test("", ".")
                     59:        test("././././", ".")
                     60:        test("/root", "/")
                     61:        test("filename", ".")
                     62:        test("dir/filename", "dir")
                     63:        test("dir/filename\\with\\backslash", "dir")
                     64: }
                     65:
                     66: func (s *Suite) Test_Path_Base(c *check.C) {
                     67:        t := s.Init(c)
                     68:
                     69:        test := func(p Path, base string) {
                     70:                t.CheckEquals(p.Base(), base)
                     71:        }
                     72:
                     73:        test("", ".") // That's a bit surprising
                     74:        test("././././", ".")
                     75:        test("/root", "root")
                     76:        test("filename", "filename")
                     77:        test("dir/filename", "filename")
                     78:        test("dir/filename\\with\\backslash", "filename\\with\\backslash")
                     79: }
                     80:
                     81: func (s *Suite) Test_Path_Split(c *check.C) {
                     82:        t := s.Init(c)
                     83:
                     84:        test := func(p, dir, base string) {
                     85:                actualDir, actualBase := NewPath(p).Split()
                     86:
                     87:                t.CheckDeepEquals(
                     88:                        []string{actualDir.String(), actualBase},
                     89:                        []string{dir, base})
                     90:        }
                     91:
                     92:        test("", "", "")
                     93:        test("././././", "././././", "")
                     94:        test("/root", "/", "root")
                     95:        test("filename", "", "filename")
                     96:        test("dir/filename", "dir/", "filename")
                     97:        test("dir/filename\\with\\backslash", "dir/", "filename\\with\\backslash")
                     98: }
                     99:
                    100: func (s *Suite) Test_Path_Parts(c *check.C) {
                    101:        t := s.Init(c)
                    102:
                    103:        test := func(p string, parts ...string) {
                    104:                t.CheckDeepEquals(NewPath(p).Parts(), parts)
                    105:        }
                    106:
1.2     ! rillig    107:        // Only the empty path returns an empty slice.
        !           108:        test("", nil...)
        !           109:
        !           110:        // The standard cases for relative paths.
        !           111:        test("relative", "relative")
        !           112:        test("relative/subdir", "relative", "subdir")
        !           113:        test("relative////subdir", "relative", "subdir")
        !           114:        test("relative/..", "relative", "..")
        !           115:        test("relative/.", "relative")
        !           116:
        !           117:        // Leading dots are removed when they are followed by something.
        !           118:        test("./relative", "relative")
        !           119:
        !           120:        // A path consisting of only dots produces a single dot.
        !           121:        test("./././.", ".")
        !           122:
        !           123:        // Slashes at the end are treated like a single dot.
        !           124:        test("././././", ".")
        !           125:        test(".///////", ".")
        !           126:
        !           127:        // Absolute paths have an empty first component.
        !           128:        test("/", "")
        !           129:        test("/.", "")
        !           130:        test("/root", "", "root")
        !           131:
        !           132:        // The backslash is not a path separator.
1.1       rillig    133:        test("dir/filename\\with\\backslash", "dir", "filename\\with\\backslash")
                    134: }
                    135:
                    136: func (s *Suite) Test_Path_Count(c *check.C) {
                    137:        t := s.Init(c)
                    138:
                    139:        test := func(p string, count int) {
                    140:                t.CheckEquals(NewPath(p).Count(), count)
                    141:        }
                    142:
1.2     ! rillig    143:        test("././././", 1)
        !           144:        test("/root", 2)
1.1       rillig    145:        test("filename", 1)
                    146:        test("dir/filename", 2)
                    147:        test("dir/filename\\with\\backslash", 2)
1.2     ! rillig    148:
        !           149:        // Only the empty path returns an empty slice.
        !           150:        test("", 0)
        !           151:
        !           152:        // The standard cases for canonical relative paths.
        !           153:        test("relative", 1)
        !           154:        test("relative/subdir", 2)
        !           155:        test("relative////subdir", 2)
        !           156:        test("relative/..", 2)
        !           157:        test("relative/.", 1)
        !           158:
        !           159:        // A path consisting of only dots produces a single dot.
        !           160:        test("./././.", 1)
        !           161:
        !           162:        // Slashes at the end are treated like a single dot.
        !           163:        test("././././", 1)
        !           164:        test(".///////", 1)
        !           165:
        !           166:        // Absolute paths have an empty first component.
        !           167:        test("/", 1)
        !           168:        test("/.", 1)
        !           169:        test("/root", 2)
        !           170:
        !           171:        // The backslash is not a path separator.
        !           172:        test("dir/filename\\with\\backslash", 2)
1.1       rillig    173: }
                    174:
                    175: func (s *Suite) Test_Path_HasPrefixText(c *check.C) {
                    176:        t := s.Init(c)
                    177:
                    178:        test := func(p, prefix string, hasPrefix bool) {
                    179:                t.CheckEquals(NewPath(p).HasPrefixText(prefix), hasPrefix)
                    180:        }
                    181:
                    182:        test("", "", true)
                    183:        test("filename", "", true)
                    184:        test("", "x", false)
                    185:        test("/root", "/r", true)
                    186:        test("/root", "/root", true)
                    187:        test("/root", "/root/", false)
                    188:        test("/root", "root/", false)
                    189: }
                    190:
                    191: func (s *Suite) Test_Path_HasPrefixPath(c *check.C) {
                    192:        t := s.Init(c)
                    193:
                    194:        test := func(p, prefix Path, hasPrefix bool) {
                    195:                t.CheckEquals(p.HasPrefixPath(prefix), hasPrefix)
                    196:        }
                    197:
                    198:        test("", "", true)
                    199:        test("filename", "", false)
                    200:        test("", "x", false)
                    201:        test("/root", "/r", false)
                    202:        test("/root", "/root", true)
1.2     ! rillig    203:
        !           204:        // Even though the textual representation of the prefix is longer than
        !           205:        // the path. The trailing slash marks the path as a directory, and
        !           206:        // there are only a few cases where the difference matters, such as
        !           207:        // in rsync and mkdir.
        !           208:        test("/root", "/root/", true)
        !           209:
1.1       rillig    210:        test("/root/", "/root", true)
1.2     ! rillig    211:        test("/root/", "root", false)
1.1       rillig    212:        test("/root/subdir", "/root", true)
1.2     ! rillig    213:        test("filename", ".", true)
        !           214:        test("filename", "./filename", true)
        !           215:        test("filename", "./file", false)
        !           216:        test("filename", "./filename/sub", false)
        !           217:        test("/anything", ".", false)
1.1       rillig    218: }
                    219:
                    220: func (s *Suite) Test_Path_ContainsText(c *check.C) {
                    221:        t := s.Init(c)
                    222:
                    223:        test := func(p Path, text string, contains bool) {
                    224:                t.CheckEquals(p.ContainsText(text), contains)
                    225:        }
                    226:
                    227:        test("", "", true)
                    228:        test("filename", "", true)
                    229:        test("filename", ".", false)
                    230:        test("a.b", ".", true)
                    231:        test("..", ".", true)
                    232:        test("", "x", false)
                    233:        test("/root", "/r", true)
                    234:        test("/root", "/root", true)
                    235:        test("/root", "/root/", false)
                    236:        test("/root", "root/", false)
                    237:        test("/root", "ro", true)
                    238:        test("/root", "ot", true)
                    239: }
                    240:
                    241: func (s *Suite) Test_Path_ContainsPath(c *check.C) {
                    242:        t := s.Init(c)
                    243:
                    244:        test := func(p, sub Path, contains bool) {
                    245:                t.CheckEquals(p.ContainsPath(sub), contains)
                    246:        }
                    247:
                    248:        test("", "", true)          // It doesn't make sense to search for empty paths.
                    249:        test(".", "", false)        // It doesn't make sense to search for empty paths.
                    250:        test("filename", ".", true) // Every path contains "." implicitly at the beginning
                    251:        test("a.b", ".", true)
                    252:        test("..", ".", true)
                    253:        test("filename", "", false)
                    254:        test("filename", "filename", true)
                    255:        test("a/b/c", "a", true)
                    256:        test("a/b/c", "b", true)
                    257:        test("a/b/c", "c", true)
                    258:        test("a/b/c", "a/b", true)
                    259:        test("a/b/c", "b/c", true)
                    260:        test("a/b/c", "a/b/c", true)
                    261:        test("aa/b/c", "a", false)
                    262:        test("a/bb/c", "b", false)
                    263:        test("a/bb/c", "b/c", false)
                    264:        test("mk/fetch/fetch.mk", "mk", true)
                    265:        test("category/package/../../wip/mk/../..", "mk", true)
                    266: }
                    267:
                    268: func (s *Suite) Test_Path_ContainsPathCanonical(c *check.C) {
                    269:        t := s.Init(c)
                    270:
                    271:        test := func(p, sub Path, contains bool) {
                    272:                t.CheckEquals(p.ContainsPathCanonical(sub), contains)
                    273:        }
                    274:
                    275:        test("", "", false)
                    276:        test(".", "", false)
                    277:        test("filename", "", false)
                    278:        test("filename", "filename", true)
                    279:        test("a/b/c", "a", true)
                    280:        test("a/b/c", "b", true)
                    281:        test("a/b/c", "c", true)
                    282:        test("a/b/c", "a/b", true)
                    283:        test("a/b/c", "b/c", true)
                    284:        test("a/b/c", "a/b/c", true)
                    285:        test("aa/b/c", "a", false)
                    286:        test("a/bb/c", "b", false)
                    287:        test("a/bb/c", "b/c", false)
                    288:        test("mk/fetch/fetch.mk", "mk", true)
                    289:        test("category/package/../../wip/mk", "mk", true)
                    290:        test("category/package/../../wip/mk/..", "mk", true) // FIXME
                    291:        test("category/package/../../wip/mk/../..", "mk", false)
                    292: }
                    293:
                    294: func (s *Suite) Test_Path_HasSuffixText(c *check.C) {
                    295:        t := s.Init(c)
                    296:
                    297:        test := func(p Path, suffix string, has bool) {
                    298:                t.CheckEquals(p.HasSuffixText(suffix), has)
                    299:        }
                    300:
                    301:        test("", "", true)
                    302:        test("a/bb/c", "", true)
                    303:        test("a/bb/c", "c", true)
                    304:        test("a/bb/c", "/c", true)
                    305:        test("a/bb/c", "b/c", true)
                    306:        test("aa/b/c", "bb", false)
                    307: }
                    308:
                    309: func (s *Suite) Test_Path_HasSuffixPath(c *check.C) {
                    310:        t := s.Init(c)
                    311:
                    312:        test := func(p, suffix Path, has bool) {
                    313:                t.CheckEquals(p.HasSuffixPath(suffix), has)
                    314:        }
                    315:
                    316:        test("", "", true)
                    317:        test("a/bb/c", "", false)
                    318:        test("a/bb/c", "c", true)
                    319:        test("a/bb/c", "/c", false)
                    320:        test("a/bb/c", "b/c", false)
                    321:        test("aa/b/c", "bb", false)
                    322: }
                    323:
                    324: func (s *Suite) Test_Path_HasBase(c *check.C) {
                    325:        t := s.Init(c)
                    326:
                    327:        test := func(p Path, suffix string, hasBase bool) {
                    328:                t.CheckEquals(p.HasBase(suffix), hasBase)
                    329:        }
                    330:
                    331:        test("dir/file", "e", false)
                    332:        test("dir/file", "file", true)
                    333:        test("dir/file", "file.ext", false)
                    334:        test("dir/file", "/file", false)
                    335:        test("dir/file", "dir/file", false)
                    336: }
                    337:
                    338: func (s *Suite) Test_Path_TrimSuffix(c *check.C) {
                    339:        t := s.Init(c)
                    340:
                    341:        test := func(p Path, suffix string, result Path) {
                    342:                t.CheckEquals(p.TrimSuffix(suffix), result)
                    343:        }
                    344:
                    345:        test("dir/file", "e", "dir/fil")
                    346:        test("dir/file", "file", "dir/")
                    347:        test("dir/file", "/file", "dir")
                    348:        test("dir/file", "dir/file", "")
                    349:        test("dir/file", "subdir/file", "dir/file")
                    350: }
                    351:
                    352: func (s *Suite) Test_Path_Replace(c *check.C) {
                    353:        t := s.Init(c)
                    354:
                    355:        test := func(p Path, from, to string, result Path) {
                    356:                t.CheckEquals(p.Replace(from, to), result)
                    357:        }
                    358:
                    359:        test("dir/file", "dir", "other", "other/file")
                    360:        test("dir/file", "r", "sk", "disk/file")
                    361:        test("aaa/file", "a", "sub/", "sub/sub/sub//file")
                    362: }
                    363:
                    364: func (s *Suite) Test_Path_JoinClean(c *check.C) {
                    365:        t := s.Init(c)
                    366:
                    367:        test := func(p Path, suffix Path, result Path) {
                    368:                t.CheckEquals(p.JoinClean(suffix), result)
                    369:        }
                    370:
                    371:        test("dir", "file", "dir/file")
                    372:        test("dir", "///file", "dir/file")
                    373:        test("dir/./../dir/", "///file", "dir/file")
                    374:        test("dir", "..", ".")
                    375: }
                    376:
                    377: func (s *Suite) Test_Path_JoinNoClean(c *check.C) {
                    378:        t := s.Init(c)
                    379:
                    380:        test := func(p, suffix Path, result Path) {
                    381:                t.CheckEquals(p.JoinNoClean(suffix), result)
                    382:        }
                    383:
                    384:        test("dir", "file", "dir/file")
                    385:        test("dir", "///file", "dir////file")
                    386:        test("dir/./../dir/", "///file", "dir/./../dir/////file")
                    387:        test("dir", "..", "dir/..")
                    388: }
                    389:
                    390: func (s *Suite) Test_Path_Clean(c *check.C) {
                    391:        t := s.Init(c)
                    392:
                    393:        test := func(p, result Path) {
                    394:                t.CheckEquals(p.Clean(), result)
                    395:        }
                    396:
                    397:        test("", ".")
                    398:        test(".", ".")
                    399:        test("./././", ".")
                    400:        test("a/bb///../c", "a/c")
                    401: }
                    402:
1.2     ! rillig    403: func (s *Suite) Test_Path_CleanDot(c *check.C) {
        !           404:        t := s.Init(c)
        !           405:
        !           406:        test := func(p, result Path) {
        !           407:                t.CheckEquals(p.CleanDot(), result)
        !           408:        }
        !           409:
        !           410:        test("", "")
        !           411:        test(".", ".")
        !           412:        test("./././", ".")
        !           413:        test("a/bb///../c", "a/bb/../c")
        !           414:        test("./filename", "filename")
        !           415:        test("/absolute", "/absolute")
        !           416:        test("/usr/pkgsrc/wip/package", "/usr/pkgsrc/wip/package")
        !           417:        test("/usr/pkgsrc/wip/package/../mk/git-package.mk", "/usr/pkgsrc/wip/package/../mk/git-package.mk")
        !           418: }
        !           419:
1.1       rillig    420: func (s *Suite) Test_Path_IsAbs(c *check.C) {
                    421:        t := s.Init(c)
                    422:
                    423:        test := func(p Path, abs bool) {
                    424:                t.CheckEquals(p.IsAbs(), abs)
                    425:        }
                    426:
                    427:        test("", false)
                    428:        test(".", false)
                    429:        test("a/b", false)
                    430:        test("/a", true)
                    431:        test("C:/", runtime.GOOS == "windows")
                    432:        test("c:/", runtime.GOOS == "windows")
                    433: }
                    434:
                    435: func (s *Suite) Test_Path_Rel(c *check.C) {
                    436:        t := s.Init(c)
                    437:
                    438:        base := NewPath(".")
                    439:        abc := NewPath("a/b/c")
                    440:        defFile := NewPath("d/e/f/file")
                    441:
                    442:        t.CheckEquals(abc.Rel(defFile), NewPath("../../../d/e/f/file"))
                    443:        t.CheckEquals(base.Rel(base), NewPath("."))
                    444:        t.CheckEquals(abc.Rel(base), NewPath("../../../."))
                    445: }
                    446:
                    447: func (s *Suite) Test_Path_Rename(c *check.C) {
                    448:        t := s.Init(c)
                    449:
                    450:        f := t.CreateFileLines("filename.old",
                    451:                "line 1")
                    452:        t.CheckEquals(f.IsFile(), true)
                    453:        dst := NewPath(f.TrimSuffix(".old").String() + ".new")
                    454:
                    455:        err := f.Rename(dst)
                    456:
                    457:        assertNil(err, "Rename")
                    458:        t.CheckEquals(f.IsFile(), false)
                    459:        t.CheckFileLines("filename.new",
                    460:                "line 1")
                    461: }
                    462:
                    463: func (s *Suite) Test_Path_Lstat(c *check.C) {
                    464:        t := s.Init(c)
                    465:
                    466:        testDir := func(f Path, isDir bool) {
                    467:                st, err := f.Lstat()
                    468:                assertNil(err, "Lstat")
                    469:                t.CheckEquals(st.Mode()&os.ModeDir != 0, isDir)
                    470:        }
                    471:
                    472:        t.CreateFileLines("subdir/file")
                    473:        t.CreateFileLines("file")
                    474:
                    475:        testDir(t.File("subdir"), true)
                    476:        testDir(t.File("file"), false)
                    477: }
                    478:
                    479: func (s *Suite) Test_Path_Stat(c *check.C) {
                    480:        t := s.Init(c)
                    481:
                    482:        testDir := func(f Path, isDir bool) {
                    483:                st, err := f.Stat()
                    484:                assertNil(err, "Stat")
                    485:                t.CheckEquals(st.Mode()&os.ModeDir != 0, isDir)
                    486:        }
                    487:
                    488:        t.CreateFileLines("subdir/file")
                    489:        t.CreateFileLines("file")
                    490:
                    491:        testDir(t.File("subdir"), true)
                    492:        testDir(t.File("file"), false)
                    493: }
                    494:
                    495: func (s *Suite) Test_Path_Exists(c *check.C) {
                    496:        t := s.Init(c)
                    497:
                    498:        test := func(f Path, exists bool) {
                    499:                t.CheckEquals(f.Exists(), exists)
                    500:        }
                    501:
                    502:        t.CreateFileLines("subdir/file")
                    503:        t.CreateFileLines("file")
                    504:
                    505:        test(t.File("subdir"), true)
                    506:        test(t.File("file"), true)
                    507:        test(t.File("enoent"), false)
                    508: }
                    509:
                    510: func (s *Suite) Test_Path_IsFile(c *check.C) {
                    511:        t := s.Init(c)
                    512:
                    513:        t.CreateFileLines("dir/file")
                    514:
                    515:        t.CheckEquals(t.File("nonexistent").IsFile(), false)
                    516:        t.CheckEquals(t.File("dir").IsFile(), false)
                    517:        t.CheckEquals(t.File("dir/nonexistent").IsFile(), false)
                    518:        t.CheckEquals(t.File("dir/file").IsFile(), true)
                    519: }
                    520:
                    521: func (s *Suite) Test_Path_IsDir(c *check.C) {
                    522:        t := s.Init(c)
                    523:
                    524:        t.CreateFileLines("dir/file")
                    525:
                    526:        t.CheckEquals(t.File("nonexistent").IsDir(), false)
                    527:        t.CheckEquals(t.File("dir").IsDir(), true)
                    528:        t.CheckEquals(t.File("dir/nonexistent").IsDir(), false)
                    529:        t.CheckEquals(t.File("dir/file").IsDir(), false)
                    530: }
                    531:
                    532: func (s *Suite) Test_Path_Chmod(c *check.C) {
                    533:        t := s.Init(c)
                    534:
                    535:        testWritable := func(f Path, writable bool) {
                    536:                lstat, err := f.Lstat()
                    537:                assertNil(err, "Lstat")
                    538:                t.CheckEquals(lstat.Mode().Perm()&0200 != 0, writable)
                    539:        }
                    540:
                    541:        f := t.CreateFileLines("file")
                    542:        testWritable(f, true)
                    543:
                    544:        err := f.Chmod(0444)
                    545:        assertNil(err, "Chmod")
                    546:
                    547:        testWritable(f, false)
                    548: }
                    549:
                    550: func (s *Suite) Test_Path_ReadDir(c *check.C) {
                    551:        t := s.Init(c)
                    552:
                    553:        t.CreateFileLines("subdir/file")
                    554:        t.CreateFileLines("file")
                    555:        t.CreateFileLines("CVS/Entries")
                    556:        t.CreateFileLines(".git/info/exclude")
                    557:
                    558:        infos, err := t.File(".").ReadDir()
                    559:
                    560:        assertNil(err, "ReadDir")
                    561:        var names []string
                    562:        for _, info := range infos {
                    563:                names = append(names, info.Name())
                    564:        }
                    565:
                    566:        t.CheckDeepEquals(names, []string{".git", "CVS", "file", "subdir"})
                    567: }
                    568:
                    569: func (s *Suite) Test_Path_Open(c *check.C) {
                    570:        t := s.Init(c)
                    571:
                    572:        t.CreateFileLines("filename",
                    573:                "line 1",
                    574:                "line 2")
                    575:
                    576:        f, err := t.File("filename").Open()
                    577:
                    578:        assertNil(err, "Open")
                    579:        defer func() { assertNil(f.Close(), "Close") }()
                    580:        var sb strings.Builder
                    581:        n, err := io.Copy(&sb, f)
                    582:        assertNil(err, "Copy")
                    583:        t.CheckEquals(n, int64(14))
                    584:        t.CheckEquals(sb.String(), "line 1\nline 2\n")
                    585: }
                    586:
                    587: func (s *Suite) Test_Path_ReadString(c *check.C) {
                    588:        t := s.Init(c)
                    589:
                    590:        t.CreateFileLines("filename",
                    591:                "line 1",
                    592:                "line 2")
                    593:
                    594:        text, err := t.File("filename").ReadString()
                    595:
                    596:        assertNil(err, "ReadString")
                    597:        t.CheckEquals(text, "line 1\nline 2\n")
                    598: }
                    599:
                    600: func (s *Suite) Test_Path_WriteString(c *check.C) {
                    601:        t := s.Init(c)
                    602:
                    603:        err := t.File("filename").WriteString("line 1\nline 2\n")
                    604:
                    605:        assertNil(err, "WriteString")
                    606:        t.CheckFileLines("filename",
                    607:                "line 1",
                    608:                "line 2")
                    609: }

CVSweb <webmaster@jp.NetBSD.org>