<s> from mock import Mock , mock_open , patch <EOL> from unittest import TestCase <EOL> from carbon . pipeline import Processor <EOL> from carbon . rewrite import PRE , RewriteProcessor , RewriteRule , RewriteRuleManager <EOL> class RewriteProcessorTest ( TestCase ) : <EOL> def tearDown ( self ) : <EOL> RewriteRuleManager . clear ( ) <EOL> def test_registers_plugin ( self ) : <EOL> self . assertTrue ( '<STR_LIT>' in Processor . plugins ) <EOL> def test_applies_rule ( self ) : <EOL> mock_rule = Mock ( spec = RewriteRule ) <EOL> RewriteRuleManager . rulesets [ PRE ] = [ mock_rule ] <EOL> list ( RewriteProcessor ( PRE ) . process ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> mock_rule . apply . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_applies_rule_and_returns_metric ( self ) : <EOL> mock_rule = Mock ( spec = RewriteRule ) <EOL> mock_rule . apply . return_value = '<STR_LIT>' <EOL> RewriteRuleManager . rulesets [ PRE ] = [ mock_rule ] <EOL> result = list ( RewriteProcessor ( PRE ) . process ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> self . assertEqual ( ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , result [ <NUM_LIT:0> ] ) <EOL> def test_passes_through_with_no_rules ( self ) : <EOL> result = list ( RewriteProcessor ( PRE ) . process ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> self . assertEqual ( ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , result [ <NUM_LIT:0> ] ) <EOL> class TestRewriteRule ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . sample_rule = RewriteRule ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_instantiation_compiles_pattern ( self ) : <EOL> self . assertTrue ( hasattr ( self . sample_rule . regex , '<STR_LIT>' ) ) <EOL> def test_apply_substitutes ( self ) : <EOL> result = self . sample_rule . apply ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , result ) <EOL> class TestRewriteRuleManager ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . sample_config = """<STR_LIT>""" <EOL> self . sample_multi_config = """<STR_LIT>""" <EOL> self . broken_pattern_config = """<STR_LIT>""" <EOL> self . commented_config = """<STR_LIT>""" <EOL> def tearDown ( self ) : <EOL> RewriteRuleManager . rules_file = None <EOL> RewriteRuleManager . rules_last_read = <NUM_LIT:0.0> <EOL> RewriteRuleManager . clear ( ) <EOL> def test_looping_call_reads_rules ( self ) : <EOL> self . assertEqual ( RewriteRuleManager . read_rules , RewriteRuleManager . read_task . f ) <EOL> def test_request_for_nonexistent_rules_returns_iterable ( self ) : <EOL> try : <EOL> iter ( RewriteRuleManager . rules ( '<STR_LIT:foo>' ) ) <EOL> except TypeError : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_read_from_starts_task ( self ) : <EOL> with patch . object ( RewriteRuleManager , '<STR_LIT>' ) : <EOL> with patch . object ( RewriteRuleManager . read_task , '<STR_LIT:start>' ) as task_start_mock : <EOL> RewriteRuleManager . read_from ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , task_start_mock . call_count ) <EOL> def test_read_records_mtime ( self ) : <EOL> import carbon . rewrite <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , mock_open ( ) , create = True ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertEqual ( <NUM_LIT> , RewriteRuleManager . rules_last_read ) <EOL> def test_read_clears_if_no_file ( self ) : <EOL> import carbon . rewrite <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = False ) ) : <EOL> with patch . object ( RewriteRuleManager , '<STR_LIT>' ) as clear_mock : <EOL> RewriteRuleManager . read_rules ( ) <EOL> clear_mock . assert_called_once_with ( ) <EOL> def test_rules_unchanged_if_mtime_unchanged ( self ) : <EOL> import carbon . rewrite <EOL> mtime = <NUM_LIT> <EOL> rulesets = { '<STR_LIT>' : [ Mock ( RewriteRule ) ] } <EOL> RewriteRuleManager . rules_last_read = mtime <EOL> RewriteRuleManager . rulesets . update ( rulesets ) <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = mtime ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertEqual ( rulesets , RewriteRuleManager . rulesets ) <EOL> def test_read_doesnt_open_file_if_mtime_unchanged ( self ) : <EOL> import carbon . rewrite <EOL> mtime = <NUM_LIT> <EOL> RewriteRuleManager . rules_last_read = mtime <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , mock_open ( ) , create = True ) as open_mock : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertFalse ( open_mock . called ) <EOL> def test_read_opens_file_if_mtime_newer ( self ) : <EOL> import carbon . rewrite <EOL> RewriteRuleManager . rules_last_read = <NUM_LIT> <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , mock_open ( ) , create = True ) as open_mock : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertTrue ( open_mock . called ) <EOL> def test_section_parsed_into_ruleset ( self ) : <EOL> import carbon . rewrite <EOL> open_mock = Mock ( return_value = iter ( self . sample_config . splitlines ( ) ) ) <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , open_mock , create = True ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertTrue ( '<STR_LIT>' in RewriteRuleManager . rulesets ) <EOL> def test_multiple_section_parsed_into_ruleset ( self ) : <EOL> import carbon . rewrite <EOL> open_mock = Mock ( return_value = iter ( self . sample_multi_config . splitlines ( ) ) ) <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , open_mock , create = True ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertTrue ( '<STR_LIT>' in RewriteRuleManager . rulesets ) <EOL> self . assertTrue ( '<STR_LIT>' in RewriteRuleManager . rulesets ) <EOL> def test_rules_parsed ( self ) : <EOL> import carbon . rewrite <EOL> open_mock = Mock ( return_value = iter ( self . sample_config . splitlines ( ) ) ) <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , open_mock , create = True ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( RewriteRuleManager . rules ( '<STR_LIT>' ) ) ) <EOL> def test_broken_patterns_ignored ( self ) : <EOL> import carbon . rewrite <EOL> open_mock = Mock ( return_value = iter ( self . broken_pattern_config . splitlines ( ) ) ) <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , open_mock , create = True ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( RewriteRuleManager . rules ( '<STR_LIT>' ) ) ) <EOL> def test_comments_ignored ( self ) : <EOL> import carbon . rewrite <EOL> open_mock = Mock ( return_value = iter ( self . commented_config . splitlines ( ) ) ) <EOL> RewriteRuleManager . rules_file = '<STR_LIT>' <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , open_mock , create = True ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = True ) ) : <EOL> with patch . object ( carbon . rewrite , '<STR_LIT>' , Mock ( return_value = <NUM_LIT> ) ) : <EOL> RewriteRuleManager . read_rules ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( RewriteRuleManager . rules ( '<STR_LIT>' ) ) ) </s>
