From 85fb47c11cc985889be9c1a802cfc0331d8ad7a8 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 8 Sep 2010 18:42:32 -0700 Subject: [PATCH] Better temporary directory handling for tests. Add a setUp and tearDown function to the test case class, and use it to create and remove the test/tmp directory for each test. TODO: amend other tests. --- test/common.js | 1 + test/simple/test-fs-symlink.js | 12 ++++-------- test/simple/test-mkdir-rmdir.js | 3 +-- test/simple/testcfg.py | 17 ++++++++++++++++- tools/test.py | 11 ++++++++++- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/test/common.js b/test/common.js index bce2f45315..cd688b7ba0 100644 --- a/test/common.js +++ b/test/common.js @@ -3,6 +3,7 @@ var path = require("path"); exports.testDir = path.dirname(__filename); exports.fixturesDir = path.join(exports.testDir, "fixtures"); exports.libDir = path.join(exports.testDir, "../lib"); +exports.tmpDir = path.join(exports.testDir, "tmp"); exports.PORT = 12346; exports.assert = require('assert'); diff --git a/test/simple/test-fs-symlink.js b/test/simple/test-fs-symlink.js index e46dad3558..0360bcf8bf 100644 --- a/test/simple/test-fs-symlink.js +++ b/test/simple/test-fs-symlink.js @@ -1,13 +1,12 @@ common = require("../common"); -assert = common.assert +var assert = common.assert; var path = require('path'); var fs = require('fs'); var completed = 0; // test creating and reading symbolic link -var linkData = "../../cycles/root.js"; -var linkPath = path.join(common.fixturesDir, "nested-index", 'one', 'symlink1.js'); -try {fs.unlinkSync(linkPath);}catch(e){} +var linkData = path.join(common.fixturesDir, "/cycles/root.js"); +var linkPath = path.join(common.tmpDir, 'symlink1.js'); fs.symlink(linkData, linkPath, function(err){ if (err) throw err; console.log('symlink done'); @@ -21,8 +20,7 @@ fs.symlink(linkData, linkPath, function(err){ // test creating and reading hard link var srcPath = path.join(common.fixturesDir, "cycles", 'root.js'); -var dstPath = path.join(common.fixturesDir, "nested-index", 'one', 'link1.js'); -try {fs.unlinkSync(dstPath);}catch(e){} +var dstPath = path.join(common.tmpDir, 'link1.js'); fs.link(srcPath, dstPath, function(err){ if (err) throw err; console.log('hard link done'); @@ -33,8 +31,6 @@ fs.link(srcPath, dstPath, function(err){ }); process.addListener("exit", function () { - try {fs.unlinkSync(linkPath);}catch(e){} - try {fs.unlinkSync(dstPath);}catch(e){} assert.equal(completed, 2); }); diff --git a/test/simple/test-mkdir-rmdir.js b/test/simple/test-mkdir-rmdir.js index aa469615a9..ea36b74532 100644 --- a/test/simple/test-mkdir-rmdir.js +++ b/test/simple/test-mkdir-rmdir.js @@ -4,8 +4,7 @@ var path = require('path'); var fs = require('fs'); var dirname = path.dirname(__filename); -var fixtures = path.join(dirname, "../fixtures"); -var d = path.join(fixtures, "dir"); +var d = path.join(common.tmpDir, "dir"); var mkdir_error = false; var rmdir_error = false; diff --git a/test/simple/testcfg.py b/test/simple/testcfg.py index 7d730edebd..ca1441f66b 100644 --- a/test/simple/testcfg.py +++ b/test/simple/testcfg.py @@ -27,6 +27,9 @@ import test import os +import shutil +from shutil import rmtree +from os import mkdir from os.path import join, dirname, exists import re @@ -42,7 +45,19 @@ class SimpleTestCase(test.TestCase): self.file = file self.config = config self.mode = mode - + + def tearDown(self): + try: + rmtree(join(dirname(self.config.root), 'tmp')) + except: + pass + + def setUp(self): + try: + mkdir(join(dirname(self.config.root), 'tmp')) + except: + pass + def GetLabel(self): return "%s %s" % (self.mode, self.GetName()) diff --git a/tools/test.py b/tools/test.py index d05dac4732..518ecb7d49 100755 --- a/tools/test.py +++ b/tools/test.py @@ -359,7 +359,16 @@ class TestCase(object): return TestOutput(self, full_command, output) def Run(self): - return self.RunCommand(self.GetCommand()) + self.setUp() + result = self.RunCommand(self.GetCommand()) + self.tearDown() + return result + + def setUp(self): + return + + def tearDown(self): + return class TestOutput(object):