From 17663172d954c4503ca135845d16cff0a64feb0b Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 10 Oct 2018 13:53:30 -0700 Subject: [PATCH] scope rest of tos.nim under blocks to avoid variable scope bugs --- tests/stdlib/tos.nim | 178 +++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index bdb1a4cc7e24c..c10f7036b00a5 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -53,122 +53,122 @@ true import os, strutils -let files = @["these.txt", "are.x", "testing.r", "files.q"] -let dirs = @["some", "created", "test", "dirs"] +block fileOperations: + let files = @["these.txt", "are.x", "testing.r", "files.q"] + let dirs = @["some", "created", "test", "dirs"] -let dname = "__really_obscure_dir_name" + let dname = "__really_obscure_dir_name" -# TODO: move these under `block XXX`, see `block normalizedPath` below, to -# avoid variable scoping bugs. -createDir(dname) -echo dirExists(dname) - -# Test creating files and dirs -for dir in dirs: - createDir(dname/dir) - echo dirExists(dname/dir) + createDir(dname) + echo dirExists(dname) -for file in files: - let fh = open(dname/file, fmReadWrite) - fh.close() - echo fileExists(dname/file) + # Test creating files and dirs + for dir in dirs: + createDir(dname/dir) + echo dirExists(dname/dir) -echo "All:" + for file in files: + let fh = open(dname/file, fmReadWrite) + fh.close() + echo fileExists(dname/file) -template norm(x): untyped = - (when defined(windows): x.replace('\\', '/') else: x) + echo "All:" -for path in walkPattern(dname/"*"): - echo path.norm + template norm(x): untyped = + (when defined(windows): x.replace('\\', '/') else: x) -echo "Files:" + for path in walkPattern(dname/"*"): + echo path.norm -for path in walkFiles(dname/"*"): - echo path.norm + echo "Files:" -echo "Dirs:" + for path in walkFiles(dname/"*"): + echo path.norm -for path in walkDirs(dname/"*"): - echo path.norm + echo "Dirs:" -# Test removal of files dirs -for dir in dirs: - removeDir(dname/dir) - echo dirExists(dname/dir) + for path in walkDirs(dname/"*"): + echo path.norm -for file in files: - removeFile(dname/file) - echo fileExists(dname/file) + # Test removal of files dirs + for dir in dirs: + removeDir(dname/dir) + echo dirExists(dname/dir) -removeDir(dname) -echo dirExists(dname) + for file in files: + removeFile(dname/file) + echo fileExists(dname/file) -# createDir should create recursive directories -createDir(dirs[0] / dirs[1]) -echo dirExists(dirs[0] / dirs[1]) # true -removeDir(dirs[0]) + removeDir(dname) + echo dirExists(dname) -# createDir should properly handle trailing separator -createDir(dname / "") -echo dirExists(dname) # true -removeDir(dname) + # createDir should create recursive directories + createDir(dirs[0] / dirs[1]) + echo dirExists(dirs[0] / dirs[1]) # true + removeDir(dirs[0]) -# createDir should raise IOError if the path exists -# and is not a directory -open(dname, fmWrite).close -try: - createDir(dname) -except IOError: - echo "Raises" -removeFile(dname) + # createDir should properly handle trailing separator + createDir(dname / "") + echo dirExists(dname) # true + removeDir(dname) -# removeFile should not remove directory -createDir(dname) -try: + # createDir should raise IOError if the path exists + # and is not a directory + open(dname, fmWrite).close + try: + createDir(dname) + except IOError: + echo "Raises" removeFile(dname) -except OSError: - echo "Raises" -removeDir(dname) -# test copyDir: -createDir("a/b") -open("a/b/file.txt", fmWrite).close -createDir("a/b/c") -open("a/b/c/fileC.txt", fmWrite).close + # removeFile should not remove directory + createDir(dname) + try: + removeFile(dname) + except OSError: + echo "Raises" + removeDir(dname) + + # test copyDir: + createDir("a/b") + open("a/b/file.txt", fmWrite).close + createDir("a/b/c") + open("a/b/c/fileC.txt", fmWrite).close -copyDir("a", "../dest/a") -removeDir("a") + copyDir("a", "../dest/a") + removeDir("a") -echo dirExists("../dest/a/b") -echo fileExists("../dest/a/b/file.txt") + echo dirExists("../dest/a/b") + echo fileExists("../dest/a/b/file.txt") -echo fileExists("../dest/a/b/c/fileC.txt") -removeDir("../dest") + echo fileExists("../dest/a/b/c/fileC.txt") + removeDir("../dest") -# test copyDir: -# if separator at the end of a path -createDir("a/b") -open("a/file.txt", fmWrite).close + # test copyDir: + # if separator at the end of a path + createDir("a/b") + open("a/file.txt", fmWrite).close -copyDir("a/", "../dest/a/") -removeDir("a") + copyDir("a/", "../dest/a/") + removeDir("a") -echo dirExists("../dest/a/b") -echo fileExists("../dest/a/file.txt") -removeDir("../dest") + echo dirExists("../dest/a/b") + echo fileExists("../dest/a/file.txt") + removeDir("../dest") -# Test get/set modification times -# Should support at least microsecond resolution import times -let tm = fromUnix(0) + 100.microseconds -writeFile("a", "") -setLastModificationTime("a", tm) - -when defined(macosx): - echo "true" -else: - echo getLastModificationTime("a") == tm -removeFile("a") +block modificationTime: + # Test get/set modification times + # Should support at least microsecond resolution + let tm = fromUnix(0) + 100.microseconds + writeFile("a", "") + setLastModificationTime("a", tm) + + when defined(macosx): + echo "true" + else: + echo getLastModificationTime("a") == tm + removeFile("a") block normalizedPath: when defined(posix):