tgetfileinfo.nim 4.1 KB

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