Skip to content

Commit

Permalink
Lint command refactored for molecule v3
Browse files Browse the repository at this point in the history
Compatibility breaking change caused by implementation of measures
described in #2293.

Fixes: #2293
  • Loading branch information
ssbarnea committed Feb 1, 2020
1 parent 81cbaa0 commit c0fc6e9
Show file tree
Hide file tree
Showing 65 changed files with 91 additions and 2,524 deletions.
4 changes: 0 additions & 4 deletions docs/ci.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,8 @@ conflict.
command: /usr/sbin/init
provisioner:
name: ansible
lint:
name: ansible-lint
verifier:
name: testinfra
lint:
name: flake8
.. _`GitHub Actions`: https://github.com/features/actions
.. _`Factors`: http://tox.readthedocs.io/en/latest/config.html#factors-and-factor-conditional-settings
Expand Down
67 changes: 37 additions & 30 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,49 @@ Podman
:undoc-members:


.. _linters:
.. _root_lint:

Lint
----

Molecule handles project linting by invoking configurable linters.
Starting with v3, Molecule handles project linting by invoking and external
lint commands as exemplified below.

Yaml Lint
^^^^^^^^^
The decision to remove the complex linting support was not easily taken as we
do find it very useful. The issue was that molecule runs on scenarios and
linting is usually performed at repository level.

It makes little sense to perform linting in more than one place per project.
Molecule was able to use up to three linters and while it was aimed to flexible
about them, it ended up creating more confusions to the users. We decided to
maximize flexibility by just calling an external shell command.

.. code-block:: yaml
lint: |
yamllint .
ansible-lint
flake8
The older format is no longer supported and you have to update the
``molecule.yml`` when you upgrade. If you don't want to do any linting,
it will be enough to remove all lint related sections from the file.

.. code-block:: yaml
# old v2 format, no longer supported
lint:
name: yamllint
enabled: true
provisioner:
lint:
name: ansible-lint
options: ...
env: ...
verifier:
lint:
name: flake8
.. autoclass:: molecule.lint.yamllint.Yamllint()
:undoc-members:
.. _platforms:

Expand All @@ -117,14 +148,6 @@ Ansible
.. autoclass:: molecule.provisioner.ansible.Ansible()
:undoc-members:

Lint
....

Molecule handles provisioner linting by invoking configurable linters.

.. autoclass:: molecule.provisioner.lint.ansible_lint.AnsibleLint()
:undoc-members:

.. _root_scenario:

Scenario
Expand Down Expand Up @@ -157,25 +180,9 @@ Ansible
.. autoclass:: molecule.verifier.ansible.Ansible()
:undoc-members:

Lint
....

.. autoclass:: molecule.verifier.lint.ansible_lint.AnsibleLint()
:undoc-members:

Testinfra
^^^^^^^^^

.. autoclass:: molecule.verifier.testinfra.Testinfra()
:undoc-members:

.. _root_lint:

Lint
....

.. autoclass:: molecule.verifier.lint.flake8.Flake8()
:undoc-members:

.. autoclass:: molecule.verifier.lint.precommit.PreCommit()
:undoc-members:
5 changes: 2 additions & 3 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ keys represent the high level components that Molecule provides. These are:
* The :ref:`driver` provider. Molecule uses `Docker`_ by default. Molecule uses
the driver to delegate the task of creating instances.

* The :ref:`linters` provider. Molecule uses :std:doc:`Yamllint
<yamllint:index>` by default to ensure that best practices are encouraged
when writing YAML.
* The :ref:`root_lint` command. Molecule can call external commands to ensure
that best practices are encouraged.

* The :ref:`platforms` definitions. Molecule relies on this to know which
instances to create, name and to which group each instance belongs. If you
Expand Down
2 changes: 0 additions & 2 deletions molecule/command/init/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,5 @@ def role(
'verifier_name': verifier_name,
}

command_args['verifier_lint_name'] = api.verifiers()[verifier_name].default_linter

r = Role(command_args)
r.execute()
2 changes: 0 additions & 2 deletions molecule/command/init/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ def scenario(
'verifier_name': verifier_name,
}

command_args['verifier_lint_name'] = api.verifiers()[verifier_name].default_linter

driver_template = driver_template or os.environ.get(
'MOLECULE_SCENARIO_DRIVER_TEMPLATE', None
)
Expand Down
29 changes: 17 additions & 12 deletions molecule/command/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from molecule import logger
from molecule.command import base
from molecule import util


LOG = logger.get_logger(__name__)

