def test_render_and_file(self): err_ini = utils.get_render('configs/config.ini', dict(config=dict(test=[]))) for line in err_ini.split('\n'): self.assertEqual(line[0], '#') self.assertTrue('# Invalid config.' in err_ini, err_ini) config_dict = dict(config=dict(main=dict(test_key="test_value"))) ini = utils.get_render('configs/config.ini', config_dict) with utils.tmp_file_context(ini, delete=False) as file: file_name = file.name file.write('\n') with open(file_name, 'r') as tmp_file: self.assertEqual(tmp_file.read(), test_config) try: self.assertFalse(utils.os.path.exists(file_name)) except AssertionError: # nocv utils.os.remove(file_name) try: with utils.tmp_file(ini) as file: file_name = file.name file.write('\n') with open(file_name, 'r') as tmp_file: self.assertEqual(tmp_file.read(), test_config) raise Exception('Normal') except AssertionError: # nocv raise except Exception: pass with utils.tmp_file_context() as file: with open(file.name, 'w') as output: with utils.redirect_stdany(output): print("Test") with open(file.name, 'r') as output: self.assertEqual(output.read(), "Test\n")
def _operate(self, operation, **env_vars): env_vars.update(self.env.get("GLOBAL", dict())) with tmp_file_context(delete=False) as tmp: if self.proj.vars.get("repo_password", None) is not None: env_vars = self._with_password(tmp, env_vars) elif self.proj.vars.get("repo_key", None) is not None: env_vars = self._with_key(tmp, env_vars) return super(Git, self)._operate(operation, **env_vars)
def get_inventory_data(self, raw_data): with tmp_file_context(data=raw_data) as tmp_file: self.path = tmp_file.name return self.get_data()