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

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")
                    528:        test(".", "dir/subdir/", "dir/subdir")  // FIXME: missing /. at the end
                    529:        test(".", "dir/subdir/.", "dir/subdir") // FIXME: missing /. at the end
1.1       rillig    530: }
                    531:
1.4       rillig    532: func (s *Suite) Test_NewCurrPath(c *check.C) {
                    533:        t := s.Init(c)
                    534:
                    535:        curr := NewCurrPath("dir/.///file")
                    536:
                    537:        t.CheckEquals(curr.String(), "dir/.///file")
                    538: }
                    539:
                    540: func (s *Suite) Test_NewCurrPathString(c *check.C) {
                    541:        t := s.Init(c)
                    542:
                    543:        curr := NewCurrPathString("dir/.///file")
                    544:
                    545:        t.CheckEquals(curr.String(), "dir/.///file")
                    546: }
                    547:
                    548: func (s *Suite) Test_NewCurrPathSlash(c *check.C) {
                    549:        t := s.Init(c)
                    550:
                    551:        test := func(path, curr string) {
                    552:                t.CheckEquals(NewCurrPathSlash(path).String(), curr)
                    553:        }
                    554:
                    555:        test("filename", "filename")
                    556:        test("dir/.///file", "dir/.///file")
                    557: }
                    558:
                    559: func (s *Suite) Test_NewCurrPathSlash__windows(c *check.C) {
                    560:        t := s.Init(c)
                    561:
1.5       rillig    562:        test := func(path, currWindows, currOther string) {
                    563:                t.CheckEquals(
                    564:                        NewCurrPathSlash(path).String(),
                    565:                        condStr(runtime.GOOS == "windows", currWindows, currOther))
1.4       rillig    566:        }
                    567:
1.5       rillig    568:        test("\\", "/", "\\")
                    569:        test("dir\\.\\\\\\file", "dir/.///file", "dir\\.\\\\\\file")
1.4       rillig    570: }
                    571:
                    572: func (s *Suite) Test_CurrPath_GoString(c *check.C) {
                    573:        t := s.Init(c)
                    574:
1.5       rillig    575:        test := func(p CurrPath, str string) {
                    576:                t.CheckEquals(p.GoString(), str)
                    577:        }
                    578:
1.4       rillig    579:        // Tabs in filenames are rare, probably typos.
1.5       rillig    580:        test("dir/file\t", "\"dir/file\\t\"")
1.4       rillig    581: }
                    582:
                    583: func (s *Suite) Test_CurrPath_String(c *check.C) {
                    584:        t := s.Init(c)
                    585:
1.5       rillig    586:        test := func(p CurrPath, str string) {
                    587:                t.CheckEquals(p.String(), str)
                    588:        }
                    589:
1.4       rillig    590:        // Tabs in filenames are rare, probably typos.
1.5       rillig    591:        test("dir/file\t", "dir/file\t")
1.4       rillig    592: }
                    593:
                    594: func (s *Suite) Test_CurrPath_AsPath(c *check.C) {
                    595:        t := s.Init(c)
                    596:
1.5       rillig    597:        test := func(curr CurrPath, asPath Path) {
                    598:                t.CheckEquals(curr.AsPath(), asPath)
                    599:        }
                    600:
1.4       rillig    601:        // Tabs in filenames are rare, probably typos.
1.5       rillig    602:        test("dir/file\t", "dir/file\t")
1.4       rillig    603: }
                    604:
                    605: func (s *Suite) Test_CurrPath_IsEmpty(c *check.C) {
                    606:        t := s.Init(c)
                    607:
                    608:        test := func(curr CurrPath, isEmpty bool) {
                    609:                t.CheckEquals(curr.IsEmpty(), isEmpty)
                    610:        }
                    611:
                    612:        test("", true)
                    613:        test(".", false)
                    614:        test("/", false)
                    615: }
                    616:
                    617: func (s *Suite) Test_CurrPath_DirClean(c *check.C) {
                    618:        t := s.Init(c)
                    619:
                    620:        test := func(curr, dir CurrPath) {
                    621:                t.CheckEquals(curr.DirClean(), dir)
                    622:        }
                    623:
                    624:        test("./dir/../dir///./file", "dir")
                    625: }
                    626:
                    627: func (s *Suite) Test_CurrPath_DirNoClean(c *check.C) {
                    628:        t := s.Init(c)
                    629:
                    630:        test := func(curr, dir CurrPath) {
                    631:                t.CheckEquals(curr.DirNoClean(), dir)
                    632:        }
                    633:
                    634:        test("./dir/../dir///./file", "./dir/../dir")
                    635: }
                    636:
                    637: func (s *Suite) Test_CurrPath_Base(c *check.C) {
                    638:        t := s.Init(c)
                    639:
                    640:        test := func(curr CurrPath, base string) {
                    641:                t.CheckEquals(curr.Base(), base)
                    642:        }
                    643:
                    644:        test("dir/file", "file")
                    645: }
                    646:
                    647: func (s *Suite) Test_CurrPath_Split(c *check.C) {
                    648:        t := s.Init(c)
                    649:
                    650:        test := func(curr, dir CurrPath, base string) {
                    651:                actualDir, actualBase := curr.Split()
                    652:                t.CheckEquals(actualDir, dir)
                    653:                t.CheckEquals(actualBase, base)
                    654:        }
                    655:
                    656:        test("dir/file", "dir/", "file")
                    657: }
                    658:
                    659: func (s *Suite) Test_CurrPath_Parts(c *check.C) {
                    660:        t := s.Init(c)
                    661:
                    662:        test := func(curr CurrPath, parts ...string) {
                    663:                t.CheckDeepEquals(curr.Parts(), parts)
                    664:        }
                    665:
                    666:        test("dir/file", "dir", "file")
                    667: }
                    668:
                    669: func (s *Suite) Test_CurrPath_IsAbs(c *check.C) {
                    670:        t := s.Init(c)
                    671:
                    672:        test := func(curr CurrPath, isAbs bool) {
                    673:                t.CheckDeepEquals(curr.IsAbs(), isAbs)
                    674:        }
                    675:
                    676:        test("/", true)
                    677:        test("./", false)
                    678:        test("C:/", runtime.GOOS == "windows")
                    679: }
                    680:
                    681: func (s *Suite) Test_CurrPath_HasPrefixPath(c *check.C) {
                    682:        t := s.Init(c)
                    683:
                    684:        test := func(curr, prefix CurrPath, hasPrefix bool) {
                    685:                t.CheckEquals(curr.HasPrefixPath(prefix), hasPrefix)
                    686:        }
                    687:
                    688:        test("dir/file", "dir", true)
                    689:        test("dir/file", "file", false)
                    690:        test("dir", ".", true)
                    691: }
                    692:
                    693: func (s *Suite) Test_CurrPath_ContainsPath(c *check.C) {
                    694:        t := s.Init(c)
                    695:
                    696:        test := func(curr CurrPath, sub Path, hasPrefix bool) {
                    697:                t.CheckEquals(curr.ContainsPath(sub), hasPrefix)
                    698:        }
                    699:
                    700:        test("dir/file", "dir", true)
                    701:        test("dir/file", "file", true)
                    702:        test("dir/file", "fi", false)
                    703:        test("dir", ".", true)
                    704: }
                    705:
                    706: func (s *Suite) Test_CurrPath_ContainsText(c *check.C) {
                    707:        t := s.Init(c)
                    708:
                    709:        test := func(curr CurrPath, sub string, hasPrefix bool) {
                    710:                t.CheckEquals(curr.ContainsText(sub), hasPrefix)
                    711:        }
                    712:
                    713:        test("dir/file", "dir", true)
                    714:        test("dir/file", "r/f", true)
                    715: }
                    716:
                    717: func (s *Suite) Test_CurrPath_HasSuffixPath(c *check.C) {
                    718:        t := s.Init(c)
                    719:
                    720:        test := func(curr CurrPath, suffix Path, hasPrefix bool) {
                    721:                t.CheckEquals(curr.HasSuffixPath(suffix), hasPrefix)
                    722:        }
                    723:
                    724:        test("dir/file", "dir", false)
                    725:        test("dir/file", "file", true)
                    726:        test("dir/file", "le", false)
                    727:
                    728:        // In contrast to HasPrefixPath, it doesn't really make sense to
                    729:        // ask whether a path ends with the current directory.
                    730:        test("dir", ".", false)
                    731: }
                    732:
                    733: func (s *Suite) Test_CurrPath_HasSuffixText(c *check.C) {
                    734:        t := s.Init(c)
                    735:
                    736:        test := func(curr CurrPath, suffix string, hasPrefix bool) {
                    737:                t.CheckEquals(curr.HasSuffixText(suffix), hasPrefix)
                    738:        }
                    739:
                    740:        test("dir/file", "dir", false)
                    741:        test("dir/file", "file", true)
                    742:        test("dir/file", "le", true)
                    743: }
                    744:
                    745: func (s *Suite) Test_CurrPath_HasBase(c *check.C) {
                    746:        t := s.Init(c)
                    747:
                    748:        test := func(curr CurrPath, base string, hasPrefix bool) {
                    749:                t.CheckEquals(curr.HasBase(base), hasPrefix)
                    750:        }
                    751:
                    752:        test("dir/file", "dir", false)
                    753:        test("dir/file", "file", true)
                    754:        test("dir/file", "le", false)
                    755: }
                    756:
                    757: func (s *Suite) Test_CurrPath_TrimSuffix(c *check.C) {
                    758:        t := s.Init(c)
                    759:
                    760:        test := func(curr CurrPath, suffix string, trimmed CurrPath) {
                    761:                t.CheckEquals(curr.TrimSuffix(suffix), trimmed)
                    762:        }
                    763:
                    764:        test("dir/file", "dir", "dir/file")
                    765:        test("dir/file", "file", "dir/")
                    766:        test("dir/file", "le", "dir/fi")
                    767: }
                    768:
                    769: func (s *Suite) Test_CurrPath_ReplaceSuffix(c *check.C) {
                    770:        t := s.Init(c)
                    771:
                    772:        test := func(curr CurrPath, from, to string, replaced CurrPath) {
                    773:                t.CheckEquals(curr.ReplaceSuffix(from, to), replaced)
                    774:        }
                    775:
                    776:        test("dir/file", "file", "subdir", "dir/subdir")
                    777:
                    778:        // The path must actually end with the suffix, otherwise there is
                    779:        // the risk of creating unintended paths.
                    780:        t.ExpectAssert(
                    781:                func() { test("dir/file", "no-match", "anything", "dir/file") })
                    782: }
                    783:
                    784: func (s *Suite) Test_CurrPath_Clean(c *check.C) {
                    785:        t := s.Init(c)
                    786:
                    787:        test := func(curr, cleaned CurrPath) {
                    788:                t.CheckEquals(curr.Clean(), cleaned)
                    789:        }
                    790:
                    791:        test("dir/file", "dir/file")
                    792:        test("dir/.////../file", "file")
                    793: }
                    794:
                    795: func (s *Suite) Test_CurrPath_CleanDot(c *check.C) {
                    796:        t := s.Init(c)
                    797:
                    798:        test := func(curr, cleaned CurrPath) {
                    799:                t.CheckEquals(curr.CleanDot(), cleaned)
                    800:        }
                    801:
                    802:        test("dir/file", "dir/file")
                    803:        test("dir/.////../file", "dir/../file")
                    804: }
                    805:
                    806: func (s *Suite) Test_CurrPath_CleanPath(c *check.C) {
                    807:        t := s.Init(c)
                    808:
                    809:        test := func(curr, cleaned CurrPath) {
                    810:                t.CheckEquals(curr.CleanPath(), cleaned)
                    811:        }
                    812:
                    813:        test("a/b/../../c/d/../../e/../f", "a/b/../../e/../f")
                    814: }
                    815:
                    816: func (s *Suite) Test_CurrPath_JoinNoClean(c *check.C) {
                    817:        t := s.Init(c)
                    818:
1.5       rillig    819:        test := func(curr CurrPath, rel RelPath, joined CurrPath) {
                    820:                t.CheckEquals(curr.JoinNoClean(rel), joined)
1.4       rillig    821:        }
                    822:
                    823:        test("", "", "/")
                    824:        test(".", "file", "./file")
                    825:        test("dir", "subdir/file", "dir/subdir/file")
                    826: }
                    827:
                    828: func (s *Suite) Test_CurrPath_JoinClean(c *check.C) {
                    829:        t := s.Init(c)
                    830:
1.5       rillig    831:        test := func(curr CurrPath, rel RelPath, joined CurrPath) {
                    832:                t.CheckEquals(curr.JoinClean(rel), joined)
1.4       rillig    833:        }
                    834:
                    835:        test("", "", "")
                    836:        test(".", "./////file", "file")
                    837:        test("dir/./.", "../subdir/file", "subdir/file")
                    838: }
                    839:
                    840: func (s *Suite) Test_CurrPath_Rel(c *check.C) {
                    841:        t := s.Init(c)
                    842:
1.5       rillig    843:        test := func(curr, rel CurrPath, result RelPath) {
                    844:                t.CheckEquals(curr.Rel(rel), result)
1.4       rillig    845:        }
                    846:
                    847:        test("dir/subdir", "dir", "..")
                    848:        test("dir/subdir", "file", "../../file")
                    849: }
                    850:
                    851: func (s *Suite) Test_CurrPath_Rename(c *check.C) {
1.1       rillig    852:        t := s.Init(c)
                    853:
                    854:        f := t.CreateFileLines("filename.old",
                    855:                "line 1")
                    856:        t.CheckEquals(f.IsFile(), true)
1.4       rillig    857:        dst := f.ReplaceSuffix(".old", ".new")
1.1       rillig    858:
                    859:        err := f.Rename(dst)
                    860:
                    861:        assertNil(err, "Rename")
                    862:        t.CheckEquals(f.IsFile(), false)
                    863:        t.CheckFileLines("filename.new",
                    864:                "line 1")
                    865: }
                    866:
