<s> from os import path <EOL> import unittest <EOL> import tempfile <EOL> import shutil <EOL> import random <EOL> import json <EOL> import dulwich <EOL> from support import SpaghettiTestCase , setup_logger , randomdata <EOL> from spaghettifs . storage import GitStorage , FeatureBlob <EOL> from spaghettifs import treetree <EOL> class BackendTestCase ( SpaghettiTestCase ) : <EOL> def test_walk ( self ) : <EOL> root = self . repo . get_root ( ) <EOL> self . assertTrue ( root . is_dir ) <EOL> self . assertEqual ( set ( root . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT:b>' ] ) ) <EOL> self . assertRaises ( KeyError , lambda : root [ '<STR_LIT>' ] ) <EOL> a_txt = root [ '<STR_LIT>' ] <EOL> self . assertFalse ( a_txt . is_dir ) <EOL> self . assertEqual ( a_txt . name , '<STR_LIT>' ) <EOL> self . assertEqual ( a_txt . size , <NUM_LIT> ) <EOL> self . assertEqual ( a_txt . _read_all_data ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( a_txt . path , '<STR_LIT>' ) <EOL> b = root [ '<STR_LIT:b>' ] <EOL> self . assertTrue ( b . is_dir ) <EOL> self . assertEqual ( set ( b . keys ( ) ) , set ( [ '<STR_LIT:c>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( b . path , '<STR_LIT>' ) <EOL> c = b [ '<STR_LIT:c>' ] <EOL> self . assertTrue ( c . is_dir ) <EOL> self . assertEqual ( set ( c . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( c . path , '<STR_LIT>' ) <EOL> d = c [ '<STR_LIT>' ] <EOL> self . assertEqual ( d . path , '<STR_LIT>' ) <EOL> def test_create_file ( self ) : <EOL> b = self . repo . get_root ( ) [ '<STR_LIT:b>' ] <EOL> g_txt = b . create_file ( '<STR_LIT>' ) <EOL> self . assertFalse ( g_txt . is_dir ) <EOL> self . assertEqual ( g_txt . size , <NUM_LIT:0> ) <EOL> self . assertEqual ( g_txt . _read_all_data ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( g_txt . name , '<STR_LIT>' ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> g_txt_2 = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] <EOL> self . assertFalse ( g_txt_2 . is_dir ) <EOL> self . assertEqual ( g_txt_2 . size , <NUM_LIT:0> ) <EOL> self . assertEqual ( g_txt_2 . _read_all_data ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( g_txt_2 . name , '<STR_LIT>' ) <EOL> def test_write_file_data ( self ) : <EOL> def assert_git_contents ( data ) : <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> h_txt_2 = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] <EOL> self . assertEqual ( h_txt_2 . size , len ( data ) ) <EOL> self . assertEqual ( h_txt_2 . _read_all_data ( ) , data ) <EOL> b = self . repo . get_root ( ) [ '<STR_LIT:b>' ] <EOL> h_txt = b . create_file ( '<STR_LIT>' ) <EOL> h_txt . write_data ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . assertEqual ( h_txt . size , <NUM_LIT:10> ) <EOL> self . assertEqual ( h_txt . _read_all_data ( ) , '<STR_LIT>' ) <EOL> assert_git_contents ( '<STR_LIT>' ) <EOL> h_txt . write_data ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . assertEqual ( h_txt . size , <NUM_LIT:15> ) <EOL> self . assertEqual ( h_txt . _read_all_data ( ) , '<STR_LIT>' ) <EOL> assert_git_contents ( '<STR_LIT>' ) <EOL> h_txt . truncate ( <NUM_LIT> ) <EOL> self . assertEqual ( h_txt . size , <NUM_LIT> ) <EOL> self . assertEqual ( h_txt . _read_all_data ( ) , '<STR_LIT>' ) <EOL> assert_git_contents ( '<STR_LIT>' ) <EOL> h_txt . truncate ( <NUM_LIT:5> ) <EOL> self . assertEqual ( h_txt . size , <NUM_LIT:5> ) <EOL> self . assertEqual ( h_txt . _read_all_data ( ) , '<STR_LIT:hello>' ) <EOL> assert_git_contents ( '<STR_LIT:hello>' ) <EOL> h_txt . write_data ( '<STR_LIT>' , <NUM_LIT:5> ) <EOL> self . assertEqual ( h_txt . size , <NUM_LIT:11> ) <EOL> self . assertEqual ( h_txt . _read_all_data ( ) , '<STR_LIT>' ) <EOL> assert_git_contents ( '<STR_LIT>' ) <EOL> def test_remove_file ( self ) : <EOL> c = self . repo . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> self . assertEqual ( set ( c . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> d_txt = c [ '<STR_LIT>' ] <EOL> d_txt . unlink ( ) <EOL> self . assertEqual ( set ( c . keys ( ) ) , set ( [ '<STR_LIT>' ] ) ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> c_2 = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> self . assertEqual ( set ( c_2 . keys ( ) ) , set ( [ '<STR_LIT>' ] ) ) <EOL> def test_make_directory ( self ) : <EOL> c = self . repo . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> x = c . create_directory ( '<STR_LIT:x>' ) <EOL> self . assertEqual ( set ( c . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:x>' ] ) ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> c_2 = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> self . assertEqual ( set ( c_2 . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:x>' ] ) ) <EOL> y = x . create_file ( '<STR_LIT:y>' ) <EOL> y . write_data ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . assertEqual ( set ( x . keys ( ) ) , set ( [ '<STR_LIT:y>' ] ) ) <EOL> self . assertEqual ( y . _read_all_data ( ) , '<STR_LIT>' ) <EOL> repo3 = GitStorage ( self . repo_path ) <EOL> c_3 = repo3 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> self . assertEqual ( set ( c_3 . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:x>' ] ) ) <EOL> x_3 = c_3 [ '<STR_LIT:x>' ] <EOL> self . assertEqual ( set ( x_3 . keys ( ) ) , set ( [ '<STR_LIT:y>' ] ) ) <EOL> y_3 = x_3 [ '<STR_LIT:y>' ] <EOL> self . assertEqual ( y_3 . _read_all_data ( ) , '<STR_LIT>' ) <EOL> x . unlink ( ) <EOL> repo4 = GitStorage ( self . repo_path ) <EOL> c_4 = repo4 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> self . assertEqual ( set ( c_4 . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_empty_directory ( self ) : <EOL> c = self . repo . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> x = c . create_directory ( '<STR_LIT:x>' ) <EOL> x . create_file ( '<STR_LIT:f>' ) <EOL> x [ '<STR_LIT:f>' ] . unlink ( ) <EOL> self . assertEqual ( set ( x . keys ( ) ) , set ( ) ) <EOL> def test_30_files ( self ) : <EOL> b = self . repo . get_root ( ) [ '<STR_LIT:b>' ] <EOL> g = b . create_directory ( '<STR_LIT:g>' ) <EOL> for c in xrange ( <NUM_LIT:30> ) : <EOL> f = g . create_file ( '<STR_LIT>' % c ) <EOL> f . write_data ( '<STR_LIT>' % c , <NUM_LIT:0> ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> g2 = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:g>' ] <EOL> for c in xrange ( <NUM_LIT:30> ) : <EOL> f2 = g2 [ '<STR_LIT>' % c ] <EOL> self . assertEqual ( f2 . _read_all_data ( ) , '<STR_LIT>' % c ) <EOL> def test_dangerous_filenames ( self ) : <EOL> g = self . repo . get_root ( ) [ '<STR_LIT:b>' ] . create_directory ( '<STR_LIT:g>' ) <EOL> h = self . repo . get_root ( ) [ '<STR_LIT:b>' ] . create_directory ( '<STR_LIT:h>' ) <EOL> fail_names = [ '<STR_LIT:.>' , '<STR_LIT:..>' , '<STR_LIT:/>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:x>' * <NUM_LIT> ] <EOL> ok_names = [ '<STR_LIT:U+0020>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:">' , "<STR_LIT:'>" , <EOL> '<STR_LIT:(>' , '<STR_LIT:)>' , '<STR_LIT:->' , '<STR_LIT:+>' , '<STR_LIT:\\>' , '<STR_LIT:=>' , '<STR_LIT:?>' , '<STR_LIT:*>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:x>' * <NUM_LIT:255> ] <EOL> for name in fail_names : <EOL> self . assertRaises ( ValueError , g . create_file , name ) <EOL> self . assertRaises ( ValueError , h . create_directory , name ) <EOL> for name in ok_names : <EOL> g . create_file ( name ) . write_data ( repr ( name * <NUM_LIT:2> ) , <NUM_LIT:0> ) <EOL> d = h . create_directory ( name ) <EOL> d . create_file ( '<STR_LIT>' ) <EOL> d . create_directory ( '<STR_LIT>' ) <EOL> self . assertEqual ( set ( ok_names ) , set ( g . keys ( ) ) ) <EOL> self . assertEqual ( set ( ok_names ) , set ( h . keys ( ) ) ) <EOL> for name in ok_names : <EOL> self . assertEqual ( g [ name ] . _read_all_data ( ) , repr ( name * <NUM_LIT:2> ) ) <EOL> g [ name ] . unlink ( ) <EOL> self . assertEqual ( set ( h [ name ] . keys ( ) ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> h [ name ] [ '<STR_LIT>' ] . unlink ( ) <EOL> h [ name ] [ '<STR_LIT>' ] . unlink ( ) <EOL> self . assertEqual ( list ( h [ name ] . keys ( ) ) , [ ] ) <EOL> h [ name ] . unlink ( ) <EOL> self . assertEqual ( list ( h . keys ( ) ) , [ ] ) <EOL> def test_read_past_eof ( self ) : <EOL> a_txt = self . repo . get_root ( ) [ '<STR_LIT>' ] <EOL> try : <EOL> data = a_txt . read_data ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> except Exception , e : <EOL> self . fail ( '<STR_LIT>' % e ) <EOL> self . assertEqual ( data , '<STR_LIT>' ) <EOL> try : <EOL> data = a_txt . read_data ( <NUM_LIT> , <NUM_LIT:100> ) <EOL> except Exception , e : <EOL> self . fail ( '<STR_LIT>' % e ) <EOL> self . assertEqual ( data , '<STR_LIT>' ) <EOL> def test_hardlink ( self ) : <EOL> root = self . repo . get_root ( ) <EOL> a = root [ '<STR_LIT>' ] <EOL> self . assertEqual ( a . inode [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> linked_a = root [ '<STR_LIT:b>' ] . link_file ( '<STR_LIT>' , a ) <EOL> self . assertTrue ( id ( a . inode ) == id ( linked_a . inode ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( a . inode [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> a . write_data ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . assertEqual ( linked_a . _read_all_data ( ) , <EOL> '<STR_LIT>' ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> a_2 = repo2 . get_root ( ) [ '<STR_LIT>' ] <EOL> linked_a_2 = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] <EOL> self . assertTrue ( id ( a_2 . inode ) == id ( linked_a_2 . inode ) ) <EOL> self . assertEqual ( a_2 . inode [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( linked_a_2 . _read_all_data ( ) , <EOL> '<STR_LIT>' ) <EOL> a . unlink ( ) <EOL> self . assertEqual ( linked_a . inode [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> inode_name = linked_a . inode . name <EOL> inodes_tt = treetree . TreeTree ( self . repo . eg . root [ '<STR_LIT>' ] , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( inode_name [ <NUM_LIT:1> : ] in inodes_tt ) <EOL> try : <EOL> self . repo . get_inode ( inode_name ) <EOL> except KeyError : <EOL> self . fail ( ) <EOL> linked_a . unlink ( ) <EOL> self . assertFalse ( inode_name in self . repo . eg . root [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( KeyError , self . repo . get_inode , inode_name ) <EOL> class LargeFileTestCase ( SpaghettiTestCase ) : <EOL> large_data = randomdata ( <NUM_LIT> * <NUM_LIT> ) <EOL> def assert_file_contents ( self , reference ) : <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> f = repo2 . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:f>' ] <EOL> self . assertEqual ( f . _read_all_data ( ) , reference , <EOL> '<STR_LIT>' ) <EOL> def test_store ( self ) : <EOL> f = self . repo . get_root ( ) [ '<STR_LIT:b>' ] . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( self . large_data , <NUM_LIT:0> ) <EOL> self . assert_file_contents ( self . large_data ) <EOL> def test_write_chunks ( self ) : <EOL> f = self . repo . get_root ( ) [ '<STR_LIT:b>' ] . create_file ( '<STR_LIT:f>' ) <EOL> block_size = <NUM_LIT:64> * <NUM_LIT> <EOL> for offset in xrange ( <NUM_LIT:0> , len ( self . large_data ) , block_size ) : <EOL> f . write_data ( self . large_data [ offset : offset + block_size ] , offset ) <EOL> self . assert_file_contents ( self . large_data ) <EOL> def test_write_random ( self ) : <EOL> f = self . repo . get_root ( ) [ '<STR_LIT:b>' ] . create_file ( '<STR_LIT:f>' ) <EOL> block_size = <NUM_LIT> * <NUM_LIT> <EOL> offsets = range ( <NUM_LIT:0> , len ( self . large_data ) , block_size ) <EOL> random . shuffle ( offsets ) <EOL> for offset in offsets : <EOL> f . write_data ( self . large_data [ offset : offset + block_size ] , offset ) <EOL> self . assert_file_contents ( self . large_data ) <EOL> def test_truncate ( self ) : <EOL> f = self . repo . get_root ( ) [ '<STR_LIT:b>' ] . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( self . large_data [ : <NUM_LIT> * <NUM_LIT> ] , <NUM_LIT:0> ) <EOL> f . truncate ( <NUM_LIT> * <NUM_LIT> ) <EOL> self . assert_file_contents ( self . large_data [ : <NUM_LIT> * <NUM_LIT> ] ) <EOL> f . write_data ( self . large_data [ <NUM_LIT> * <NUM_LIT> : ] , <NUM_LIT> * <NUM_LIT> ) <EOL> self . assert_file_contents ( self . large_data ) <EOL> def test_write_at_boundaries ( self ) : <EOL> kb64 = <NUM_LIT:64> * <NUM_LIT> <EOL> b = self . repo . get_root ( ) [ '<STR_LIT:b>' ] <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . assert_file_contents ( '<STR_LIT>' ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( self . large_data [ : kb64 - <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> self . assert_file_contents ( self . large_data [ : kb64 - <NUM_LIT:1> ] ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( self . large_data [ : kb64 ] , <NUM_LIT:0> ) <EOL> self . assert_file_contents ( self . large_data [ : kb64 ] ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( self . large_data [ : kb64 + <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> self . assert_file_contents ( self . large_data [ : kb64 + <NUM_LIT:1> ] ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT>' , kb64 ) <EOL> self . assert_file_contents ( '<STR_LIT>' * kb64 ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:3> * kb64 + <NUM_LIT> ) <EOL> self . assert_file_contents ( '<STR_LIT>' * ( <NUM_LIT:3> * kb64 + <NUM_LIT> ) ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:x>' , kb64 - <NUM_LIT:1> ) <EOL> self . assert_file_contents ( '<STR_LIT>' * ( kb64 - <NUM_LIT:1> ) + '<STR_LIT:x>' ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:x>' , kb64 ) <EOL> self . assert_file_contents ( '<STR_LIT>' * ( kb64 ) + '<STR_LIT:x>' ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:3> * kb64 ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:3> * kb64 - <NUM_LIT:1> ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:3> * kb64 + <NUM_LIT:1> ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT:x>' , <NUM_LIT:3> * kb64 - <NUM_LIT:1> ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * ( <NUM_LIT:3> * kb64 - <NUM_LIT:1> ) + '<STR_LIT:x>' + '<STR_LIT:_>' * ( <NUM_LIT:7> * kb64 ) ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT:x>' , <NUM_LIT:3> * kb64 ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * ( <NUM_LIT:3> * kb64 ) + '<STR_LIT:x>' + '<STR_LIT:_>' * ( <NUM_LIT:7> * kb64 - <NUM_LIT:1> ) ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT:x>' , <NUM_LIT:3> * kb64 + <NUM_LIT:1> ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * ( <NUM_LIT:3> * kb64 + <NUM_LIT:1> ) + '<STR_LIT:x>' + '<STR_LIT:_>' * ( <NUM_LIT:7> * kb64 - <NUM_LIT:2> ) ) <EOL> f . unlink ( ) <EOL> f = b . create_file ( '<STR_LIT:f>' ) <EOL> f . write_data ( '<STR_LIT:_>' * <NUM_LIT:10> * kb64 , <NUM_LIT:0> ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:3> * kb64 - <NUM_LIT:1> ) <EOL> self . assert_file_contents ( '<STR_LIT:_>' * ( <NUM_LIT:3> * kb64 - <NUM_LIT:1> ) + '<STR_LIT>' + '<STR_LIT:_>' * ( <NUM_LIT:7> * kb64 - <NUM_LIT:1> ) ) <EOL> f . unlink ( ) <EOL> class InodeMetaTestCase ( SpaghettiTestCase ) : <EOL> def test_read ( self ) : <EOL> a = self . repo . get_root ( ) [ '<STR_LIT>' ] <EOL> self . assertEqual ( a . inode [ '<STR_LIT>' ] , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertEqual ( a . inode [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( a . inode [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( a . inode [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> def test_write ( self ) : <EOL> a = self . repo . get_root ( ) [ '<STR_LIT>' ] <EOL> a . inode [ '<STR_LIT>' ] = <NUM_LIT:0> <NUM_LIT> <EOL> a . inode [ '<STR_LIT>' ] = <NUM_LIT:1000> <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> a_2 = repo2 . get_root ( ) [ '<STR_LIT>' ] <EOL> self . assertEqual ( a_2 . inode [ '<STR_LIT>' ] , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertEqual ( a_2 . inode [ '<STR_LIT>' ] , <NUM_LIT:1000> ) <EOL> class GitStructureTestCase ( SpaghettiTestCase ) : <EOL> def test_commit_chain ( self ) : <EOL> def assert_head_ancestor ( repo , ancestor_id ) : <EOL> commit = repo . commit ( repo . head ( ) ) <EOL> while True : <EOL> try : <EOL> commit_id = commit . get_parents ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> if commit_id == ancestor_id : <EOL> return <EOL> commit = repo . commit ( commit_id ) <EOL> c = self . repo . get_root ( ) [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] <EOL> HEAD_0 = dulwich . repo . Repo ( self . repo_path ) . head ( ) <EOL> c . create_directory ( '<STR_LIT:x>' ) <EOL> repo = dulwich . repo . Repo ( self . repo_path ) <EOL> HEAD_1 = repo . head ( ) <EOL> assert_head_ancestor ( repo , HEAD_0 ) <EOL> c [ '<STR_LIT:x>' ] . create_file ( '<STR_LIT:f>' ) <EOL> repo = dulwich . repo . Repo ( self . repo_path ) <EOL> assert_head_ancestor ( repo , HEAD_1 ) <EOL> class RepoInitTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> self . repo_path = path . join ( self . tmpdir , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> def test_create_repo ( self ) : <EOL> repo = GitStorage . create ( self . repo_path ) <EOL> git = dulwich . repo . Repo ( self . repo_path ) <EOL> commit_tree = git . tree ( git . commit ( git . head ( ) ) . tree ) <EOL> self . assertEqual ( len ( commit_tree . entries ( ) ) , <NUM_LIT:4> ) <EOL> inodes_tree = git . tree ( commit_tree [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( len ( inodes_tree ) , <NUM_LIT:0> ) <EOL> root_ls_blob = git . get_blob ( commit_tree [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( root_ls_blob . data , '<STR_LIT>' ) <EOL> root_sub_tree = git . tree ( commit_tree [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( len ( root_sub_tree . entries ( ) ) , <NUM_LIT:0> ) <EOL> features_blob = git . get_blob ( commit_tree [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> features_dict = json . loads ( features_blob . data ) <EOL> self . assertEqual ( features_dict [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> def test_create_first_objects ( self ) : <EOL> repo = GitStorage . create ( self . repo_path ) <EOL> root = repo . get_root ( ) <EOL> root . create_directory ( '<STR_LIT>' ) <EOL> repo . commit ( '<STR_LIT>' ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> self . assertEqual ( set ( repo2 . get_root ( ) . keys ( ) ) , <EOL> set ( [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( repo2 . get_root ( ) [ '<STR_LIT>' ] . keys ( ) ) , set ( ) ) <EOL> f = root . create_file ( '<STR_LIT>' ) <EOL> self . assertEqual ( f . inode . name , '<STR_LIT>' ) <EOL> f . write_data ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> repo . commit ( '<STR_LIT>' ) <EOL> repo2 = GitStorage ( self . repo_path ) <EOL> self . assertEqual ( set ( repo2 . get_root ( ) . keys ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( repo2 . get_root ( ) [ '<STR_LIT>' ] . _read_all_data ( ) , '<STR_LIT>' ) <EOL> class MockBlob ( object ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = data <EOL> class FeaturesTestCase ( unittest . TestCase ) : <EOL> def test_read ( self ) : <EOL> features = FeatureBlob ( MockBlob ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( features [ '<STR_LIT:a>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( features . get ( '<STR_LIT:a>' ) , <NUM_LIT> ) <EOL> self . assertRaises ( KeyError , lambda : features [ '<STR_LIT:b>' ] ) <EOL> self . assertRaises ( KeyError , lambda : features . get ( '<STR_LIT:b>' ) ) <EOL> self . assertEqual ( features . get ( '<STR_LIT:b>' , '<STR_LIT:x>' ) , '<STR_LIT:x>' ) <EOL> def test_write ( self ) : <EOL> mb = MockBlob ( '<STR_LIT>' ) <EOL> features = FeatureBlob ( mb ) <EOL> features [ '<STR_LIT:b>' ] = '<STR_LIT>' <EOL> self . assertEqual ( json . loads ( mb . data ) , { '<STR_LIT:a>' : <NUM_LIT> , '<STR_LIT:b>' : '<STR_LIT>' } ) <EOL> def test_write_error ( self ) : <EOL> features = FeatureBlob ( MockBlob ( '<STR_LIT>' ) ) <EOL> def set_bad_key ( ) : <EOL> features [ <NUM_LIT> ] = '<STR_LIT>' <EOL> self . assertRaises ( AssertionError , set_bad_key ) <EOL> def set_bad_value ( ) : <EOL> features [ '<STR_LIT:b>' ] = [ '<STR_LIT>' ] <EOL> self . assertRaises ( AssertionError , set_bad_value ) <EOL> try : <EOL> features [ '<STR_LIT:b>' ] = '<STR_LIT>' <EOL> features [ '<STR_LIT:c>' ] = <NUM_LIT:15> <EOL> except ValueError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> setup_logger ( '<STR_LIT>' ) <EOL> unittest . main ( ) </s>
