def test_filesystem_loader(tmpdir): build(dict(name='test/foo', content='Hello, world'), target=str(tmpdir)) build(dict(name='test/bar', content='Hello, bar'), target=str(tmpdir)) loader = FilesystemLoader(str(tmpdir)) templates = sorted(t.replace(os.sep, '/') for t in loader.list_templates()) assert templates == ['test/bar', 'test/foo'] assert loader.get_source('test/foo') == 'Hello, world'
def test_unicode_filename(tmpdir): """Create a file with a Unicode filename""" funky_char = '\N{Euro sign}' defn = {'name': funky_char} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / funky_char).check(file=1) assert (tmpdir / funky_char).size() == 0
def test_latin1_file(tmpdir): """Create a text file, ensure explicit encoding is respected""" funky_char = '\N{Pound sign}' defn = {'name': 'foo', 'content': funky_char, 'encoding': 'latin-1'} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / 'foo').check(file=1) assert (tmpdir / 'foo').read_binary() == funky_char.encode('latin-1')
def test_utf8_file(tmpdir): """Create a text file, ensure default encoding is utf8""" funky_char = '\N{Euro sign}' defn = {'name': 'foo', 'content': funky_char} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / 'foo').check(file=1) assert (tmpdir / 'foo').read_binary() == funky_char.encode('utf-8')
def test_simple_template(tmpdir): build(dict(name='test/foo', content='Hello, world'), target=str(tmpdir)) build(dict(name='test/bar', content='Hello, bar'), target=str(tmpdir)) dest = tmpdir.mkdir('target') subprocess.check_call( ['gen', '--target', str(dest), 'test'], cwd=str(tmpdir)) assert len(dest.listdir()) == 2 assert (dest / 'foo').read_text(encoding='ascii') == 'Hello, world' assert (dest / 'bar').read_text(encoding='ascii') == 'Hello, bar'
def test_format_rendering(tmpdir): var = {'var': 'bar'} build(dict(name='foo', content='$var {var} %(var)s', renderer=renderer.format_renderer), variables=var, target=str(tmpdir)) actual = (tmpdir / 'foo').read_text(encoding='utf-8') assert actual == '$var bar %(var)s'
def test_no_rendering(tmpdir): build(dict(name='foo', content='$var {var} %(var)s'), target=str(tmpdir)) actual = (tmpdir / 'foo').read_text(encoding='utf-8') assert actual == '$var {var} %(var)s'
def build_cmd(**variables): for spec in defn['definition']: build(spec, variables, target='xx')
def test_empty_dir(tmpdir): """Create an empty directory""" defn = {'name': 'foo', 'directory': True} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / 'foo').check(dir=1) assert len((tmpdir / 'foo').listdir()) == 0
def test_text_file(tmpdir): """Create a text file""" defn = {'name': 'foo', 'content': 'Hello'} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / 'foo').check(file=1) assert (tmpdir / 'foo').read_binary() == b'Hello'
def test_empty_file(tmpdir): """Create an empty file""" defn = {'name': 'foo'} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / 'foo').check(file=1) assert (tmpdir / 'foo').size() == 0
def test_empty_dir_trailing_slash(tmpdir): """A name with a trailing slash creates an empty directory""" defn = {'name': 'foo/'} build(defn, target=str(tmpdir), variables={}) assert (tmpdir / 'foo').check(dir=1) assert len((tmpdir / 'foo').listdir()) == 0