1.4       rillig    867: func (s *Suite) Test_CurrPath_Lstat(c *check.C) {
1.1       rillig    868:        t := s.Init(c)
                    869:
1.5       rillig    870:        test := func(f CurrPath, isDir bool) {
1.1       rillig    871:                st, err := f.Lstat()
                    872:                assertNil(err, "Lstat")
                    873:                t.CheckEquals(st.Mode()&os.ModeDir != 0, isDir)
                    874:        }
                    875:
                    876:        t.CreateFileLines("subdir/file")
                    877:        t.CreateFileLines("file")
                    878:
1.5       rillig    879:        test(t.File("subdir"), true)
                    880:        test(t.File("file"), false)
1.1       rillig    881: }
                    882:
1.4       rillig    883: func (s *Suite) Test_CurrPath_Stat(c *check.C) {
1.1       rillig    884:        t := s.Init(c)
                    885:
1.5       rillig    886:        test := func(f CurrPath, isDir bool) {
1.1       rillig    887:                st, err := f.Stat()
                    888:                assertNil(err, "Stat")
                    889:                t.CheckEquals(st.Mode()&os.ModeDir != 0, isDir)
                    890:        }
                    891:
                    892:        t.CreateFileLines("subdir/file")
                    893:        t.CreateFileLines("file")
                    894:
1.5       rillig    895:        test(t.File("subdir"), true)
                    896:        test(t.File("file"), false)
1.1       rillig    897: }
                    898:
