'Test cloning of containers'

def test_clone(self):

    for fmt in (u'epub', u'azw3'):
        base = os.path.join(self.tdir, (fmt + u'-'))
        book = get_simple_book(fmt)
        tdir = (base + u'first')
        os.mkdir(tdir)
        c1 = get_container(book, tdir=tdir)
        tdir = (base + u'second')
        os.mkdir(tdir)
        c2 = clone_container(c1, tdir)
        for c in (c1, c2):
            for (name, path) in c.name_path_map.iteritems():
                self.assertEqual(2, nlinks_file(path), (u'The    file    %s    is    not    linked' % name))
        for name in c1.name_path_map:
            self.assertIn(name, c2.name_path_map)
            self.assertEqual(c1.open(name).read(), c2.open(name).read(), (u'The    file    %s    differs' % name))
        spine_names = tuple((x[0] for x in c1.spine_names))
        text = spine_names[0]
        root = c2.parsed(text)
        root.xpath(u'//*[local-name()="body"]')[0].set(u'id', u'changed    id    for    test')
        c2.dirty(text)
        c2.commit_item(text)
        for c in (c1, c2):
            self.assertEqual(1, nlinks_file(c.name_path_map[text]))
        self.assertNotEqual(c1.open(text).read(), c2.open(text).read())
        name = spine_names[1]
        with c1.open(name, mode=u'r+b') as f:
            f.seek(0, 2)
            f.write('                ')
        for c in (c1, c2):
            self.assertEqual(1, nlinks_file(c.name_path_map[name]))
        self.assertNotEqual(c1.open(name).read(), c2.open(name).read())
        x = ((base + u'out.') + fmt)
        for c in (c1, c2):
            c.commit(outpath=x)
