def test_creating_dir_fails_if_there_is_a_file_in_the_way(): # Let's first create a place where to store our stuff. out = fs.mkdir() rock = fs.mkfile(join(os.path.basename(out), 'rock')) assert isdir(out) assert os.path.isfile(rock) # Now let's create a tree that has a directory with the same # name as the file we created previously. They will conflict. root = ipe.Dir('root') root.create_dir('rock') # We'll store these with the CreateMissingDirectories tool. tool = ipe.storage.CreateMissingDirectories(out) # It must not succeed because there is a file in the way. try: ipe.top_down(root, tool) assert False, 'There is a file in the way!' except RuntimeError: pass
def test_store_different_types_of_contents(): # Let's first create a place where to store our stuff. out = fs.mkdir() assert isdir(out) # Files can have all sorts of contents. Unicode strings... txt_contents = u'O ipê amarelo é a árvore nacional do Brasil.' # ...iterables... lst_contents = [ txt_contents ] # ...generator functions... def gen_contents(): for line in lst_contents: yield line # ...callables... def def_contents(): return gen_contents # ...and files. fil_contents = open(fs.mkfile('tempfile', contents = txt_contents)) # So, let's create the files. Each with a different kind of content root = ipe.Dir('root') f1 = root.create_file('f1', contents = txt_contents) f2 = root.create_file('f2', contents = lst_contents) f3 = root.create_file('f3', contents = gen_contents) f4 = root.create_file('f4', contents = def_contents) f5 = root.create_file('f5', contents = fil_contents) # Before creating files, let's first create the directories. We # know it works because we've tested it before! tool = ipe.storage.CreateMissingDirectories(out) ipe.top_down(root, tool) # Now that we know the directories are in place, we'll use # the StoreFiles tool to write the files down. tool = ipe.storage.StoreFiles(out) ipe.top_down(root, tool) # Now, let's check what's in those files assert codecs.open(join(out, f1.path), encoding='utf-8').read() == txt_contents assert codecs.open(join(out, f2.path), encoding='utf-8').read() == txt_contents assert codecs.open(join(out, f3.path), encoding='utf-8').read() == txt_contents assert codecs.open(join(out, f4.path), encoding='utf-8').read() == txt_contents assert codecs.open(join(out, f5.path), encoding='utf-8').read() == txt_contents
def test_top_down(): d1 = Dir('A') d2 = Dir('B') d3 = Dir('C') d4 = Dir('D') d3.parent = d2 d4.parent = d2 d2.parent = d1 order = [] def tool(item): order.append(item.name) top_down(d1, tool) assert order == ['A', 'B', 'C', 'D'], str(order)
def test_creating_directories_using_the_built_in_tool(): # Let's first create a place where to store our stuff. out = fs.mkdir() assert isdir(out) # Now, let's create a totally artificial tree, containing files # and directories. root = ipe.Dir('root') a = root.create_dir('a') x = a.create_file('x') y = a.create_file('y') b = root.create_dir('b') z = b.create_file('z') c = root.create_dir('c') d = c.create_dir('d') # We'll use the builtin CreateMissingDirectories tool to create # the directories. It will output to the 'out' directory we have # just created. tool = ipe.storage.CreateMissingDirectories(out) ipe.top_down(root, tool) # First of all, the root directory should NOT be created. The root # of the tree is the output directory, "out". assert not isdir(join(out, root.name)) # Each directory should have been properly created. assert isdir(join(out, a.path)) assert isdir(join(out, b.path)) assert isdir(join(out, c.path)) assert isdir(join(out, d.path)) # None of the files should be present, however. assert not exists(join(out, x.path)) assert not exists(join(out, y.path)) assert not exists(join(out, z.path))
def test_creating_files_with_the_built_in_tool(): # Let's first create a place where to store our stuff. out = fs.mkdir() assert isdir(out) # Now, let's create a totally artificial tree, containing files # and directories. The contents of each file will be its name. root = ipe.Dir('root') a = root.create_dir('a') x = a.create_file('x', contents = u'x') y = a.create_file('y', contents = u'y') b = root.create_dir('b') z = b.create_file('z', contents = u'z') c = root.create_dir('c') d = c.create_dir('d') # Before creating files, let's first create the directories. We # know it works because we've tested it before! tool = ipe.storage.CreateMissingDirectories(out) ipe.top_down(root, tool) # Now that we know the directories are in place, we'll use # the StoreFiles tool to write the files down. tool = ipe.storage.StoreFiles(out) ipe.top_down(root, tool) # First let's check if the files were really created. assert isfile(join(out, x.path)) assert isfile(join(out, y.path)) assert isfile(join(out, z.path)) # Now, let's check what's in those files assert codecs.open(join(out, x.path), encoding='utf-8').read() == u'x' assert codecs.open(join(out, y.path), encoding='utf-8').read() == u'y' assert codecs.open(join(out, z.path), encoding='utf-8').read() == u'z'