def test_environment_prefix(self): source = ''' {% for key, value in environment('X_') %}{{ key }} = {{ value }} {% endfor %} ''' expected = ''' foo = bar ''' self.assertEquals(envtpl.render(source, {'X_foo': 'bar', 'baz': 'X_qux'}, True), expected)
def test_quoted(self): source = ''' foo = {{ FOO | default(123) }} bar = "{{ BAR | default("abc") }}" ''' expected = ''' foo = 456 bar = "abc" ''' self.assertEquals(envtpl.render(source, {'FOO': 456}, True), expected)
def test_if_block(self): source = ''' {% if BAZ is defined %} foo = 123 {% else %} foo = 456 {% endif %} bar = "abc"''' expected = ''' foo = 456 bar = "abc"''' self.assertEquals(envtpl.render(source, {}, True), expected)
def do_bootstrap(directory, use_envtpl): for root, dirs, files in os.walk(directory, followlinks=True): parents = root.split('/') if len(parents) > 1 and parents[1] == 'files': for filename in files: remote_root = '/' + '/'.join(parents[2:]) use_sudo = not (len(parents) >= 4 and parents[2] == 'home' and parents[3] == 'ubuntu') if use_sudo: sudo('mkdir -p "%s"' % remote_root) else: run('mkdir -p "%s"' % remote_root) local_filename = '%s/%s' % (root, filename) remote_filename = '%s/%s' % (remote_root, filename) if use_envtpl and local_filename.endswith('.tpl'): remote_filename = remote_filename[:-4] # remove .tpl variables = os.environ with open(local_filename, 'r') as f: compiled = envtpl.render(f.read(), variables, die_on_missing_variable=True) put(StringIO(compiled), remote_filename, use_sudo=use_sudo) else: put(local_filename, remote_filename, use_sudo=use_sudo) scripts = glob('%s/*.sh' % directory) remote_scripts_directory = '/tmp/bootstrap_scripts' sudo('mkdir -p %s' % remote_scripts_directory) for path in sorted(scripts): filename = os.path.basename(path) remote_script = '%s/%s' % (remote_scripts_directory, filename) put('%s/%s' % (directory, filename), remote_script, use_sudo=True) run('source %s' % remote_script)
def test_empty(self): self.assertEquals(envtpl.render('', {}, True), '')
def test_defaults(self): self.assertEquals(envtpl.render('{{ FOO | default("abc") }}', {}, True), 'abc') self.assertEquals(envtpl.render('{{ FOO | default("abc") }}', {'FOO': 'def'}, True), 'def')
def test_dont_die_on_missing(self): self.assertEquals(envtpl.render('{{ FOO }}', {}, False), '')