Skip to content

Commit

Permalink
Add template creation test, --skip-editor option (spack#11970)
Browse files Browse the repository at this point in the history
* Add template creation test
* Added --skip-editor option to "spack create": normally
  "spack create" opens an editor for the user after generating a
  package file; when the --skip-editor option is used, "spack create"
  only generates the package file and does not open an editor
* Added --skip-editor option to bash completion
  • Loading branch information
tldahlgren authored and scheibelp committed Jul 12, 2019
1 parent 505ad89 commit d615d0a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/spack/spack/cmd/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ def setup_parser(subparser):
subparser.add_argument(
'-f', '--force', action='store_true',
help="overwrite any existing package file with the same name")
subparser.add_argument(
'--skip-editor', action='store_true',
help="skip the edit session for the package (e.g., automation)")


class BuildSystemGuesser:
Expand Down Expand Up @@ -671,5 +674,6 @@ def create(parser, args):
package.write(pkg_path)
tty.msg("Created package file: {0}".format(pkg_path))

# Open up the new package file in your $EDITOR
editor(pkg_path)
# Optionally open up the new package file in your $EDITOR
if not args.skip_editor:
editor(pkg_path)
57 changes: 57 additions & 0 deletions lib/spack/spack/test/cmd/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import argparse
import os
import pytest

import spack.cmd.create
import spack.util.editor

from spack.main import SpackCommand


create = SpackCommand('create')


@pytest.fixture("module")
def cmd_create_repo(tmpdir_factory):
repo_namespace = 'cmd_create_repo'
repodir = tmpdir_factory.mktemp(repo_namespace)
repodir.ensure(spack.repo.packages_dir_name, dir=True)
yaml = repodir.join('repo.yaml')
yaml.write("""
repo:
namespace: cmd_create_repo
""")

repo = spack.repo.RepoPath(str(repodir))
with spack.repo.swap(repo):
yield repo, repodir


@pytest.fixture(scope='module')
def parser():
"""Returns the parser for the module"""
prs = argparse.ArgumentParser()
spack.cmd.create.setup_parser(prs)
return prs


def test_create_template(parser, cmd_create_repo):
"""Test template creation."""
repo, repodir = cmd_create_repo

name = 'test-package'
args = parser.parse_args(['--skip-editor', name])
spack.cmd.create.create(parser, args)

filename = repo.filename_for_package_name(name)
assert os.path.exists(filename)

with open(filename, 'r') as package_file:
content = ' '.join(package_file.readlines())
for entry in [r'TestPackage(Package)', r'def install(self']:
assert content.find(entry) > -1
2 changes: 1 addition & 1 deletion share/spack/spack-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function _spack_create {
if $list_options
then
compgen -W "-h --help --keep-stage -n --name -t --template -r --repo
-N --namespace -f --force" -- "$cur"
-N --namespace -f --force --skip-editor" -- "$cur"
fi
}

Expand Down

0 comments on commit d615d0a

Please sign in to comment.