From 75dce1eefd6139b8a395d9ac6ece211e809efe78 Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 30 Mar 2023 18:15:16 -0700 Subject: [PATCH] Generate loadable dummy .bzl/BUILD/WORKSPACE files Until now, dummy files were saved in the wrong place by default (outside the test's scratch workspace); and had the string "dummy" as the content, regardless of file extension. This broken behavior somehow worked; presumably, until now, all tests using TestProjectBuilder performed no I/O at analysis time on their dummy files. The situation is different for .bzl/BUILD/WORKSPACE files: a rule (such as the new stardoc rules) may attempt to load and parse the file during the analysis phase. So we must take care to generate something that parses as valid Starlark and is saved under the right path. PiperOrigin-RevId: 520794401 Change-Id: Id12cbcd2898a505cf4d1cf50e254a35603d466d5 --- .../util/TestProjectBuilder.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/google/devtools/build/lib/generatedprojecttest/util/TestProjectBuilder.java b/src/test/java/com/google/devtools/build/lib/generatedprojecttest/util/TestProjectBuilder.java index ee32d724e6554a..f61211b617ed49 100644 --- a/src/test/java/com/google/devtools/build/lib/generatedprojecttest/util/TestProjectBuilder.java +++ b/src/test/java/com/google/devtools/build/lib/generatedprojecttest/util/TestProjectBuilder.java @@ -16,7 +16,6 @@ import com.google.devtools.build.lib.testutil.BuildRuleBuilder; import com.google.devtools.build.lib.testutil.Scratch; - import java.io.IOException; /** @@ -33,10 +32,8 @@ public final class TestProjectBuilder { // The directory name to use for the workspace. private final String workspace; - /** - * Provides functionality to create and manipulate a scratch file system. - */ - private final Scratch scratch = new Scratch(); + /** Provides functionality to create and manipulate a scratch file system. */ + private final Scratch scratch; /** * Creates a builder that will use the default workspace name as the directory. @@ -50,6 +47,7 @@ public TestProjectBuilder() { */ public TestProjectBuilder(String workspace) { this.workspace = workspace; + this.scratch = new Scratch(String.format("/%s", workspace)); } /** @@ -73,12 +71,9 @@ public Scratch getScratch() { return this.scratch; } - /** - * Creates a dummy file with 'dummy' as content in the given package with the given name. - * @throws IOException - */ + /** Creates a dummy file with dummy content in the given package with the given name. */ public void createDummyFileInDir(String pkg, String fileName) throws IOException { - scratch.file(String.format("%s/%s", pkg, fileName), "dummy"); + scratch.file(String.format("%s/%s", pkg, fileName), dummyContentFor(fileName)); } /** @@ -86,7 +81,22 @@ public void createDummyFileInDir(String pkg, String fileName) throws IOException */ public void createFilesToGenerate(BuildRuleBuilder ruleBuilder) throws IOException { for (String file : ruleBuilder.getFilesToGenerate()) { - scratch.file(file, "dummy"); + scratch.file(file, dummyContentFor(file)); + } + } + + /** Generates dummy content for a file based on its name and extension. */ + private static String dummyContentFor(String filePath) { + String fileName = filePath.substring(filePath.lastIndexOf('/') + 1); + String extension = fileName.substring(fileName.lastIndexOf('.') + 1); + if (extension.equals("bzl") + || fileName.equals("BUILD") + || fileName.equals("BUILD.bazel") + || fileName.equals("WORKSPACE") + || fileName.equals("WORKSPACE.bazel")) { + return "# dummy"; + } else { + return "dummy"; } } }