Expand Down Expand Up @@ -71,18 +73,21 @@ def execute(self):
:return: None
"""
self.print_info()
linters = [
l
for l in [
self._config.lint,
self._config.verifier.lint,
self._config.provisioner.lint,
]
if l
]

for l in linters:
l.execute()

# v3 migration code:
cmd = self._config.lint
if cmd == 'yamllint':
LOG.warning(
"Deprecated linter command found, migrate to newer syntax using <url>"
)
cmd = 'yamllint .'

try:
# import pdb; pdb.set_trace()
LOG.info("Executing: %s" % cmd)
util.run(cmd, shell=True, universal_newlines=True, check=True)
except Exception as e:
util.sysexit_with_message("Lint failed: %s: %s" % (e, e))


@click.command()
Expand Down
8 changes: 1 addition & 7 deletions molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from molecule.dependency import ansible_galaxy
from molecule.dependency import gilt
from molecule.dependency import shell
from molecule.lint import yamllint
from molecule.model import schema_v2
from molecule.provisioner import ansible

Expand Down Expand Up @@ -181,21 +180,17 @@ def env(self):
'MOLECULE_INSTANCE_CONFIG': self.driver.instance_config,
'MOLECULE_DEPENDENCY_NAME': self.dependency.name,
'MOLECULE_DRIVER_NAME': self.driver.name,
'MOLECULE_LINT_NAME': self.lint.name,
'MOLECULE_PROVISIONER_NAME': self.provisioner.name,
'MOLECULE_PROVISIONER_LINT_NAME': self.provisioner.lint.name,
'MOLECULE_SCENARIO_NAME': self.scenario.name,
'MOLECULE_VERIFIER_NAME': self.verifier.name,
'MOLECULE_VERIFIER_LINT_NAME': self.verifier.lint.name,
'MOLECULE_VERIFIER_TEST_DIRECTORY': self.verifier.directory,
}

@property
@util.lru_cache()
def lint(self):
lint_name = self.config['lint']['name']
if lint_name == 'yamllint':
return yamllint.Yamllint(self)
return lint_name

@property
@util.lru_cache()
Expand Down Expand Up @@ -413,7 +408,6 @@ def _get_defaults(self):
'options': {},
'env': {},
'additional_files_or_dirs': [],
'lint': {'name': 'flake8', 'enabled': True, 'options': {}, 'env': {}},
},
}

Expand Down
3 changes: 1 addition & 2 deletions molecule/cookiecutter/molecule/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
"provisioner_lint_name": "ansible-lint",
"scenario_name": "OVERRIDDEN",
"role_name": "OVERRIDDEN",
"verifier_name": "OVERRIDDEN",
"verifier_lint_name": "flake8"
"verifier_name": "OVERRIDDEN"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ dependency:
name: {{ cookiecutter.dependency_name }}
driver:
name: {{ cookiecutter.driver_name }}
lint:
name: {{ cookiecutter.lint_name }}
platforms:
{%- if cookiecutter.driver_name == 'docker' %}
- name: instance
Expand All @@ -19,10 +17,5 @@ platforms:
{%- endif %}
provisioner:
name: {{ cookiecutter.provisioner_name }}
lint:
name: {{ cookiecutter.provisioner_lint_name }}
enabled: false
verifier:
name: {{ cookiecutter.verifier_name }}
lint:
name: {{ cookiecutter.verifier_lint_name }}
16 changes: 0 additions & 16 deletions molecule/model/schema_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ def pre_validate_base_schema(env, keep_string):
'molecule_env_var': True,
'allowed': api.verifiers(),
},
'lint': {
'type': 'dict',
'schema': {
'name': {
'type': 'string',
'molecule_env_var': True,
'allowed': [
'flake8',
'pre-commit',
'rubocop',
'yamllint',
'ansible-lint',
],
}
},
},
},
},
}
Expand Down
10 changes: 0 additions & 10 deletions molecule/provisioner/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

import abc

from molecule.util import lru_cache
from molecule.provisioner.lint import ansible_lint


class Base(object):
"""Provisioner Base Class."""
Expand Down Expand Up @@ -66,10 +63,3 @@ def name(self): # pragma: no cover
:returns: str
"""
pass

@property
@lru_cache()
def lint(self):
lint_name = self._config.config['provisioner']['lint']['name']
if lint_name == 'ansible-lint':
return ansible_lint.AnsibleLint(self._config)
1 change: 0 additions & 1 deletion molecule/provisioner/lint/__init__.py

This file was deleted.

0 comments on commit c0fc6e9

Please sign in to comment.