Skip to content

Commit

Permalink
Generate loadable dummy .bzl/BUILD/WORKSPACE files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tetromino authored and copybara-github committed Mar 31, 2023
1 parent c024b8a commit 75dce1e
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.devtools.build.lib.testutil.BuildRuleBuilder;
import com.google.devtools.build.lib.testutil.Scratch;

import java.io.IOException;

/**
Expand All @@ -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.
Expand All @@ -50,6 +47,7 @@ public TestProjectBuilder() {
*/
public TestProjectBuilder(String workspace) {
this.workspace = workspace;
this.scratch = new Scratch(String.format("/%s", workspace));
}

/**
Expand All @@ -73,20 +71,32 @@ 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));
}

/**
* Generates the files necessary for the rule.
*/
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";
}
}
}

0 comments on commit 75dce1e

Please sign in to comment.