def main(): parser = setup_parser() args = parser.parse_args() args.charmhome = args.charmhome or os.getenv('CHARM_HOME', '.') args.config = None if args.verbose: logging.basicConfig( format='%(levelname)s %(filename)s: %(message)s', level=logging.DEBUG, ) else: logging.basicConfig( format='%(levelname)s: %(message)s', level=logging.INFO, ) if not args.template: log.info( "Using default charm template (%s). To select a different " "template, use the -t option.", DEFAULT_TEMPLATE) args.template = DEFAULT_TEMPLATE elif args.template not in get_installed_templates(): raise Exception("No template available for '%s'. Available templates " "may be listed by running 'charm create --help'.") generator = CharmGenerator(args) try: generator.create_charm() return 0 except CharmGeneratorException as e: log.error(str(e)) return 1
def main(): parser = setup_parser() args = parser.parse_args() args.charmhome = args.charmhome or os.getenv('CHARM_HOME', '.') args.config = None if args.verbose: logging.basicConfig( format='%(levelname)s %(filename)s: %(message)s', level=logging.DEBUG, ) else: logging.basicConfig( format='%(levelname)s: %(message)s', level=logging.INFO, ) if not args.template: log.info( "Using default charm template (%s). To select a different " "template, use the -t option.", DEFAULT_TEMPLATE) args.template = DEFAULT_TEMPLATE generator = CharmGenerator(args) try: generator.create_charm() except CharmGeneratorException as e: log.error(e) return 1
def setUp(self): class opts(object): charmname = 'testcharm' charmhome = tempfile.mkdtemp() template = 'bash' accept_defaults = False self.c = CharmGenerator(opts)
class CharmGeneratorTest(TestCase): def setUp(self): class opts(object): charmname = 'testcharm' charmhome = tempfile.mkdtemp() template = 'bash' accept_defaults = False self.c = CharmGenerator(opts) def tearDown(self): shutil.rmtree(self.c.opts.charmhome, ignore_errors=True) def test_load_plugin(self): plugin = self.c._load_plugin() self.assertIsInstance(plugin, BashCharmTemplate) @patch.dict(os.environ, {'USER': '******'}) @patch('os.path.expanduser') def test_create_charm_output_path_exists(self, eu): eu.return_value = self.c.opts.charmhome os.mkdir(self.c._get_output_path()) with self.assertRaises(CharmGeneratorException) as e: self.c.create_charm() self.assertEqual( str(e), '{} exists. Please move it out of the way.'.format( self.c._get_output_path())) def test_create_charm_error(self): with patch.object(self.c.plugin, 'create_charm') as create_charm, \ patch.object(self.c, '_cleanup') as _cleanup: create_charm.side_effect = Exception with self.assertRaises(Exception): self.c.create_charm() self.assertTrue(_cleanup.called) @patch('charmtools.generators.generator.apt_fill') @patch('charmtools.generators.generator.get_maintainer') def test_get_metadata(self, get_maintainer, apt_fill): get_maintainer.return_value = ('Tester', '*****@*****.**') apt_fill.return_value = { 'summary': 'Charm summary', 'description': 'Charm description', } self.assertEqual( self.c._get_metadata(), { 'package': 'testcharm', 'maintainer': 'Tester <*****@*****.**>', 'summary': 'Charm summary', 'description': 'Charm description'}) @patch('__builtin__.raw_input') def test_get_user_config_from_prompts(self, raw_input_): raw_input_.return_value = 'Yes' with patch.object(self.c.plugin, 'prompts') as prompts: prompts.return_value = [ Prompt('symlink', 'symlink hooks?', 'y', 'bool')] config = self.c._get_user_config() self.assertEqual(config, {'symlink': True}) def test_get_user_config_from_defaults(self): self.c.opts.accept_defaults = True with patch.object(self.c.plugin, 'prompts') as prompts: prompts.return_value = [ Prompt('symlink', 'symlink hooks?', 'y', 'bool')] config = self.c._get_user_config() self.assertEqual(config, {'symlink': True}) def test_prompt_none(self): with patch.object(self.c.plugin, 'configure_prompt') as f: f.return_value = None self.assertIsNone(self.c._prompt(Mock(), {})) def test_prompt_accept_default(self): self.c.opts.accept_defaults = True prompt = Prompt('symlink', 'symlink hooks?', 'y', 'bool') self.assertTrue(self.c._prompt(prompt, {})) @patch('__builtin__.raw_input') def test_prompt_no_input(self, raw_input_): raw_input_.return_value = '' prompt = Prompt('symlink', 'symlink hooks?', 'y', 'bool') self.assertTrue(self.c._prompt(prompt, {})) @patch('__builtin__.raw_input') def test_prompt_invalid_input(self, raw_input_): raw_input_.side_effect = ['foo', '18'] prompt = Prompt('age', 'your age?', '42', 'int') self.assertEqual(self.c._prompt(prompt, {}), 18) self.assertEqual(raw_input_.call_count, 2) @patch('__builtin__.raw_input') def test_prompt_valid_input(self, raw_input_): raw_input_.return_value = 'Joe' prompt = Prompt('name', 'your name?', 'Name') self.assertEqual(self.c._prompt(prompt, {}), 'Joe') self.assertEqual(raw_input_.call_count, 1) def test_get_output_path(self): path = self.c._get_output_path() self.assertEqual( path, os.path.join(self.c.opts.charmhome, self.c.opts.charmname)) def test_get_tempdir(self): tempdir = self.c._get_tempdir() self.assertTrue(os.path.isdir(tempdir)) self.c._cleanup(tempdir) def test_cleanup(self): tempdir = self.c._get_tempdir() self.c._cleanup(tempdir) self.assertFalse(os.path.exists(tempdir))
class CharmGeneratorTest(TestCase): def setUp(self): class opts(object): charmname = 'testcharm' charmhome = tempfile.mkdtemp() template = 'bash' accept_defaults = False self.c = CharmGenerator(opts) def tearDown(self): shutil.rmtree(self.c.opts.charmhome, ignore_errors=True) def test_load_plugin(self): plugin = self.c._load_plugin() self.assertIsInstance(plugin, BashCharmTemplate) @patch.dict(os.environ, {'USER': '******'}) @patch('os.path.expanduser') def test_create_charm_output_path_exists(self, eu): eu.return_value = self.c.opts.charmhome os.mkdir(self.c._get_output_path()) with self.assertRaises(CharmGeneratorException) as e: self.c.create_charm() self.assertEqual( str(e), '{} exists. Please move it out of the way.'.format( self.c._get_output_path())) def test_create_charm_error(self): with patch.object(self.c.plugin, 'create_charm') as create_charm, \ patch.object(self.c, '_cleanup') as _cleanup: create_charm.side_effect = Exception with self.assertRaises(Exception): self.c.create_charm() self.assertTrue(_cleanup.called) @patch('charmtools.generators.generator.apt_fill') @patch('charmtools.generators.generator.get_maintainer') def test_get_metadata(self, get_maintainer, apt_fill): get_maintainer.return_value = ('Tester', '*****@*****.**') apt_fill.return_value = { 'summary': 'Charm summary', 'description': 'Charm description', } self.assertEqual( self.c._get_metadata(), { 'package': 'testcharm', 'maintainer': 'Tester <*****@*****.**>', 'summary': 'Charm summary', 'description': 'Charm description'}) @patch('charmtools.generators.generator.rinput') def test_get_user_config_from_prompts(self, raw_input_): raw_input_.return_value = 'Yes' with patch.object(self.c.plugin, 'prompts') as prompts: prompts.return_value = [ Prompt('symlink', 'symlink hooks?', 'y', 'bool')] config = self.c._get_user_config() self.assertEqual(config, {'symlink': True}) def test_get_user_config_from_defaults(self): self.c.opts.accept_defaults = True with patch.object(self.c.plugin, 'prompts') as prompts: prompts.return_value = [ Prompt('symlink', 'symlink hooks?', 'y', 'bool')] config = self.c._get_user_config() self.assertEqual(config, {'symlink': True}) def test_prompt_none(self): with patch.object(self.c.plugin, 'configure_prompt') as f: f.return_value = None self.assertIsNone(self.c._prompt(Mock(), {})) def test_prompt_accept_default(self): self.c.opts.accept_defaults = True prompt = Prompt('symlink', 'symlink hooks?', 'y', 'bool') self.assertTrue(self.c._prompt(prompt, {})) @patch('charmtools.generators.generator.rinput') def test_prompt_no_input(self, raw_input_): raw_input_.return_value = '' prompt = Prompt('symlink', 'symlink hooks?', 'y', 'bool') self.assertTrue(self.c._prompt(prompt, {})) @patch('charmtools.generators.generator.rinput') def test_prompt_invalid_input(self, raw_input_): raw_input_.side_effect = ['foo', '18'] prompt = Prompt('age', 'your age?', '42', 'int') self.assertEqual(self.c._prompt(prompt, {}), 18) self.assertEqual(raw_input_.call_count, 2) @patch('charmtools.generators.generator.rinput') def test_prompt_valid_input(self, raw_input_): raw_input_.return_value = 'Joe' prompt = Prompt('name', 'your name?', 'Name') self.assertEqual(self.c._prompt(prompt, {}), 'Joe') self.assertEqual(raw_input_.call_count, 1) def test_get_output_path(self): path = self.c._get_output_path() self.assertEqual( path, os.path.join(self.c.opts.charmhome, self.c.opts.charmname)) def test_get_tempdir(self): tempdir = self.c._get_tempdir() self.assertTrue(os.path.isdir(tempdir)) self.c._cleanup(tempdir) def test_cleanup(self): tempdir = self.c._get_tempdir() self.c._cleanup(tempdir) self.assertFalse(os.path.exists(tempdir))