def test_must_fail_when_repo_not_found(self):
        location = str(Path("my", "folder"))

        with patch.object(repository, "unzip") as unzip_mock:
            unzip_mock.side_effect = RepositoryNotFound("repo")

            with self.assertRaises(ArbitraryProjectDownloadFailed):
                generate_non_cookiecutter_project(location, self.output_dir)
Esempio n. 2
0
    def generate_project(self, context: Dict):
        """
        Generates a project based on this cookiecutter template and the given context. The context is first
        processed and manipulated by series of preprocessors(if any) then the project is generated and finally
        the series of postprocessors(if any) are executed.

        Parameters
        ----------
        context: Dict
            the cookiecutter context to fulfill the values of cookiecutter.json keys

        Raise:
        ------
        InvalidLocationError: if the given location is not from a known repo type
        GenerateProjectFailedError: if something else went wrong
        """
        try:
            LOG.debug("preprocessing the cookiecutter context")
            for processor in self._preprocessors:
                context = processor.run(context)
        except Exception as e:
            raise PreprocessingError(template=self._location,
                                     provider_error=e) from e

        try:
            LOG.debug(
                "Baking a new template with cookiecutter with all parameters")
            cookiecutter(template=self._location,
                         output_dir=".",
                         no_input=True,
                         extra_context=context)
        except RepositoryNotFound as e:
            # cookiecutter.json is not found in the template. Let's just clone it directly without
            # using cookiecutter and call it done.
            LOG.debug(
                "Unable to find cookiecutter.json in the project. Downloading it directly without treating "
                "it as a cookiecutter template")
            generate_non_cookiecutter_project(location=self._location,
                                              output_dir=".")
        except UnknownRepoType as e:
            raise InvalidLocationError(template=self._location) from e
        except Exception as e:
            raise GenerateProjectFailedError(template=self._location,
                                             provider_error=e) from e

        try:
            LOG.debug("postprocessing the cookiecutter context")
            for processor in self._postprocessors:
                context = processor.run(context)
        except Exception as e:
            raise PostprocessingError(template=self._location,
                                      provider_error=e) from e
    def test_support_zip_files(self, location, is_url, osutils_mock):

        with patch.object(repository, "unzip") as unzip_mock:
            unzip_mock.return_value = "unzipped_dir"

            generate_non_cookiecutter_project(location, self.output_dir)

            unzip_mock.assert_called_with(zip_uri=location,
                                          is_url=is_url,
                                          no_input=True,
                                          clone_to_dir=ANY)

            osutils_mock.copytree.assert_called_with("unzipped_dir",
                                                     self.output_dir,
                                                     ignore=ANY)
    def test_support_source_control_repos(self, osutils_mock):
        abbreviated_location = "gh:awslabs/aws-sam-cli"
        location = "https://github.com/awslabs/aws-sam-cli.git"

        with patch.object(repository, "clone") as clone_mock:
            clone_mock.return_value = "cloned_dir"

            generate_non_cookiecutter_project(abbreviated_location,
                                              self.output_dir)

            clone_mock.assert_called_with(repo_url=location,
                                          no_input=True,
                                          clone_to_dir=ANY)

            osutils_mock.copytree.assert_called_with("cloned_dir",
                                                     self.output_dir,
                                                     ignore=ANY)
    def test_must_fail_on_local_folders(self):
        location = str(Path("my", "folder"))

        with self.assertRaises(ArbitraryProjectDownloadFailed):
            generate_non_cookiecutter_project(location, self.output_dir)