Skip to content

Change into newly created temporary directory for executing a block of code.

License

Notifications You must be signed in to change notification settings

e3krisztian/temp_dir

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

temp_dir

Change into newly created temporary directory for executing a block of code.

Upon leaving the code block the temporary directory and all its contents are removed and the original working directory is restored.

Intended as a tool to ease "integration" testing.

The package contains only two functions: in_temp_dir to be used with the with statement and within_temp_dir to decorate a function.

Examples

Function decorator:

@within_temp_dir
def f():
    return os.getcwd()


class TestScript(unittest.TestCase):

    @within_temp_dir
    def test_output_file_created(self):
        self.create_input_file()
        call_script('input')
        self.assertTrue(os.path.exists('output'))

Context manager:

with in_temp_dir():
    with open('tempfile', 'w') as f:
        f.write(...)
    process_file('tempfile')

Similar functionality elsewhere

Similar functionality can be found at

About

Change into newly created temporary directory for executing a block of code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages