|
1 | 1 | # Copyright (c) Amber Brown, 2015 |
2 | 2 | # See LICENSE for details. |
3 | 3 |
|
| 4 | +import collections |
| 5 | +import datetime |
4 | 6 | import os |
5 | 7 | import tempfile |
6 | 8 | import textwrap |
7 | 9 |
|
8 | | -from datetime import date |
9 | 10 | from pathlib import Path |
10 | 11 | from subprocess import call |
11 | 12 | from textwrap import dedent |
|
15 | 16 | from twisted.trial.unittest import TestCase |
16 | 17 |
|
17 | 18 | from .._shell import cli |
18 | | -from ..build import _main |
19 | | -from .helpers import read, with_git_project, with_project, write |
| 19 | +from ..build import ( |
| 20 | + BUILD_TIME_ENV_VAR_NAME, |
| 21 | + ISO_8601_DATE_FORMAT, |
| 22 | + _main, |
| 23 | +) |
| 24 | +from .helpers import ( |
| 25 | + read, |
| 26 | + with_fake_fragments, |
| 27 | + with_git_project, |
| 28 | + with_project, |
| 29 | + write, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +_default_fake_fragments = collections.OrderedDict( |
| 34 | + [ |
| 35 | + # Off the shelf news fragment. |
| 36 | + ("foo/newsfragments/123.feature", "Adds levitation"), |
| 37 | + # Towncrier treats this as '124.feature', ignoring '.rst' suffix. |
| 38 | + ("foo/newsfragments/124.feature.rst", "Extends levitation"), |
| 39 | + # Towncrier supports non-numeric news fragment file names. |
| 40 | + ("foo/newsfragments/baz.feature.rst", "Baz levitation"), |
| 41 | + # Towncrier supports file names that have a dot in the name of the |
| 42 | + # news fragment |
| 43 | + ("foo/newsfragments/fix-1.2.feature", "Baz fix levitation"), |
| 44 | + # Towncrier supports fragments not linked to a feature. |
| 45 | + ("foo/newsfragments/+anything.feature", "Orphaned feature"), |
| 46 | + ("foo/newsfragments/+xxx.feature", "Another orphaned feature"), |
| 47 | + ( |
| 48 | + "foo/newsfragments/+123_orphaned.feature", |
| 49 | + "An orphaned feature starting with a number", |
| 50 | + ), |
| 51 | + ( |
| 52 | + "foo/newsfragments/+12.3_orphaned.feature", |
| 53 | + "An orphaned feature starting with a dotted number", |
| 54 | + ), |
| 55 | + ( |
| 56 | + "foo/newsfragments/+orphaned_123.feature", |
| 57 | + "An orphaned feature ending with a number", |
| 58 | + ), |
| 59 | + ( |
| 60 | + "foo/newsfragments/+orphaned_12.3.feature", |
| 61 | + "An orphaned feature ending with a dotted number", |
| 62 | + ), |
| 63 | + # Towncrier ignores file names that don't have a dot. |
| 64 | + ("foo/newsfragments/README", "Blah blah"), |
| 65 | + # And file names that don't have a valid category. |
| 66 | + ("foo/newsfragments/README.rst", "**Blah blah**"), |
| 67 | + ], |
| 68 | +) |
20 | 69 |
|
21 | 70 |
|
22 | 71 | class TestCli(TestCase): |
23 | 72 | maxDiff = None |
24 | 73 |
|
25 | 74 | @with_project() |
| 75 | + @with_fake_fragments(_default_fake_fragments) |
26 | 76 | def _test_command(self, command, runner): |
27 | | - # Off the shelf newsfragment |
28 | | - with open("foo/newsfragments/123.feature", "w") as f: |
29 | | - f.write("Adds levitation") |
30 | | - # Towncrier treats this as 124.feature, ignoring .rst extension |
31 | | - with open("foo/newsfragments/124.feature.rst", "w") as f: |
32 | | - f.write("Extends levitation") |
33 | | - # Towncrier supports non-numeric newsfragment names. |
34 | | - with open("foo/newsfragments/baz.feature.rst", "w") as f: |
35 | | - f.write("Baz levitation") |
36 | | - # Towncrier supports files that have a dot in the name of the |
37 | | - # newsfragment |
38 | | - with open("foo/newsfragments/fix-1.2.feature", "w") as f: |
39 | | - f.write("Baz fix levitation") |
40 | | - # Towncrier supports fragments not linked to a feature |
41 | | - with open("foo/newsfragments/+anything.feature", "w") as f: |
42 | | - f.write("Orphaned feature") |
43 | | - with open("foo/newsfragments/+xxx.feature", "w") as f: |
44 | | - f.write("Another orphaned feature") |
45 | | - with open("foo/newsfragments/+123_orphaned.feature", "w") as f: |
46 | | - f.write("An orphaned feature starting with a number") |
47 | | - with open("foo/newsfragments/+12.3_orphaned.feature", "w") as f: |
48 | | - f.write("An orphaned feature starting with a dotted number") |
49 | | - with open("foo/newsfragments/+orphaned_123.feature", "w") as f: |
50 | | - f.write("An orphaned feature ending with a number") |
51 | | - with open("foo/newsfragments/+orphaned_12.3.feature", "w") as f: |
52 | | - f.write("An orphaned feature ending with a dotted number") |
53 | | - # Towncrier ignores files that don't have a dot |
54 | | - with open("foo/newsfragments/README", "w") as f: |
55 | | - f.write("Blah blah") |
56 | | - # And files that don't have a valid category |
57 | | - with open("foo/newsfragments/README.rst", "w") as f: |
58 | | - f.write("**Blah blah**") |
59 | | - |
60 | 77 | result = runner.invoke(command, ["--draft", "--date", "01-01-2001"]) |
61 | 78 |
|
62 | 79 | self.assertEqual(0, result.exit_code, result.output) |
@@ -385,21 +402,58 @@ def run_order_scenario(sections, types): |
385 | 402 | ) |
386 | 403 |
|
387 | 404 | @with_git_project() |
388 | | - def test_draft_no_date(self, runner, commit): |
| 405 | + @with_fake_fragments(_default_fake_fragments) |
| 406 | + def test_draft_no_date_option_source_date_epoch(self, runner, commit): |
389 | 407 | """ |
390 | | - If no date is passed, today's date is used. |
| 408 | + If no date, and 'SOURCE_DATE_EPOCH' set, use it for build date. |
391 | 409 | """ |
392 | | - fragment_path1 = "foo/newsfragments/123.feature" |
393 | | - fragment_path2 = "foo/newsfragments/124.feature.rst" |
394 | | - with open(fragment_path1, "w") as f: |
395 | | - f.write("Adds levitation") |
396 | | - with open(fragment_path2, "w") as f: |
397 | | - f.write("Extends levitation") |
| 410 | + commit() |
398 | 411 |
|
| 412 | + fake_clock_datetime = datetime.datetime( |
| 413 | + 2015, |
| 414 | + 12, |
| 415 | + 24, |
| 416 | + 0, |
| 417 | + 0, |
| 418 | + 0, |
| 419 | + tzinfo=datetime.timezone.utc, |
| 420 | + ) |
| 421 | + fake_clock_timestamp = int(fake_clock_datetime.timestamp()) |
| 422 | + fake_clock_timestamp_text = str(fake_clock_timestamp) |
| 423 | + fake_os_environ = { |
| 424 | + **os.environ, |
| 425 | + BUILD_TIME_ENV_VAR_NAME: fake_clock_timestamp_text, |
| 426 | + } |
| 427 | + with patch("os.environ", fake_os_environ): |
| 428 | + result = runner.invoke(_main, ["--draft"]) |
| 429 | + |
| 430 | + self.assertEqual(0, result.exit_code) |
| 431 | + expected_date = fake_clock_datetime.date() |
| 432 | + expected_date_text = expected_date.strftime(ISO_8601_DATE_FORMAT) |
| 433 | + self.assertIn(f"Foo 1.2.3 ({expected_date_text})", result.output) |
| 434 | + |
| 435 | + @with_git_project() |
| 436 | + @with_fake_fragments(_default_fake_fragments) |
| 437 | + def test_draft_no_date_option_no_source_date_epoch( |
| 438 | + self, |
| 439 | + runner, |
| 440 | + commit, |
| 441 | + ): |
| 442 | + """ |
| 443 | + If no date option and no 'SOURCE_DATE_EPOCH', should use system clock. |
| 444 | + """ |
399 | 445 | commit() |
400 | 446 |
|
401 | | - today = date.today() |
402 | | - result = runner.invoke(_main, ["--draft"]) |
| 447 | + # Ensure the 'SOURCE_DATE_EPOCH' environment variable is not set. |
| 448 | + fake_os_environ = { |
| 449 | + key: value |
| 450 | + for key, value in os.environ.items() |
| 451 | + if (key != BUILD_TIME_ENV_VAR_NAME) |
| 452 | + } |
| 453 | + |
| 454 | + today = datetime.date.today() |
| 455 | + with patch("os.environ", fake_os_environ): |
| 456 | + result = runner.invoke(_main, ["--draft"]) |
403 | 457 |
|
404 | 458 | self.assertEqual(0, result.exit_code) |
405 | 459 | self.assertIn(f"Foo 1.2.3 ({today.isoformat()})", result.output) |
|
0 commit comments