1.4       rillig    899: func (s *Suite) Test_CurrPath_Exists(c *check.C) {
1.1       rillig    900:        t := s.Init(c)
                    901:
1.4       rillig    902:        test := func(f CurrPath, exists bool) {
1.1       rillig    903:                t.CheckEquals(f.Exists(), exists)
                    904:        }
                    905:
                    906:        t.CreateFileLines("subdir/file")
                    907:        t.CreateFileLines("file")
                    908:
                    909:        test(t.File("subdir"), true)
                    910:        test(t.File("file"), true)
                    911:        test(t.File("enoent"), false)
                    912: }
                    913:
1.4       rillig    914: func (s *Suite) Test_CurrPath_IsFile(c *check.C) {
1.1       rillig    915:        t := s.Init(c)
                    916:
                    917:        t.CreateFileLines("dir/file")
1.5       rillig    918:        t.Chdir(".")
1.1       rillig    919:
1.5       rillig    920:        test := func(curr CurrPath, isFile bool) {
                    921:                t.CheckEquals(curr.IsFile(), isFile)
                    922:        }
                    923:
                    924:        test("nonexistent", false)
                    925:        test("dir", false)
                    926:        test("dir/nonexistent", false)
                    927:        test("dir/file", true)
1.1       rillig    928: }
                    929:
