Skip to content

Commit

Permalink
Merge pull request #298 from facundobatista/bundle-readme-is-mandatory
Browse files Browse the repository at this point in the history
Mandatorily include a README.md file when packing bundles.
  • Loading branch information
facundobatista authored Apr 20, 2021
2 parents 6634a18 + 6e2b0d6 commit 65d735b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 6 additions & 4 deletions charmcraft/commands/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
logger = logging.getLogger(__name__)

# the minimum set of files in a bundle
MANDATORY_FILES = {'bundle.yaml', 'manifest.yaml'}
MANDATORY_FILES = {'bundle.yaml', 'manifest.yaml', 'README.md'}


def build_zip(zippath, basedir, fpaths):
Expand All @@ -44,10 +44,12 @@ def get_paths_to_include(config):
dirpath = config.project.dirpath
allpaths = set()

# all mandatory files, which must exist (currently only bundles.yaml is mandatory, and
# it's verified before)
# all mandatory files
for fname in MANDATORY_FILES:
allpaths.add(dirpath / fname)
fpath = dirpath / fname
if not fpath.exists():
raise CommandError("Missing mandatory file: {}.".format(fpath))
allpaths.add(fpath)

# the extra files (relative paths)
for spec in config.parts:
Expand Down
14 changes: 14 additions & 0 deletions tests/commands/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ def test_simple_succesful_build(tmp_path, caplog, bundle_yaml, config):
"""A simple happy story."""
caplog.set_level(logging.INFO, logger="charmcraft.commands")

# mandatory files (other thant the automatically provided manifest)
content = bundle_yaml(name='testbundle')
config.set(type='bundle')
(tmp_path / 'README.md').write_text("test readme")

# build!
PackCommand('group', config).run(noargs)
Expand All @@ -68,6 +70,7 @@ def test_simple_succesful_build(tmp_path, caplog, bundle_yaml, config):
zf = zipfile.ZipFile(str(zipname)) # str() for Py3.5 support
assert 'charmcraft.yaml' not in [x.filename for x in zf.infolist()]
assert zf.read('bundle.yaml') == content.encode('ascii')
assert zf.read('README.md') == b"test readme"

expected = "Created '{}'.".format(zipname)
assert [expected] == [rec.message for rec in caplog.records]
Expand All @@ -89,6 +92,17 @@ def test_missing_bundle_file(tmp_path, config):
"Missing or invalid main bundle file: '{}'.".format(tmp_path / 'bundle.yaml'))


def test_missing_other_mandatory_file(tmp_path, config, bundle_yaml):
"""Can not build a bundle without any of the mandatory files."""
bundle_yaml(name='testbundle')
config.set(type='bundle')

# build without a README!
with pytest.raises(CommandError) as cm:
PackCommand('group', config).run(noargs)
assert str(cm.value) == "Missing mandatory file: {}.".format(tmp_path / 'README.md')


def test_missing_name_in_bundle(tmp_path, bundle_yaml, config):
"""Can not build a bundle without name."""
config.set(type='bundle')
Expand Down

0 comments on commit 65d735b

Please sign in to comment.