<commit_before>import unittest

from dotbriefs.clean import CleanSecret


class TestCleanSecret(unittest.TestCase):

    def setUp(self):
        self.secrets = []
        self.secrets.append(CleanSecret('passwd',
            r'password(\s*)=(\s*)(?#UpToHash)',
            r'password\1=\2(?#Key)',
            'Mutt passwords',
            True))

    def test_state(self):
        state = self.secrets[0].__getstate__()
        other_secret = CleanSecret('', '', '')
        other_secret.__setstate__(state)
        self.assertEqual(self.secrets[0].__dict__, other_secret.__dict__)


if __name__ == '__main__':
    unittest.main()<commit_msg>Add more tests for CleanSecret<commit_after>import unittest

from dotbriefs.clean import CleanSecret


class TestCleanSecret(unittest.TestCase):

    def setUp(self):
        self.secrets = []
        self.secrets.append(CleanSecret('passwd',
            r'password(\s*)=(\s*)(?#UpToHash)',
            r'password\1=\2(?#Key)',
            'Mutt passwords',
            True))
        self.secrets.append(CleanSecret('passwd',
            r'password(\s*)=(\s*)(?#QuotedString)',
            r'password\1=\2(?#Key)',
            'Mutt passwords',
            True))
        self.secrets.append(CleanSecret('abc',
            r'password(\s*)=(\s*)(?#DoesNotExist)',
            r'password\1=\2(?#Key)',
            'Mutt passwords',
            True))

    def test_state(self):
        """Test getting and setting state"""
        state = self.secrets[0].__getstate__()
        other_secret = CleanSecret('', '', '')
        other_secret.__setstate__(state)
        self.assertEqual(self.secrets[0].__dict__, other_secret.__dict__)

    def test_regex_sub(self):
        """Test regex substitution with predefined short cuts"""
        self.assertEqual(self.secrets[0].regex.pattern,
                r'password(\s*)=(\s*)([^\s#]+(?:[ \t\v\f]*[^\s#]+)+)')
        self.assertEqual(self.secrets[1].regex.pattern,
                r'password(\s*)=(\s*)("[^"\\]*(?:\\.[^"\\]*)*"|\'[^\'\\]*(?:\\.[^\'\\]*)*\')')
        self.assertEqual(self.secrets[2].regex.pattern,
                r'password(\s*)=(\s*)(?#DoesNotExist)')

    def test_sub_uth_no_match(self):
        """Test uptohash copy line on no match"""
        line = 'paaasword = df35@$^%ds'
        out = self.secrets[0].sub(line)
        self.assertEqual(out, line)

    def test_sub_uth_single_match(self):
        """Test uptohash single match"""
        line = 'password =   <PASSWORD>@$^%ds'
        out = self.secrets[0].sub(line)
        self.assertEqual(out, 'password =   <PASSWORD>: passwd_1$')

    def test_sub_uth_single_match_comment(self):
        """Test uptohash single match with comment"""
        line = 'password =   <PASSWORD>@$^%ds    # comment'
        out = self.secrets[0].sub(line)
        self.assertEqual(out, 'password =   <PASSWORD>: passwd_1$    # comment')

    def test_sub_uth_single_match_inside_comment(self):
        """Test uptohash single match inside comment"""
        line = '#password =   <PASSWORD>@$^%ds    # comment'
        out = self.secrets[0].sub(line)
        self.assertEqual(out, '#password =   $<PASSWORD>: passwd_1$    # comment')

    def test_sub_uth_double_match_in_comment(self):
        """Test uptohash one match before comment, other match inside comment"""
        line = 'password =   <PASSWORD>@$^%ds    # comment password = <PASSWORD> # comment'
        out = self.secrets[0].sub(line)
        self.assertEqual(out, 'password =   <PASSWORD>: passwd_1$    # comment password = $<PASSWORD>: passwd_2$ # comment')

    def test_sub_qs_single_match(self):
        """Test quotedstring single match two times"""
        line = 'password = "df35@$^%ds"'
        out = self.secrets[1].sub(line)
        self.assertEqual(out, 'password = $<PASSWORD>: <PASSWORD>$')
        line = "password = '<PASSWORD>'"
        out = self.secrets[1].sub(line)
        self.assertEqual(out, 'password = $<PASSWORD>: <PASSWORD>$')

    def test_sub_qs_single_match_inner_dquotes(self):
        """Test quotedstring single match with inner double quotes"""
        line = 'password = "<PASSWORD>\\"@\\"$^%ds"'
        out = self.secrets[1].sub(line)
        self.assertEqual(out, 'password = $<PASSWORD>: <PASSWORD>$')

    def test_sub_qs_single_match_inner_squotes(self):
        """Test quotedstring single match with inner single quotes"""
        line = 'password = "<PASSWORD>\'@\'$^<PASSWORD>"'
        out = self.secrets[1].sub(line)
        self.assertEqual(out, 'password = $<PASSWORD>s: <PASSWORD>$')


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.17043478260869566, 'fraction_numerical': 0.007173913043478261, 'mean_word_length': 2.749796251018745, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7123452', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1444, 'n_words': 259}