1.4       rillig    930: func (s *Suite) Test_CurrPath_IsDir(c *check.C) {
1.1       rillig    931:        t := s.Init(c)
                    932:
                    933:        t.CreateFileLines("dir/file")
1.5       rillig    934:        t.Chdir(".")
1.1       rillig    935:
1.5       rillig    936:        test := func(curr CurrPath, isFile bool) {
                    937:                t.CheckEquals(curr.IsDir(), isFile)
                    938:        }
                    939:
                    940:        test("nonexistent", false)
                    941:        test("dir", true)
                    942:        test("dir/nonexistent", false)
                    943:        test("dir/file", false)
1.1       rillig    944: }
                    945:
1.4       rillig    946: func (s *Suite) Test_CurrPath_Chmod(c *check.C) {
1.1       rillig    947:        t := s.Init(c)
                    948:
1.4       rillig    949:        testWritable := func(f CurrPath, writable bool) {
1.1       rillig    950:                lstat, err := f.Lstat()
                    951:                assertNil(err, "Lstat")
                    952:                t.CheckEquals(lstat.Mode().Perm()&0200 != 0, writable)
                    953:        }
                    954:
                    955:        f := t.CreateFileLines("file")
                    956:        testWritable(f, true)
                    957:
                    958:        err := f.Chmod(0444)
                    959:        assertNil(err, "Chmod")
                    960:
                    961:        testWritable(f, false)
                    962: }
                    963:
1.4       rillig    964: func (s *Suite) Test_CurrPath_ReadDir(c *check.C) {
1.1       rillig    965:        t := s.Init(c)
                    966:
                    967:        t.CreateFileLines("subdir/file")
                    968:        t.CreateFileLines("file")
                    969:        t.CreateFileLines("CVS/Entries")
                    970:        t.CreateFileLines(".git/info/exclude")
1.5       rillig    971:        t.Chdir(".")
1.1       rillig    972:
1.5       rillig    973:        test := func(curr CurrPath, entries ...string) {
                    974:                infos, err := curr.ReadDir()
                    975:                assertNil(err, "ReadDir")
                    976:
                    977:                var names []string
                    978:                for _, info := range infos {
                    979:                        names = append(names, info.Name())
                    980:                }
                    981:
                    982:                t.CheckDeepEquals(names, entries)
                    983:        }
                    984:
                    985:        test(".",
                    986:                ".git", "CVS", "file", "subdir")
                    987:        test("subdir",
                    988:                "file")
                    989:        test("CVS",
                    990:                "Entries")
1.1       rillig    991: }
                    992:
1.4       rillig    993: func (s *Suite) Test_CurrPath_ReadPaths(c *check.C) {
                    994:        t := s.Init(c)
                    995:
                    996:        t.CreateFileLines("dir/subdir/file")
                    997:        t.CreateFileLines("dir/CVS/Entries")
                    998:        t.CreateFileLines("dir/file")
1.5       rillig    999:        t.Chdir(".")
1.4       rillig   1000:
1.5       rillig   1001:        test := func(dir CurrPath, entries ...CurrPath) {
                   1002:                t.CheckDeepEquals(dir.ReadPaths(), entries)
                   1003:        }
1.4       rillig   1004:
1.5       rillig   1005:        test(".",
                   1006:                "dir")
1.4       rillig   1007:
1.5       rillig   1008:        test("dir",
                   1009:                "dir/file", "dir/subdir")
1.4       rillig   1010: }
                   1011:
                   1012: func (s *Suite) Test_CurrPath_Open(c *check.C) {
1.1       rillig   1013:        t := s.Init(c)
                   1014:
                   1015:        t.CreateFileLines("filename",
                   1016:                "line 1",
                   1017:                "line 2")
1.5       rillig   1018:        t.Chdir(".")
                   1019:
                   1020:        test := func(curr CurrPath, content string) {
                   1021:                f, err := curr.Open()
                   1022:                assertNil(err, "Open")
                   1023:                defer func() { assertNil(f.Close(), "Close") }()
1.1       rillig   1024:
1.5       rillig   1025:                var sb strings.Builder
                   1026:                n, err := io.Copy(&sb, f)
                   1027:                assertNil(err, "Copy")
1.1       rillig   1028:
1.5       rillig   1029:                t.CheckEquals(n, int64(len(content)))
                   1030:                t.CheckEquals(sb.String(), content)
                   1031:        }
                   1032:
                   1033:        test("filename", "line 1\nline 2\n")
1.1       rillig   1034: }
                   1035:
