Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko committed Sep 3, 2024
1 parent c0eaaa3 commit 600b4ad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 64 deletions.
23 changes: 12 additions & 11 deletions compiler/test/stdlib/fs.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ assert Fs.readDir(baseDirPath=Some(testPath("test-dir")), path("dir")) ==
Ok([{ name: "nested.txt", fileType: Fs.File }])
assert Fs.readDir(path("test/nonexisting")) == Err(Fs.NoSuchFileOrDirectory)

// makeDir
// createDir
assert !Fs.exists(testPath("dir2"))
assert Fs.makeDir(testPath("dir2")) == Ok(void)
assert Fs.createDir(testPath("dir2")) == Ok(void)
assert Fs.readDir(testPath("dir2")) == Ok([])

assert Fs.makeDir(baseDirPath=Some(testPath("dir2")), path("inner")) == Ok(void)
assert Fs.createDir(baseDirPath=Some(testPath("dir2")), path("inner")) ==
Ok(void)
assert Fs.readDir(testPath("dir2")) ==
Ok([{ name: "inner", fileType: Fs.Directory }])

assert Fs.makeDir(testPath("dir2")) == Err(Fs.FileExists)
assert Fs.createDir(testPath("dir2")) == Err(Fs.FileExists)

// readLink
assert Fs.readLink(testPath("test-dir/link")) == Ok(path("file1.txt"))
Expand All @@ -132,17 +133,17 @@ assert Fs.readLink(baseDirPath=Some(testPath("test-dir")), path("link")) ==
assert Fs.readLink(testPath("blahblah")) == Err(Fs.NoSuchFileOrDirectory)
assert Fs.readLink(testPath("foo.txt")) == Err(Fs.InvalidArgument)

