<s> import io <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import tempfile <EOL> import unittest <EOL> import gitlint <EOL> class E2EBase ( object ) : <EOL> @ staticmethod <EOL> def lint ( ) : <EOL> """<STR_LIT>""" <EOL> out = io . StringIO ( ) <EOL> response = gitlint . main ( [ ] , stdout = out , stderr = out ) <EOL> return response , out . getvalue ( ) <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . original_cwd = os . getcwd ( ) <EOL> cls . temp_directory = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> os . chdir ( cls . temp_directory ) <EOL> cls . init_repo ( ) <EOL> def setUp ( self ) : <EOL> self . filename_repo = None <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> shutil . rmtree ( cls . temp_directory , True ) <EOL> os . chdir ( cls . original_cwd ) <EOL> def tearDown ( self ) : <EOL> if self . filename_repo is None : <EOL> return <EOL> with open ( self . filename_repo , '<STR_LIT:w>' ) as f : <EOL> pass <EOL> self . add ( self . filename_repo ) <EOL> self . commit ( '<STR_LIT>' ) <EOL> def test_extension_not_defined ( self ) : <EOL> extension = '<STR_LIT>' <EOL> filename = os . path . join ( self . temp_directory , '<STR_LIT:filename>' + extension ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> self . add ( filename ) <EOL> response , output = self . lint ( ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:0> , response , '<STR_LIT>' % ( response , output ) ) <EOL> self . assertIn ( os . path . relpath ( filename ) , output ) <EOL> self . assertIn ( '<STR_LIT>' , output ) <EOL> self . assertIn ( extension , output ) <EOL> def get_linter_output ( self , linter_name , file_path ) : <EOL> cache_path = os . path . expanduser ( '<STR_LIT>' ) <EOL> filename = os . path . join ( cache_path , linter_name , file_path [ <NUM_LIT:1> : ] ) <EOL> if not os . path . exists ( filename ) : <EOL> return '<STR_LIT>' % filename <EOL> with open ( filename ) as f : <EOL> output = f . read ( ) <EOL> return output <EOL> def assert_linter_works ( self , linter_name , extension ) : <EOL> """<STR_LIT>""" <EOL> data_dirname = os . path . join ( <EOL> os . path . dirname ( os . path . realpath ( __file__ ) ) , '<STR_LIT:data>' ) <EOL> self . filename_repo = filename_repo = os . path . join ( <EOL> self . temp_directory , '<STR_LIT>' % ( linter_name , extension ) ) <EOL> filename_original = os . path . join ( <EOL> data_dirname , linter_name , '<STR_LIT>' % extension ) <EOL> filename_error = os . path . join ( <EOL> data_dirname , linter_name , '<STR_LIT>' % extension ) <EOL> filename_nonewerror = os . path . join ( <EOL> data_dirname , linter_name , '<STR_LIT>' % extension ) <EOL> self . assertTrue ( <EOL> os . path . exists ( filename_original ) , <EOL> '<STR_LIT>' % filename_original ) <EOL> self . assertTrue ( <EOL> os . path . exists ( filename_error ) , <EOL> '<STR_LIT>' % filename_error ) <EOL> self . assertTrue ( os . path . exists ( <EOL> filename_nonewerror ) , <EOL> '<STR_LIT>' % filename_nonewerror ) <EOL> shutil . copy ( filename_original , filename_repo ) <EOL> self . add ( filename_repo ) <EOL> self . commit ( '<STR_LIT>' ) <EOL> shutil . copy ( filename_error , filename_repo ) <EOL> response , output = self . lint ( ) <EOL> self . assertNotEquals ( <EOL> <NUM_LIT:0> , response , <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) % <EOL> ( filename_error , <EOL> output , <EOL> self . get_linter_output ( linter_name , filename_repo ) ) ) <EOL> self . add ( filename_repo ) <EOL> self . commit ( '<STR_LIT>' ) <EOL> shutil . copy ( filename_nonewerror , filename_repo ) <EOL> response , output = self . lint ( ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:0> , response , <EOL> ( '<STR_LIT>' ) % <EOL> ( filename_nonewerror , output ) ) <EOL> self . add ( filename_repo ) <EOL> self . commit ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def add_linter_check ( cls , linter_name , extension ) : <EOL> """<STR_LIT>""" <EOL> def test_linter ( self ) : <EOL> self . assert_linter_works ( linter_name , extension ) <EOL> test_linter . __name__ = '<STR_LIT>' % ( linter_name , <EOL> extension [ <NUM_LIT:1> : ] ) <EOL> setattr ( cls , test_linter . __name__ , test_linter ) <EOL> @ classmethod <EOL> def add_linter_checks ( cls ) : <EOL> """<STR_LIT>""" <EOL> for extension , linter_list in gitlint . get_config ( None ) . items ( ) : <EOL> for linter in linter_list : <EOL> cls . add_linter_check ( linter . args [ <NUM_LIT:0> ] , extension ) <EOL> E2EBase . add_linter_checks ( ) <EOL> def execute ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs [ '<STR_LIT>' ] = subprocess . STDOUT <EOL> try : <EOL> subprocess . check_output ( * args , ** kwargs ) <EOL> except subprocess . CalledProcessError as error : <EOL> print ( error . output ) <EOL> raise <EOL> class TestGitE2E ( E2EBase , unittest . TestCase ) : <EOL> @ classmethod <EOL> def init_repo ( cls ) : <EOL> """<STR_LIT>""" <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) : <EOL> pass <EOL> cls . add ( '<STR_LIT>' ) <EOL> cls . commit ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def commit ( message ) : <EOL> """<STR_LIT>""" <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , message , '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def add ( filename ) : <EOL> """<STR_LIT>""" <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' , filename ] ) <EOL> def test_submodules ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> original_cwd = os . getcwd ( ) <EOL> submodule_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> os . chdir ( submodule_dir ) <EOL> self . init_repo ( ) <EOL> repo_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> os . chdir ( repo_dir ) <EOL> self . init_repo ( ) <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , submodule_dir ] ) <EOL> self . commit ( '<STR_LIT>' ) <EOL> submodule_name = os . path . basename ( submodule_dir ) <EOL> with open ( os . path . join ( submodule_name , '<STR_LIT>' ) , '<STR_LIT:w>' ) : <EOL> pass <EOL> self . lint ( ) <EOL> finally : <EOL> os . chdir ( original_cwd ) <EOL> if submodule_dir : <EOL> shutil . rmtree ( submodule_dir ) <EOL> if repo_dir : <EOL> shutil . rmtree ( repo_dir ) <EOL> class TestHgE2E ( E2EBase , unittest . TestCase ) : <EOL> @ staticmethod <EOL> def init_repo ( ) : <EOL> """<STR_LIT>""" <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def commit ( message ) : <EOL> """<STR_LIT>""" <EOL> environ = dict ( os . environ ) <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , message ] , env = environ ) <EOL> @ staticmethod <EOL> def add ( filename ) : <EOL> """<STR_LIT>""" <EOL> execute ( [ '<STR_LIT>' , '<STR_LIT>' , filename ] ) </s>
