def test_run_with_specified_args(self): kwargs = { 'spam': 'something', 'nospam': 'hahaha', '__magic_val__': 'it\'s magical', '__assistant__': self.ass['leaf'] } com = Command( 'use', { 'sect': 'self.run', 'args': { 'spam': 'something else', 'spam2': 'blah' } }, kwargs) class Matcher(object): def __eq__(self, other): assert len(other) == 4 assert other['spam'] == 'something else' assert other['spam2'] == 'blah' assert other['__magic_val__'] == 'it\'s magical' assert '__assistant__' in other return True flexmock(lang).should_receive('run_section').with_args(list, Matcher()) com.run()
def test_scl_with_nested_calls(self): # https://github.com/bkabrda/devassistant/issues/234 # tests proper nesting of SCL commands and also elimination of identical calls inp = [{ 'scl enable hamham foo': [{ 'scl enable spamspam': [{ 'cl': 'ls' }] }] }] c = Command('scl enable spamspam', inp) with pytest.raises(RunException): c.run() # make sure to remove scl command processors from ClHelper ClHelper.command_processors = {} res_lines = [[ 'scl enable spamspam - << DA_SCL_enable_spamspam_EOF', 'scl enable hamham foo - << DA_SCL_enable_hamham_foo_EOF', 'ls', 'DA_SCL_enable_hamham_foo_EOF', 'DA_SCL_enable_spamspam_EOF' ]] # shuffle first two and last two lines to get possible permutations of scl # call order (command processors are in dict, which has arbitrary order) res_lines.append(copy.deepcopy(res_lines[0])) res_lines[1][0], res_lines[1][1] = res_lines[1][1], res_lines[1][0] res_lines[1][3], res_lines[1][4] = res_lines[1][4], res_lines[1][3] assert ('DEBUG', '\n'.join(res_lines[0])) in self.tlh.msgs or \ ('DEBUG', '\n'.join(res_lines[1])) in self.tlh.msgs
def test_scl_passes_scls_list_to_command_invocation(self): # please don't use $__scls__ in actual assistants :) # scl runner has to use the unformatted input inp = [{'log_i': '$__scls__'}] c = Command('scl enable foo bar', inp) c.run() assert ('INFO', "[['enable', 'foo', 'bar']]") in self.tlh.msgs
def test_scl_passes_scls_list_to_command_invocation(self): # please don't use $__scls__ in actual assistants :) # scl runner has to use the unformatted input inp = [{'log_i': '$__scls__'}] fmtd_inp = [{'log_i': 'this should not be used'}] c = Command('scl enable foo bar', inp) c.run() assert ('INFO', "[['enable', 'foo', 'bar']]") in self.tlh.msgs
def test_render_dir(self, tmpdir): dr = 'dirwithmoretemplates' self.make_sure_file_does_not_exists(tmpdir, dr) inp = {'template': {'source': dr}, 'data': {'foo': 'foo', 'bar': 'bar'}, 'destination': tmpdir.strpath} c = Command('jinja_render_dir', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, 'asd') and self.get_file_contents(tmpdir, 'asd') == 'foo' assert self.is_file_exists(tmpdir, 'foo/sdf') and self.get_file_contents(tmpdir, 'foo/sdf') == 'bar'
def test_render_tpl_file_default_case_2(self, tmpdir): fn = 'jinja_template.py' # Case 2: output filename will be the same! fntpl = fn self.make_sure_file_does_not_exists(tmpdir, fn) inp = {'template': {'source': fntpl}, 'data': {'what': 'foo'}, 'destination': tmpdir.strpath} c = Command('jinja_render', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, fn) and self.get_file_contents(tmpdir, fn) == 'print("foo")'
def test_render_tpl_file_set_output_case(self, tmpdir): # Case 3: set desired output name explicitly fn ='rendered_jinja_template.py' fntpl = 'jinja_template.py.tpl' self.make_sure_file_does_not_exists(tmpdir, fn) inp = {'template': {'source': fntpl}, 'data': {'what': 'foo'}, 'output': fn, 'destination': tmpdir.strpath} c = Command('jinja_render', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, fn) and self.get_file_contents(tmpdir, fn) == 'print("foo")'
def test_render_with_tpl_in_file_subdir(self, tmpdir): # if we get a template with source e.g. dirwithmoretemplates/foo.tpl, # we should still get just foo.tpl without the subdir as a result fn = 'asd' fntpl = 'dirwithmoretemplates/asd.tpl' self.make_sure_file_does_not_exists(tmpdir, fn) inp = {'template': {'source': fntpl}, 'data': {'foo': 'foo'}, 'output': fn, 'destination': tmpdir.strpath} c = Command('jinja_render', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, fn) and self.get_file_contents(tmpdir, fn) == 'foo'
def test_correct_cases(self, tmpdir, path, comm_args, normalized, varnames): if not varnames: varnames = ['contdir', 'topdir', 'topdir_normalized'] kwargs = {'name': path} comm_args['from'] = '$name' c = Command('setup_project_dir', comm_args, kwargs=kwargs) with tmpdir.as_cwd(): ret = c.run() create_topdir = comm_args.get('create_topdir', True) p = path if create_topdir is True: assert ret == (True, p) elif create_topdir == 'normalized': p = os.path.join(os.path.dirname(path), normalized) assert ret == (True, p) else: p = os.path.dirname(path) assert ret == (True, p) assert os.path.isdir(p) # if os.path.dirname(path) == '', then '.' should be saved instead assert kwargs[varnames[0]] == (os.path.dirname(path) or '.') assert kwargs[varnames[1]] == os.path.basename(path) assert kwargs[varnames[2]] == normalized
def test_doesn_fail_when_dir_exists_but_overrided(self, tmpdir): c = Command('setup_project_dir', { 'from': 'foo', 'on_existing': 'pass' }) with tmpdir.as_cwd(): os.makedirs('foo') assert c.run() == (True, 'foo')
def test_run_with_specified_args(self): kwargs = {'spam': 'something', 'nospam': 'hahaha', '__magic_val__': 'it\'s magical', '__assistant__': self.ass['leaf']} com = Command('use', {'sect': 'self.run', 'args': {'spam': 'something else', 'spam2': 'blah'}}, kwargs) class Matcher(object): def __eq__(self, other): assert len(other) == 4 assert other['spam'] == 'something else' assert other['spam2'] == 'blah' assert other['__magic_val__'] == 'it\'s magical' assert '__assistant__' in other return True flexmock(lang).should_receive('run_section').with_args(list, Matcher()) com.run()
def test_render_tpl_file_default_case_2(self, tmpdir): fn = 'jinja_template.py' # Case 2: output filename will be the same! fntpl = fn self.make_sure_file_does_not_exists(tmpdir, fn) inp = { 'template': { 'source': fntpl }, 'data': { 'what': 'foo' }, 'destination': tmpdir.strpath } c = Command('jinja_render', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, fn) and \ self.get_file_contents(tmpdir, fn) == 'print("foo")'
def test_scl_with_nested_calls(self): # https://github.com/bkabrda/devassistant/issues/234 # tests proper nesting of SCL commands and also elimination of identical calls inp = [{'scl enable hamham foo': [{'scl enable spamspam': [{'cl': 'ls'}]}]}] c = Command('scl enable spamspam', inp) with pytest.raises(RunException): c.run() # make sure to remove scl command processors from ClHelper ClHelper.command_processors = {} res_lines = [['scl enable spamspam - << DA_SCL_enable_spamspam_EOF', 'scl enable hamham foo - << DA_SCL_enable_hamham_foo_EOF', 'ls', 'DA_SCL_enable_hamham_foo_EOF', 'DA_SCL_enable_spamspam_EOF']] # shuffle first two and last two lines to get possible permutations of scl # call order (command processors are in dict, which has arbitrary order) res_lines.append(copy.deepcopy(res_lines[0])) res_lines[1][0], res_lines[1][1] = res_lines[1][1], res_lines[1][0] res_lines[1][3], res_lines[1][4] = res_lines[1][4], res_lines[1][3] assert ('DEBUG', '\n'.join(res_lines[0])) in self.tlh.msgs or \ ('DEBUG', '\n'.join(res_lines[1])) in self.tlh.msgs
def test_render_dir(self, tmpdir): dr = 'dirwithmoretemplates' self.make_sure_file_does_not_exists(tmpdir, dr) inp = { 'template': { 'source': dr }, 'data': { 'foo': 'foo', 'bar': 'bar' }, 'destination': tmpdir.strpath } c = Command('jinja_render_dir', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, 'asd') and \ self.get_file_contents(tmpdir, 'asd') == 'foo' assert self.is_file_exists(tmpdir, 'foo/sdf') and \ self.get_file_contents(tmpdir, 'foo/sdf') == 'bar'
def test_render_tpl_file_set_output_case(self, tmpdir): # Case 3: set desired output name explicitly fn = 'rendered_jinja_template.py' fntpl = 'jinja_template.py.tpl' self.make_sure_file_does_not_exists(tmpdir, fn) inp = { 'template': { 'source': fntpl }, 'data': { 'what': 'foo' }, 'output': fn, 'destination': tmpdir.strpath } c = Command('jinja_render', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, fn) and \ self.get_file_contents(tmpdir, fn) == 'print("foo")'
def test_render_with_tpl_in_file_subdir(self, tmpdir): # if we get a template with source e.g. dirwithmoretemplates/foo.tpl, # we should still get just foo.tpl without the subdir as a result fn = 'asd' fntpl = 'dirwithmoretemplates/asd.tpl' self.make_sure_file_does_not_exists(tmpdir, fn) inp = { 'template': { 'source': fntpl }, 'data': { 'foo': 'foo' }, 'output': fn, 'destination': tmpdir.strpath } c = Command('jinja_render', inp, kwargs={'__files_dir__': [self.filesdir]}) c.run() assert self.is_file_exists(tmpdir, fn) and self.get_file_contents( tmpdir, fn) == 'foo'
def test_failure_cases(self, inp): c = Command('setup_project_dir', inp) with pytest.raises(CommandException): c.run()
def test_doesn_fail_when_dir_exists_but_overrided(self, tmpdir): c = Command('setup_project_dir', {'from': 'foo', 'on_existing': 'pass'}) with tmpdir.as_cwd(): os.makedirs('foo') assert c.run() == (True, 'foo')
def test_fails_when_dir_exists(self, tmpdir): c = Command('setup_project_dir', {'from': 'foo'}) with tmpdir.as_cwd(): open('foo', 'w').close() with pytest.raises(CommandException): c.run()