// makeSymlink
// createSymlink
assert !Fs.exists(testPath("symlink"))
assert Fs.makeSymlink(path("foo.txt"), testPath("symlink")) == Ok(void)
assert Fs.createSymlink(path("foo.txt"), testPath("symlink")) == Ok(void)
assert Fs.readLink(testPath("symlink")) == Ok(path("foo.txt"))
assert Result.unwrap(Fs.stats(testPath("symlink"), followSymlink=false)).fileType ==
Fs.SymbolicLink
assert Result.unwrap(Fs.stats(testPath("symlink"), followSymlink=true)).fileType ==
Fs.File
assert Fs.makeSymlink(path("bar.txt"), testPath("symlink")) ==
assert Fs.createSymlink(path("bar.txt"), testPath("symlink")) ==
Err(Fs.FileExists)
assert Fs.makeSymlink(
assert Fs.createSymlink(
path("bar.txt"),
targetBaseDirPath=Some(testPath("test-dir")),
path("symlink")
Expand Down Expand Up @@ -199,7 +200,7 @@ assert Result.unwrap(
).fileType ==
Fs.File

assert Fs.makeSymlink(path("test-dir"), testPath("linktodir")) == Ok(void)
assert Fs.createSymlink(path("test-dir"), testPath("linktodir")) == Ok(void)
assert Fs.copy(
testPath("linktodir"),
testPath("copied-link-to-dir"),
Expand Down Expand Up @@ -255,7 +256,7 @@ assert Fs.Utf8.readFile(testPath("boofar.txt")) ==
// remove
assert Fs.remove(testPath("baz.txt")) == Ok(void)
assert Fs.remove(testPath("newdir")) == Err(Fs.IsADirectory)
assert Fs.makeDir(testPath("newdir/innerdir")) == Ok(void)
assert Fs.createDir(testPath("newdir/innerdir")) == Ok(void)
assert Fs.remove(testPath("newdir"), removeMode=Fs.RemoveEmptyDirectory) ==
Err(Fs.DirectoryNotEmpty)
assert Fs.remove(
Expand All @@ -264,7 +265,7 @@ assert Fs.remove(
removeMode=Fs.RemoveEmptyDirectory
) ==
Ok(void)
assert Fs.makeDir(testPath("newdir/innerdir")) == Ok(void)
assert Fs.createDir(testPath("newdir/innerdir")) == Ok(void)
assert Fs.Utf8.writeFile(testPath("newdir/innerdir/file.txt"), "content") ==
Ok(void)
assert Fs.remove(testPath("newdir"), removeMode=Fs.RemoveRecursive) == Ok(void)
Expand Down
65 changes: 33 additions & 32 deletions stdlib/fs.gr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @example Fs.Utf8.writeFile(Path.fromString("baz.txt"), "Hello World\n")
* @example Fs.copy(Path.fromString("foo.txt"), Path.fromString("foocopy.txt"))
*
* @since v0.6.5
* @since v0.6.7
*/
module Fs

Expand Down Expand Up @@ -215,7 +215,7 @@ provide record Stats {
}

/**
* Represents information about an item in a directory
* Represents information about an item in a directory.
*/
provide record DirectoryEntry {
name: String,
Expand Down Expand Up @@ -528,18 +528,18 @@ let rec removeRecursive = (parentFd, path) => {
/**
* Removes a file or directory.
*
* @param removeMode: The type of removal to perform; `RemoveFile` by default
* @param baseDirPath: The path to the directory in which path resolution starts
* @param path: The path of the file or directory to remove
* @param removeMode: The type of removal to perform; `RemoveFile` by default
* @returns `Ok(void)` if the operation succeeds, `Err(err)` if a file system error is encountered
*
* @example Fs.remove(Path.fromString("file.txt")) // removes a file
* @example Fs.remove(Path.fromString("dir"), removeMode=Fs.RemoveEmptyDirectory) // removes an empty directory
* @example Fs.remove(Path.fromString("dir"), removeMode=Fs.RemoveRecursive) // removes the directory and its contents
* @example Fs.remove(removeMode=Fs.RemoveEmptyDirectory, Path.fromString("dir")) // removes an empty directory
* @example Fs.remove(removeMode=Fs.RemoveRecursive, Path.fromString("dir")) // removes the directory and its contents
*
* @since v0.6.5
* @since v0.6.7
*/
provide let remove = (baseDirPath=None, path, removeMode=RemoveFile) => {
provide let remove = (removeMode=RemoveFile, baseDirPath=None, path) => {
let (baseDirPath, path) = getBaseDirAndPath(baseDirPath, path)
match (removeMode) {
RemoveFile =>
Expand Down Expand Up @@ -570,7 +570,7 @@ provide let remove = (baseDirPath=None, path, removeMode=RemoveFile) => {
* @param path: The path to the directory to read
* @returns `Ok(contents)` containing the directory contents or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let readDir = (baseDirPath=None, path) => {
let path = joinPathOnBaseDir(baseDirPath, path)
Expand All @@ -592,9 +592,9 @@ provide let readDir = (baseDirPath=None, path) => {
* @param path: The path to create the new directory, relative to the base directory
* @returns `Ok(void)` if the operation succeeds, `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let makeDir = (baseDirPath=None, path) => {
provide let createDir = (baseDirPath=None, path) => {
let (baseDirPath, path) = getBaseDirAndPath(baseDirPath, path)
fileOp(
baseDirPath,
Expand All @@ -611,9 +611,9 @@ provide let makeDir = (baseDirPath=None, path) => {
* @param targetPath: The path to the target of the link
* @returns `Ok(void)` if the operation succeeds, `Err(err)` if a file system error or relativization error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let makeSymlink = (linkContents, targetBaseDirPath=None, targetPath) => {
provide let createSymlink = (linkContents, targetBaseDirPath=None, targetPath) => {
let (targetBaseDirPath, targetPath) = getBaseDirAndPath(
targetBaseDirPath,
targetPath
Expand All @@ -633,14 +633,14 @@ provide let makeSymlink = (linkContents, targetBaseDirPath=None, targetPath) =>
/**
* Queries information about a file.
*
* @param followSymlink: Whether to follow symlinks or not; if `true` then the stats of a valid symlink's underlying file will be returned. `true` by default
* @param baseDirPath: The path to the directory in which the path resolution starts
* @param path: The path of the file to query
* @param followSymlink: Whether to follow symlinks or not; if `true` then the stats of a valid symlink's underlying file will be returned. `true` by default
* @returns `Ok(stats)` containing metadata or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let stats = (baseDirPath=None, path, followSymlink=true) => {
provide let stats = (followSymlink=true, baseDirPath=None, path) => {
let (baseDirPath, path) = getBaseDirAndPath(baseDirPath, path)
let statsResult = fileOp(baseDirPath, Stats, dirFd => {
File.pathFilestats(
Expand Down Expand Up @@ -669,7 +669,7 @@ provide let stats = (baseDirPath=None, path, followSymlink=true) => {
* @param path: The path of the file to query
* @returns `true` if a file or directory exists at the path or `false` otherwise
*
* @since v0.6.5
* @since v0.6.7
*/
provide let exists = (baseDirPath=None, path) => {
stats(baseDirPath=baseDirPath, path, followSymlink=false) !=
Expand All @@ -695,7 +695,7 @@ let readLinkHelper = (dirFd, path, stats: File.Filestats) => {
* @param path: The path to the link to read
* @returns `Ok(path)` containing the link contents or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let readLink = (baseDirPath=None, path) => {
let (baseDirPath, path) = getBaseDirAndPath(baseDirPath, path)
Expand Down Expand Up @@ -866,20 +866,21 @@ let rec copyRecursive = (
/**
* Copies a file or directory.
*
* @param copyMode: The type of copy to perform; `CopyFile` by default
* @param followSymlink: Whether to follow symlinks or not; if `true` then the stats of a valid symlink's underlying file will be returned. `true` by default
* @param sourceBaseDirPath: The path to the directory in which the source path resolution starts
* @param sourcePath: The path of the file or directory to copy
* @param targetBaseDirPath: The path to the directory in which the target path resolution starts
* @param targetPath: The path to copy the file or directory to
* @param copyMode: The type of copy to perform; `CopyFile` by default
* @returns `Ok(void)` if the operation succeeds, `Err(err)` if a file system error is encountered
*/
provide let copy = (
copyMode=CopyFile,
followSymlink=true,
sourceBaseDirPath=None,
sourcePath,
targetBaseDirPath=None,
targetPath,
copyMode=CopyFile,
followSymlink=true,
) => {
let (sourceBaseDirPath, sourcePath) = getBaseDirAndPath(
sourceBaseDirPath,
Expand Down Expand Up @@ -913,7 +914,7 @@ provide let copy = (
* @param targetPath: The new path of the file
* @returns `Ok(void)` if the operation succeeds, `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let rename = (
sourceBaseDirPath=None,
Expand Down Expand Up @@ -950,7 +951,7 @@ provide let rename = (
/**
* Functionality for reading and writing `Bytes` to files.
*
* @since v0.6.5
* @since v0.6.7
*/
provide module Binary {
/**
Expand All @@ -960,7 +961,7 @@ provide module Binary {
* @param path: The file path to read from
* @returns `Ok(contents)` containing the bytes read if successful or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let readFile = (baseDirPath=None, path) => {
let path = joinPathOnBaseDir(baseDirPath, path)
Expand All @@ -978,15 +979,15 @@ provide module Binary {
/**
* Write `Bytes` to a file.
*
* @param writeMode: The type of write operation to perform; `Truncate` by default
* @param baseDirPath: The path to the directory to begin path resolution
* @param path: The file path to write to
* @param data: The bytes to write to the file
* @param writeMode: The type of write operation to perform; `Truncate` by default
* @returns `Ok(void)` if the operation is successful or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let writeFile = (baseDirPath=None, path, data, writeMode=Truncate) => {
provide let writeFile = (writeMode=Truncate, baseDirPath=None, path, data) => {
let path = joinPathOnBaseDir(baseDirPath, path)
let openMode = match (writeMode) {
Truncate => WriteFile,
Expand All @@ -1013,7 +1014,7 @@ provide module Binary {
/**
* Functionality for reading and writing `String`s to files.
*
* @since v0.6.5
* @since v0.6.7
*/
provide module Utf8 {
/**
Expand All @@ -1023,7 +1024,7 @@ provide module Utf8 {
* @param path: The file path to read from
* @returns `Ok(contents)` containing the string read if successful or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let readFile = (baseDirPath=None, path) => {
let bytes = Binary.readFile(baseDirPath=baseDirPath, path)
Expand All @@ -1033,16 +1034,16 @@ provide module Utf8 {
/**
* Write a `String` to a file.
*
* @param writeMode: The type of write operation to perform; `Truncate` by default
* @param baseDirPath: The path to the directory to begin path resolution
* @param path: The file path to write to
* @param data: The string to write to the file
* @param writeMode: The type of write operation to perform; `Truncate` by default
* @returns `Ok(void)` if the operation is successful or `Err(err)` if a file system error is encountered
*
* @since v0.6.5
* @since v0.6.7
*/
provide let writeFile = (baseDirPath=None, path, data, writeMode=Truncate) => {
provide let writeFile = (writeMode=Truncate, baseDirPath=None, path, data) => {
let bytes = String.encode(data, String.UTF8)
Binary.writeFile(baseDirPath=baseDirPath, path, bytes, writeMode=writeMode)
Binary.writeFile(writeMode=writeMode, baseDirPath=baseDirPath, path, bytes)
}
}
Loading

0 comments on commit 600b4ad

Please sign in to comment.