10000 Strict jinja environment by hackebrot · Pull Request #598 · cookiecutter/cookiecutter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Strict jinja environment #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 4, 2015

Conversation

hackebrot
Copy link
Member
  • This PR changes the jinja2 environment to raise an error if cookiecutter tries to render a template that contains an undefined variable.
  • It also improves the overall robustness of the generation as we do solely create templates via the our configured environment (previously jinja2 tried to create an environment on-the-fly)
  • Cookiecutter now exists with a user-friendly warning for invalid directory names, file names and files that contain undefined variables
Unable to create project directory '{{cookiecutter.project_slug}}'.
Error message: 'dict object' has no attribute 'project_slug'.
Context: {u'cookiecutter': {u'project_name': u'Fake Project Templated', u'project_short_description': u'This is a fake project.', u'year': u'2013', u'release_date': u'2013-07-28', u'version': u'0.1', u'full_name': u'Raphael Pierzina', u'github_username': u'hackebrot', u'email': u'raphael@hackebrot.de', u'repo_name': u'fake-project-templated'}}

Resolve #111
Resolve #586
Close #592

@hackebrot hackebrot added bug This issue/PR relates to a bug. enhancement This issue/PR relates to a feature request. needs-review PR Only: This PR require review from other developer labels Nov 22, 2015
@hackebrot
Copy link
Member Author

💤 git on appveyor...

@codecov-io
Copy link

Current coverage is 99.25%

Merging #598 into master will increase coverage by +0.04% as of fd060d7

@@            master    #598   diff @@
======================================
  Files           12      12       
  Stmts          511     540    +29
  Branches         0       0       
  Methods          0       0       
======================================
+ Hit            507     536    +29
  Partial          0       0       
  Missed           4       4       

Review entire Coverage Diff as of fd060d7


Uncovered Suggestions

  1. +0.37% via ...okiecutter/config.py#19...20
  2. +0.37% via ...ookiecutter/utils.py#27...28

Powered by Codecov. Updated on successful CI builds.

@pydanny
Copy link
Member
pydanny commented Nov 23, 2015

Epic bit of work here. Alas, looks like @appveyor timed out on git. :(

@hackebrot
Copy link
Member Author

Thanks brother! 🙇 Rebasing on #602

@hackebrot hackebrot force-pushed the strict-jinja-environment branch from a13eaaa to 8ca5d60 Compare November 23, 2015 15:35
@hackebrot
Copy link
Member Author

We can remove the xfail on one of the tests as soon as audreyfeldroy/cookiecutter-jquery#2 is merged 😄

cc @audreyr

@hackebrot
Copy link
Member Author

There we go 😹

@luzfcb
Copy link
Contributor
luzfcb commented Nov 25, 2015

@hackebrot this has proved useful because it showed me a bug in cookiecutter-django 🎉

the error output was this:

Unable to create file 'config/settings/local.py'. Error message: 'dict object' has no attribute 'celery_support'. Context: {'cookiecutter': {'now': '2015/11/04', 'use_newrelic': 'y', 'email': 'bnafta@gmail.com', 'use_celery': 'y', 'windows': 'y', 'use_whitenoise': 'y', 'description': 'A short description of the project.', 'use_mailhog': 'y', 'repo_name': 'novo_projeto', 'use_sentry': 'y', 'version': '0.1.0', 'use_python2': 'n', 'project_name': 'novo_projeto', 'timezone': 'UTC', 'year': '2015', 'domain_name': 'example.com', 'author_name': 'Fábio Cáritas Barrionuevo da Luz', 'use_opbeat': 'y'}}

I was initially a little confused by the error text, condensed all on one line.
I think maybe a line break before Context: can leave a little more readable.

said that, I do not know if it's good practice to put line break on Exceptions output text

@hackebrot
Copy link
Member Author

@luzfcb: I usually don't put linebreaks in string literals as you don't really know what the actual display will be. I personally prefer to leave linewrapping to the terminal output which accounts for linewidth etc. However I completely understand your feedback...we could have a custom catch in cookiecutter.cli and create the output str ourselves

...
    except (OutputDirExistsException,
            InvalidModeException,
            FailedHookException) as e:
        click.echo(e)
        sys.exit(1)
    except UndefinedVariableInTemplate as e:
        click.echo(e.message)
        click.echo('Error message: {}'.format(e.error.message))
        click.echo('Context: {}'.format(e.context))
        sys.exit(1)

@hackebrot
Copy link
Member Author

@maiksensi: Cookiecutter implements rm -rf in cookiecutter.utils.rmtree which I use to clean up the project_dir (based on a similar SO question, see doc str)

@luzfcb
Copy link
Contributor
luzfcb commented Nov 25, 2015

@hackebrot nice idea.

click supports color output, perhaps it would be useful to give more emphasis in the important part of error message, and let a little more readable (or not 😔 ).

http://click.pocoo.org/6/utils/#ansi-colors

click require colorama to enable color output support.

pip install colorama

...
    except (OutputDirExistsException,
            InvalidModeException,
            FailedHookException) as e:
        click.echo(e)
        sys.exit(1)
    except UndefinedVariableInTemplate as e:
        click.echo(e.message)
        # click.echo('Error message: {}'.format(e.error.message))
        click.secho('Error message: {}'.format(e.error.message), fg='red')
        click.echo('Context: {}'.format(e.context))
        sys.exit(1)

but, support the color output must be treated at another issue

@hackebrot
Copy link
Member Author

Update: Changed echo according to @luzfcb's feedback:

Example error:

Unable to create file 'README.md'
Error message: 'dict object' has no attribute 'defaultPluginName'
Context: {
    "cookiecutter": {
        "className": "BoilerPlate", 
        "email": "raphael@hackebrot.de", 
        "full_name": "Raphael Pierzina", 
        "github_username": "hackebrot", 
        "pluginName": "boilerPlate", 
        "project_name": "jQuery Boilerplate", 
        "project_short_description": "A jump-start for jQuery plugins development.", 
        "release_date": "2013-08-14", 
        "repo_name": "boilerplate", 
        "version": "0.1.0", 
        "year": "2013"
    }
}

I have to say this is pretty dope 😹

@hackebrot
Copy link
Member Author

(I does not support colored output)

@hackebrot
Copy link
Member Author

Any objections?

@hackebrot hackebrot mentioned this pull request Nov 29, 2015
@maiksensi
Copy link
Contributor

great work here 👍

@johtso
Copy link
Contributor
johtso commented Nov 30, 2015

Great stuff! 👍

@pydanny
Copy link
Member
pydanny commented Dec 3, 2015

Reviewing cause this one is HUUUUUGE

@hackebrot
Copy link
Member Author

😹

pydanny added a commit that referenced this pull request Dec 4, 2015
@pydanny pydanny merged commit f7da446 into cookiecutter:master Dec 4, 2015
@hackebrot hackebrot deleted the strict-jinja-environment branch December 4, 2015 22:11
@hackebrot hackebrot mentioned this pull request Dec 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug. enhancement This issue/PR relates to a feature request. needs-review PR Only: This PR require review from other developer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0