1.4       rillig   1036: func (s *Suite) Test_CurrPath_ReadString(c *check.C) {
1.1       rillig   1037:        t := s.Init(c)
                   1038:
1.5       rillig   1039:        t.Chdir(".")
                   1040:        t.CreateFileLines("empty")
1.1       rillig   1041:        t.CreateFileLines("filename",
                   1042:                "line 1",
                   1043:                "line 2")
                   1044:
1.5       rillig   1045:        test := func(curr CurrPath, content string) {
                   1046:                text, err := curr.ReadString()
1.1       rillig   1047:
1.5       rillig   1048:                assertNil(err, "ReadString")
                   1049:                t.CheckEquals(text, content)
                   1050:        }
                   1051:
                   1052:        test("empty", "")
                   1053:        test("filename", "line 1\nline 2\n")
1.1       rillig   1054: }
                   1055:
1.4       rillig   1056: func (s *Suite) Test_CurrPath_WriteString(c *check.C) {
1.1       rillig   1057:        t := s.Init(c)
                   1058:
1.5       rillig   1059:        t.Chdir(".")
                   1060:
                   1061:        test := func(curr CurrPath, content string, lines ...string) {
                   1062:                err := curr.WriteString(content)
                   1063:                assertNil(err, "WriteString")
1.1       rillig   1064:
1.5       rillig   1065:                t.CheckFileLines(NewRelPath(curr.AsPath()),
                   1066:                        lines...)
                   1067:        }
                   1068:
                   1069:        test("empty", "",
                   1070:                nil...)
                   1071:
                   1072:        test("filename", "line 1\nline 2\n",
1.1       rillig   1073:                "line 1",
                   1074:                "line 2")
                   1075: }
1.4       rillig   1076:
                   1077: func (s *Suite) Test_NewPkgsrcPath(c *check.C) {
                   1078:        t := s.Init(c)
                   1079:
                   1080:        p := NewPkgsrcPath("category/package")
                   1081:
                   1082:        t.CheckEquals(p.AsPath(), NewPath("category/package"))
                   1083: }
                   1084:
                   1085: func (s *Suite) Test_PkgsrcPath_String(c *check.C) {
                   1086:        t := s.Init(c)
                   1087:
                   1088:        p := NewPkgsrcPath("any string..././")
                   1089:
                   1090:        str := p.String()
                   1091:
                   1092:        // No normalization takes place because it is typically not needed.
                   1093:        t.CheckEquals(str, "any string..././")
                   1094: }
                   1095:
                   1096: func (s *Suite) Test_PkgsrcPath_AsPath(c *check.C) {
                   1097:        t := s.Init(c)
                   1098:
                   1099:        pp := NewPkgsrcPath("./category/package/Makefile")
                   1100:
                   1101:        p := pp.AsPath()
                   1102:
                   1103:        t.CheckEquals(p.String(), "./category/package/Makefile")
                   1104: }
                   1105:
1.5       rillig   1106: func (s *Suite) Test_PkgsrcPath_AsRelPath(c *check.C) {
                   1107:        t := s.Init(c)
                   1108:
                   1109:        pp := NewPkgsrcPath("./category/package/Makefile")
                   1110:
                   1111:        rel := pp.AsRelPath()
                   1112:
                   1113:        t.CheckEquals(rel.String(), "./category/package/Makefile")
                   1114: }
                   1115:
1.4       rillig   1116: func (s *Suite) Test_PkgsrcPath_DirClean(c *check.C) {
                   1117:        t := s.Init(c)
                   1118:
1.5       rillig   1119:        test := func(pp, cleaned PkgsrcPath) {
                   1120:                t.CheckEquals(pp.DirClean(), cleaned)
                   1121:        }
1.4       rillig   1122:
1.5       rillig   1123:        test("./dir/../dir/base///.", "dir/base")
1.4       rillig   1124: }
                   1125:
                   1126: func (s *Suite) Test_PkgsrcPath_DirNoClean(c *check.C) {
                   1127:        t := s.Init(c)
                   1128:
1.5       rillig   1129:        test := func(pp, cleaned PkgsrcPath) {
                   1130:                t.CheckEquals(pp.DirNoClean(), cleaned)
                   1131:        }
1.4       rillig   1132:
1.5       rillig   1133:        test("./dir/../dir/base///.", "./dir/../dir/base")
1.4       rillig   1134: }
                   1135:
                   1136: func (s *Suite) Test_PkgsrcPath_Base(c *check.C) {
                   1137:        t := s.Init(c)
                   1138:
1.5       rillig   1139:        test := func(pp PkgsrcPath, base string) {
                   1140:                t.CheckEquals(pp.Base(), base)
                   1141:        }
1.4       rillig   1142:
1.5       rillig   1143:        test("./dir/../dir/base///.", ".")
1.4       rillig   1144: }
                   1145:
                   1146: func (s *Suite) Test_PkgsrcPath_Count(c *check.C) {
                   1147:        t := s.Init(c)
                   1148:
1.5       rillig   1149:        test := func(pp PkgsrcPath, count int) {
                   1150:                t.CheckEquals(pp.Count(), count)
                   1151:        }
1.4       rillig   1152:
1.5       rillig   1153:        test("./...////dir", 2)
1.4       rillig   1154: }
                   1155:
                   1156: func (s *Suite) Test_PkgsrcPath_HasPrefixPath(c *check.C) {
                   1157:        t := s.Init(c)
                   1158:
1.5       rillig   1159:        test := func(pp PkgsrcPath, prefix Path, hasPrefixPath bool) {
                   1160:                t.CheckEquals(pp.HasPrefixPath(prefix), hasPrefixPath)
                   1161:        }
1.4       rillig   1162:
1.5       rillig   1163:        test("./././///prefix/suffix", "prefix", true)
1.4       rillig   1164: }
                   1165:
                   1166: func (s *Suite) Test_PkgsrcPath_JoinNoClean(c *check.C) {
                   1167:        t := s.Init(c)
                   1168:
1.5       rillig   1169:        test := func(pp PkgsrcPath, rel RelPath, joined PkgsrcPath) {
                   1170:                t.CheckEquals(pp.JoinNoClean(rel), joined)
                   1171:        }
1.4       rillig   1172:
1.5       rillig   1173:        test("base///.", "./../rel", "base///././../rel")
1.4       rillig   1174: }
                   1175:
