File: Tests.hs

package info (click to toggle)
git-annex 10.20230126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 69,344 kB
  • sloc: haskell: 74,654; javascript: 9,103; sh: 1,304; makefile: 203; perl: 136; ansic: 44
file content (81 lines) | stat: -rw-r--r-- 2,636 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{- Tests for Utility.Path. Split into a separate module to avoid it needing
 - QuickCheck.
 -
 - Copyright 2010-2021 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}

module Utility.Path.Tests (
	prop_upFrom_basics,
	prop_relPathDirToFileAbs_basics,
	prop_relPathDirToFileAbs_regressionTest,
	prop_dirContains_regressionTest,
) where

import System.FilePath.ByteString
import qualified Data.ByteString as B
import Data.List
import Data.Maybe
import Data.Char
import Control.Applicative
import Prelude

import Utility.Path
import Utility.FileSystemEncoding
import Utility.QuickCheck

prop_upFrom_basics :: TestableFilePath -> Bool
prop_upFrom_basics tdir
	| dir == "/" = p == Nothing
	| otherwise = p /= Just dir
  where
	p = fromRawFilePath <$> upFrom (toRawFilePath dir)
	dir = fromTestableFilePath tdir

prop_relPathDirToFileAbs_basics :: TestableFilePath -> Bool
prop_relPathDirToFileAbs_basics pt = and
	[ relPathDirToFileAbs p (p </> "bar") == "bar"
	, relPathDirToFileAbs (p </> "bar") p == ".."
	, relPathDirToFileAbs p p == ""
	]
  where
	-- relPathDirToFileAbs needs absolute paths, so make the path
	-- absolute by adding a path separator to the front.
	p = pathSeparator `B.cons` relf
	-- Make the input a relative path. On windows, make sure it does
	-- not contain anything that looks like a drive letter.
	relf = B.dropWhile isPathSeparator $
		B.filter (not . skipchar) $
		toRawFilePath (fromTestableFilePath pt)
	skipchar b = b == (fromIntegral (ord ':'))

prop_relPathDirToFileAbs_regressionTest :: Bool
prop_relPathDirToFileAbs_regressionTest = same_dir_shortcurcuits_at_difference
  where
	{- Two paths have the same directory component at the same
	 - location, but it's not really the same directory.
	 - Code used to get this wrong. -}
	same_dir_shortcurcuits_at_difference =
		relPathDirToFileAbs (joinPath [pathSeparator `B.cons` "tmp", "r", "lll", "xxx", "yyy", "18"])
			(joinPath [pathSeparator `B.cons` "tmp", "r", ".git", "annex", "objects", "18", "gk", "SHA256-foo", "SHA256-foo"])
				== joinPath ["..", "..", "..", "..", ".git", "annex", "objects", "18", "gk", "SHA256-foo", "SHA256-foo"]

prop_dirContains_regressionTest :: Bool
prop_dirContains_regressionTest = and
	[ not $ dirContains "." ".."
	, not $ dirContains ".." "../.."
	, dirContains "." "foo"
	, dirContains "." "."
	, dirContains ".." ".."
	, dirContains "../.." "../.."
	, dirContains "." "./foo"
	, dirContains ".." "../foo"
	, dirContains "../.." "../foo"
	, dirContains "../.." "../../foo"
	, not $ dirContains "../.." "../../.."
	]