<s> import os <EOL> import shutil <EOL> import tempfile <EOL> import unittest <EOL> import contextlib <EOL> from . . import archivebot <EOL> def touch ( path ) : <EOL> with open ( path , "<STR_LIT:wb>" ) : <EOL> pass <EOL> @ contextlib . contextmanager <EOL> def tmpdir ( ) : <EOL> tmp = tempfile . mkdtemp ( ) <EOL> try : <EOL> yield tmp <EOL> finally : <EOL> shutil . rmtree ( tmp ) <EOL> class TestUniqueWritableFile ( unittest . TestCase ) : <EOL> def test_should_create_a_file_without_a_sequence_number_when_no_files_exist ( self ) : <EOL> with tmpdir ( ) as tmp : <EOL> with archivebot . _unique_writable_file ( tmp , "<STR_LIT:filename>" , "<STR_LIT>" ) as ( filename , _ ) : <EOL> self . assertEqual ( filename , os . path . join ( tmp , "<STR_LIT>" ) ) <EOL> def test_should_pick_the_next_sequential_number_when_files_exist ( self ) : <EOL> with tmpdir ( ) as tmp : <EOL> touch ( os . path . join ( tmp , "<STR_LIT>" ) ) <EOL> touch ( os . path . join ( tmp , "<STR_LIT>" ) ) <EOL> touch ( os . path . join ( tmp , "<STR_LIT>" ) ) <EOL> with archivebot . _unique_writable_file ( tmp , "<STR_LIT:filename>" , "<STR_LIT>" ) as ( filename , _ ) : <EOL> self . assertEqual ( filename , os . path . join ( tmp , "<STR_LIT>" ) ) <EOL> def test_should_raise_OSError_other_than_EEXIST ( self ) : <EOL> def try_to_create ( directory ) : <EOL> with archivebot . _unique_writable_file ( directory , "<STR_LIT:filename>" , "<STR_LIT>" ) : <EOL> pass <EOL> with tmpdir ( ) as tmp : <EOL> non_existing_dir = os . path . join ( tmp , "<STR_LIT>" ) <EOL> self . assertRaises ( OSError , try_to_create , non_existing_dir ) <EOL> class TestRename ( unittest . TestCase ) : <EOL> def test_should_not_go_outside_the_original_directory ( self ) : <EOL> with tmpdir ( ) as directory : <EOL> tmp_file = tempfile . NamedTemporaryFile ( dir = directory , delete = False ) <EOL> new_name = archivebot . _rename ( tmp_file . name ) <EOL> self . assertEqual ( os . path . abspath ( os . path . dirname ( new_name ) ) , os . path . abspath ( directory ) ) <EOL> def test_should_move_the_original_file ( self ) : <EOL> with tmpdir ( ) as directory : <EOL> tmp_file = tempfile . NamedTemporaryFile ( dir = directory , delete = False ) <EOL> new_name = archivebot . _rename ( tmp_file . name ) <EOL> with open ( new_name , "<STR_LIT:rb>" ) as new_file : <EOL> self . assertTrue ( os . path . sameopenfile ( new_file . fileno ( ) , tmp_file . fileno ( ) ) ) <EOL> def test_should_not_keep_the_original_file ( self ) : <EOL> with tmpdir ( ) as directory : <EOL> tmp_file = tempfile . NamedTemporaryFile ( dir = directory , delete = False ) <EOL> archivebot . _rename ( tmp_file . name ) <EOL> self . assertFalse ( os . path . isfile ( tmp_file . name ) ) <EOL> def test_should_change_the_file_name ( self ) : <EOL> with tmpdir ( ) as directory : <EOL> tmp_file = tempfile . NamedTemporaryFile ( dir = directory , delete = False ) <EOL> new_name = archivebot . _rename ( tmp_file . name ) <EOL> self . assertNotEqual ( os . path . abspath ( tmp_file . name ) , os . path . abspath ( new_name ) ) <EOL> def test_should_create_a_filename_with_the_dotcompress_suffix ( self ) : <EOL> with tmpdir ( ) as directory : <EOL> tmp_file = tempfile . NamedTemporaryFile ( dir = directory , delete = False ) <EOL> new_name = archivebot . _rename ( tmp_file . name ) <EOL> self . assertTrue ( "<STR_LIT>" in os . path . basename ( new_name ) ) <EOL> class TestCompress ( unittest . TestCase ) : <EOL> def test_dotcompress ( self ) : <EOL> with tempfile . NamedTemporaryFile ( prefix = "<STR_LIT>" ) as tmp : <EOL> self . assertRaises ( ValueError , archivebot . _compress , tmp . name ) <EOL> def test_valid_compress ( self ) : <EOL> with tmpdir ( ) as directory : <EOL> tmp = tempfile . NamedTemporaryFile ( suffix = "<STR_LIT>" , dir = directory , delete = False ) <EOL> tmp . write ( "<STR_LIT:test>" ) <EOL> gz_file = archivebot . _compress ( tmp . name ) <EOL> try : <EOL> self . assertEqual ( gz_file , tmp . name [ : - <NUM_LIT> ] + "<STR_LIT>" ) <EOL> finally : <EOL> os . remove ( gz_file ) </s>
