<s> import unittest <EOL> import glob <EOL> import os <EOL> import shutil <EOL> import mockfs <EOL> class MockFSTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mfs = mockfs . replace_builtins ( ) <EOL> def tearDown ( self ) : <EOL> mockfs . restore_builtins ( ) <EOL> def _mkfs ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : { } , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> def test_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertFalse ( os . path . exists ( '<STR_LIT:/>' ) ) <EOL> def test_first_level_subdir ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mfs . add_entries ( { '<STR_LIT>' : '<STR_LIT:bar>' } ) <EOL> self . assertTrue ( os . path . exists ( '<STR_LIT:/>' ) ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT:/>' ) ) <EOL> self . assertTrue ( os . path . exists ( '<STR_LIT>' ) ) <EOL> def test_nested_directories ( self ) : <EOL> """<STR_LIT>""" <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> for path in filesystem : <EOL> self . assertTrue ( os . path . isdir ( os . path . dirname ( path ) ) ) <EOL> self . assertTrue ( os . path . exists ( path ) ) <EOL> self . assertTrue ( os . path . isfile ( path ) ) <EOL> def test_unsanitized_entries ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mfs . add_entries ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . exists ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> def test_predicates_on_unsanitized_paths ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mfs . add_entries ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . exists ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> def test_os_remove ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> self . assertTrue ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . isfile ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( os . listdir ( '<STR_LIT>' ) , [ ] ) <EOL> def test_os_remove_does_not_exist ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> self . assertRaises ( OSError , os . remove , '<STR_LIT>' ) <EOL> def test_os_remove_dir ( self ) : <EOL> self . _mkfs ( ) <EOL> self . assertRaises ( OSError , os . remove , '<STR_LIT>' ) <EOL> def test_remove_from_subdir ( self ) : <EOL> self . _mkfs ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT:b>' ) <EOL> entries = os . listdir ( '<STR_LIT>' ) <EOL> self . assertEqual ( entries , [ '<STR_LIT:a>' ] ) <EOL> entries = os . listdir ( '<STR_LIT:.>' ) <EOL> self . assertEqual ( entries , [ '<STR_LIT:a>' ] ) <EOL> def test_os_rmdir ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> self . assertRaises ( OSError , os . rmdir , '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> os . rmdir ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> def test_os_rmdir_file ( self ) : <EOL> self . _mkfs ( ) <EOL> self . assertRaises ( OSError , os . rmdir , '<STR_LIT>' ) <EOL> def test_os_rmdir_nonempty ( self ) : <EOL> self . _mkfs ( ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> self . assertRaises ( OSError , os . rmdir , '<STR_LIT>' ) <EOL> os . rmdir ( '<STR_LIT>' ) <EOL> self . assertRaises ( OSError , os . rmdir , '<STR_LIT>' ) <EOL> def test_os_makedirs ( self ) : <EOL> os . makedirs ( '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT:/>' ) ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> def test_glob_head ( self ) : <EOL> self . _mkfs ( ) <EOL> values = glob . glob ( '<STR_LIT>' ) <EOL> self . assertEqual ( values , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_glob_mid ( self ) : <EOL> self . _mkfs ( ) <EOL> values = glob . glob ( '<STR_LIT>' ) <EOL> self . assertEqual ( values , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_glob_tail ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _mkfs ( ) <EOL> values = glob . glob ( '<STR_LIT>' ) <EOL> self . assertEqual ( values , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_glob_multi ( self ) : <EOL> self . _mkfs ( ) <EOL> values = glob . glob ( '<STR_LIT>' ) <EOL> self . assertEquals ( values , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_chdir ( self ) : <EOL> self . _mkfs ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> self . assertEqual ( os . getcwd ( ) , '<STR_LIT>' ) <EOL> def test_chdir_file ( self ) : <EOL> self . _mkfs ( ) <EOL> self . assertRaises ( OSError , os . chdir , '<STR_LIT>' ) <EOL> def test_chdir_bogus ( self ) : <EOL> self . _mkfs ( ) <EOL> self . assertRaises ( OSError , os . chdir , '<STR_LIT>' ) <EOL> def test_cwd ( self ) : <EOL> self . _mkfs ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> self . assertEqual ( os . getcwd ( ) , '<STR_LIT>' ) <EOL> def test_cwd_rogue ( self ) : <EOL> self . _mkfs ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> self . assertEqual ( os . getcwd ( ) , '<STR_LIT>' ) <EOL> def test_chdir_and_relative_glob_from_root ( self ) : <EOL> self . _mkfs ( ) <EOL> os . chdir ( '<STR_LIT:/>' ) <EOL> globs = glob . glob ( '<STR_LIT>' ) <EOL> self . assertEqual ( globs , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_chdir_and_relative_glob_from_subdir ( self ) : <EOL> self . _mkfs ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> globs = glob . glob ( '<STR_LIT>' ) <EOL> self . assertEqual ( globs , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_listdir_from_subdir ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> entries = os . listdir ( '<STR_LIT:.>' ) <EOL> self . assertEqual ( entries , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> def test_os_getsize ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:{}>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> a_size = os . path . getsize ( '<STR_LIT>' ) <EOL> self . assertEqual ( a_size , <NUM_LIT:0> ) <EOL> b_size = os . path . getsize ( '<STR_LIT>' ) <EOL> self . assertEqual ( b_size , <NUM_LIT:2> ) <EOL> def test_os_getsize_directory ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> dir_size = os . path . getsize ( '<STR_LIT>' ) <EOL> self . assertEqual ( dir_size , <NUM_LIT:2> ) <EOL> def test_os_getsize_subdir ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:{}>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> a_size = os . path . getsize ( '<STR_LIT:a>' ) <EOL> self . assertEqual ( a_size , <NUM_LIT:0> ) <EOL> b_size = os . path . getsize ( '<STR_LIT:b>' ) <EOL> self . assertEqual ( b_size , <NUM_LIT:2> ) <EOL> def test_shutil_rmtree ( self ) : <EOL> filesystem = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . mfs . add_entries ( filesystem ) <EOL> shutil . rmtree ( '<STR_LIT>' ) <EOL> self . assertEqual ( os . listdir ( '<STR_LIT:/>' ) , [ ] ) <EOL> def test_os_makedirs_creates_new_entry ( self ) : <EOL> os . makedirs ( '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . isdir ( '<STR_LIT>' ) ) <EOL> def test_os_makedirs_raises_on_root ( self ) : <EOL> self . assertRaises ( OSError , os . makedirs , '<STR_LIT:/>' ) <EOL> def test_os_makedirs_on_second_try ( self ) : <EOL> os . makedirs ( '<STR_LIT>' ) <EOL> self . assertRaises ( OSError , os . makedirs , '<STR_LIT>' ) <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( MockFSTestCase ) ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
