Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mini_tar and switch to it for our blackbox tests. #14935

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@
import java.util.stream.Collectors;
import org.junit.Test;

/** Tests pkg_tar and http_archive. */
/** Tests mini_tar and http_archive. */
public class BazelEmbeddedStarlarkBlackBoxTest extends AbstractBlackBoxTest {

private static final String HELLO_FROM_EXTERNAL_REPOSITORY = "Hello from external repository!";
private static final String HELLO_FROM_MAIN_REPOSITORY = "Hello from main repository!";

@Test
public void testPkgTar() throws Exception {
public void testMiniTar() throws Exception {
context().write("main/WORKSPACE", BlackBoxTestEnvironment.getWorkspaceWithDefaultRepos());
context().write("main/foo.txt", "Hello World");
context().write("main/bar.txt", "Hello World, again");
context()
.write(
"main/BUILD",
"load(\"@bazel_tools//tools/build_defs/pkg:pkg.bzl\", \"pkg_tar\")",
"pkg_tar(name = \"data\", srcs = ['foo.txt', 'bar.txt'],)");
"load(\"@bazel_tools//tools/build_defs/pkg:tar.bzl\", \"mini_tar\")",
"mini_tar(name = \"data\", srcs = ['foo.txt', 'bar.txt'],)");

BuilderRunner bazel = bazel();
bazel.build("...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* <p>empty WORKSPACE file,
*
* <p>BUILD file, with write_to_file target and pkg_tar target for packing the contents of the
* <p>BUILD file, with write_to_file target and mini_tar target for packing the contents of the
* generated repository.
*
* <p>Intended to be used by workspace tests.
Expand Down Expand Up @@ -130,10 +130,10 @@ Path setupRepository() throws IOException {
PathUtils.writeFileInDir(
root,
"BUILD",
"load(\"@bazel_tools//tools/build_defs/pkg:pkg.bzl\", \"pkg_tar\")",
"load(\"@bazel_tools//tools/build_defs/pkg:tar.bzl\", \"mini_tar\")",
loadRule(""),
callRule(target, outFile, outputText),
String.format("pkg_tar(name = \"%s\", srcs = glob([\"*\"]),)", getPkgTarTarget()));
String.format("mini_tar(name = \"%s\", srcs = glob([\"*\"]),)", getPkgTarTarget()));
}
return workspace.getParent();
}
Expand Down Expand Up @@ -162,8 +162,8 @@ static String callRule(String name, String filename, String text) {
"%s(name = '%s', filename = '%s', text ='%s')", RULE_NAME, name, filename, text);
}

/** @return name of the generated pkg_tar target */
/** @return name of the generated mini_tar target */
String getPkgTarTarget() {
return "pkg_tar_" + target;
return "mini_tar_" + target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
@RunWith(JUnit4.class)
public class RepoWithRuleWritingTextGeneratorTest {
private static final String BUILD_TEXT =
"load(\"@bazel_tools//tools/build_defs/pkg:pkg.bzl\", \"pkg_tar\")\n"
"load(\"@bazel_tools//tools/build_defs/pkg:tar.bzl\", \"mini_tar\")\n"
+ "load('//:helper.bzl', 'write_to_file')\n"
+ "write_to_file(name = 'write_text', filename = 'out', text ='HELLO')\n"
+ "pkg_tar(name = \"pkg_tar_write_text\", srcs = glob([\"*\"]),)";
+ "mini_tar(name = \"mini_tar_write_text\", srcs = glob([\"*\"]),)";
private static final String BUILD_TEXT_PARAMS =
"load(\"@bazel_tools//tools/build_defs/pkg:pkg.bzl\", \"pkg_tar\")\n"
"load(\"@bazel_tools//tools/build_defs/pkg:tar.bzl\", \"mini_tar\")\n"
+ "load('//:helper.bzl', 'write_to_file')\n"
+ "write_to_file(name = 'target', filename = 'file', text ='text')\n"
+ "pkg_tar(name = \"pkg_tar_target\", srcs = glob([\"*\"]),)";
+ "mini_tar(name = \"mini_tar_target\", srcs = glob([\"*\"]),)";

@Test
public void testOutput() throws IOException {
Expand All @@ -51,7 +51,7 @@ public void testOutput() throws IOException {

String buildText = String.join("\n", PathUtils.readFile(repository.resolve("BUILD")));
assertThat(buildText).isEqualTo(BUILD_TEXT);
assertThat(generator.getPkgTarTarget()).isEqualTo("pkg_tar_write_text");
assertThat(generator.getPkgTarTarget()).isEqualTo("mini_tar_write_text");
} finally {
PathUtils.deleteTree(directory);
}
Expand All @@ -73,7 +73,7 @@ public void testOutputWithParameters() throws IOException {

String buildText = String.join("\n", PathUtils.readFile(repository.resolve("BUILD")));
assertThat(buildText).isEqualTo(BUILD_TEXT_PARAMS);
assertThat(generator.getPkgTarTarget()).isEqualTo("pkg_tar_target");
assertThat(generator.getPkgTarTarget()).isEqualTo("mini_tar_target");
} finally {
PathUtils.deleteTree(directory);
}
Expand Down
87 changes: 87 additions & 0 deletions tools/build_defs/pkg/tar.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""mini_tar: A limited functionality tar utility."""

load(":path.bzl", "compute_data_path", "dest_path")

# Filetype to restrict inputs
tar_filetype = [".tar", ".tar.gz", ".tgz", ".tar.bz2"]

def _quote(filename, protect = "="):
"""Quote the filename, by escaping = by \\= and \\ by \\\\"""
return filename.replace("\\", "\\\\").replace(protect, "\\" + protect)

def _mini_tar_impl(ctx):
"""Implementation of the mini_tar rule."""

# Compute the relative path
data_path = compute_data_path(ctx.outputs.out, ctx.attr.strip_prefix)

# Start building the arguments.
args = [
"--output=" + ctx.outputs.out.path,
"--mode=" + ctx.attr.mode,
"--owner=" + ctx.attr.owner,
]
if ctx.attr.package_dir:
args.append("--directory=" + ctx.attr.package_dir)
if ctx.attr.mtime != -1: # Note: Must match default in rule def.
args.append("--mtime=%d" % ctx.attr.mtime)

file_inputs = ctx.files.srcs[:]
args += [
"--file=%s=%s" % (_quote(f.path), dest_path(f, data_path))
for f in file_inputs
]
arg_file = ctx.actions.declare_file(ctx.label.name + ".args")
ctx.actions.write(arg_file, "\n".join(args))
ctx.actions.run(
inputs = file_inputs + [arg_file],
executable = ctx.executable.build_tar,
arguments = ["--flagfile", arg_file.path],
outputs = [ctx.outputs.out],
mnemonic = "PackageTar",
use_default_shell_env = True,
)

# A rule for creating a tar file, see README.md
_real_mini_tar = rule(
implementation = _mini_tar_impl,
attrs = {
"mode": attr.string(default = "0555"),
"mtime": attr.int(default = -1),
"out": attr.output(),
"owner": attr.string(default = "0.0"),
"package_dir": attr.string(),
"srcs": attr.label_list(allow_files = True),
"strip_prefix": attr.string(),

# Implicit dependencies.
"build_tar": attr.label(
default = Label("//tools/build_defs/pkg:build_tar"),
cfg = "exec",
executable = True,
allow_files = True,
),
},
)

def mini_tar(name, out = None, **kwargs):
if not out:
out = name + ".tar"
_real_mini_tar(
name = name,
out = out,
**kwargs
)