Changeset 27098

Show
Ignore:
Timestamp:
07/07/2009 07:20:40 PM (7 months ago)
Author:
thijs
Message:
Merge trial-tmp-marker-3481: Trial now drops a marker file into _trial_temp and refuses to delete and re-create any directory that doesn't have the marker. Author: jonathanj, thijs Reviewer: glyph Fixes: #3481
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/twisted/trial/runner.py

    r26734 r27098  
    11# -*- test-case-name: twisted.trial.test.test_runner -*- 
    2 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. 
     2# Copyright (c) 2001-2009 Twisted Matrix Laboratories. 
    33# See LICENSE for details. 
    44 
     
    1010 
    1111 
    12 import pdb, shutil 
     12import pdb 
    1313import os, types, warnings, sys, inspect, imp 
    1414import random, doctest, time 
    1515 
    16 from twisted.python import reflect, log, failure, modules 
     16from twisted.python import reflect, log, failure, modules, filepath 
    1717from twisted.python.util import dsu 
    1818from twisted.python.compat import set 
     
    3737    A working directory was specified to the runner, but another test run is 
    3838    currently using that directory. 
     39    """ 
     40 
     41 
     42 
     43class _NoTrialMarker(Exception): 
     44    """ 
     45    No trial marker file could be found. 
     46 
     47    Raised when trial attempts to remove a trial temporary working directory 
     48    that does not contain a marker file. 
    3949    """ 
    4050 
     
    6676    """ 
    6777    return os.path.abspath(filename1) == os.path.abspath(filename2) 
     78 
    6879 
    6980def filenameToModule(fn): 
     
    698709 
    699710    def _removeSafely(self, path): 
     711        """ 
     712        Safely remove a path, recursively. 
     713 
     714        If C{path} does not contain a node named C{_trial_marker}, a 
     715        L{_NoTrialmarker} exception is raised and the path is not removed. 
     716        """ 
     717        if not path.child('_trial_marker').exists(): 
     718            raise _NoTrialMarker( 
     719                '%r is not a trial temporary path, refusing to remove it' 
     720                % (path,)) 
     721 
    700722        try: 
    701             shutil.rmtree(path) 
     723            path.remove() 
    702724        except OSError, e: 
    703725            print ("could not remove %r, caught OSError [Errno %s]: %s" 
    704                    % (path, e.errno,e.strerror)) 
     726                   % (path, e.errno, e.strerror)) 
    705727            try: 
    706                 os.rename(path, 
    707                           os.path.abspath("_trial_temp_old%s" 
    708                                           % random.randint(0, 99999999))) 
     728                newPath = FilePath('_trial_temp_old%s' 
     729                                   % random.randint(0, 99999999)) 
     730                path.moveTo(newPath) 
    709731            except OSError, e: 
    710732                print ("could not rename path, caught OSError [Errno %s]: %s" 
     
    716738        self._tearDownLogFile() 
    717739        currentDir = os.getcwd() 
    718         base = os.path.normpath(os.path.abspath(self.workingDirectory)) 
     740        base = filepath.FilePath(self.workingDirectory) 
    719741        counter = 0 
    720742        while True: 
    721743            if counter: 
    722                 testdir = '%s-%d' % (base, counter) 
     744                testdir = base.sibling('%s-%d' % (base.basename(), counter)) 
    723745            else: 
    724746                testdir = base 
    725747 
    726             self._testDirLock = FilesystemLock(testdir + '.lock') 
     748            self._testDirLock = FilesystemLock(testdir.path + '.lock') 
    727749            if self._testDirLock.lock(): 
    728750                # It is not in use 
    729                 if os.path.exists(testdir): 
     751                if testdir.exists(): 
    730752                    # It exists though - delete it 
    731753                    self._removeSafely(testdir) 
     
    738760                    raise _WorkingDirectoryBusy() 
    739761 
    740         os.mkdir(testdir) 
    741         os.chdir(testdir) 
     762        testdir.makedirs() 
     763        os.chdir(testdir.path) 
     764        file('_trial_marker', 'w').close() 
    742765        return currentDir 
    743766 
  • trunk/twisted/trial/test/test_runner.py

    r25279 r27098  
    1 # Copyright (c) 2005-2008 Twisted Matrix Laboratories. 
     1# Copyright (c) 2005-2009 Twisted Matrix Laboratories. 
    22# See LICENSE for details. 
    33# 
     
    476476        self.assertEqual(self.standardReport, result._calls) 
    477477        self.assertEqual(['runcall'], debugger._calls) 
     478 
     479 
     480    def test_noMarker(self): 
     481        """ 
     482        Specifying a temp directory that does not have a trial marker results 
     483        in an exception and does not remove the specified directory. 
     484        """ 
     485        tempPath = self.mktemp() 
     486        # Create our working directory outside of trial. 
     487        os.mkdir(tempPath) 
     488        self.parseOptions(['--temp-directory', tempPath, 
     489                           'twisted.trial.test.sample']) 
     490        myRunner = self.getRunner() 
     491        loader = runner.TestLoader() 
     492        suite = loader.loadByName('twisted.trial.test.sample', True) 
     493        self.assertRaises(runner._NoTrialMarker, myRunner.run, suite) 
     494        self.assertTrue(os.path.exists(tempPath)) 
    478495 
    479496