1.5       rillig   1176: func (s *Suite) Test_NewPackagePath(c *check.C) {
1.4       rillig   1177:        t := s.Init(c)
                   1178:
1.5       rillig   1179:        p := NewPackagePath("../../category/package")
1.4       rillig   1180:
1.5       rillig   1181:        t.CheckEquals(p.AsPath(), NewPath("../../category/package"))
1.4       rillig   1182: }
                   1183:
1.5       rillig   1184: func (s *Suite) Test_NewPackagePathString(c *check.C) {
1.4       rillig   1185:        t := s.Init(c)
                   1186:
1.5       rillig   1187:        p := NewPackagePathString("../../category/package")
1.4       rillig   1188:
                   1189:        t.CheckEquals(p.AsPath(), NewPath("../../category/package"))
                   1190: }
                   1191:
                   1192: func (s *Suite) Test_PackagePath_AsPath(c *check.C) {
                   1193:        t := s.Init(c)
                   1194:
                   1195:        pp := NewPackagePath("../../category/package/Makefile")
                   1196:
                   1197:        p := pp.AsPath()
                   1198:
                   1199:        t.CheckEquals(p.String(), "../../category/package/Makefile")
                   1200: }
                   1201:
1.5       rillig   1202: func (s *Suite) Test_PackagePath_AsRelPath(c *check.C) {
                   1203:        t := s.Init(c)
                   1204:
                   1205:        pp := NewPackagePath("./category/package/Makefile")
                   1206:
                   1207:        rel := pp.AsRelPath()
                   1208:
                   1209:        t.CheckEquals(rel.String(), "./category/package/Makefile")
                   1210: }
                   1211:
1.4       rillig   1212: func (s *Suite) Test_PackagePath_String(c *check.C) {
                   1213:        t := s.Init(c)
                   1214:
                   1215:        pp := NewPackagePath("../../category/package/Makefile")
                   1216:
                   1217:        str := pp.String()
                   1218:
                   1219:        t.CheckEquals(str, "../../category/package/Makefile")
                   1220: }
                   1221:
                   1222: func (s *Suite) Test_PackagePath_JoinNoClean(c *check.C) {
                   1223:        t := s.Init(c)
                   1224:
1.5       rillig   1225:        test := func(pp PackagePath, other RelPath, joined PackagePath) {
                   1226:                t.CheckEquals(pp.JoinNoClean(other), joined)
1.4       rillig   1227:
1.5       rillig   1228:        }
1.4       rillig   1229:
1.5       rillig   1230:        test("../../category/package/patches", "patch-aa",
                   1231:                "../../category/package/patches/patch-aa")
1.4       rillig   1232: }
                   1233:
                   1234: func (s *Suite) Test_PackagePath_IsEmpty(c *check.C) {
                   1235:        t := s.Init(c)
                   1236:
                   1237:        test := func(p PackagePath, isEmpty bool) {
                   1238:                t.CheckEquals(p.IsEmpty(), isEmpty)
                   1239:        }
                   1240:
                   1241:        test("", true)
                   1242:        test(".", false)
                   1243: }
                   1244:
                   1245: func (s *Suite) Test_NewRelPath(c *check.C) {
                   1246:        t := s.Init(c)
                   1247:
                   1248:        rel := NewRelPath("dir/file")
                   1249:
                   1250:        t.CheckEquals(rel.String(), "dir/file")
                   1251: }
                   1252:
                   1253: func (s *Suite) Test_NewRelPathString(c *check.C) {
                   1254:        t := s.Init(c)
                   1255:
                   1256:        rel := NewRelPathString("dir/file")
                   1257:
                   1258:        t.CheckEquals(rel.String(), "dir/file")
                   1259: }
                   1260:
                   1261: func (s *Suite) Test_RelPath_AsPath(c *check.C) {
                   1262:        t := s.Init(c)
                   1263:
                   1264:        rel := NewRelPath("relative")
                   1265:
                   1266:        path := rel.AsPath()
                   1267:
                   1268:        t.CheckEquals(path.String(), "relative")
                   1269: }
                   1270:
                   1271: func (s *Suite) Test_RelPath_String(c *check.C) {
                   1272:        t := s.Init(c)
                   1273:
                   1274:        rel := NewRelPath(".///rel")
                   1275:
                   1276:        str := rel.String()
                   1277:
                   1278:        t.CheckEquals(str, ".///rel")
                   1279: }
                   1280:
