Make formatBuildTimestamp more flexible when a template that accesses stamping information is run without stamping enabled. (#207)
I had forgotten to push this commit to PR #205, sorry!
diff --git a/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java b/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java
index c486f57..6db2f43 100644
--- a/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java
+++ b/src/main/java/com/google/devtools/build/skydoc/rendering/MarkdownUtil.java
@@ -14,6 +14,7 @@
package com.google.devtools.build.skydoc.rendering;
+import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.joining;
@@ -451,6 +452,17 @@
* UTC`
*/
public String formatBuildTimestamp(String buildTimestampSeconds, String zoneId, String format) {
+ // If stamp is not set to True in the stardoc() rule, then $stamping.volatile.BUILD_TIMESTAMP
+ // will be null, so return the empty string rather than crash. Alternatively, if this function
+ // is called as:
+ //
+ // $util.formatBuildTimestamp("$!stamping.volatile.BUILD_TIMESTAMP", "UTC", "yyyy MMM dd,
+ // HH:mm")
+ //
+ // then buildTimestampSeconds will be the empty string, so return the empty string too.
+ if (isNullOrEmpty(buildTimestampSeconds)) {
+ return "";
+ }
return Instant.ofEpochMilli(Long.parseLong(buildTimestampSeconds) * 1000)
.atZone(ZoneId.of(zoneId))
.format(DateTimeFormatter.ofPattern(format));
diff --git a/test/BUILD b/test/BUILD
index 1f5c2f4..035bc87 100644
--- a/test/BUILD
+++ b/test/BUILD
@@ -301,6 +301,15 @@
test_legacy_extractor = False,
)
+stardoc_test(
+ name = "stamping_with_stamping_off_test",
+ golden_file = "testdata/stamping_test/golden_stamping_off.md",
+ header_template = "testdata/stamping_test/stamping_header_stamping_off.vm",
+ input_file = "testdata/stamping_test/input.bzl",
+ stamp = False,
+ test_legacy_extractor = False,
+)
+
sh_test(
name = "local_repository_test_e2e_test",
srcs = ["diff_test_runner.sh"],
diff --git a/test/testdata/stamping_test/golden_stamping_off.md b/test/testdata/stamping_test/golden_stamping_off.md
new file mode 100644
index 0000000..525a3f6
--- /dev/null
+++ b/test/testdata/stamping_test/golden_stamping_off.md
@@ -0,0 +1,6 @@
+This Stardoc was built on ``.
+
+This Stardoc was built on ``.
+
+Host: ``.
+
diff --git a/test/testdata/stamping_test/stamping_header_stamping_off.vm b/test/testdata/stamping_test/stamping_header_stamping_off.vm
new file mode 100644
index 0000000..860b5e5
--- /dev/null
+++ b/test/testdata/stamping_test/stamping_header_stamping_off.vm
@@ -0,0 +1,5 @@
+This Stardoc was built on `$util.formatBuildTimestamp($stamping.volatile.BUILD_TIMESTAMP, "UTC", "yyyy MMM dd, HH:mm")`.
+
+This Stardoc was built on `$util.formatBuildTimestamp("$!stamping.volatile.BUILD_TIMESTAMP", "UTC", "yyyy MMM dd, HH:mm")`.
+
+Host: `$!stamping.stable.BUILD_HOST`.