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

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

CVSweb <webmaster@jp.NetBSD.org>