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_load_empty_dir(): d = fs.mkdir('empty') t = ipe.load_tree(d) eq_(t.name, 'empty') eq_(t.path, '') eq_(t.abspath, d) k = reduce(lambda x, y: x + 1, t.children(), 0) eq_(k, 0)
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_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)
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_artificial_file(): dn = fs.mkdir('artificial-files') root = ipe.load_tree(dn) # This is an artificial file. f = root.create_file('hello.c') # Artificial files have no content, initially. assert f.contents is None # But we can set it. f.contents = u'Hello!' # And get it back. assert f.contents == u'Hello!'