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

Allow for defining arbitrary project version and name in configuratio… #166

Merged
merged 11 commits into from
Dec 7, 2020
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ apidocs/
.tox/
.coverage.*
.vscode
.idea
altendky marked this conversation as resolved.
Show resolved Hide resolved
.python-version
14 changes: 12 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Install from PyPI::
.. note::

``towncrier``, as a command line tool, works on Python 3.5+ only.
It is usable by projects written in other languages, provided you give it the version of the project when invoking it.
It is usable by projects written in other languages, provided you specify the project version either in the configuration file or on the command line.
For Python 2/3 compatible projects, the version can be discovered automatically.

In your project root, add a ``pyproject.toml`` file.
Expand Down Expand Up @@ -64,13 +64,21 @@ Using the above example, your news fragments would be ``src/myproject/newsfragme

This will keep the folder around, but otherwise "empty".

``towncrier`` needs to know what version your project is, and there are two ways you can give it:
``towncrier`` needs to know what version your project is, and there are three ways you can give it:

- For Python 2/3 compatible projects, a ``__version__`` in the top level package.
This can be either a string literal, a tuple, or an `Incremental <https://github.com/hawkowl/incremental>`_ version.

- Manually passing ``--version=<myversionhere>`` when interacting with ``towncrier``.

- Definining a ``version`` option in a configuration file:

.. code-block:: ini

[tool.towncrier]
# ...
version = "1.2.3" # project version if maintained separately

To create a new news fragment, use the ``towncrier create`` command.
For example::

Expand Down Expand Up @@ -123,6 +131,8 @@ Towncrier has the following global options, which can be specified in the toml f
single_file = true # if false, filename is formatted like `title_format`.
filename = "NEWS.rst"
directory = "directory/of/news/fragments"
version = "1.2.3" # project version if maintained separately
name = "arbitrary project name"
altendky marked this conversation as resolved.
Show resolved Hide resolved
template = "path/to/template.rst"
start_line = "start of generated content"
title_format = "{name} {version} ({project_date})" # or false if template includes title
Expand Down
2 changes: 2 additions & 0 deletions src/towncrier/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def parse_toml(base_path, config):
"single_file": single_file,
"filename": config.get("filename", "NEWS.rst"),
"directory": config.get("directory"),
"version": config.get("version"),
"name": config.get("name"),
"sections": sections,
"types": types,
"template": template,
Expand Down
28 changes: 16 additions & 12 deletions src/towncrier/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,24 @@ def __main(
)

if project_version is None:
project_version = get_version(
os.path.join(base_directory, config["package_dir"]), config["package"]
).strip()
project_version = config.get('version')
if project_version is None:
project_version = get_version(
os.path.join(base_directory, config["package_dir"]), config["package"]
).strip()

if project_name is None:
package = config.get("package")
if package:
project_name = get_project_name(
os.path.abspath(os.path.join(base_directory, config["package_dir"])),
package,
)
else:
# Can't determine a project_name, but maybe it is not needed.
project_name = ""
project_name = config.get('name')
if not project_name:
package = config.get("package")
if package:
project_name = get_project_name(
os.path.abspath(os.path.join(base_directory, config["package_dir"])),
package,
)
else:
# Can't determine a project_name, but maybe it is not needed.
project_name = ""

if project_date is None:
project_date = _get_date().strip()
Expand Down
2 changes: 2 additions & 0 deletions src/towncrier/newsfragments/165.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow definition of the project ``version`` and ``name`` in the configuration file.
This allows use of towncrier seamlessly with non-Python projects.
84 changes: 84 additions & 0 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,90 @@ def test_projectless_changelog(self):
).lstrip(),
)

def test_version_in_config(self):
"""The calling towncrier with version defined in configfile.

Specifying a version in toml file will be helpful if version
is maintained by i.e. bumpversion and it's not a python project.
"""
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'version = "7.8.9"\n')
os.mkdir("newsfragments")
with open("newsfragments/123.feature", "w") as f:
f.write("Adds levitation")

result = runner.invoke(
_main, ["--date", "01-01-2001", "--draft"]
)

self.assertEqual(0, result.exit_code, result.output)
self.assertEqual(
result.output,
dedent(
"""
Loading template...
Finding news fragments...
Rendering news fragments...
Draft only -- nothing has been written.
What is seen below is what would be written.

7.8.9 (01-01-2001)
==================

Features
--------

- Adds levitation (#123)

"""
).lstrip(),
)

def test_project_name_in_config(self):
"""The calling towncrier with project name defined in configfile.

Specifying a project name in toml file will be helpful to keep the
project name consistent as part of the towncrier configuration, not call.
"""
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'name = "ImGoProject"\n')
os.mkdir("newsfragments")
with open("newsfragments/123.feature", "w") as f:
f.write("Adds levitation")

result = runner.invoke(
_main, ["--version", "7.8.9", "--date", "01-01-2001", "--draft"]
)

self.assertEqual(0, result.exit_code, result.output)
self.assertEqual(
result.output,
dedent(
"""
Loading template...
Finding news fragments...
Rendering news fragments...
Draft only -- nothing has been written.
What is seen below is what would be written.

ImGoProject 7.8.9 (01-01-2001)
==============================

Features
--------

- Adds levitation (#123)

"""
).lstrip(),
)

def test_no_package_changelog(self):
"""The calling towncrier with any package argument.

Expand Down