def test_api_for_create_file(): path = fs.mkdir('root') fs.mkdir('root/code') fs.mkfile('root/code/main.c', contents = u'int main() { return 0; }') tree = ipe.load_tree(path) # We might want to create files and directories that are not in # the disk. To do that we use 'create_file' and 'create_dir', that # are provided by the Dir class. tree.create_file('.emacs') # The file is now available for us dotemacs = tree.get('.emacs') # Files that we create are called "artificial". Files that exist # are called "natural." assert dotemacs.is_artificial assert tree.get('code/main.c').is_natural # Initially, artificial files have no content. assert dotemacs.contents is None # No problem. We can set it ourselves. lisp = u'(setq tab-width 4)' dotemacs.contents = lisp assert dotemacs.contents == lisp # We cannot create a file or a directory if its name would # conflict with that of an existing file. try: tree.create_dir('.emacs') except ipe.DuplicateChild: pass # If we're tired of an artificial file, we can remove it. tree.remove(dotemacs) assert not tree.contains(dotemacs) # And now we can proceed creating the directory again. It will not # conflict. d = tree.create_dir('.emacs')
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_invalid_dir(): r = fs.mkfile('i/am/not/a/dir.txt') try: t = ipe.load_tree(r) assert False, 'IO Error' except IOError: pass try: t = ipe.load_tree(fs.abspath('not/created/yet')) assert False, 'IO Error' except IOError: pass t = ipe.load_tree(os.path.dirname(r)) eq_(t.get('dir.txt').parent, t)
def test_natural_file(): dn = fs.mkdir('natural-files') fn = fs.mkfile('natural-files/file.txt', contents = u'test') root = ipe.load_tree(dn) # This is a natural file. f = root.get('file.txt') # By default, it has contents. The standard value is a file-like # object with f.contents as stream: # It is a read only stream try: stream.write('anything') assert False, 'The file should be read only.' except IOError: pass # And it must represent the contents of the file on disk. text = stream.read().decode('utf-8') assert text == u'test' # Replacing it with an UTF-8 string is OK. f.contents = u'replaced' assert f.contents == u'replaced' # It should not overwrite the contents of the file, though. with open(fn, 'rb') as stream: text = stream.read().decode('utf-8') assert text == u'test' # Setting to None should not reset the file to the original state. f.contents = None assert f.contents == None
def test_create_some_files(): d = fs.mkdir('hello/world') f = fs.mkfile('hello/world/again.txt')
def test_load_dir(): r = fs.mkdir('root') fs.mkdir('root/vanilla') fs.mkdir('root/foo/bar') fs.mkfile('root/a.txt') fs.mkfile('root/b.txt', contents = u'André') fs.mkfile('root/vanilla/c.txt') fs.mkfile('root/vanilla/d.txt') fs.mkfile('root/foo/e.txt') fs.mkfile('root/foo/bar/f.txt') fs.mkfile('root/foo/bar/g.txt') t = ipe.load_tree(r) eq_(t.get('vanilla/c.txt').name, 'c.txt') eq_(t.get('foo/bar/g.txt').parent.get('f.txt').abspath, t.get('foo/bar/f.txt').abspath)