Skip to content

Commit 6f59f7c

Browse files
tests: Use fixed copyright year in tests. (#4036)
The generator use the current year to generate copyright headers, so there are multiple tests failing when a new year comes. The general fix is to introduce an environment variable `TEST_CURRENT_YEAR` so that we can override it in tests. See below for the exact fix for each scenario: - Java generator golden unit tests - [Override it with Maven](https://github.com/googleapis/sdk-platform-java/blob/9ac79fc6c856a0b71111e28e2f69eeb0c01b4fc9/gapic-generator-java/pom.xml#L326-L328). - Java generator golden integration tests - Updated the golden files. These tests are generated from Bazel rules. Bazel builds are pretty hermetic and I couldn't find a way to override the environment variable. This is because the Bazel rule `java_gapic_library` [calls the generator](https://github.com/googleapis/sdk-platform-java/blob/b43f77c66d93d2423744d0f6d6a0a2a53a06e6d9/rules_java_gapic/java_gapic.bzl#L224-L235) through `proto_custom_library` which does not pass any environment variables. - Python hermetic build scripts unit tests. - Override it with [mocks](https://github.com/googleapis/sdk-platform-java/blob/9ac79fc6c856a0b71111e28e2f69eeb0c01b4fc9/hermetic_build/library_generation/tests/owlbot/java_unit_tests.py#L176). This requires a small refactoring in java.py. - Hermetic build scripts integration tests. - Changed these tests to non-required for now. These tests use a [hardcoded generator version](https://github.com/googleapis/sdk-platform-java/blob/b43f77c66d93d2423744d0f6d6a0a2a53a06e6d9/.cloudbuild/library_generation/cloudbuild-library-generation-integration-test.yaml#L69-L74) that does not recognize the environment variable. Once the generator is released, we can update the generator version and override the environment variable in CloudBuild yamls. - SpringCodeGen unit tests. - Once the generator is released, we can override the same environment variable with Maven in spring-cloud-gcp. Separately, updated zlib version in Bazel build because there is a known incompatibility between old zlib library and the latest macOS version. fixes: #3547 --------- Co-authored-by: cloud-java-bot <[email protected]>
1 parent b43f77c commit 6f59f7c

File tree

2,170 files changed

+2227
-2187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,170 files changed

+2227
-2187
lines changed

.bazeliskrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# See https://github.com/bazelbuild/bazelisk
22
# Version required for Java 24 support (https://github.com/bazelbuild/bazel/commit/806a6e82320956b63f1351ebe2b0da8483f36f19).
3-
USE_BAZEL_VERSION=7.6.1
3+
USE_BAZEL_VERSION=7.7.0

WORKSPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ com_google_api_gax_java_repositories()
2424

2525
_googleapis_commit = "7438480b2a1bc6371d748e974f7a3647f90c4e8d"
2626

27+
# This is required for local testing on macOS. There is known incompatibility between old zlib and the latest macOS (Sequoia 15.7.3 as of writing)
28+
http_archive(
29+
name = "zlib",
30+
build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
31+
sha256 = "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23",
32+
strip_prefix = "zlib-1.3.1",
33+
urls = ["https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz"],
34+
)
35+
2736
http_archive(
2837
name = "com_google_googleapis",
2938
strip_prefix = "googleapis-%s" % _googleapis_commit,

gapic-generator-java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<googleapis.commit>9fcfbea0aa5b50fa22e190faceb073d74504172b</googleapis.commit>
1818
<maven.compiler.source>1.8</maven.compiler.source>
1919
<maven.compiler.target>1.8</maven.compiler.target>
20+
<currentYearOverride>2025</currentYearOverride>
2021
</properties>
2122

2223
<parent>
@@ -323,6 +324,10 @@
323324
target/generated-test-resources/protobuf/descriptor-sets
324325
</additionalClasspathElement>
325326
</additionalClasspathElements>
327+
<!-- Set CURRENT_YEAR_OVERRIDE to a fixed year so the unit test result is consistent -->
328+
<environmentVariables>
329+
<CURRENT_YEAR_OVERRIDE>${currentYearOverride}</CURRENT_YEAR_OVERRIDE>
330+
</environmentVariables>
326331
</configuration>
327332
</plugin>
328333

gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/comment/CommentComposer.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.api.generator.engine.ast.CommentStatement;
1919
import com.google.api.generator.engine.ast.LineComment;
2020
import com.google.api.generator.engine.ast.Statement;
21+
import com.google.common.base.Strings;
2122
import java.util.Arrays;
2223
import java.util.Calendar;
2324
import java.util.List;
@@ -35,7 +36,7 @@ public class CommentComposer {
3536
+ "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
3637
+ "See the License for the specific language governing permissions and\n"
3738
+ "limitations under the License.",
38-
Calendar.getInstance().get(Calendar.YEAR));
39+
getCurrentYear());
3940

4041
private static final String AUTO_GENERATED_CLASS_DISCLAIMER_STRING =
4142
"AUTO-GENERATED DOCUMENTATION AND CLASS.";
@@ -78,4 +79,20 @@ public class CommentComposer {
7879
CommentStatement.withComment(
7980
LineComment.withComment(
8081
"https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library")));
82+
83+
// This environment variable is mainly used to override the current year to a fixed year in tests
84+
// so we don't have to update golden tests every year.
85+
// This does not work for golden integration tests that are generated from Bazel rules. Because
86+
// the Bazel rule java_gapic_library calls the generator through proto_custom_library which does
87+
// not pass any environment variables. Golden integration tests still need to be updated every
88+
// year.
89+
private static final String CURRENT_YEAR_OVERRIDE = "CURRENT_YEAR_OVERRIDE";
90+
91+
private static String getCurrentYear() {
92+
String testCurrentYear = System.getenv(CURRENT_YEAR_OVERRIDE);
93+
int currentYearFromCalendar = Calendar.getInstance().get(Calendar.YEAR);
94+
return Strings.isNullOrEmpty(testCurrentYear)
95+
? String.valueOf(currentYearFromCalendar)
96+
: testCurrentYear;
97+
}
8198
}

hermetic_build/library_generation/owlbot/synthtool/languages/java.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,9 @@
2727
logger = logging.getLogger()
2828
logger.setLevel(logging.DEBUG)
2929

30-
3130
JAR_DOWNLOAD_URL = "https://github.com/google/google-java-format/releases/download/google-java-format-{version}/google-java-format-{version}-all-deps.jar"
3231
DEFAULT_FORMAT_VERSION = "1.7"
33-
CURRENT_YEAR = date.today().year
34-
GOOD_LICENSE = f"""/*
35-
* Copyright {CURRENT_YEAR} Google LLC
36-
*
37-
* Licensed under the Apache License, Version 2.0 (the "License");
38-
* you may not use this file except in compliance with the License.
39-
* You may obtain a copy of the License at
40-
*
41-
* https://www.apache.org/licenses/LICENSE-2.0
42-
*
43-
* Unless required by applicable law or agreed to in writing, software
44-
* distributed under the License is distributed on an "AS IS" BASIS,
45-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46-
* See the License for the specific language governing permissions and
47-
* limitations under the License.
48-
*/
49-
"""
32+
5033
PROTOBUF_HEADER = "// Generated by the protocol buffer compiler. DO NOT EDIT!"
5134
BAD_LICENSE = """/\\*
5235
\\* Copyright \\d{4} Google LLC
@@ -71,6 +54,27 @@
7154
LIBRARY_VERSION_ENV_KEY = "SYNTHTOOL_LIBRARY_VERSION"
7255

7356

57+
def _get_good_license():
58+
current_year = int(os.getenv("CURRENT_YEAR_OVERRIDE", date.today().year))
59+
good_license = f"""/*
60+
* Copyright {current_year} Google LLC
61+
*
62+
* Licensed under the Apache License, Version 2.0 (the "License");
63+
* you may not use this file except in compliance with the License.
64+
* You may obtain a copy of the License at
65+
*
66+
* https://www.apache.org/licenses/LICENSE-2.0
67+
*
68+
* Unless required by applicable law or agreed to in writing, software
69+
* distributed under the License is distributed on an "AS IS" BASIS,
70+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
71+
* See the License for the specific language governing permissions and
72+
* limitations under the License.
73+
*/
74+
"""
75+
return good_license
76+
77+
7478
def _file_has_header(path: Path) -> bool:
7579
"""Return true if the file already contains a license header."""
7680
with open(path, "rt") as fp:
@@ -100,13 +104,13 @@ def fix_proto_headers(proto_root: Path) -> None:
100104
s.replace(
101105
_filter_no_header([proto_root / "src/**/*.java"]),
102106
PROTOBUF_HEADER,
103-
f"{GOOD_LICENSE}{PROTOBUF_HEADER}",
107+
f"{_get_good_license()}{PROTOBUF_HEADER}",
104108
)
105109
# https://github.com/googleapis/gapic-generator/issues/3074
106110
s.replace(
107111
[proto_root / "src/**/*Name.java", proto_root / "src/**/*Names.java"],
108112
BAD_LICENSE,
109-
GOOD_LICENSE,
113+
_get_good_license(),
110114
)
111115

112116

@@ -118,7 +122,7 @@ def fix_grpc_headers(grpc_root: Path, package_name: str = "unused") -> None:
118122
s.replace(
119123
_filter_no_header([grpc_root / "src/**/*.java"]),
120124
"^package (.*);",
121-
f"{GOOD_LICENSE}package \\1;",
125+
f"{_get_good_license()}package \\1;",
122126
)
123127

124128

hermetic_build/library_generation/tests/owlbot/java_unit_tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
TEST_OWLBOT = Path(__file__).parent.parent / "resources" / "test-owlbot"
2828
FIXTURES = Path(__file__).parent.parent / "resources" / "test-owlbot" / "fixtures"
2929
TEMPLATES_PATH = Path(__file__).parent.parent.parent / "owlbot" / "templates"
30+
CURRENT_YEAR_OVERRIDE = "2025"
3031

3132
SAMPLE_METADATA = """
3233
<metadata>
@@ -172,6 +173,7 @@ def test_deprecate_method(self):
172173
)
173174
os.chdir(cwd)
174175

