Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib): System path library #1452

Merged
merged 13 commits into from
Dec 3, 2022
Merged
165 changes: 165 additions & 0 deletions compiler/test/stdlib/path.test.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import Path from "path"
import Result from "result"
import Option from "option"
import List from "list"

// test that a variety of properties hold for valid relative/absolute paths
let relTests = [
(
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
"./dir/file.txt",
"./dir",
"./dir/file.txt",
Some("file.txt"),
Some("file"),
Some(".txt"),
),
("file", ".", "./file", Some("file"), Some("file"), None),
("dir/../../", "../..", "..", None, None, None),
(".git", ".", "./.git", Some(".git"), Some(".git"), None),
(".", "..", ".", None, None, None),
("..", "../..", "..", None, None, None),
("../../file", "../..", "../../file", Some("file"), Some("file"), None),
("./", "..", ".", None, None, None),
(
"dir//file.txt///",
"dir",
"./dir/file.txt",
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
Some("file.txt"),
Some("file"),
Some(".txt"),
),
(
".\\dir\\file.txt",
".",
"./.\\dir\\file.txt",
Some(".\\dir\\file.txt"),
Some(".\\dir\\file"),
Some(".txt"),
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
),
]

List.forEach(((pathStr, expParent, expStr, expName, expStem, expExt)) => {
let path = Option.unwrap(Path.relative(pathStr))
assert Path.toString(path, Path.Posix) == expStr
assert Option.unwrap(Path.relative(expParent)) == Path.parent(path)
assert expName == Path.name(path)
assert expStem == Path.stem(path)
assert expExt == Path.extension(path)
}, relTests)

let absTests = [
("/usr/bin", "/usr", "/usr/bin", Path.Root, Some("bin"), Some("bin"), None),
("/bin", "/", "/bin", Path.Root, Some("bin"), Some("bin"), None),
("/bin/dir/..", "/", "/bin", Path.Root, Some("bin"), Some("bin"), None),
("/../..", "/", "/", Path.Root, None, None, None),
("/", "/", "/", Path.Root, None, None, None),
("C:/", "C:/", "C:/", Path.Drive("C"), None, None, None),
("C:/.././..", "C:/", "C:/", Path.Drive("C"), None, None, None),
(
"C:/Users/me.txt",
"C:/Users",
"C:/Users/me.txt",
Path.Drive("C"),
Some("me.txt"),
Some("me"),
Some(".txt"),
),
(
"label:/me",
"label:/",
"label:/me",
Path.Drive("label"),
phated marked this conversation as resolved.
Show resolved Hide resolved
Some("me"),
Some("me"),
None,
),
]

List.forEach(((
pathStr, expParent, expStr, expRoot, expName, expStem, expExt,
)) => {
let path = Option.unwrap(Path.absolute(pathStr))
assert Path.toString(path, Path.Posix) == expStr
assert Option.unwrap(Path.absolute(expParent)) == Path.parent(path)
assert expRoot == Path.root(path)
assert expName == Path.name(path)
assert expStem == Path.stem(path)
assert expExt == Path.extension(path)
}, absTests)

// test invalidly trying to construct paths
assert Path.relative("/usr/bin") == None
assert Path.relative("C:/Users/me") == None
assert Path.absolute("./file.txt") == None
assert Path.absolute("file.txt") == None
assert Path.absolute("../..") == None

// test appending onto relative and absolute paths
let appendRelTests = [
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
("dir", "inner", "dir/inner"),
("./dir/inner", "./innerer/file.txt", "dir/inner/innerer/file.txt"),
(".", ".././../", "../.."),
]

List.forEach(((base, toAppend, final)) => {
let path = Option.unwrap(Path.relative(base))
let finalPath = Option.unwrap(Path.relative(final))
let appendStr = Option.unwrap(Path.appendString(path, toAppend))
let appendPath = Path.append(path, Option.unwrap(Path.relative(toAppend)))
assert appendStr == finalPath
assert appendPath == finalPath
}, appendRelTests)

let appendAbsTests = [
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
("/usr", "./bin", "/usr/bin"),
("/usr", "../bin", "/bin"),
("/", "../..", "/"),
("C:/", "../..", "C:/"),
]

List.forEach(((base, toAppend, final)) => {
let path = Option.unwrap(Path.absolute(base))
let finalPath = Option.unwrap(Path.absolute(final))
let appendStr = Option.unwrap(Path.appendString(path, toAppend))
let appendPath = Path.append(path, Option.unwrap(Path.relative(toAppend)))
assert appendStr == finalPath
assert appendPath == finalPath
}, appendAbsTests)

// test the relativeTo function
let valid = path => Ok(Option.unwrap(Path.relative(path)))
let relativeToRelTests = [
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
("./dir", "./dir/inner", valid("./inner")),
("./dir", ".", valid("..")),
(".", ".", valid(".")),
("..", "../../other", valid("../other")),
("..", ".", Err(Path.ImpossibleRelativization)),
]

List.forEach(((source, dest, result)) => {
let source = Option.unwrap(Path.relative(source))
let dest = Option.unwrap(Path.relative(dest))
assert Path.relativeTo(source, dest) == result
}, relativeToRelTests)

let relativeToAbsTests = [
alex-snezhko marked this conversation as resolved.
Show resolved Hide resolved
("/usr", "/usr/bin", valid("./bin")),
("/", "/", valid(".")),
("/usr/bin", "/usr/bin", valid(".")),
("/usr/bin", "/", valid("../..")),
("/usr/bin", "/home/me", valid("../../home/me")),
("/bin", "C:/Users", Err(Path.DifferentRoots)),
]

List.forEach(((source, dest, result)) => {
let source = Option.unwrap(Path.absolute(source))
let dest = Option.unwrap(Path.absolute(dest))
assert Path.relativeTo(source, dest) == result
}, relativeToAbsTests)

// miscellaneous tests
assert Path.toString(
Option.unwrap(Path.absolute("C:/Users/me")),
Path.Windows
) ==
"C:\\Users\\me"
1 change: 1 addition & 0 deletions compiler/test/suites/stdlib.re
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe("stdlib", ({test, testSkip}) => {
assertStdlib("marshal.test");
assertStdlib("number.test");
assertStdlib("option.test");
assertStdlib("path.test");
assertStdlib("pervasives.test");
assertStdlib("queue.test");
assertStdlib("range.test");
Expand Down
Loading