def execute(argv): create_parser = argparse.ArgumentParser( prog='cpm create', description='Create a new cpm project', add_help=False) create_parser.add_argument('project_name') create_parser.add_argument('-s', '--no-sample-code', required=False, action='store_true', default=False) args = create_parser.parse_args(argv) project_loader = ProjectLoader() service = CreationService(project_loader) options = CreationOptions( generate_sample_code=not args.no_sample_code, project_name=args.project_name, directory=args.project_name, init_from_existing_sources=False, ) result = new_project(service, options) return result
def execute(argv): create_parser = argparse.ArgumentParser( prog='cpm create', description='Create a new cpm project', add_help=False) create_parser.add_argument('project_name') create_parser.add_argument('-s', '--repository-url', required=False, action='store', default='https://repo.cpmbits.com:8000') create_parser.add_argument('-t', '--template', required=False, action='store') args = create_parser.parse_args(argv) project_loader = ProjectLoader() cpm_hub_connector = CpmHubConnectorV1(repository_url=args.repository_url) service = CreationService(project_loader, cpm_hub_connector=cpm_hub_connector) template_name, template_version = __template_to_use(args.template) options = CreationOptions( project_name=args.project_name, directory=args.project_name, init_from_existing_sources=False, init_from_template=True if args.template else False, template_name=template_name, template_version=template_version) result = new_project(service, options) return result
def new_project(creation_service, options=CreationOptions()): if creation_service.exists(options.directory): return Result(FAIL, f'error: directory {options.directory} already exists') creation_service.create(options) return Result(OK, f'Created project {options.project_name}')
def test_returns_error_code_when_project_already_exists(self): creation_service = mock.MagicMock() creation_service.exists.return_value = True options = CreationOptions(project_name='Awesome Project') result = new_project(creation_service, options) assert result.status_code == 1
def init_project(creation_service, options=CreationOptions(init_from_existing_sources=True)): if creation_service.exists(options.directory): directory_print = 'current directory' if options.directory == '.' else f'directory {options.directory}' return Result( FAIL, f'error: {directory_print} already contains a cpm project') creation_service.create(options) return Result(OK, f'Created project {options.project_name}')
def test_creation_service_returns_generated_project(self, filesystem): project_loader = mock.MagicMock() creation_service = CreationService(project_loader) creation_options = CreationOptions(project_name='AwesomeProject') project = creation_service.create(creation_options) assert project.name == 'AwesomeProject' assert project.build.sources == ['main.cpp']
def test_create_project_uses_creation_service_for_initializing_project( self): creation_service = mock.MagicMock() creation_service.exists.return_value = False options = CreationOptions(project_name='Awesome Project') result = new_project(creation_service, options) assert result.status_code == 0 creation_service.create.assert_called_once_with(options)
def test_create_project_accepts_options_from_api(self): creation_service = mock.MagicMock() creation_service.exists.return_value = False options = CreationOptions(project_name='Awesome Project', generate_sample_code=True) result = new_project(creation_service, options) assert result.status_code == 0 creation_service.create.assert_called_once_with(options)
def test_create_project_fails_when_template_used_does_not_exist(self): creation_service = mock.MagicMock() creation_service.exists.return_value = False options = CreationOptions(project_name='Awesome Project') creation_service.create.side_effect = TemplateNotFound result = new_project(creation_service, options) assert result.status_code == 1 creation_service.create.assert_called_once()
def test_creation_service_generates_default_sample_code_when_selected( self, filesystem): project_loader = mock.MagicMock() creation_service = CreationService(project_loader) creation_options = CreationOptions(directory='AwesomeProject', project_name='AwesomeProject') creation_service.create(creation_options) filesystem.create_file.assert_called_with('AwesomeProject/main.cpp', CPP_HELLO_WORLD)
def test_init_project_uses_creation_service_for_initializing_project(self): creation_service = mock.MagicMock() creation_service.exists.return_value = False options = CreationOptions(init_from_existing_sources=True, directory='.', project_name='Awesome Project') result = init_project(creation_service, options) assert result.status_code == 0 creation_service.create.assert_called_once_with(options)
def new_project(creation_service, options=CreationOptions()): try: if creation_service.exists(options.directory): return Result( FAIL, f'error: directory {options.directory} already exists') creation_service.create(options) except TemplateNotFound: return Result( FAIL, f'error: template {options.template_name}:{options.template_version} not found' ) return Result(OK, f'Created project {options.project_name}')
def test_it_creates_directory_and_descriptor_file_when_creating_project( self, filesystem): project_loader = mock.MagicMock() creation_service = CreationService(project_loader) creation_options = CreationOptions(generate_sample_code=False, directory='AwesomeProject', project_name='AwesomeProject') creation_service.create(creation_options) filesystem.create_directory.assert_called_once_with('AwesomeProject') filesystem.create_file.assert_called_once_with( 'AwesomeProject/project.yaml', mock.ANY)
def test_it_only_creates_descriptor_file_when_creating_project_from_existing_sources( self, filesystem): project_loader = mock.MagicMock() creation_service = CreationService(project_loader) creation_options = CreationOptions(generate_sample_code=False, directory='.', project_name='AwesomeProject', init_from_existing_sources=True) creation_service.create(creation_options) filesystem.create_directory.assert_not_called() filesystem.create_file.assert_called_once_with('./project.yaml', mock.ANY)
def execute(argv): create_parser = argparse.ArgumentParser( prog='cpm init', description='Initialize a project from existing sources', add_help=False) create_parser.add_argument('project_name') args = create_parser.parse_args(argv) project_loader = ProjectLoader() service = CreationService(project_loader) options = CreationOptions(init_from_existing_sources=True, generate_sample_code=False, directory='.', project_name=args.project_name) result = init_project(service, options) return result
def test_creation_service_uses_cpm_hub_connector_to_download_template(self, project_descriptor_editor): project_loader = mock.MagicMock() cpm_hub_connector = mock.MagicMock() template_installer = mock.MagicMock() creation_service = CreationService(project_loader, cpm_hub_connector, template_installer) options = CreationOptions( project_name='AwesomeProject', init_from_template=True, template_name='arduino-uno', ) template_project = Project(name=options.project_name, version=DEFAULT_PROJECT_VERSION) project_loader.load.return_value = template_project project_template = ProjectTemplate( name='arduino-uno', version='1.0.0', payload='payload' ) cpm_hub_connector.download_template.return_value = project_template project = creation_service.create(options) assert project.name == 'AwesomeProject' template_installer.install.assert_called_once_with(project_template, options.directory) project_descriptor_editor.update.assert_called_once()