[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.7

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_Path_String(c *check.C) {
                     20:        t := s.Init(c)
                     21:
1.5       rillig     22:        test := func(p Path) {
                     23:                t.CheckEquals(p.String(), string(p))
1.1       rillig     24:        }
1.5       rillig     25:
                     26:        test("")
                     27:        test("filename")
                     28:        test("a/b")
                     29:
                     30:        // No normalization takes place here.
                     31:        // That's what NewPathSlash is for.
                     32:        test("c\\d")
1.1       rillig     33: }
                     34:
                     35: func (s *Suite) Test_Path_GoString(c *check.C) {
                     36:        t := s.Init(c)
                     37:
                     38:        test := func(p Path, s string) {
                     39:                t.CheckEquals(p.GoString(), s)
                     40:        }
                     41:
                     42:        test("", "\"\"")
                     43:        test("filename", "\"filename\"")
                     44:        test("a/b", "\"a/b\"")
                     45:        test("c\\d", "\"c\\\\d\"")
                     46: }
                     47:
1.3       rillig     48: func (s *Suite) Test_Path_IsEmpty(c *check.C) {
                     49:        t := s.Init(c)
                     50:
                     51:        test := func(p Path, isEmpty bool) {
                     52:                t.CheckEquals(p.IsEmpty(), isEmpty)
                     53:        }
                     54:
                     55:        test("", true)
                     56:        test(".", false)
                     57:        test("/", false)
                     58: }
                     59:
1.4       rillig     60: func (s *Suite) Test_Path_DirClean(c *check.C) {
1.1       rillig     61:        t := s.Init(c)
                     62:
                     63:        test := func(p, dir Path) {
1.4       rillig     64:                t.CheckEquals(p.DirClean(), dir)
                     65:        }
                     66:
                     67:        test("", ".")
                     68:        test("././././", ".")
                     69:        test("/root", "/")
                     70:        test("filename", ".")
                     71:        test("dir/filename", "dir")
                     72:        test("dir/filename\\with\\backslash", "dir")
                     73:
                     74:        test("././././dir/filename", "dir")
                     75: }
                     76:
                     77: func (s *Suite) Test_Path_DirNoClean(c *check.C) {
                     78:        t := s.Init(c)
                     79:
                     80:        test := func(p, dir Path) {
                     81:                t.CheckEquals(p.DirNoClean(), dir)
1.1       rillig     82:        }
                     83:
                     84:        test("", ".")
                     85:        test("././././", ".")
                     86:        test("/root", "/")
                     87:        test("filename", ".")
                     88:        test("dir/filename", "dir")
                     89:        test("dir/filename\\with\\backslash", "dir")
1.6       rillig     90:        test("dir/./file", "dir")
                     91:        test("./file", ".")
1.1       rillig     92: }
                     93:
                     94: func (s *Suite) Test_Path_Base(c *check.C) {
                     95:        t := s.Init(c)
                     96:
                     97:        test := func(p Path, base string) {
                     98:                t.CheckEquals(p.Base(), base)
                     99:        }
                    100:
                    101:        test("", ".") // That's a bit surprising
                    102:        test("././././", ".")
                    103:        test("/root", "root")
                    104:        test("filename", "filename")
                    105:        test("dir/filename", "filename")
                    106:        test("dir/filename\\with\\backslash", "filename\\with\\backslash")
                    107: }
                    108:
                    109: func (s *Suite) Test_Path_Split(c *check.C) {
                    110:        t := s.Init(c)
                    111:
                    112:        test := func(p, dir, base string) {
                    113:                actualDir, actualBase := NewPath(p).Split()
                    114:
                    115:                t.CheckDeepEquals(
                    116:                        []string{actualDir.String(), actualBase},
                    117:                        []string{dir, base})
                    118:        }
                    119:
                    120:        test("", "", "")
                    121:        test("././././", "././././", "")
                    122:        test("/root", "/", "root")
                    123:        test("filename", "", "filename")
                    124:        test("dir/filename", "dir/", "filename")
                    125:        test("dir/filename\\with\\backslash", "dir/", "filename\\with\\backslash")
                    126: }
                    127:
                    128: func (s *Suite) Test_Path_Parts(c *check.C) {
                    129:        t := s.Init(c)
                    130:
                    131:        test := func(p string, parts ...string) {
                    132:                t.CheckDeepEquals(NewPath(p).Parts(), parts)
                    133:        }
                    134:
1.2       rillig    135:        // Only the empty path returns an empty slice.
                    136:        test("", nil...)
                    137:
                    138:        // The standard cases for relative paths.
                    139:        test("relative", "relative")
                    140:        test("relative/subdir", "relative", "subdir")
                    141:        test("relative////subdir", "relative", "subdir")
                    142:        test("relative/..", "relative", "..")
                    143:        test("relative/.", "relative")
                    144:
                    145:        // Leading dots are removed when they are followed by something.
                    146:        test("./relative", "relative")
                    147:
                    148:        // A path consisting of only dots produces a single dot.
                    149:        test("./././.", ".")
                    150:
                    151:        // Slashes at the end are treated like a single dot.
                    152:        test("././././", ".")
                    153:        test(".///////", ".")
                    154:
                    155:        // Absolute paths have an empty first component.
                    156:        test("/", "")
                    157:        test("/.", "")
                    158:        test("/root", "", "root")
                    159:
                    160:        // The backslash is not a path separator.
1.1       rillig    161:        test("dir/filename\\with\\backslash", "dir", "filename\\with\\backslash")
                    162: }
                    163:
                    164: func (s *Suite) Test_Path_Count(c *check.C) {
                    165:        t := s.Init(c)
                    166:
                    167:        test := func(p string, count int) {
                    168:                t.CheckEquals(NewPath(p).Count(), count)
                    169:        }
                    170:
1.2       rillig    171:        test("././././", 1)
                    172:        test("/root", 2)
1.1       rillig    173:        test("filename", 1)
                    174:        test("dir/filename", 2)
                    175:        test("dir/filename\\with\\backslash", 2)
1.2       rillig    176:
                    177:        // Only the empty path returns an empty slice.
                    178:        test("", 0)
                    179:
                    180:        // The standard cases for canonical relative paths.
                    181:        test("relative", 1)
                    182:        test("relative/subdir", 2)
                    183:        test("relative////subdir", 2)
                    184:        test("relative/..", 2)
                    185:        test("relative/.", 1)
                    186:
                    187:        // A path consisting of only dots produces a single dot.
                    188:        test("./././.", 1)
                    189:
                    190:        // Slashes at the end are treated like a single dot.
                    191:        test("././././", 1)
                    192:        test(".///////", 1)
                    193:
                    194:        // Absolute paths have an empty first component.
                    195:        test("/", 1)
                    196:        test("/.", 1)
                    197:        test("/root", 2)
                    198:
                    199:        // The backslash is not a path separator.
                    200:        test("dir/filename\\with\\backslash", 2)
1.1       rillig    201: }
                    202:
                    203: func (s *Suite) Test_Path_HasPrefixText(c *check.C) {
                    204:        t := s.Init(c)
                    205:
                    206:        test := func(p, prefix string, hasPrefix bool) {
                    207:                t.CheckEquals(NewPath(p).HasPrefixText(prefix), hasPrefix)
                    208:        }
                    209:
                    210:        test("", "", true)
                    211:        test("filename", "", true)
                    212:        test("", "x", false)
                    213:        test("/root", "/r", true)
                    214:        test("/root", "/root", true)
                    215:        test("/root", "/root/", false)
                    216:        test("/root", "root/", false)
                    217: }
                    218:
                    219: func (s *Suite) Test_Path_HasPrefixPath(c *check.C) {
                    220:        t := s.Init(c)
                    221:
                    222:        test := func(p, prefix Path, hasPrefix bool) {
                    223:                t.CheckEquals(p.HasPrefixPath(prefix), hasPrefix)
                    224:        }
                    225:
                    226:        test("", "", true)
                    227:        test("filename", "", false)
                    228:        test("", "x", false)
                    229:        test("/root", "/r", false)
                    230:        test("/root", "/root", true)
1.2       rillig    231:
                    232:        // Even though the textual representation of the prefix is longer than
                    233:        // the path. The trailing slash marks the path as a directory, and
                    234:        // there are only a few cases where the difference matters, such as
                    235:        // in rsync and mkdir.
                    236:        test("/root", "/root/", true)
                    237:
1.1       rillig    238:        test("/root/", "/root", true)
1.2       rillig    239:        test("/root/", "root", false)
1.1       rillig    240:        test("/root/subdir", "/root", true)
1.2       rillig    241:        test("filename", ".", true)
                    242:        test("filename", "./filename", true)
                    243:        test("filename", "./file", false)
                    244:        test("filename", "./filename/sub", false)
                    245:        test("/anything", ".", false)
1.1       rillig    246: }
                    247:
                    248: func (s *Suite) Test_Path_ContainsText(c *check.C) {
                    249:        t := s.Init(c)
                    250:
                    251:        test := func(p Path, text string, contains bool) {
                    252:                t.CheckEquals(p.ContainsText(text), contains)
                    253:        }
                    254:
                    255:        test("", "", true)
                    256:        test("filename", "", true)
                    257:        test("filename", ".", false)
                    258:        test("a.b", ".", true)
                    259:        test("..", ".", true)
                    260:        test("", "x", false)
                    261:        test("/root", "/r", true)
                    262:        test("/root", "/root", true)
                    263:        test("/root", "/root/", false)
                    264:        test("/root", "root/", false)
                    265:        test("/root", "ro", true)
                    266:        test("/root", "ot", true)
                    267: }
                    268:
                    269: func (s *Suite) Test_Path_ContainsPath(c *check.C) {
                    270:        t := s.Init(c)
                    271:
                    272:        test := func(p, sub Path, contains bool) {
                    273:                t.CheckEquals(p.ContainsPath(sub), contains)
                    274:        }
                    275:
                    276:        test("", "", true)          // It doesn't make sense to search for empty paths.
                    277:        test(".", "", false)        // It doesn't make sense to search for empty paths.
1.3       rillig    278:        test("filename", ".", true) // Every relative path contains "." implicitly at the beginning
1.1       rillig    279:        test("a.b", ".", true)
                    280:        test("..", ".", true)
                    281:        test("filename", "", false)
                    282:        test("filename", "filename", true)
                    283:        test("a/b/c", "a", true)
                    284:        test("a/b/c", "b", true)
                    285:        test("a/b/c", "c", true)
                    286:        test("a/b/c", "a/b", true)
                    287:        test("a/b/c", "b/c", true)
                    288:        test("a/b/c", "a/b/c", true)
                    289:        test("aa/b/c", "a", false)
                    290:        test("a/bb/c", "b", false)
                    291:        test("a/bb/c", "b/c", false)
                    292:        test("mk/fetch/fetch.mk", "mk", true)
                    293:        test("category/package/../../wip/mk/../..", "mk", true)
1.3       rillig    294:
                    295:        test("a", "a", true)
                    296:        test("a", "b", false)
                    297:        test("a", "A", false)
                    298:        test("a/b/c", "a", true)
                    299:        test("a/b/c", "b", true)
                    300:        test("a/b/c", "c", true)
                    301:        test("a/b/c", "a/b", true)
                    302:        test("a/b/c", "b/c", true)
                    303:        test("a/b/c", "a/b/c", true)
                    304:        test("aa/bb/cc", "a/b", false)
                    305:        test("aa/bb/cc", "a/bb", false)
                    306:        test("aa/bb/cc", "aa/b", false)
                    307:        test("aa/bb/cc", "aa/bb", true)
                    308:        test("aa/bb/cc", "a", false)
                    309:        test("aa/bb/cc", "b", false)
                    310:        test("aa/bb/cc", "c", false)
1.1       rillig    311: }
                    312:
                    313: func (s *Suite) Test_Path_HasSuffixText(c *check.C) {
                    314:        t := s.Init(c)
                    315:
                    316:        test := func(p Path, suffix string, has bool) {
                    317:                t.CheckEquals(p.HasSuffixText(suffix), has)
                    318:        }
                    319:
                    320:        test("", "", true)
                    321:        test("a/bb/c", "", true)
                    322:        test("a/bb/c", "c", true)
                    323:        test("a/bb/c", "/c", true)
                    324:        test("a/bb/c", "b/c", true)
                    325:        test("aa/b/c", "bb", false)
                    326: }
                    327:
                    328: func (s *Suite) Test_Path_HasSuffixPath(c *check.C) {
                    329:        t := s.Init(c)
                    330:
                    331:        test := func(p, suffix Path, has bool) {
                    332:                t.CheckEquals(p.HasSuffixPath(suffix), has)
                    333:        }
                    334:
                    335:        test("", "", true)
                    336:        test("a/bb/c", "", false)
                    337:        test("a/bb/c", "c", true)
                    338:        test("a/bb/c", "/c", false)
                    339:        test("a/bb/c", "b/c", false)
                    340:        test("aa/b/c", "bb", false)
                    341: }
                    342:
                    343: func (s *Suite) Test_Path_HasBase(c *check.C) {
                    344:        t := s.Init(c)
                    345:
                    346:        test := func(p Path, suffix string, hasBase bool) {
                    347:                t.CheckEquals(p.HasBase(suffix), hasBase)
                    348:        }
                    349:
                    350:        test("dir/file", "e", false)
                    351:        test("dir/file", "file", true)
                    352:        test("dir/file", "file.ext", false)
                    353:        test("dir/file", "/file", false)
                    354:        test("dir/file", "dir/file", false)
                    355: }
                    356:
                    357: func (s *Suite) Test_Path_TrimSuffix(c *check.C) {
                    358:        t := s.Init(c)
                    359:
                    360:        test := func(p Path, suffix string, result Path) {
                    361:                t.CheckEquals(p.TrimSuffix(suffix), result)
                    362:        }
                    363:
                    364:        test("dir/file", "e", "dir/fil")
                    365:        test("dir/file", "file", "dir/")
                    366:        test("dir/file", "/file", "dir")
                    367:        test("dir/file", "dir/file", "")
                    368:        test("dir/file", "subdir/file", "dir/file")
                    369: }
                    370:
                    371: func (s *Suite) Test_Path_Replace(c *check.C) {
                    372:        t := s.Init(c)
                    373:
                    374:        test := func(p Path, from, to string, result Path) {
                    375:                t.CheckEquals(p.Replace(from, to), result)
                    376:        }
                    377:
                    378:        test("dir/file", "dir", "other", "other/file")
                    379:        test("dir/file", "r", "sk", "disk/file")
                    380:        test("aaa/file", "a", "sub/", "sub/sub/sub//file")
                    381: }
                    382:
                    383: func (s *Suite) Test_Path_JoinClean(c *check.C) {
                    384:        t := s.Init(c)
                    385:
1.5       rillig    386:        test := func(p Path, rel RelPath, result Path) {
                    387:                t.CheckEquals(p.JoinClean(rel), result)
1.1       rillig    388:        }
                    389:
                    390:        test("dir", "file", "dir/file")
                    391:        test("dir", "///file", "dir/file")
                    392:        test("dir/./../dir/", "///file", "dir/file")
                    393:        test("dir", "..", ".")
                    394: }
                    395:
                    396: func (s *Suite) Test_Path_JoinNoClean(c *check.C) {
                    397:        t := s.Init(c)
                    398:
1.5       rillig    399:        test := func(p, rel RelPath, result RelPath) {
                    400:                t.CheckEquals(p.JoinNoClean(rel), result)
1.1       rillig    401:        }
                    402:
                    403:        test("dir", "file", "dir/file")
                    404:        test("dir", "///file", "dir////file")
                    405:        test("dir/./../dir/", "///file", "dir/./../dir/////file")
                    406:        test("dir", "..", "dir/..")
                    407: }
                    408:
                    409: func (s *Suite) Test_Path_Clean(c *check.C) {
                    410:        t := s.Init(c)
                    411:
                    412:        test := func(p, result Path) {
                    413:                t.CheckEquals(p.Clean(), result)
                    414:        }
                    415:
                    416:        test("", ".")
                    417:        test(".", ".")
                    418:        test("./././", ".")
                    419:        test("a/bb///../c", "a/c")
                    420: }
                    421:
1.2       rillig    422: func (s *Suite) Test_Path_CleanDot(c *check.C) {
                    423:        t := s.Init(c)
                    424:
                    425:        test := func(p, result Path) {
                    426:                t.CheckEquals(p.CleanDot(), result)
                    427:        }
                    428:
                    429:        test("", "")
                    430:        test(".", ".")
                    431:        test("./././", ".")
1.6       rillig    432:        test("dir/", "dir/") // TODO: Or maybe "dir/."?
1.2       rillig    433:        test("a/bb///../c", "a/bb/../c")
                    434:        test("./filename", "filename")
                    435:        test("/absolute", "/absolute")
                    436:        test("/usr/pkgsrc/wip/package", "/usr/pkgsrc/wip/package")
                    437:        test("/usr/pkgsrc/wip/package/../mk/git-package.mk", "/usr/pkgsrc/wip/package/../mk/git-package.mk")
1.4       rillig    438:        test("a//b", "a/b")
                    439: }
                    440:
                    441: func (s *Suite) Test_Path_CleanPath(c *check.C) {
                    442:        t := s.Init(c)
                    443:
                    444:        test := func(from, to Path) {
                    445:                t.CheckEquals(from.CleanPath(), to)
                    446:        }
                    447:
                    448:        test("simple/path", "simple/path")
                    449:        test("/absolute/path", "/absolute/path")
                    450:
                    451:        // Single dot components are removed, unless it's the only component of the path.
                    452:        test("./././.", ".")
                    453:        test("./././", ".")
                    454:        test("dir/multi/././/file", "dir/multi/file")
                    455:        test("dir/", "dir")
                    456:
                    457:        test("dir/", "dir")
                    458:
                    459:        // Components like aa/bb/../.. are removed, but not in the initial part of the path,
                    460:        // and only if they are not followed by another "..".
                    461:        test("dir/../dir/../dir/../dir/subdir/../../Makefile", "dir/../dir/../dir/../Makefile")
                    462:        test("111/222/../../333/444/../../555/666/../../777/888/9", "111/222/../../777/888/9")
                    463:        test("1/2/3/../../4/5/6/../../7/8/9/../../../../10", "1/2/3/../../4/7/8/9/../../../../10")
                    464:        test("cat/pkg.v1/../../cat/pkg.v2/Makefile", "cat/pkg.v1/../../cat/pkg.v2/Makefile")
                    465:        test("aa/../../../../../a/b/c/d", "aa/../../../../../a/b/c/d")
                    466:        test("aa/bb/../../../../a/b/c/d", "aa/bb/../../../../a/b/c/d")
                    467:        test("aa/bb/cc/../../../a/b/c/d", "aa/bb/cc/../../../a/b/c/d")
                    468:        test("aa/bb/cc/dd/../../a/b/c/d", "aa/bb/a/b/c/d")
                    469:        test("aa/bb/cc/dd/ee/../a/b/c/d", "aa/bb/cc/dd/ee/../a/b/c/d")
                    470:        test("../../../../../a/b/c/d", "../../../../../a/b/c/d")
                    471:        test("aa/../../../../a/b/c/d", "aa/../../../../a/b/c/d")
                    472:        test("aa/bb/../../../a/b/c/d", "aa/bb/../../../a/b/c/d")
                    473:        test("aa/bb/cc/../../a/b/c/d", "aa/bb/cc/../../a/b/c/d")
                    474:        test("aa/bb/cc/dd/../a/b/c/d", "aa/bb/cc/dd/../a/b/c/d")
                    475:        test("aa/../cc/../../a/b/c/d", "aa/../cc/../../a/b/c/d")
                    476:
                    477:        // The initial 2 components of the path are typically category/package, when
                    478:        // pkglint is called from the pkgsrc top-level directory.
                    479:        // This path serves as the context and therefore is always kept.
                    480:        test("aa/bb/../../cc/dd/../../ee/ff", "aa/bb/../../ee/ff")
                    481:        test("aa/bb/../../cc/dd/../..", "aa/bb/../..")
                    482:        test("aa/bb/cc/dd/../..", "aa/bb")
                    483:        test("aa/bb/../../cc/dd/../../ee/ff/buildlink3.mk", "aa/bb/../../ee/ff/buildlink3.mk")
                    484:        test("./aa/bb/../../cc/dd/../../ee/ff/buildlink3.mk", "aa/bb/../../ee/ff/buildlink3.mk")
                    485:
                    486:        test("../.", "..")
                    487:        test("../././././././.", "..")
                    488:        test(".././././././././", "..")
                    489:
                    490:        test(
                    491:                "x11/kde-runtime4/../../misc/kdepimlibs4/../../databases/openldap-client/buildlink3.mk",
                    492:                "x11/kde-runtime4/../../databases/openldap-client/buildlink3.mk")
1.2       rillig    493: }
                    494:
1.1       rillig    495: func (s *Suite) Test_Path_IsAbs(c *check.C) {
                    496:        t := s.Init(c)
                    497:
                    498:        test := func(p Path, abs bool) {
                    499:                t.CheckEquals(p.IsAbs(), abs)
                    500:        }
                    501:
                    502:        test("", false)
                    503:        test(".", false)
                    504:        test("a/b", false)
                    505:        test("/a", true)
                    506:        test("C:/", runtime.GOOS == "windows")
                    507:        test("c:/", runtime.GOOS == "windows")
                    508: }
                    509:
                    510: func (s *Suite) Test_Path_Rel(c *check.C) {
                    511:        t := s.Init(c)
                    512:
1.5       rillig    513:        test := func(base Path, other Path, result RelPath) {
                    514:                t.CheckEquals(base.Rel(other), result)
                    515:        }
                    516:
                    517:        test("a/b/c", "d/e/f/file", "../../../d/e/f/file")
                    518:        test(".", ".", ".")
                    519:
                    520:        // The trailing dot marks the difference between a file and a directory.
                    521:        // This is the same behavior as with filepath.Rel.
                    522:        test("a/b/c", ".", "../../../.")
                    523:
                    524:        // Intermediate dotdot components are removed.
                    525:        test("a/../b", "c/../d", "../d")
                    526:
                    527:        test(".", "dir/file", "dir/file")
1.7     ! rillig    528:        // XXX: maybe the /. is missing at the end
        !           529:        test(".", "dir/subdir/", "dir/subdir")
        !           530:        // XXX: maybe the /. is missing at the end
        !           531:        test(".", "dir/subdir/.", "dir/subdir")
1.1       rillig    532: }
                    533:
1.4       rillig    534: func (s *Suite) Test_NewCurrPath(c *check.C) {
                    535:        t := s.Init(c)
                    536:
                    537:        curr := NewCurrPath("dir/.///file")
                    538:
                    539:        t.CheckEquals(curr.String(), "dir/.///file")
                    540: }
                    541:
                    542: func (s *Suite) Test_NewCurrPathString(c *check.C) {
                    543:        t := s.Init(c)
                    544:
                    545:        curr := NewCurrPathString("dir/.///file")
                    546:
                    547:        t.CheckEquals(curr.String(), "dir/.///file")
                    548: }
                    549:
                    550: func (s *Suite) Test_NewCurrPathSlash(c *check.C) {
                    551:        t := s.Init(c)
                    552:
                    553:        test := func(path, curr string) {
                    554:                t.CheckEquals(NewCurrPathSlash(path).String(), curr)
                    555:        }
                    556:
                    557:        test("filename", "filename")
                    558:        test("dir/.///file", "dir/.///file")
                    559: }
                    560:
                    561: func (s *Suite) Test_NewCurrPathSlash__windows(c *check.C) {
                    562:        t := s.Init(c)
                    563:
1.5       rillig    564:        test := func(path, currWindows, currOther string) {
                    565:                t.CheckEquals(
                    566:                        NewCurrPathSlash(path).String(),
                    567:                        condStr(runtime.GOOS == "windows", currWindows, currOther))
1.4       rillig    568:        }
                    569:
1.5       rillig    570:        test("\\", "/", "\\")
                    571:        test("dir\\.\\\\\\file", "dir/.///file", "dir\\.\\\\\\file")
1.4       rillig    572: }
                    573:
                    574: func (s *Suite) Test_CurrPath_GoString(c *check.C) {
                    575:        t := s.Init(c)
                    576:
1.5       rillig    577:        test := func(p CurrPath, str string) {
                    578:                t.CheckEquals(p.GoString(), str)
                    579:        }
                    580:
1.4       rillig    581:        // Tabs in filenames are rare, probably typos.
1.5       rillig    582:        test("dir/file\t", "\"dir/file\\t\"")
1.4       rillig    583: }
                    584:
                    585: func (s *Suite) Test_CurrPath_String(c *check.C) {
                    586:        t := s.Init(c)
                    587:
1.5       rillig    588:        test := func(p CurrPath, str string) {
                    589:                t.CheckEquals(p.String(), str)
                    590:        }
                    591:
1.4       rillig    592:        // Tabs in filenames are rare, probably typos.
1.5       rillig    593:        test("dir/file\t", "dir/file\t")
1.4       rillig    594: }
                    595:
                    596: func (s *Suite) Test_CurrPath_AsPath(c *check.C) {
                    597:        t := s.Init(c)
                    598:
1.5       rillig    599:        test := func(curr CurrPath, asPath Path) {
                    600:                t.CheckEquals(curr.AsPath(), asPath)
                    601:        }
                    602:
1.4       rillig    603:        // Tabs in filenames are rare, probably typos.
1.5       rillig    604:        test("dir/file\t", "dir/file\t")
1.4       rillig    605: }
                    606:
                    607: func (s *Suite) Test_CurrPath_IsEmpty(c *check.C) {
                    608:        t := s.Init(c)
                    609:
                    610:        test := func(curr CurrPath, isEmpty bool) {
                    611:                t.CheckEquals(curr.IsEmpty(), isEmpty)
                    612:        }
                    613:
                    614:        test("", true)
                    615:        test(".", false)
                    616:        test("/", false)
                    617: }
                    618:
                    619: func (s *Suite) Test_CurrPath_DirClean(c *check.C) {
                    620:        t := s.Init(c)
                    621:
                    622:        test := func(curr, dir CurrPath) {
                    623:                t.CheckEquals(curr.DirClean(), dir)
                    624:        }
                    625:
                    626:        test("./dir/../dir///./file", "dir")
                    627: }
                    628:
                    629: func (s *Suite) Test_CurrPath_DirNoClean(c *check.C) {
                    630:        t := s.Init(c)
                    631:
                    632:        test := func(curr, dir CurrPath) {
                    633:                t.CheckEquals(curr.DirNoClean(), dir)
                    634:        }
                    635:
                    636:        test("./dir/../dir///./file", "./dir/../dir")
                    637: }
                    638:
                    639: func (s *Suite) Test_CurrPath_Base(c *check.C) {
                    640:        t := s.Init(c)
                    641:
                    642:        test := func(curr CurrPath, base string) {
                    643:                t.CheckEquals(curr.Base(), base)
                    644:        }
                    645:
                    646:        test("dir/file", "file")
                    647: }
                    648:
                    649: func (s *Suite) Test_CurrPath_Split(c *check.C) {
                    650:        t := s.Init(c)
                    651:
                    652:        test := func(curr, dir CurrPath, base string) {
                    653:                actualDir, actualBase := curr.Split()
                    654:                t.CheckEquals(actualDir, dir)
                    655:                t.CheckEquals(actualBase, base)
                    656:        }
                    657:
                    658:        test("dir/file", "dir/", "file")
                    659: }
                    660:
                    661: func (s *Suite) Test_CurrPath_Parts(c *check.C) {
                    662:        t := s.Init(c)
                    663:
                    664:        test := func(curr CurrPath, parts ...string) {
                    665:                t.CheckDeepEquals(curr.Parts(), parts)
                    666:        }
                    667:
                    668:        test("dir/file", "dir", "file")
                    669: }
                    670:
                    671: func (s *Suite) Test_CurrPath_IsAbs(c *check.C) {
                    672:        t := s.Init(c)
                    673:
                    674:        test := func(curr CurrPath, isAbs bool) {
                    675:                t.CheckDeepEquals(curr.IsAbs(), isAbs)
                    676:        }
                    677:
                    678:        test("/", true)
                    679:        test("./", false)
                    680:        test("C:/", runtime.GOOS == "windows")
                    681: }
                    682:
                    683: func (s *Suite) Test_CurrPath_HasPrefixPath(c *check.C) {
                    684:        t := s.Init(c)
                    685:
                    686:        test := func(curr, prefix CurrPath, hasPrefix bool) {
                    687:                t.CheckEquals(curr.HasPrefixPath(prefix), hasPrefix)
                    688:        }
                    689:
                    690:        test("dir/file", "dir", true)
                    691:        test("dir/file", "file", false)
                    692:        test("dir", ".", true)
                    693: }
                    694:
                    695: func (s *Suite) Test_CurrPath_ContainsPath(c *check.C) {
                    696:        t := s.Init(c)
                    697:
                    698:        test := func(curr CurrPath, sub Path, hasPrefix bool) {
                    699:                t.CheckEquals(curr.ContainsPath(sub), hasPrefix)
                    700:        }
                    701:
                    702:        test("dir/file", "dir", true)
                    703:        test("dir/file", "file", true)
                    704:        test("dir/file", "fi", false)
                    705:        test("dir", ".", true)
                    706: }
                    707:
                    708: func (s *Suite) Test_CurrPath_ContainsText(c *check.C) {
                    709:        t := s.Init(c)
                    710:
                    711:        test := func(curr CurrPath, sub string, hasPrefix bool) {
                    712:                t.CheckEquals(curr.ContainsText(sub), hasPrefix)
                    713:        }
                    714:
                    715:        test("dir/file", "dir", true)
                    716:        test("dir/file", "r/f", true)
                    717: }
                    718:
                    719: func (s *Suite) Test_CurrPath_HasSuffixPath(c *check.C) {
                    720:        t := s.Init(c)
                    721:
                    722:        test := func(curr CurrPath, suffix Path, hasPrefix bool) {
                    723:                t.CheckEquals(curr.HasSuffixPath(suffix), hasPrefix)
                    724:        }
                    725:
                    726:        test("dir/file", "dir", false)
                    727:        test("dir/file", "file", true)
                    728:        test("dir/file", "le", false)
                    729:
                    730:        // In contrast to HasPrefixPath, it doesn't really make sense to
                    731:        // ask whether a path ends with the current directory.
                    732:        test("dir", ".", false)
                    733: }
                    734:
                    735: func (s *Suite) Test_CurrPath_HasSuffixText(c *check.C) {
                    736:        t := s.Init(c)
                    737:
                    738:        test := func(curr CurrPath, suffix string, hasPrefix bool) {
                    739:                t.CheckEquals(curr.HasSuffixText(suffix), hasPrefix)
                    740:        }
                    741:
                    742:        test("dir/file", "dir", false)
                    743:        test("dir/file", "file", true)
                    744:        test("dir/file", "le", true)
                    745: }
                    746:
                    747: func (s *Suite) Test_CurrPath_HasBase(c *check.C) {
                    748:        t := s.Init(c)
                    749:
                    750:        test := func(curr CurrPath, base string, hasPrefix bool) {
                    751:                t.CheckEquals(curr.HasBase(base), hasPrefix)
                    752:        }
                    753:
                    754:        test("dir/file", "dir", false)
                    755:        test("dir/file", "file", true)
                    756:        test("dir/file", "le", false)
                    757: }
                    758:
                    759: func (s *Suite) Test_CurrPath_TrimSuffix(c *check.C) {
                    760:        t := s.Init(c)
                    761:
                    762:        test := func(curr CurrPath, suffix string, trimmed CurrPath) {
                    763:                t.CheckEquals(curr.TrimSuffix(suffix), trimmed)
                    764:        }
                    765:
                    766:        test("dir/file", "dir", "dir/file")
                    767:        test("dir/file", "file", "dir/")
                    768:        test("dir/file", "le", "dir/fi")
                    769: }
                    770:
                    771: func (s *Suite) Test_CurrPath_ReplaceSuffix(c *check.C) {
                    772:        t := s.Init(c)
                    773:
                    774:        test := func(curr CurrPath, from, to string, replaced CurrPath) {
                    775:                t.CheckEquals(curr.ReplaceSuffix(from, to), replaced)
                    776:        }
                    777:
                    778:        test("dir/file", "file", "subdir", "dir/subdir")
                    779:
                    780:        // The path must actually end with the suffix, otherwise there is
                    781:        // the risk of creating unintended paths.
                    782:        t.ExpectAssert(
                    783:                func() { test("dir/file", "no-match", "anything", "dir/file") })
                    784: }
                    785:
                    786: func (s *Suite) Test_CurrPath_Clean(c *check.C) {
                    787:        t := s.Init(c)
                    788:
                    789:        test := func(curr, cleaned CurrPath) {
                    790:                t.CheckEquals(curr.Clean(), cleaned)
                    791:        }
                    792:
                    793:        test("dir/file", "dir/file")
                    794:        test("dir/.////../file", "file")
                    795: }
                    796:
                    797: func (s *Suite) Test_CurrPath_CleanDot(c *check.C) {
                    798:        t := s.Init(c)
                    799:
                    800:        test := func(curr, cleaned CurrPath) {
                    801:                t.CheckEquals(curr.CleanDot(), cleaned)
                    802:        }
                    803:
                    804:        test("dir/file", "dir/file")
                    805:        test("dir/.////../file", "dir/../file")
                    806: }
                    807:
                    808: func (s *Suite) Test_CurrPath_CleanPath(c *check.C) {
                    809:        t := s.Init(c)
                    810:
                    811:        test := func(curr, cleaned CurrPath) {
                    812:                t.CheckEquals(curr.CleanPath(), cleaned)
                    813:        }
                    814:
                    815:        test("a/b/../../c/d/../../e/../f", "a/b/../../e/../f")
                    816: }
                    817:
                    818: func (s *Suite) Test_CurrPath_JoinNoClean(c *check.C) {
                    819:        t := s.Init(c)
                    820:
1.5       rillig    821:        test := func(curr CurrPath, rel RelPath, joined CurrPath) {
                    822:                t.CheckEquals(curr.JoinNoClean(rel), joined)
1.4       rillig    823:        }
                    824:
                    825:        test("", "", "/")
                    826:        test(".", "file", "./file")
                    827:        test("dir", "subdir/file", "dir/subdir/file")
                    828: }
                    829:
                    830: func (s *Suite) Test_CurrPath_JoinClean(c *check.C) {
                    831:        t := s.Init(c)
                    832:
1.5       rillig    833:        test := func(curr CurrPath, rel RelPath, joined CurrPath) {
                    834:                t.CheckEquals(curr.JoinClean(rel), joined)
1.4       rillig    835:        }
                    836:
                    837:        test("", "", "")
                    838:        test(".", "./////file", "file")
                    839:        test("dir/./.", "../subdir/file", "subdir/file")
                    840: }
                    841:
                    842: func (s *Suite) Test_CurrPath_Rel(c *check.C) {
                    843:        t := s.Init(c)
                    844:
1.5       rillig    845:        test := func(curr, rel CurrPath, result RelPath) {
                    846:                t.CheckEquals(curr.Rel(rel), result)
1.4       rillig    847:        }
                    848:
                    849:        test("dir/subdir", "dir", "..")
                    850:        test("dir/subdir", "file", "../../file")
                    851: }
                    852:
                    853: func (s *Suite) Test_CurrPath_Rename(c *check.C) {
1.1       rillig    854:        t := s.Init(c)
                    855:
                    856:        f := t.CreateFileLines("filename.old",
                    857:                "line 1")
                    858:        t.CheckEquals(f.IsFile(), true)
1.4       rillig    859:        dst := f.ReplaceSuffix(".old", ".new")
1.1       rillig    860:
                    861:        err := f.Rename(dst)
                    862:
                    863:        assertNil(err, "Rename")
                    864:        t.CheckEquals(f.IsFile(), false)
                    865:        t.CheckFileLines("filename.new",
                    866:                "line 1")
                    867: }
                    868:
1.4       rillig    869: func (s *Suite) Test_CurrPath_Lstat(c *check.C) {
1.1       rillig    870:        t := s.Init(c)
                    871:
1.5       rillig    872:        test := func(f CurrPath, isDir bool) {
1.1       rillig    873:                st, err := f.Lstat()
                    874:                assertNil(err, "Lstat")
                    875:                t.CheckEquals(st.Mode()&os.ModeDir != 0, isDir)
                    876:        }
                    877:
                    878:        t.CreateFileLines("subdir/file")
                    879:        t.CreateFileLines("file")
                    880:
1.5       rillig    881:        test(t.File("subdir"), true)
                    882:        test(t.File("file"), false)
1.1       rillig    883: }
                    884:
1.4       rillig    885: func (s *Suite) Test_CurrPath_Stat(c *check.C) {
1.1       rillig    886:        t := s.Init(c)
                    887:
1.5       rillig    888:        test := func(f CurrPath, isDir bool) {
1.1       rillig    889:                st, err := f.Stat()
                    890:                assertNil(err, "Stat")
                    891:                t.CheckEquals(st.Mode()&os.ModeDir != 0, isDir)
                    892:        }
                    893:
                    894:        t.CreateFileLines("subdir/file")
                    895:        t.CreateFileLines("file")
                    896:
1.5       rillig    897:        test(t.File("subdir"), true)
                    898:        test(t.File("file"), false)
1.1       rillig    899: }
                    900:
1.4       rillig    901: func (s *Suite) Test_CurrPath_Exists(c *check.C) {
1.1       rillig    902:        t := s.Init(c)
                    903:
1.4       rillig    904:        test := func(f CurrPath, exists bool) {
1.1       rillig    905:                t.CheckEquals(f.Exists(), exists)
                    906:        }
                    907:
                    908:        t.CreateFileLines("subdir/file")
                    909:        t.CreateFileLines("file")
                    910:
                    911:        test(t.File("subdir"), true)
                    912:        test(t.File("file"), true)
                    913:        test(t.File("enoent"), false)
                    914: }
                    915:
1.4       rillig    916: func (s *Suite) Test_CurrPath_IsFile(c *check.C) {
1.1       rillig    917:        t := s.Init(c)
                    918:
                    919:        t.CreateFileLines("dir/file")
1.5       rillig    920:        t.Chdir(".")
1.1       rillig    921:
1.5       rillig    922:        test := func(curr CurrPath, isFile bool) {
                    923:                t.CheckEquals(curr.IsFile(), isFile)
                    924:        }
                    925:
                    926:        test("nonexistent", false)
                    927:        test("dir", false)
                    928:        test("dir/nonexistent", false)
                    929:        test("dir/file", true)
1.1       rillig    930: }
                    931:
1.4       rillig    932: func (s *Suite) Test_CurrPath_IsDir(c *check.C) {
1.1       rillig    933:        t := s.Init(c)
                    934:
                    935:        t.CreateFileLines("dir/file")
1.5       rillig    936:        t.Chdir(".")
1.1       rillig    937:
1.5       rillig    938:        test := func(curr CurrPath, isFile bool) {
                    939:                t.CheckEquals(curr.IsDir(), isFile)
                    940:        }
                    941:
                    942:        test("nonexistent", false)
                    943:        test("dir", true)
                    944:        test("dir/nonexistent", false)
                    945:        test("dir/file", false)
1.1       rillig    946: }
                    947:
1.4       rillig    948: func (s *Suite) Test_CurrPath_Chmod(c *check.C) {
1.1       rillig    949:        t := s.Init(c)
                    950:
1.4       rillig    951:        testWritable := func(f CurrPath, writable bool) {
1.1       rillig    952:                lstat, err := f.Lstat()
                    953:                assertNil(err, "Lstat")
                    954:                t.CheckEquals(lstat.Mode().Perm()&0200 != 0, writable)
                    955:        }
                    956:
                    957:        f := t.CreateFileLines("file")
                    958:        testWritable(f, true)
                    959:
                    960:        err := f.Chmod(0444)
                    961:        assertNil(err, "Chmod")
                    962:
                    963:        testWritable(f, false)
                    964: }
                    965:
1.4       rillig    966: func (s *Suite) Test_CurrPath_ReadDir(c *check.C) {
1.1       rillig    967:        t := s.Init(c)
                    968:
                    969:        t.CreateFileLines("subdir/file")
                    970:        t.CreateFileLines("file")
                    971:        t.CreateFileLines("CVS/Entries")
                    972:        t.CreateFileLines(".git/info/exclude")
1.5       rillig    973:        t.Chdir(".")
1.1       rillig    974:
1.5       rillig    975:        test := func(curr CurrPath, entries ...string) {
                    976:                infos, err := curr.ReadDir()
                    977:                assertNil(err, "ReadDir")
                    978:
                    979:                var names []string
                    980:                for _, info := range infos {
                    981:                        names = append(names, info.Name())
                    982:                }
                    983:
                    984:                t.CheckDeepEquals(names, entries)
                    985:        }
                    986:
                    987:        test(".",
                    988:                ".git", "CVS", "file", "subdir")
                    989:        test("subdir",
                    990:                "file")
                    991:        test("CVS",
                    992:                "Entries")
1.1       rillig    993: }
                    994:
1.4       rillig    995: func (s *Suite) Test_CurrPath_ReadPaths(c *check.C) {
                    996:        t := s.Init(c)
                    997:
                    998:        t.CreateFileLines("dir/subdir/file")
                    999:        t.CreateFileLines("dir/CVS/Entries")
                   1000:        t.CreateFileLines("dir/file")
1.5       rillig   1001:        t.Chdir(".")
1.4       rillig   1002:
1.5       rillig   1003:        test := func(dir CurrPath, entries ...CurrPath) {
                   1004:                t.CheckDeepEquals(dir.ReadPaths(), entries)
                   1005:        }
1.4       rillig   1006:
1.5       rillig   1007:        test(".",
                   1008:                "dir")
1.4       rillig   1009:
1.5       rillig   1010:        test("dir",
                   1011:                "dir/file", "dir/subdir")
1.4       rillig   1012: }
                   1013:
                   1014: func (s *Suite) Test_CurrPath_Open(c *check.C) {
1.1       rillig   1015:        t := s.Init(c)
                   1016:
                   1017:        t.CreateFileLines("filename",
                   1018:                "line 1",
                   1019:                "line 2")
1.5       rillig   1020:        t.Chdir(".")
                   1021:
                   1022:        test := func(curr CurrPath, content string) {
                   1023:                f, err := curr.Open()
                   1024:                assertNil(err, "Open")
                   1025:                defer func() { assertNil(f.Close(), "Close") }()
1.1       rillig   1026:
1.5       rillig   1027:                var sb strings.Builder
                   1028:                n, err := io.Copy(&sb, f)
                   1029:                assertNil(err, "Copy")
1.1       rillig   1030:
1.5       rillig   1031:                t.CheckEquals(n, int64(len(content)))
                   1032:                t.CheckEquals(sb.String(), content)
                   1033:        }
                   1034:
                   1035:        test("filename", "line 1\nline 2\n")
1.1       rillig   1036: }
                   1037:
1.4       rillig   1038: func (s *Suite) Test_CurrPath_ReadString(c *check.C) {
1.1       rillig   1039:        t := s.Init(c)
                   1040:
1.5       rillig   1041:        t.Chdir(".")
                   1042:        t.CreateFileLines("empty")
1.1       rillig   1043:        t.CreateFileLines("filename",
                   1044:                "line 1",
                   1045:                "line 2")
                   1046:
1.5       rillig   1047:        test := func(curr CurrPath, content string) {
                   1048:                text, err := curr.ReadString()
1.1       rillig   1049:
1.5       rillig   1050:                assertNil(err, "ReadString")
                   1051:                t.CheckEquals(text, content)
                   1052:        }
                   1053:
                   1054:        test("empty", "")
                   1055:        test("filename", "line 1\nline 2\n")
1.1       rillig   1056: }
                   1057:
1.4       rillig   1058: func (s *Suite) Test_CurrPath_WriteString(c *check.C) {
1.1       rillig   1059:        t := s.Init(c)
                   1060:
1.5       rillig   1061:        t.Chdir(".")
                   1062:
                   1063:        test := func(curr CurrPath, content string, lines ...string) {
                   1064:                err := curr.WriteString(content)
                   1065:                assertNil(err, "WriteString")
1.1       rillig   1066:
1.5       rillig   1067:                t.CheckFileLines(NewRelPath(curr.AsPath()),
                   1068:                        lines...)
                   1069:        }
                   1070:
                   1071:        test("empty", "",
                   1072:                nil...)
                   1073:
                   1074:        test("filename", "line 1\nline 2\n",
1.1       rillig   1075:                "line 1",
                   1076:                "line 2")
                   1077: }
1.4       rillig   1078:
                   1079: func (s *Suite) Test_NewPkgsrcPath(c *check.C) {
                   1080:        t := s.Init(c)
                   1081:
                   1082:        p := NewPkgsrcPath("category/package")
                   1083:
                   1084:        t.CheckEquals(p.AsPath(), NewPath("category/package"))
                   1085: }
                   1086:
                   1087: func (s *Suite) Test_PkgsrcPath_String(c *check.C) {
                   1088:        t := s.Init(c)
                   1089:
                   1090:        p := NewPkgsrcPath("any string..././")
                   1091:
                   1092:        str := p.String()
                   1093:
                   1094:        // No normalization takes place because it is typically not needed.
                   1095:        t.CheckEquals(str, "any string..././")
                   1096: }
                   1097:
                   1098: func (s *Suite) Test_PkgsrcPath_AsPath(c *check.C) {
                   1099:        t := s.Init(c)
                   1100:
                   1101:        pp := NewPkgsrcPath("./category/package/Makefile")
                   1102:
                   1103:        p := pp.AsPath()
                   1104:
                   1105:        t.CheckEquals(p.String(), "./category/package/Makefile")
                   1106: }
                   1107:
1.5       rillig   1108: func (s *Suite) Test_PkgsrcPath_AsRelPath(c *check.C) {
                   1109:        t := s.Init(c)
                   1110:
                   1111:        pp := NewPkgsrcPath("./category/package/Makefile")
                   1112:
                   1113:        rel := pp.AsRelPath()
                   1114:
                   1115:        t.CheckEquals(rel.String(), "./category/package/Makefile")
                   1116: }
                   1117:
1.4       rillig   1118: func (s *Suite) Test_PkgsrcPath_DirClean(c *check.C) {
                   1119:        t := s.Init(c)
                   1120:
1.5       rillig   1121:        test := func(pp, cleaned PkgsrcPath) {
                   1122:                t.CheckEquals(pp.DirClean(), cleaned)
                   1123:        }
1.4       rillig   1124:
1.5       rillig   1125:        test("./dir/../dir/base///.", "dir/base")
1.4       rillig   1126: }
                   1127:
                   1128: func (s *Suite) Test_PkgsrcPath_DirNoClean(c *check.C) {
                   1129:        t := s.Init(c)
                   1130:
1.5       rillig   1131:        test := func(pp, cleaned PkgsrcPath) {
                   1132:                t.CheckEquals(pp.DirNoClean(), cleaned)
                   1133:        }
1.4       rillig   1134:
1.5       rillig   1135:        test("./dir/../dir/base///.", "./dir/../dir/base")
1.4       rillig   1136: }
                   1137:
                   1138: func (s *Suite) Test_PkgsrcPath_Base(c *check.C) {
                   1139:        t := s.Init(c)
                   1140:
1.5       rillig   1141:        test := func(pp PkgsrcPath, base string) {
                   1142:                t.CheckEquals(pp.Base(), base)
                   1143:        }
1.4       rillig   1144:
1.5       rillig   1145:        test("./dir/../dir/base///.", ".")
1.4       rillig   1146: }
                   1147:
                   1148: func (s *Suite) Test_PkgsrcPath_Count(c *check.C) {
                   1149:        t := s.Init(c)
                   1150:
1.5       rillig   1151:        test := func(pp PkgsrcPath, count int) {
                   1152:                t.CheckEquals(pp.Count(), count)
                   1153:        }
1.4       rillig   1154:
1.5       rillig   1155:        test("./...////dir", 2)
1.4       rillig   1156: }
                   1157:
                   1158: func (s *Suite) Test_PkgsrcPath_HasPrefixPath(c *check.C) {
                   1159:        t := s.Init(c)
                   1160:
1.5       rillig   1161:        test := func(pp PkgsrcPath, prefix Path, hasPrefixPath bool) {
                   1162:                t.CheckEquals(pp.HasPrefixPath(prefix), hasPrefixPath)
                   1163:        }
1.4       rillig   1164:
1.5       rillig   1165:        test("./././///prefix/suffix", "prefix", true)
1.4       rillig   1166: }
                   1167:
                   1168: func (s *Suite) Test_PkgsrcPath_JoinNoClean(c *check.C) {
                   1169:        t := s.Init(c)
                   1170:
1.5       rillig   1171:        test := func(pp PkgsrcPath, rel RelPath, joined PkgsrcPath) {
                   1172:                t.CheckEquals(pp.JoinNoClean(rel), joined)
                   1173:        }
1.4       rillig   1174:
1.5       rillig   1175:        test("base///.", "./../rel", "base///././../rel")
1.4       rillig   1176: }
                   1177:
1.5       rillig   1178: func (s *Suite) Test_NewPackagePath(c *check.C) {
1.4       rillig   1179:        t := s.Init(c)
                   1180:
1.5       rillig   1181:        p := NewPackagePath("../../category/package")
1.4       rillig   1182:
1.5       rillig   1183:        t.CheckEquals(p.AsPath(), NewPath("../../category/package"))
1.4       rillig   1184: }
                   1185:
1.5       rillig   1186: func (s *Suite) Test_NewPackagePathString(c *check.C) {
1.4       rillig   1187:        t := s.Init(c)
                   1188:
1.5       rillig   1189:        p := NewPackagePathString("../../category/package")
1.4       rillig   1190:
                   1191:        t.CheckEquals(p.AsPath(), NewPath("../../category/package"))
                   1192: }
                   1193:
                   1194: func (s *Suite) Test_PackagePath_AsPath(c *check.C) {
                   1195:        t := s.Init(c)
                   1196:
                   1197:        pp := NewPackagePath("../../category/package/Makefile")
                   1198:
                   1199:        p := pp.AsPath()
                   1200:
                   1201:        t.CheckEquals(p.String(), "../../category/package/Makefile")
                   1202: }
                   1203:
1.5       rillig   1204: func (s *Suite) Test_PackagePath_AsRelPath(c *check.C) {
                   1205:        t := s.Init(c)
                   1206:
                   1207:        pp := NewPackagePath("./category/package/Makefile")
                   1208:
                   1209:        rel := pp.AsRelPath()
                   1210:
                   1211:        t.CheckEquals(rel.String(), "./category/package/Makefile")
                   1212: }
                   1213:
1.4       rillig   1214: func (s *Suite) Test_PackagePath_String(c *check.C) {
                   1215:        t := s.Init(c)
                   1216:
                   1217:        pp := NewPackagePath("../../category/package/Makefile")
                   1218:
                   1219:        str := pp.String()
                   1220:
                   1221:        t.CheckEquals(str, "../../category/package/Makefile")
                   1222: }
                   1223:
                   1224: func (s *Suite) Test_PackagePath_JoinNoClean(c *check.C) {
                   1225:        t := s.Init(c)
                   1226:
1.5       rillig   1227:        test := func(pp PackagePath, other RelPath, joined PackagePath) {
                   1228:                t.CheckEquals(pp.JoinNoClean(other), joined)
1.4       rillig   1229:
1.5       rillig   1230:        }
1.4       rillig   1231:
1.5       rillig   1232:        test("../../category/package/patches", "patch-aa",
                   1233:                "../../category/package/patches/patch-aa")
1.4       rillig   1234: }
                   1235:
                   1236: func (s *Suite) Test_PackagePath_IsEmpty(c *check.C) {
                   1237:        t := s.Init(c)
                   1238:
                   1239:        test := func(p PackagePath, isEmpty bool) {
                   1240:                t.CheckEquals(p.IsEmpty(), isEmpty)
                   1241:        }
                   1242:
                   1243:        test("", true)
                   1244:        test(".", false)
                   1245: }
                   1246:
                   1247: func (s *Suite) Test_NewRelPath(c *check.C) {
                   1248:        t := s.Init(c)
                   1249:
                   1250:        rel := NewRelPath("dir/file")
                   1251:
                   1252:        t.CheckEquals(rel.String(), "dir/file")
                   1253: }
                   1254:
                   1255: func (s *Suite) Test_NewRelPathString(c *check.C) {
                   1256:        t := s.Init(c)
                   1257:
                   1258:        rel := NewRelPathString("dir/file")
                   1259:
                   1260:        t.CheckEquals(rel.String(), "dir/file")
                   1261: }
                   1262:
                   1263: func (s *Suite) Test_RelPath_AsPath(c *check.C) {
                   1264:        t := s.Init(c)
                   1265:
                   1266:        rel := NewRelPath("relative")
                   1267:
                   1268:        path := rel.AsPath()
                   1269:
                   1270:        t.CheckEquals(path.String(), "relative")
                   1271: }
                   1272:
                   1273: func (s *Suite) Test_RelPath_String(c *check.C) {
                   1274:        t := s.Init(c)
                   1275:
                   1276:        rel := NewRelPath(".///rel")
                   1277:
                   1278:        str := rel.String()
                   1279:
                   1280:        t.CheckEquals(str, ".///rel")
                   1281: }
                   1282:
1.5       rillig   1283: func (s *Suite) Test_RelPath_IsEmpty(c *check.C) {
                   1284:        t := s.Init(c)
                   1285:
                   1286:        test := func(rel RelPath, isEmpty bool) {
                   1287:                t.CheckEquals(rel.IsEmpty(), isEmpty)
                   1288:        }
                   1289:
                   1290:        test("", true)
                   1291:        test(".", false)
                   1292:        test("/", false)
                   1293: }
                   1294:
                   1295: func (s *Suite) Test_RelPath_Split(c *check.C) {
                   1296:        t := s.Init(c)
                   1297:
                   1298:        test := func(rel RelPath, dir RelPath, base string) {
                   1299:                actualDir, actualBase := rel.Split()
                   1300:                t.CheckEquals(actualDir, dir)
                   1301:                t.CheckEquals(actualBase, base)
                   1302:        }
                   1303:
                   1304:        test("dir/file", "dir/", "file")
                   1305:        test("././///file", "././///", "file")
                   1306:
                   1307:        t.ExpectAssert(
                   1308:                func() { test("/", "/", "") })
                   1309:
                   1310: }
                   1311:
1.4       rillig   1312: func (s *Suite) Test_RelPath_DirClean(c *check.C) {
                   1313:        t := s.Init(c)
                   1314:
1.5       rillig   1315:        test := func(rel RelPath, dir RelPath) {
                   1316:                t.CheckEquals(rel.DirClean(), dir)
                   1317:        }
1.4       rillig   1318:
1.5       rillig   1319:        test("./dir/../dir///./file", "dir")
1.4       rillig   1320: }
                   1321:
                   1322: func (s *Suite) Test_RelPath_DirNoClean(c *check.C) {
                   1323:        t := s.Init(c)
                   1324:
1.5       rillig   1325:        test := func(rel RelPath, dir RelPath) {
                   1326:                t.CheckEquals(rel.DirNoClean(), dir)
                   1327:        }
1.4       rillig   1328:
1.5       rillig   1329:        test("./dir/../dir///./file", "./dir/../dir")
1.4       rillig   1330: }
                   1331:
                   1332: func (s *Suite) Test_RelPath_Base(c *check.C) {
                   1333:        t := s.Init(c)
                   1334:
1.5       rillig   1335:        test := func(rel RelPath, base string) {
                   1336:                t.CheckEquals(rel.Base(), base)
                   1337:        }
1.4       rillig   1338:
1.5       rillig   1339:        test("./dir/../dir///./file", "file")
1.4       rillig   1340: }
                   1341:
                   1342: func (s *Suite) Test_RelPath_HasBase(c *check.C) {
                   1343:        t := s.Init(c)
                   1344:
                   1345:        test := func(rel RelPath, base string, hasBase bool) {
                   1346:                t.CheckEquals(rel.HasBase(base), hasBase)
                   1347:        }
                   1348:
                   1349:        test("./dir/Makefile", "Makefile", true)
                   1350:        test("./dir/Makefile", "Make", false)
                   1351:        test("./dir/Makefile", "file", false)
                   1352:        test("./dir/Makefile", "dir/Makefile", false)
                   1353: }
                   1354:
                   1355: func (s *Suite) Test_RelPath_Parts(c *check.C) {
                   1356:        t := s.Init(c)
                   1357:
1.5       rillig   1358:        test := func(rel RelPath, parts ...string) {
                   1359:                t.CheckDeepEquals(rel.Parts(), parts)
                   1360:        }
1.4       rillig   1361:
1.5       rillig   1362:        test("./dir/.///base", "dir", "base")
1.4       rillig   1363: }
                   1364:
                   1365: func (s *Suite) Test_RelPath_Count(c *check.C) {
                   1366:        t := s.Init(c)
                   1367:
1.5       rillig   1368:        test := func(rel RelPath, count int) {
                   1369:                t.CheckEquals(rel.Count(), count)
                   1370:        }
1.4       rillig   1371:
1.5       rillig   1372:        test("./dir/.///base", 2)
1.4       rillig   1373: }
                   1374:
                   1375: func (s *Suite) Test_RelPath_Clean(c *check.C) {
                   1376:        t := s.Init(c)
                   1377:
1.5       rillig   1378:        test := func(rel RelPath, cleaned RelPath) {
                   1379:                t.CheckDeepEquals(rel.Clean(), cleaned)
                   1380:        }
1.4       rillig   1381:
1.5       rillig   1382:        test("a/b/../../c/d/../.././e/../f", "f")
                   1383: }
                   1384:
                   1385: func (s *Suite) Test_RelPath_CleanDot(c *check.C) {
                   1386:        t := s.Init(c)
1.4       rillig   1387:
1.5       rillig   1388:        test := func(rel RelPath, cleaned RelPath) {
                   1389:                t.CheckEquals(rel.CleanDot(), cleaned)
                   1390:        }
                   1391:
                   1392:        test("a/b/../../c/d/../.././e/../f", "a/b/../../c/d/../../e/../f")
1.4       rillig   1393: }
                   1394:
                   1395: func (s *Suite) Test_RelPath_CleanPath(c *check.C) {
                   1396:        t := s.Init(c)
                   1397:
1.5       rillig   1398:        test := func(rel RelPath, cleaned RelPath) {
                   1399:                t.CheckEquals(rel.CleanPath(), cleaned)
                   1400:        }
1.4       rillig   1401:
1.5       rillig   1402:        test("a/b/../../c/d/../.././e/../f", "a/b/../../e/../f")
1.4       rillig   1403: }
                   1404:
                   1405: func (s *Suite) Test_RelPath_JoinNoClean(c *check.C) {
                   1406:        t := s.Init(c)
                   1407:
1.5       rillig   1408:        test := func(rel, other, joined RelPath) {
                   1409:                t.CheckEquals(rel.JoinNoClean(other), joined)
                   1410:        }
1.4       rillig   1411:
1.5       rillig   1412:        test("basedir/.//", "./other", "basedir/.///./other")
1.4       rillig   1413: }
                   1414:
                   1415: func (s *Suite) Test_RelPath_Replace(c *check.C) {
                   1416:        t := s.Init(c)
                   1417:
1.5       rillig   1418:        test := func(rel RelPath, from, to string, result RelPath) {
                   1419:                t.CheckEquals(rel.Replace(from, to), result)
                   1420:        }
                   1421:
                   1422:        test("dir/subdir/file", "/", ":", "dir:subdir:file")
                   1423: }
1.4       rillig   1424:
1.5       rillig   1425: func (s *Suite) Test_RelPath_HasPrefixPath(c *check.C) {
                   1426:        t := s.Init(c)
                   1427:
                   1428:        test := func(rel RelPath, prefix Path, hasPrefixPath bool) {
                   1429:                t.CheckEquals(rel.HasPrefixPath(prefix), hasPrefixPath)
                   1430:        }
1.4       rillig   1431:
1.5       rillig   1432:        test("dir/subdir/file", "dir", true)
                   1433:        test("dir/subdir/file", "dir/sub", false)
                   1434:        test("dir/subdir/file", "subdir", false)
1.4       rillig   1435: }
                   1436:
1.5       rillig   1437: func (s *Suite) Test_RelPath_HasPrefixText(c *check.C) {
1.4       rillig   1438:        t := s.Init(c)
                   1439:
1.5       rillig   1440:        test := func(rel RelPath, prefix string, hasPrefixPath bool) {
                   1441:                t.CheckEquals(rel.HasPrefixText(prefix), hasPrefixPath)
                   1442:        }
1.4       rillig   1443:
1.5       rillig   1444:        test("dir/subdir/file", "dir", true)
                   1445:        test("dir/subdir/file", "dir/sub", true)
                   1446:        test("dir/subdir/file", "subdir", false)
                   1447:        test("dir/subdir/file", "super", false)
1.4       rillig   1448: }
                   1449:
                   1450: func (s *Suite) Test_RelPath_ContainsPath(c *check.C) {
                   1451:        t := s.Init(c)
                   1452:
1.5       rillig   1453:        test := func(rel RelPath, prefix Path, hasPrefixPath bool) {
                   1454:                t.CheckEquals(rel.ContainsPath(prefix), hasPrefixPath)
                   1455:        }
1.4       rillig   1456:
1.5       rillig   1457:        test("dir/subdir/file", "dir", true)
                   1458:        test("dir/subdir/file", "dir/sub", false)
                   1459:        test("dir/subdir/file", "subdir", true)
1.4       rillig   1460: }
                   1461:
                   1462: func (s *Suite) Test_RelPath_ContainsText(c *check.C) {
                   1463:        t := s.Init(c)
                   1464:
1.5       rillig   1465:        test := func(rel RelPath, prefix string, hasPrefixPath bool) {
                   1466:                t.CheckEquals(rel.ContainsText(prefix), hasPrefixPath)
                   1467:        }
1.4       rillig   1468:
1.5       rillig   1469:        test("dir/subdir/file", "dir", true)
                   1470:        test("dir/subdir/file", "dir/sub", true)
                   1471:        test("dir/subdir/file", "subdir", true)
                   1472:        test("dir/subdir/file", "super", false)
1.4       rillig   1473: }
                   1474:
                   1475: func (s *Suite) Test_RelPath_HasSuffixPath(c *check.C) {
                   1476:        t := s.Init(c)
                   1477:
1.5       rillig   1478:        test := func(rel RelPath, prefix Path, hasPrefixPath bool) {
                   1479:                t.CheckEquals(rel.HasSuffixPath(prefix), hasPrefixPath)
                   1480:        }
                   1481:
                   1482:        test("dir/subdir/file", "dir", false)
                   1483:        test("dir/subdir/file", "file", true)
                   1484:        test("dir/subdir/file", "le", false)
                   1485:        test("dir/subdir/file", "subdir/file", true)
                   1486:        test("dir/subdir/file", "subdir", false)
                   1487: }
                   1488:
                   1489: func (s *Suite) Test_RelPath_HasSuffixText(c *check.C) {
                   1490:        t := s.Init(c)
                   1491:
                   1492:        test := func(rel RelPath, prefix string, hasPrefixPath bool) {
                   1493:                t.CheckEquals(rel.HasSuffixText(prefix), hasPrefixPath)
                   1494:        }
                   1495:
                   1496:        test("dir/subdir/file", "dir", false)
                   1497:        test("dir/subdir/file", "file", true)
                   1498:        test("dir/subdir/file", "le", true)
                   1499:        test("dir/subdir/file", "subdir/file", true)
                   1500:        test("dir/subdir/file", "subdir", false)
                   1501: }
                   1502:
                   1503: func (s *Suite) Test_RelPath_Rel(c *check.C) {
                   1504:        t := s.Init(c)
                   1505:
                   1506:        test := func(base RelPath, other Path, result RelPath) {
                   1507:                t.CheckEquals(base.Rel(other), result)
                   1508:        }
                   1509:
                   1510:        test("a/b/c", "d/e/f/file", "../../../d/e/f/file")
                   1511:        test(".", ".", ".")
1.4       rillig   1512:
1.5       rillig   1513:        // The trailing dot marks the difference between a file and a directory.
                   1514:        // This is the same behavior as with filepath.Rel.
                   1515:        test("a/b/c", ".", "../../../.")
1.4       rillig   1516: }

CVSweb <webmaster@jp.NetBSD.org>