Exemplo n.º 1
0
 def test_readme_single(self):
     readme = Readme(0)
     readme.mapping['(default)'].append('Testing Readme')
     filename = self.create_cache('cache', {
         'version': 1,
         Readme.cache_key: {
             'filename': readme.serialize(),
         }
     })
     cache = FileCache(Readme, filename)
     self.assertIsNotNone(cache)
     self.assertEqual(len(cache), 1)
     self.assertIn('filename', cache)
     self.assertEqual(cache['filename'].mapping['(default)'],
                      ['Testing Readme'])
Exemplo n.º 2
0
 def test_save_readme_two(self):
     readme = Readme(0)
     readme.mapping['(default)'].append('Testing Readme')
     readme2 = Readme(0)
     readme2.mapping['(default)'].append('Testing Readme 2')
     filename = os.path.join(self.tmpdir, 'cache')
     cache = FileCache(Readme, filename)
     cache.mapping['filename'] = readme
     cache.mapping['filename2'] = readme2
     cache.save()
     self.assertTrue(os.path.exists(filename))
     with lzma.open(filename, 'rt', encoding='utf-8') as df:
         saved = json.load(df)
         self.assertEqual(saved['version'], FileCache.cache_version)
         self.assertEqual(len(saved[Readme.cache_key]), 2)
         self.assertIn('filename', saved[Readme.cache_key])
         self.assertIn('filename2', saved[Readme.cache_key])
Exemplo n.º 3
0
 def test_load_readme_not_found(self):
     """
     Ditto above
     """
     readme = Readme(0)
     readme.mapping['(default)'].append('Testing Readme')
     filename = os.path.join(self.tmpdir, 'cache')
     cache = FileCache(Readme, filename)
     cache.mapping['filename'] = readme
     cache.save()
     dirinfo = DirInfo(self.tmpdir, '', ['filename'])
     with self.assertRaises(FileNotFoundError) as cm:
         cache.load(dirinfo, 'filename')
Exemplo n.º 4
0
    def test_load_readme_newer(self):
        readme_filename = self.make_file('', 'filename', ['testing'], mtime=84)
        readme = Readme(42)
        readme.mapping['(default)'].append('overwrite')
        cache_filename = os.path.join(self.tmpdir, 'cache')
        cache = FileCache(Readme, cache_filename)
        cache.mapping[readme_filename] = readme
        cache.save()

        # Reload from disk, just in case anything's weird
        cache = FileCache(Readme, cache_filename)
        dirinfo = DirInfo('/tmp/doesnotexist', self.tmpdir, ['filename'])
        loaded_readme = cache.load(dirinfo, 'filename')
        self.assertIsNotNone(loaded_readme)
        self.assertEqual(loaded_readme.status, ModFile.S_UPDATED)
        self.assertIn(readme_filename, cache)
        self.assertEqual(loaded_readme.mapping['(default)'], ['testing'])
Exemplo n.º 5
0
 def setUp(self):
     """
     Initialize some vars we'll need on every test.
     """
     self.readme = Readme(0)
     self.df = io.StringIO()
