<s> from tests import unittest <EOL> import os <EOL> import tempfile <EOL> import shutil <EOL> from awsshell . utils import FSLayer <EOL> from awsshell . utils import InMemoryFSLayer <EOL> from awsshell . utils import FileReadError <EOL> from awsshell . utils import temporary_file <EOL> class TestFSLayer ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . temporary_filename = os . path . join ( <EOL> self . tempdir , '<STR_LIT>' ) <EOL> self . fslayer = FSLayer ( ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . tempdir ) <EOL> def test_can_read_file_contents ( self ) : <EOL> with open ( self . temporary_filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> self . fslayer . file_contents ( self . temporary_filename ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> self . fslayer . file_contents ( self . temporary_filename , binary = True ) , <EOL> b'<STR_LIT>' ) <EOL> def test_file_exists ( self ) : <EOL> self . assertFalse ( self . fslayer . file_exists ( self . temporary_filename ) ) <EOL> with open ( self . temporary_filename , '<STR_LIT:w>' ) as f : <EOL> pass <EOL> self . assertTrue ( self . fslayer . file_exists ( self . temporary_filename ) ) <EOL> def test_file_does_not_exist_error ( self ) : <EOL> with self . assertRaises ( FileReadError ) : <EOL> self . fslayer . file_contents ( '<STR_LIT>' ) <EOL> class TestInMemoryFSLayer ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . file_mapping = { } <EOL> self . fslayer = InMemoryFSLayer ( self . file_mapping ) <EOL> def test_file_exists ( self ) : <EOL> self . file_mapping [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertTrue ( self . fslayer . file_exists ( '<STR_LIT>' ) ) <EOL> def test_can_read_file_contents ( self ) : <EOL> self . file_mapping [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertEqual ( self . fslayer . file_contents ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( self . fslayer . file_contents ( '<STR_LIT>' , binary = True ) , <EOL> b'<STR_LIT>' ) <EOL> def test_file_does_not_exist_error ( self ) : <EOL> with self . assertRaises ( FileReadError ) : <EOL> self . fslayer . file_contents ( '<STR_LIT>' ) <EOL> class TestTemporaryFile ( unittest . TestCase ) : <EOL> def test_can_use_as_context_manager ( self ) : <EOL> with temporary_file ( '<STR_LIT:w>' ) as f : <EOL> filename = f . name <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> self . assertEqual ( open ( filename ) . read ( ) , "<STR_LIT>" ) <EOL> def test_is_removed_after_exiting_context ( self ) : <EOL> with temporary_file ( '<STR_LIT:w>' ) as f : <EOL> filename = f . name <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> self . assertFalse ( os . path . isfile ( filename ) ) <EOL> def test_can_open_in_read ( self ) : <EOL> with temporary_file ( '<STR_LIT:r>' ) as f : <EOL> filename = f . name <EOL> assert f . read ( ) == '<STR_LIT>' <EOL> with open ( filename , '<STR_LIT:w>' ) as f2 : <EOL> f2 . write ( "<STR_LIT>" ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> assert f . read ( ) == "<STR_LIT>" <EOL> self . assertFalse ( os . path . isfile ( filename ) ) </s>
