Skip to content

Commit d195dec

Browse files
committed
Make copyright year fakable in golden tests
We need this to avoid having golden tests fail when the year changes. Happy new year, everyone! Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 79fe15f commit d195dec

File tree

14 files changed

+37
-14
lines changed

14 files changed

+37
-14
lines changed

cookiecutter/local_extensions.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
project structure.
88
"""
99

10+
import datetime as _datetime
1011
import json as _json
1112
import os as _os
1213
import pathlib as _pathlib
@@ -40,6 +41,7 @@ def __init__(self, environment: _Environment):
4041
self.environment.globals.update(
4142
{
4243
"random_weekday": self._get_random_weekday(),
44+
"copyright_year": str(self._get_copyright_year()),
4345
}
4446
)
4547

@@ -90,6 +92,14 @@ def _build_identifier(self, repo_type: str, name: str, separator: str) -> str:
9092
middle = f"{repo_type}{separator}" if repo_type != "lib" else ""
9193
return f"frequenz{separator}{middle}{name}"
9294

95+
def _is_golden_testing(self) -> bool:
96+
"""Return `True` if we are running as part of a golden testing.
97+
98+
Returns:
99+
Whether we are running as part of a golden testing.
100+
"""
101+
return _os.environ.get("GOLDEN_TEST", None) is not None
102+
93103
def _get_from_json(self, key: str) -> str:
94104
"""Get a string from the cookiecutter.json file.
95105
@@ -109,7 +119,7 @@ def _get_random_weekday(self) -> str:
109119
A random weekday.
110120
"""
111121
# Make sure tests are reproduceable
112-
if _os.environ.get("PYTEST_CURRENT_TEST") is not None:
122+
if self._is_golden_testing():
113123
return "monday"
114124
return _random.choice(
115125
[
@@ -123,6 +133,19 @@ def _get_random_weekday(self) -> str:
123133
]
124134
)
125135

136+
def _get_copyright_year(self) -> int:
137+
"""Get the copyright year.
138+
139+
This will be faked when running the golden tests to make sure the tests are
140+
reproduceable.
141+
142+
Returns:
143+
The copyright year.
144+
"""
145+
if self._is_golden_testing():
146+
return 2023
147+
return _datetime.datetime.now().year
148+
126149
def _as_identifier_filter(self, name: str) -> str:
127150
"""Convert a name to a valid identifier.
128151

cookiecutter/{{cookiecutter.github_repo_name}}/.github/containers/nox-cross-arch/arm64-ubuntu-20.04-python-3.11.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# License: {{cookiecutter.license}}
2-
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
2+
# Copyright © {{copyright_year}} {{cookiecutter.author_name}}
33
{% raw -%}
44

55
# This Dockerfile is used to run the tests in arm64, which is not supported by

cookiecutter/{{cookiecutter.github_repo_name}}/.github/containers/nox-cross-arch/entrypoint.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# License: {{cookiecutter.license}}
3-
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
3+
# Copyright © {{copyright_year}} {{cookiecutter.author_name}}
44
{% raw -%}
55
set -e
66

cookiecutter/{{cookiecutter.github_repo_name}}/.github/containers/test-installation/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# License: {{cookiecutter.license}}
2-
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
2+
# Copyright © {{copyright_year}} {{cookiecutter.author_name}}
33
{% raw -%}
44

55
# This Dockerfile is used to test the installation of the python package in

cookiecutter/{{cookiecutter.github_repo_name}}/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
3+
Copyright © {{copyright_year}} {{cookiecutter.author_name}}
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

cookiecutter/{{cookiecutter.github_repo_name}}/docs/_scripts/macros.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# License: {{cookiecutter.license}}
2-
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
2+
# Copyright © {{copyright_year}} {{cookiecutter.author_name}}
33

44
"""This module defines macros for use in Markdown files."""
55

cookiecutter/{{cookiecutter.github_repo_name}}/docs/_scripts/mkdocstrings_autoapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# License: {{cookiecutter.license}}
2-
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
2+
# Copyright © {{copyright_year}} {{cookiecutter.author_name}}
33

44
"""Generate the code reference pages."""
55

cookiecutter/{{cookiecutter.github_repo_name}}/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
site_name: "{{cookiecutter.title}}"
66
site_description: "{{cookiecutter.description}}"
77
site_author: "{{cookiecutter.author_name}}"
8-
copyright: "Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}"
8+
copyright: "Copyright © {{copyright_year}} {{cookiecutter.author_name}}"
99
repo_name: "{{cookiecutter.github_repo_name}}"
1010
repo_url: "https://github.com/{{cookiecutter.github_org}}/{{cookiecutter.github_repo_name}}"
1111
# TODO(cookiecutter): "main" is the GitHub repo default branch, you might want to update it

cookiecutter/{{cookiecutter.github_repo_name}}/noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# License: {{cookiecutter.license}}
2-
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
2+
# Copyright © {{copyright_year}} {{cookiecutter.author_name}}
33

44
"""Configuration file for nox."""
55

cookiecutter/{{cookiecutter.github_repo_name}}/proto/frequenz/api/{{cookiecutter.name | as_identifier}}/{{cookiecutter.name | as_identifier}}.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// TODO(cookiecutter): Add a more descriptive package description.
66
//
77
// Copyright:
8-
// Copyright {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
8+
// Copyright {{copyright_year}} {{cookiecutter.author_name}}
99
//
1010
// License:
1111
// {{cookiecutter.license}}

0 commit comments

Comments
 (0)