Exemplo n.º 6
0
class ReadmeReadFileObjTests(unittest.TestCase):
    """
    Testing reading our README files
    """
    def setUp(self):
        """
        Initialize some vars we'll need on every test.
        """
        self.readme = Readme(0)
        self.df = io.StringIO()

    def set_df_contents(self, lines):
        """
        Sets the contents of the "file" that we're gonna read in.
        """
        for line in lines:
            print(line, file=self.df)
        self.df.seek(0)

    def read(self):
        """
        Read in our file.  Note that the `is_markdown` flag is
        currently not actually used, so we're not bothering to
        test for it at all.
        """
        self.readme.read_file_obj(self.df, False)

    def test_load_empty(self):
        self.set_df_contents([])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
        })

    def test_initial_comment(self):
        self.set_df_contents([
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_two_comments(self):
        self.set_df_contents([
            'Testing',
            'Testing 2',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing', 'Testing 2'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_hash_section(self):
        for hashes in ['#', '##', '###', '####']:
            with self.subTest(hashes=hashes):
                self.set_df_contents([
                    '{} Section'.format(hashes),
                    'Testing',
                ])
                self.read()
                self.assertEqual(self.readme.mapping, {
                    '(default)': [],
                    'section': ['Testing'],
                })
                self.assertEqual(self.readme.first_section, 'section')

    def test_default_and_one_hash_section(self):
        self.set_df_contents([
            'Initial',
            '# Section',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Initial'],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_one_hash_section_two_lines(self):
        self.set_df_contents([
            '# Section',
            'Testing',
            'Testing 2',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing', 'Testing 2'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_double_underline_section(self):
        self.set_df_contents([
            'Section',
            '=======',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_single_underline_section(self):
        self.set_df_contents([
            'Section',
            '-------',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_initial_double_line(self):
        self.set_df_contents([
            '=======',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['=======', 'Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_single_line(self):
        self.set_df_contents([
            '-------',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['-------', 'Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_dash_section(self):
        self.set_df_contents([
            '- Section',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_two_sections(self):
        self.set_df_contents([
            '- Section',
            'Testing',
            '# Section 2',
            'More Testing',
        ])
        self.read()
        self.assertEqual(
            self.readme.mapping, {
                '(default)': [],
                'section': ['Testing'],
                'section 2': ['More Testing'],
            })
        self.assertEqual(self.readme.first_section, 'section')

    def test_two_sections_multiline(self):
        self.set_df_contents([
            'Initial',
            'Text',
            '- Section',
            'Testing',
            'Foo',
            '# Section 2',
            'More Testing',
            'Bar',
        ])
        self.read()
        self.assertEqual(
            self.readme.mapping, {
                '(default)': ['Initial', 'Text'],
                'section': ['Testing', 'Foo'],
                'section 2': ['More Testing', 'Bar'],
            })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_default_blank(self):
        self.set_df_contents([
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_default_blank_double(self):
        self.set_df_contents([
            '',
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_section_blank(self):
        self.set_df_contents([
            '- Section',
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_initial_section_blank_double(self):
        self.set_df_contents([
            '- Section',
            '',
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_trailing_blank(self):
        self.set_df_contents([
            'Testing',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_trailing_section_blank(self):
        self.set_df_contents([
            '# Section',
            'Testing',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_two_trailing_section_blank(self):
        self.set_df_contents([
            'Main',
            '',
            '# Section',
            'Testing',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Main'],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_double_trailing_blank(self):
        self.set_df_contents([
            'Testing',
            '',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')
class ReadmeReadFindMatchingTests(unittest.TestCase):
    """
    Testing finding matching entries in README files
    """
    def setUp(self):
        """
        Initialize some vars we'll need on every test.
        """
        self.readme = Readme(0)
        self.df = io.StringIO()

    def set_df_contents(self, lines):
        """
        Sets the contents of the "file" that we're gonna read in.
        """
        for line in lines:
            print(line, file=self.df)
        self.df.seek(0)

    def read(self, lines):
        """
        Read in our file.  Note that the `is_markdown` flag is
        currently not actually used, so we're not bothering to
        test for it at all.
        """
        self.set_df_contents(lines)
        self.readme.read_file_obj(self.df, False)

    def test_single_mod_overview(self):
        self.read([
            'Beginning Text',
            '# Overview',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_exact(self):
        self.read([
            'Beginning Text',
            '# xyzzy',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_fuzzy(self):
        self.read([
            'Beginning Text',
            '# xyzzyz',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_default(self):
        self.read([
            'Beginning Text',
            '# Unrelated',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Beginning Text'])

    def test_single_mod_first_section(self):
        self.read([
            '# Unrelated',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_empty(self):
        self.read([])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         [])

    def test_multi_mod_exact(self):
        self.read([
            'Beginning Text',
            '# xyzzy',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=False),
                         ['Testing'])

    def test_multi_mod_fuzzy(self):
        self.read([
            'Beginning Text',
            '# xyzzyz',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=False),
                         ['Testing'])

    def test_multi_mod_nomatch(self):
        self.read([
            'Beginning Text',
            '# Unrelated',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        # Unlike the BL2/TPS cabinet, multi-mod matches *will* include the
        # first category.
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=False),
                         ['Beginning Text'])