176+
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
175177
def test_fix_proto_license(self):
176178
with tempfile.TemporaryDirectory() as tempdir:
177179
cwd = os.getcwd()
@@ -188,6 +190,7 @@ def test_fix_proto_license(self):
188190
)
189191
os.chdir(cwd)
190192

193+
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
191194
def test_fix_proto_license_idempotent(self):
192195
with tempfile.TemporaryDirectory() as tempdir:
193196
cwd = os.getcwd()
@@ -206,6 +209,7 @@ def test_fix_proto_license_idempotent(self):
206209
)
207210
os.chdir(cwd)
208211

212+
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
209213
def test_fix_grpc_license(self):
210214
with tempfile.TemporaryDirectory() as tempdir:
211215
cwd = os.getcwd()
@@ -222,6 +226,7 @@ def test_fix_grpc_license(self):
222226
)
223227
os.chdir(cwd)
224228

229+
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
225230
def test_fix_grpc_license_idempotent(self):
226231
with tempfile.TemporaryDirectory() as tempdir:
227232
cwd = os.getcwd()

java-common-protos/grpc-google-common-protos/src/main/java/com/google/cloud/location/LocationsGrpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Google LLC
2+
* Copyright 2026 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

java-common-protos/grpc-google-common-protos/src/main/java/com/google/longrunning/OperationsGrpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Google LLC
2+
* Copyright 2026 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

java-common-protos/proto-google-common-protos/src/main/java/com/google/api/Advice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Google LLC
2+
* Copyright 2026 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

java-common-protos/proto-google-common-protos/src/main/java/com/google/api/AdviceOrBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Google LLC
2+
* Copyright 2026 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)