tgetfileinfo.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. discard """
  2. output: "pcDir\npcFile\npcLinkToDir\npcLinkToFile\n"
  3. joinable: false
  4. """
  5. import os, strutils
  6. import std/[syncio, assertions]
  7. # Cases
  8. # 1 - String : Existing File : Symlink true
  9. # 2 - String : Existing File : Symlink false
  10. # 3 - String : Non-existing File : Symlink true
  11. # 4 - String : Non-existing File : Symlink false
  12. # 5 - Handle : Valid File
  13. # 6 - Handle : Invalid File
  14. # 7 - Handle : Valid Handle
  15. # 8 - Handle : Invalid Handle
  16. proc genBadFileName(limit = 100): string =
  17. ## Generates a filename of a nonexistent file.
  18. ## Returns "" if generation fails.
  19. result = "a"
  20. var hitLimit = true
  21. for i in 0..100:
  22. if fileExists(result):
  23. result.add("a")
  24. else:
  25. hitLimit = false
  26. break
  27. if hitLimit:
  28. result = ""
  29. proc caseOneAndTwo(followLink: bool) =
  30. try:
  31. discard getFileInfo(getAppFilename(), followLink)
  32. #echo("String : Existing File : Symlink $# : Success" % $followLink)
  33. except OSError:
  34. echo("String : Existing File : Symlink $# : Failure" % $followLink)
  35. proc caseThreeAndFour(followLink: bool) =
  36. var invalidName = genBadFileName()
  37. try:
  38. discard getFileInfo(invalidName, true)
  39. echo("String : Non-existing File : Symlink $# : Failure" % $followLink)
  40. except OSError:
  41. discard
  42. #echo("String : Non-existing File : Symlink $# : Success" % $followLink)
  43. proc testGetFileInfo =
  44. # Case 1
  45. caseOneAndTwo(true)
  46. # Case 2
  47. caseOneAndTwo(false)
  48. # Case 3
  49. caseThreeAndFour(true)
  50. # Case 4
  51. caseThreeAndFour(false)
  52. # Case 5 and 7
  53. block:
  54. let
  55. testFile = open(getAppFilename())
  56. testHandle = getFileHandle(testFile)
  57. try:
  58. discard getFileInfo(testFile)
  59. #echo("Handle : Valid File : Success")
  60. except IOError:
  61. echo("Handle : Valid File : Failure")
  62. try:
  63. discard getFileInfo(testHandle)
  64. #echo("Handle : Valid File : Success")
  65. except IOError:
  66. echo("Handle : Valid File : Failure")
  67. # Case 6 and 8
  68. block:
  69. let
  70. testFile: File = nil
  71. testHandle = FileHandle(-1)
  72. try:
  73. discard getFileInfo(testFile)
  74. echo("Handle : Invalid File : Failure")
  75. except IOError, OSError:
  76. discard
  77. #echo("Handle : Invalid File : Success")
  78. try:
  79. discard getFileInfo(testHandle)
  80. echo("Handle : Invalid File : Failure")
  81. except IOError, OSError:
  82. discard
  83. #echo("Handle : Invalid File : Success")
  84. # Test kind for files, directories and symlinks.
  85. block:
  86. let
  87. tmp = getTempDir()
  88. dirPath = tmp / "test-dir"
  89. filePath = tmp / "test-file"
  90. linkDirPath = tmp / "test-link-dir"
  91. linkFilePath = tmp / "test-link-file"
  92. createDir(dirPath)
  93. writeFile(filePath, "")
  94. when defined(posix):
  95. createSymlink(dirPath, linkDirPath)
  96. createSymlink(filePath, linkFilePath)
  97. let
  98. dirInfo = getFileInfo(dirPath)
  99. fileInfo = getFileInfo(filePath)
  100. when defined(posix):
  101. let
  102. linkDirInfo = getFileInfo(linkDirPath, followSymlink = false)
  103. linkFileInfo = getFileInfo(linkFilePath, followSymlink = false)
  104. echo dirInfo.kind
  105. echo fileInfo.kind
  106. when defined(posix):
  107. echo linkDirInfo.kind
  108. echo linkFileInfo.kind
  109. else:
  110. echo pcLinkToDir
  111. echo pcLinkToFile
  112. doAssert dirInfo.isSpecial == false
  113. doAssert fileInfo.isSpecial == false
  114. when defined(posix):
  115. doAssert linkDirInfo.isSpecial == false
  116. doAssert linkFileInfo.isSpecial == false
  117. removeDir(dirPath)
  118. removeFile(filePath)
  119. when defined(posix):
  120. removeFile(linkDirPath)
  121. removeFile(linkFilePath)
  122. # Test that `isSpecial` is set correctly
  123. block:
  124. when defined(posix):
  125. let
  126. tmp = getTempDir()
  127. fifoPath = tmp / "test-fifo"
  128. linkFifoPath = tmp / "test-link-fifo"
  129. doAssert execShellCmd("mkfifo " & fifoPath) == 0
  130. createSymlink(fifoPath, linkFifoPath)
  131. let
  132. fifoInfo = getFileInfo(fifoPath)
  133. linkFifoInfo = getFileInfo(linkFifoPath)
  134. doAssert fifoInfo.isSpecial == true
  135. doAssert linkFifoInfo.isSpecial == true
  136. removeFile(fifoPath)
  137. removeFile(linkFifoPath)
  138. testGetFileInfo()