1.5       rillig   1281: func (s *Suite) Test_RelPath_IsEmpty(c *check.C) {
                   1282:        t := s.Init(c)
                   1283:
                   1284:        test := func(rel RelPath, isEmpty bool) {
                   1285:                t.CheckEquals(rel.IsEmpty(), isEmpty)
                   1286:        }
                   1287:
                   1288:        test("", true)
                   1289:        test(".", false)
                   1290:        test("/", false)
                   1291: }
                   1292:
                   1293: func (s *Suite) Test_RelPath_Split(c *check.C) {
                   1294:        t := s.Init(c)
                   1295:
                   1296:        test := func(rel RelPath, dir RelPath, base string) {
                   1297:                actualDir, actualBase := rel.Split()
                   1298:                t.CheckEquals(actualDir, dir)
                   1299:                t.CheckEquals(actualBase, base)
                   1300:        }
                   1301:
                   1302:        test("dir/file", "dir/", "file")
                   1303:        test("././///file", "././///", "file")
                   1304:
                   1305:        t.ExpectAssert(
                   1306:                func() { test("/", "/", "") })
                   1307:
                   1308: }
                   1309:
1.4       rillig   1310: func (s *Suite) Test_RelPath_DirClean(c *check.C) {
                   1311:        t := s.Init(c)
                   1312:
1.5       rillig   1313:        test := func(rel RelPath, dir RelPath) {
                   1314:                t.CheckEquals(rel.DirClean(), dir)
                   1315:        }
1.4       rillig   1316:
1.5       rillig   1317:        test("./dir/../dir///./file", "dir")
1.4       rillig   1318: }
                   1319:
                   1320: func (s *Suite) Test_RelPath_DirNoClean(c *check.C) {
                   1321:        t := s.Init(c)
                   1322:
1.5       rillig   1323:        test := func(rel RelPath, dir RelPath) {
                   1324:                t.CheckEquals(rel.DirNoClean(), dir)
                   1325:        }
1.4       rillig   1326:
1.5       rillig   1327:        test("./dir/../dir///./file", "./dir/../dir")
1.4       rillig   1328: }
                   1329:
                   1330: func (s *Suite) Test_RelPath_Base(c *check.C) {
                   1331:        t := s.Init(c)
                   1332:
1.5       rillig   1333:        test := func(rel RelPath, base string) {
                   1334:                t.CheckEquals(rel.Base(), base)
                   1335:        }
1.4       rillig   1336:
1.5       rillig   1337:        test("./dir/../dir///./file", "file")
1.4       rillig   1338: }
                   1339:
                   1340: func (s *Suite) Test_RelPath_HasBase(c *check.C) {
                   1341:        t := s.Init(c)
                   1342:
                   1343:        test := func(rel RelPath, base string, hasBase bool) {
                   1344:                t.CheckEquals(rel.HasBase(base), hasBase)
                   1345:        }
                   1346:
                   1347:        test("./dir/Makefile", "Makefile", true)
                   1348:        test("./dir/Makefile", "Make", false)
                   1349:        test("./dir/Makefile", "file", false)
                   1350:        test("./dir/Makefile", "dir/Makefile", false)
                   1351: }
                   1352:
                   1353: func (s *Suite) Test_RelPath_Parts(c *check.C) {
                   1354:        t := s.Init(c)
                   1355:
1.5       rillig   1356:        test := func(rel RelPath, parts ...string) {
                   1357:                t.CheckDeepEquals(rel.Parts(), parts)
                   1358:        }
1.4       rillig   1359:
1.5       rillig   1360:        test("./dir/.///base", "dir", "base")
1.4       rillig   1361: }
                   1362:
                   1363: func (s *Suite) Test_RelPath_Count(c *check.C) {
                   1364:        t := s.Init(c)
                   1365:
1.5       rillig   1366:        test := func(rel RelPath, count int) {
                   1367:                t.CheckEquals(rel.Count(), count)
                   1368:        }
1.4       rillig   1369:
1.5       rillig   1370:        test("./dir/.///base", 2)
1.4       rillig   1371: }
                   1372:
                   1373: func (s *Suite) Test_RelPath_Clean(c *check.C) {
                   1374:        t := s.Init(c)
                   1375:
1.5       rillig   1376:        test := func(rel RelPath, cleaned RelPath) {
                   1377:                t.CheckDeepEquals(rel.Clean(), cleaned)
                   1378:        }
1.4       rillig   1379:
1.5       rillig   1380:        test("a/b/../../c/d/../.././e/../f", "f")
                   1381: }
                   1382:
                   1383: func (s *Suite) Test_RelPath_CleanDot(c *check.C) {
                   1384:        t := s.Init(c)
1.4       rillig   1385:
1.5       rillig   1386:        test := func(rel RelPath, cleaned RelPath) {
                   1387:                t.CheckEquals(rel.CleanDot(), cleaned)
                   1388:        }
                   1389:
                   1390:        test("a/b/../../c/d/../.././e/../f", "a/b/../../c/d/../../e/../f")
1.4       rillig   1391: }
                   1392:
                   1393: func (s *Suite) Test_RelPath_CleanPath(c *check.C) {
                   1394:        t := s.Init(c)
                   1395:
1.5       rillig   1396:        test := func(rel RelPath, cleaned RelPath) {
                   1397:                t.CheckEquals(rel.CleanPath(), cleaned)
                   1398:        }
1.4       rillig   1399:
1.5       rillig   1400:        test("a/b/../../c/d/../.././e/../f", "a/b/../../e/../f")
1.4       rillig   1401: }
                   1402:
                   1403: func (s *Suite) Test_RelPath_JoinNoClean(c *check.C) {
                   1404:        t := s.Init(c)
                   1405:
1.5       rillig   1406:        test := func(rel, other, joined RelPath) {
                   1407:                t.CheckEquals(rel.JoinNoClean(other), joined)
                   1408:        }
1.4       rillig   1409:
1.5       rillig   1410:        test("basedir/.//", "./other", "basedir/.///./other")
1.4       rillig   1411: }
                   1412:
                   1413: func (s *Suite) Test_RelPath_Replace(c *check.C) {
                   1414:        t := s.Init(c)
                   1415:
1.5       rillig   1416:        test := func(rel RelPath, from, to string, result RelPath) {
                   1417:                t.CheckEquals(rel.Replace(from, to), result)
                   1418:        }
                   1419:
                   1420:        test("dir/subdir/file", "/", ":", "dir:subdir:file")
                   1421: }
