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

Add staticFileExists and staticDirExists #22278

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ proc registerAdditionalOps*(c: PCtx) =
systemop getCurrentException
registerCallback c, "stdlib.osdirs.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
registerCallback c, "stdlib.staticos.staticDirExists", proc (a: VmArgs) {.nimcall.} =
setResult(a, dirExists(getString(a, 0)))
registerCallback c, "stdlib.staticos.staticFileExists", proc (a: VmArgs) {.nimcall.} =
setResult(a, fileExists(getString(a, 0)))
registerCallback c, "stdlib.compilesettings.querySetting", proc (a: VmArgs) =
setResult(a, querySettingImpl(c.config, getInt(a, 0)))
registerCallback c, "stdlib.compilesettings.querySettingSeq", proc (a: VmArgs) =
Expand Down
13 changes: 13 additions & 0 deletions lib/std/staticos.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## This module implements path handling like os module but works at only compile-time.
## This module works even when cross compiling to OS that is not supported by os module.

proc staticFileExists*(filename: string): bool {.compileTime.} =
## Returns true if `filename` exists and is a regular file or symlink.
##
## Directories, device files, named pipes and sockets return false.
discard

proc staticDirExists*(dir: string): bool {.compileTime.} =
## Returns true if the directory `dir` exists. If `dir` is a file, false
## is returned. Follows symlinks.
discard
8 changes: 8 additions & 0 deletions tests/stdlib/tstaticos.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import std/[assertions, staticos, os]

block:
static:
doAssert staticDirExists("MISSINGFILE") == false
doAssert staticFileExists("MISSINGDIR") == false
doAssert staticDirExists(currentSourcePath().parentDir)
doAssert staticFileExists(currentSourcePath())
2 changes: 2 additions & 0 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ block:

doAssert fileExists("MISSINGFILE") == false
doAssert dirExists("MISSINGDIR") == false
doAssert fileExists(currentSourcePath())
doAssert dirExists(currentSourcePath().parentDir)

# bug #7210
block:
Expand Down