'Test renaming of files'

def test_file_rename(self):

    book = get_simple_book()
    count = [0]
    def new_container():
        count[0] += 1
        tdir = os.mkdir(os.path.join(self.tdir, str(count[0])))
        return get_container(book, tdir=tdir)
    c = new_container()
    orig_name = c.opf_name
    name = u'renamed    opf.opf'
    rename_files(c, {c.opf_name: name})
    self.assertEqual(c.opf_name, name)
    for x in (u'name_path_map', u'mime_map'):
        self.assertNotIn(orig_name, getattr(c, x))
        self.assertIn(name, getattr(c, x))
    self.assertNotIn(name, c.dirtied)
    root = c.parsed(u'META-INF/container.xml')
    vals = set(root.xpath(u'child::ocf:rootfiles/ocf:rootfile/@full-path', namespaces={u'ocf': OCF_NS}))
    self.assertSetEqual(vals, {name})
    self.check_links(c)
    c = new_container()
    orig_name = c.opf_name
    name = u'renamed/again/metadata.opf'
    rename_files(c, {c.opf_name: name})
    self.check_links(c)
    c = new_container()
    name = next(c.spine_names)[0]
    root = c.parsed(name)
    root.xpath(u'//*[local-name()="body"]')[0].set(u'id', u'rename-dirty-test')
    rename_files(c, {name: (u'other/' + name)})
    with c.open((u'other/' + name)) as f:
        raw = f.read()
    self.assertIn('id="rename-dirty-test"', raw)
    self.check_links(c)
    c = new_container()
    rename_files(c, {u'stylesheet.css': u'styles/s    1.css', u'page_styles.css': u'styles/p    1.css'})
    self.check_links(c)
    c = new_container()
    rename_files(c, {u'cover.png': u'images/cover    img.png', u'light_wood.png': u'images/light    wood.png', u'marked.png': u'images/marked    img.png'})
    self.check_links(c)
    c = new_container()
    rename_files(c, {u'toc.ncx': u'toc/toc    file.ncx'})
    self.check_links(c)
    c = new_container()
    fname = u'LiberationMono-Regular.ttf'
    if (fname not in c.name_path_map):
        fname = fname.lower()
    rename_files(c, {fname: u'fonts/LiberationMono    Regular.ttf'})
    self.check_links(c)
    c = new_container()
    rename_files(c, {u'index_split_000.html': u'text/page    one    f\xe4llen.html', u'index_split_001.html': u'text/page    two    f\xe4llen.html'})
    self.check_links(c)
    c = new_container()
    rename_files(c, {u'index_split_000.html': u'Index_split_000.html'})
    self.check_links(c)