1.4       rillig   1422:
1.5       rillig   1423: func (s *Suite) Test_RelPath_HasPrefixPath(c *check.C) {
                   1424:        t := s.Init(c)
                   1425:
                   1426:        test := func(rel RelPath, prefix Path, hasPrefixPath bool) {
                   1427:                t.CheckEquals(rel.HasPrefixPath(prefix), hasPrefixPath)
                   1428:        }
1.4       rillig   1429:
1.5       rillig   1430:        test("dir/subdir/file", "dir", true)
                   1431:        test("dir/subdir/file", "dir/sub", false)
                   1432:        test("dir/subdir/file", "subdir", false)
1.4       rillig   1433: }
                   1434:
1.5       rillig   1435: func (s *Suite) Test_RelPath_HasPrefixText(c *check.C) {
1.4       rillig   1436:        t := s.Init(c)
                   1437:
1.5       rillig   1438:        test := func(rel RelPath, prefix string, hasPrefixPath bool) {
                   1439:                t.CheckEquals(rel.HasPrefixText(prefix), hasPrefixPath)
                   1440:        }
1.4       rillig   1441:
1.5       rillig   1442:        test("dir/subdir/file", "dir", true)
                   1443:        test("dir/subdir/file", "dir/sub", true)
                   1444:        test("dir/subdir/file", "subdir", false)
                   1445:        test("dir/subdir/file", "super", false)
1.4       rillig   1446: }
                   1447:
                   1448: func (s *Suite) Test_RelPath_ContainsPath(c *check.C) {
                   1449:        t := s.Init(c)
                   1450:
1.5       rillig   1451:        test := func(rel RelPath, prefix Path, hasPrefixPath bool) {
                   1452:                t.CheckEquals(rel.ContainsPath(prefix), hasPrefixPath)
                   1453:        }
1.4       rillig   1454:
1.5       rillig   1455:        test("dir/subdir/file", "dir", true)
                   1456:        test("dir/subdir/file", "dir/sub", false)
                   1457:        test("dir/subdir/file", "subdir", true)
1.4       rillig   1458: }
                   1459:
                   1460: func (s *Suite) Test_RelPath_ContainsText(c *check.C) {
                   1461:        t := s.Init(c)
                   1462:
1.5       rillig   1463:        test := func(rel RelPath, prefix string, hasPrefixPath bool) {
                   1464:                t.CheckEquals(rel.ContainsText(prefix), hasPrefixPath)
                   1465:        }
1.4       rillig   1466:
1.5       rillig   1467:        test("dir/subdir/file", "dir", true)
                   1468:        test("dir/subdir/file", "dir/sub", true)
                   1469:        test("dir/subdir/file", "subdir", true)
                   1470:        test("dir/subdir/file", "super", false)
1.4       rillig   1471: }
                   1472:
                   1473: func (s *Suite) Test_RelPath_HasSuffixPath(c *check.C) {
                   1474:        t := s.Init(c)
                   1475:
1.5       rillig   1476:        test := func(rel RelPath, prefix Path, hasPrefixPath bool) {
                   1477:                t.CheckEquals(rel.HasSuffixPath(prefix), hasPrefixPath)
                   1478:        }
                   1479:
                   1480:        test("dir/subdir/file", "dir", false)
                   1481:        test("dir/subdir/file", "file", true)
                   1482:        test("dir/subdir/file", "le", false)
                   1483:        test("dir/subdir/file", "subdir/file", true)
                   1484:        test("dir/subdir/file", "subdir", false)
                   1485: }
                   1486:
                   1487: func (s *Suite) Test_RelPath_HasSuffixText(c *check.C) {
                   1488:        t := s.Init(c)
                   1489:
                   1490:        test := func(rel RelPath, prefix string, hasPrefixPath bool) {
                   1491:                t.CheckEquals(rel.HasSuffixText(prefix), hasPrefixPath)
                   1492:        }
                   1493:
                   1494:        test("dir/subdir/file", "dir", false)
                   1495:        test("dir/subdir/file", "file", true)
                   1496:        test("dir/subdir/file", "le", true)
                   1497:        test("dir/subdir/file", "subdir/file", true)
                   1498:        test("dir/subdir/file", "subdir", false)
                   1499: }
                   1500:
                   1501: func (s *Suite) Test_RelPath_Rel(c *check.C) {
                   1502:        t := s.Init(c)
                   1503:
                   1504:        test := func(base RelPath, other Path, result RelPath) {
                   1505:                t.CheckEquals(base.Rel(other), result)
                   1506:        }
                   1507:
                   1508:        test("a/b/c", "d/e/f/file", "../../../d/e/f/file")
                   1509:        test(".", ".", ".")
1.4       rillig   1510:
1.5       rillig   1511:        // The trailing dot marks the difference between a file and a directory.
                   1512:        // This is the same behavior as with filepath.Rel.
                   1513:        test("a/b/c", ".", "../../../.")
1.4       rillig   1514: }

CVSweb <webmaster@jp.NetBSD.org>