tgetfileinfo.nim 3.3 KB

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