Skip to content
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

Use tmt init --template, not --mini|--base|--full #69

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ Before starting a new project initialize the metadata tree root::

$ tmt init
Tree '/tmp/try' initialized.
To populate it with example content, use --template with mini, base or full.

You can also populate it with a minimal plan example::

$ tmt init --mini
$ tmt init --template mini
Tree '/tmp/try' initialized.
Applying template 'mini'.
Directory '/tmp/try/plans' created.
Plan '/tmp/try/plans/example.fmf' created.

Create a plan and a test::

$ tmt init --base
$ tmt init --template base
Tree '/tmp/try' initialized.
Applying template 'base'.
Directory '/tmp/try/tests/example' created.
Test metadata '/tmp/try/tests/example/main.fmf' created.
Test script '/tmp/try/tests/example/test.sh' created.
Expand All @@ -50,8 +53,9 @@ Create a plan and a test::
Initialize with a richer example that also includes the story
(overwriting existing files)::

$ tmt init --full --force
$ tmt init --template full --force
Tree '/tmp/try' already exists.
Applying template 'full'.
Directory '/tmp/try/tests/example' already exists.
Test metadata '/tmp/try/tests/example/main.fmf' overwritten.
Test script '/tmp/try/tests/example/test.sh' overwritten.
Expand Down
8 changes: 6 additions & 2 deletions stories/cli/init.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ tested: /tests/unit

/mini:
summary: Create a tree with simple examples
example: tmt init --mini
example: tmt init --template mini

/base:
summary: Create a tree with some examples
example: tmt init --template base

/full:
summary: Create a tree with full examples
example: tmt init --full
example: tmt init --template full
15 changes: 8 additions & 7 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ def test_init():
assert 'initialized' in result.output
result = runner.invoke(tmt.cli.main, ['init'])
assert 'already exists' in result.output
result = runner.invoke(tmt.cli.main, ['init', '--mini'])
result = runner.invoke(tmt.cli.main, ['init', '--template', 'mini'])
assert 'plans/example' in result.output
result = runner.invoke(tmt.cli.main, ['init', '--mini'])
result = runner.invoke(tmt.cli.main, ['init', '--template', 'mini'])
assert result.exception
result = runner.invoke(tmt.cli.main, ['init', '--full', '--force'])
result = runner.invoke(tmt.cli.main, ['init', '--template', 'full',
'--force'])
assert 'overwritten' in result.output
# tmt init --mini in a clean directory
# tmt init --template mini in a clean directory
os.system('rm -rf .fmf *')
result = runner.invoke(tmt.cli.main, ['init', '--mini'])
result = runner.invoke(tmt.cli.main, ['init', '--template', 'mini'])
assert 'plans/example' in result.output
# tmt init --full in a clean directory
# tmt init --template full in a clean directory
os.system('rm -rf .fmf *')
result = runner.invoke(tmt.cli.main, ['init', '--full'])
result = runner.invoke(tmt.cli.main, ['init', '--template', 'full'])
assert 'tests/example' in result.output
os.chdir(original_directory)
shutil.rmtree(tmp)
Expand Down
30 changes: 21 additions & 9 deletions tmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,23 +695,29 @@ def export(
# Init
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

_init_template_choices = ['empty', 'mini', 'base', 'full']
_init_templates = listed(_init_template_choices, join='or')
@main.command()
@click.pass_context
@click.argument('path', default='.')
@click.option(
'--mini', is_flag=True, help='Create a minimal plan.')
@click.option(
'--base', is_flag=True, help='Create a plan and a test.')
@click.option(
'--full', is_flag=True, help='Create a story, a plan and a test.')
'-t', '--template', default='empty', metavar='TEMPLATE',
type=click.Choice(_init_template_choices),
help='Template ({}).'.format(_init_templates))
@verbose_debug_quiet
@force_dry
def init(context, path, mini, base, full, force, **kwargs):
def init(context, path, template, force, **kwargs):
"""
Initialize a new tmt tree.

By default tree is created in the current directory.
Provide a PATH to create it in a different location.

\b
A tree can be optionally populated with example metadata:
* 'mini' template contains a minimal plan and no tests,
* 'base' template contains a plan and a beakerlib test,
* 'full' template contains a 'full' story, an 'full' plan and a shell test.
"""

# Check for existing tree
Expand All @@ -737,12 +743,18 @@ def init(context, path, mini, base, full, force, **kwargs):
echo("Tree '{}' initialized.".format(tree.root))

# Populate the tree with example objects if requested
if mini:
if template == 'empty':
non_empty_choices = [c for c in _init_template_choices if c != 'empty']
echo("To populate it with example content, use --template with "
"{}.".format(listed(non_empty_choices, join='or')))
else:
echo("Applying template '{}'.".format(template, _init_templates))
if template == 'mini':
tmt.Plan.create('/plans/example', 'mini', tree, force)
if base:
elif template == 'base':
tmt.Test.create('/tests/example', 'beakerlib', tree, force)
tmt.Plan.create('/plans/example', 'base', tree, force)
if full:
elif template == 'full':
tmt.Test.create('/tests/example', 'shell', tree, force)
tmt.Plan.create('/plans/example', 'full', tree, force)
tmt.Story.create('/stories/example', 'full', tree, force)