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

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

CVSweb <webmaster@jp.NetBSD.org>