Skip to content

Commit

Permalink
[singlejar] Use Bazel runfiles lib in test and other Windows fixes
Browse files Browse the repository at this point in the history
See #2241

Closes #6251.

PiperOrigin-RevId: 219810346
  • Loading branch information
rongjiecomputer authored and Copybara-Service committed Nov 2, 2018
1 parent f445af5 commit 4b53e8f
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 103 deletions.
31 changes: 11 additions & 20 deletions src/tools/singlejar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ cc_test(
":zlib_interface",
],
tags = [
# https://github.com/bazelbuild/bazel/issues/6512
# TODO(laszlocsomor) Enable this test when BuildKite uses Bazel that includes
# https://github.com/bazelbuild/bazel/commit/914b4ce14624171a97ff8b41f9202058f10d15b2
"no_windows",
],
deps = [
Expand All @@ -117,11 +118,6 @@ cc_test(
data = [
"data/empty.zip",
],
tags = [
# TODO(@rongjiecomputer): make this work on Windows as part of
# https://github.com/bazelbuild/bazel/issues/2241
"no_windows",
],
deps = [
":input_jar",
":test_util",
Expand All @@ -132,17 +128,13 @@ cc_test(

cc_test(
name = "input_jar_preambled_test",
size = "large",
srcs = [
"input_jar_preambled_test.cc",
],
data = [
":test1",
],
tags = [
# TODO(@rongjiecomputer): make this work on Windows as part of
# https://github.com/bazelbuild/bazel/issues/2241
"no_windows",
],
deps = [
":input_jar",
":test_util",
Expand All @@ -158,7 +150,10 @@ cc_test(
"input_jar_scan_entries_test.h",
"input_jar_scan_jartool_test.cc",
],
copts = ["-DJAR_TOOL_PATH=\\\"$(JAVABASE)/bin/jar\\\""],
copts = select({
"//src/conditions:windows": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar.exe\\\""],
"//conditions:default": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar\\\""],
}),
data = ["@bazel_tools//tools/jdk:current_java_runtime"],
# Timing out, see https://github.com/bazelbuild/bazel/issues/1555
tags = ["manual"],
Expand Down Expand Up @@ -228,9 +223,10 @@ cc_test(
srcs = [
"output_jar_simple_test.cc",
],
# TODO(@rongjiecomputer): update copts to handle Windows and add
# ".exe" extension.
copts = ["-DJAR_TOOL_PATH=\\\"$(JAVABASE)/bin/jar\\\""],
copts = select({
"//src/conditions:windows": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar.exe\\\""],
"//conditions:default": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar\\\""],
}),
data = [
":data1",
":data2",
Expand All @@ -239,11 +235,6 @@ cc_test(
":test2",
"@bazel_tools//tools/jdk:current_java_runtime",
],
tags = [
# TODO(@rongjiecomputer): make this work on Windows as part of
# https://github.com/bazelbuild/bazel/issues/2241
"no_windows",
],
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
deps = [
":input_jar",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/combiners_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CombinersTest : public ::testing::Test {
ASSERT_EQ(0, system("zip -qm combiners.zip tag1.xml tag2.xml"));
}

static void TearDownTestCase() { system("rm -f xmls.zip"); }
static void TearDownTestCase() { remove("xmls.zip"); }

static bool CreateFile(const char *filename, const char *contents) {
FILE *fp = fopen(filename, "wb");
Expand Down
21 changes: 13 additions & 8 deletions src/tools/singlejar/input_jar_empty_jar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/

#include <errno.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <memory>
#include <string>

Expand All @@ -36,17 +38,15 @@
#include "src/tools/singlejar/test_util.h"
#include "googletest/include/gtest/gtest.h"

using singlejar_test_util::OutputFilePath;
using bazel::tools::cpp::runfiles::Runfiles;
using singlejar_test_util::AllocateFile;
using singlejar_test_util::OutputFilePath;
using singlejar_test_util::RunCommand;

namespace {

#if !defined(DATA_DIR_TOP)
#define DATA_DIR_TOP
#endif

const char kEmptyJar[] = DATA_DIR_TOP "src/tools/singlejar/data/empty.zip";
const char kEmptyJar[] =
"io_bazel/src/tools/singlejar/data/empty.zip";

void VerifyEmpty(const std::string &jar_path) {
InputJar input_jar;
Expand All @@ -61,14 +61,19 @@ void VerifyEmpty(const std::string &jar_path) {

// Check that empty zip file (i.e., a valid zip file with no entries) is
// handled correctly.
TEST(InputJarBadjarTest, EmptyZipFile) { VerifyEmpty(kEmptyJar); }
TEST(InputJarBadjarTest, EmptyZipFile) {
std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
VerifyEmpty(runfiles->Rlocation(kEmptyJar).c_str());
}

// Preambled empty zip.
TEST(InputJarPreambledTest, Empty) {
std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
std::string out_path = OutputFilePath("empty.jwp");
std::string exe_path = OutputFilePath("exe");
ASSERT_TRUE(AllocateFile(exe_path, 100));
ASSERT_EQ(0, RunCommand("cat", exe_path.c_str(), kEmptyJar, ">",
ASSERT_EQ(0, RunCommand("cat", exe_path.c_str(),
runfiles->Rlocation(kEmptyJar).c_str(), ">",
out_path.c_str(), nullptr));
VerifyEmpty(out_path);
}
Expand Down
18 changes: 13 additions & 5 deletions src/tools/singlejar/input_jar_preambled_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/

#include <errno.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <memory>
#include <string>

Expand All @@ -38,9 +40,7 @@

namespace {

#if !defined(DATA_DIR_TOP)
#define DATA_DIR_TOP
#endif
using bazel::tools::cpp::runfiles::Runfiles;

void Verify(const std::string &path) {
InputJar input_jar;
Expand All @@ -65,28 +65,36 @@ void Verify(const std::string &path) {

// Archive not containing 64-bit End of Central Directory/Locator with preamble.
TEST(InputJarPreambledTest, Small) {
std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
std::string out_path = singlejar_test_util::OutputFilePath("out.jwp");
std::string exe_path = singlejar_test_util::OutputFilePath("exe");
ASSERT_TRUE(singlejar_test_util::AllocateFile(exe_path, 100));
ASSERT_EQ(
0,
singlejar_test_util::RunCommand(
"cat", exe_path.c_str(),
DATA_DIR_TOP "src/tools/singlejar/libtest1.jar",
runfiles
->Rlocation(
"io_bazel/src/tools/singlejar/libtest1.jar")
.c_str(),
">", out_path.c_str(), nullptr));
Verify(out_path);
}

// Same as above with zip -A applied to the file.
TEST(InputJarPreambledTest, SmallAdjusted) {
std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
std::string out_path = singlejar_test_util::OutputFilePath("out.jwp");
std::string exe_path = singlejar_test_util::OutputFilePath("exe");
ASSERT_TRUE(singlejar_test_util::AllocateFile(exe_path, 100));
ASSERT_EQ(
0,
singlejar_test_util::RunCommand(
"cat", exe_path.c_str(),
DATA_DIR_TOP "src/tools/singlejar/libtest1.jar",
runfiles
->Rlocation(
"io_bazel/src/tools/singlejar/libtest1.jar")
.c_str(),
">", out_path.c_str(), nullptr));
ASSERT_EQ(0, singlejar_test_util::RunCommand("zip", "-A", out_path.c_str(),
nullptr));
Expand Down
12 changes: 11 additions & 1 deletion src/tools/singlejar/input_jar_scan_entries_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#define BAZEL_SRC_TOOLS_SINGLEJAR_INPUT_JAR_SCAN_ENTRIES_TEST_H_ 1

#include <errno.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <memory>
#include <string>

Expand Down Expand Up @@ -84,7 +86,11 @@ class InputJarScanEntries : public testing::Test {
for (int dir = 0; dir < 256; ++dir) {
char dirname[10];
snprintf(dirname, sizeof(dirname), "dir%d", dir);
#ifdef _WIN32
ASSERT_EQ(0, mkdir(dirname));
#else
ASSERT_EQ(0, mkdir(dirname, 0777));
#endif
for (int file = 0; file < 256; ++file) {
char filepath[20];
snprintf(filepath, sizeof(filepath), "%s/%d", dirname, file);
Expand All @@ -94,8 +100,12 @@ class InputJarScanEntries : public testing::Test {
ASSERT_EQ(0, ZipCreator::Jar(false, kJar, "dir*", nullptr));
for (int dir = 0; dir < 256; ++dir) {
char rmdircmd[100];
#ifdef _WIN32
snprintf(rmdircmd, sizeof(rmdircmd), "rmdir /S /Q dir%d", dir);
#else
snprintf(rmdircmd, sizeof(rmdircmd), "rm dir%d/* && rmdir dir%d", dir,
dir);
#endif
ASSERT_EQ(0, system(rmdircmd));
}
}
Expand Down Expand Up @@ -234,7 +244,7 @@ TYPED_TEST_P(InputJarScanEntries, TestZip64) {
TYPED_TEST_P(InputJarScanEntries, LotsOfEntries) {
ASSERT_EQ(0, chdir(getenv("TEST_TMPDIR")));
this->CreateJarWithLotsOfEntries();
#if !defined(__APPLE__)
#if !defined(__APPLE__) && !defined(_WIN32)
const char kTailUnzip[] = "unzip -v jar.jar | tail";
ASSERT_EQ(0, system(kTailUnzip)) << "Failed command: " << kTailUnzip;
#endif
Expand Down
29 changes: 13 additions & 16 deletions src/tools/singlejar/input_jar_scan_jartool_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,26 @@

/* Verify that InputJar can scan zip/jar files created by JDK's jar tool. */

#include <stdarg.h>
#include <stdlib.h>
#include "src/tools/singlejar/port.h"

// Suppress [build/include_order] linter errors, because port.h must be the
// topmost header file as it defines types used by later-included headers.
#include <stdarg.h> // NOLINT
#include <stdlib.h> // NOLINT

#include "src/tools/singlejar/input_jar_scan_entries_test.h"

#if !defined(JAR_TOOL_PATH)
#error "The path to jar tool has to be defined via -DJAR_TOOL_PATH="
#endif
using bazel::tools::cpp::runfiles::Runfiles;

class JartoolCreator {
public:
static void SetUpTestCase() {
jar_path_ = realpath(JAR_TOOL_PATH, nullptr);
if (!jar_path_) {
// At least show what's available.
system("ls -1R");
}
ASSERT_NE(nullptr, jar_path_);
std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
static std::string jar_path_str = runfiles->Rlocation(JAR_TOOL_PATH);
jar_path_ = jar_path_str.c_str();
}

static void TearDownTestCase() {
free(jar_path_);
}
static void TearDownTestCase() {}

static int Jar(bool compress, const char *output_jar, ...) {
std::string command(jar_path_);
Expand All @@ -55,10 +52,10 @@ class JartoolCreator {
}
return system(command.c_str());
}
static char * jar_path_;
static const char *jar_path_;
};

char *JartoolCreator::jar_path_;
const char *JartoolCreator::jar_path_ = nullptr;

typedef testing::Types<JartoolCreator> Creators;
INSTANTIATE_TYPED_TEST_CASE_P(Jartool, InputJarScanEntries, Creators);
17 changes: 11 additions & 6 deletions src/tools/singlejar/output_huge_jar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@

namespace {

using bazel::tools::cpp::runfiles::Runfiles;
using singlejar_test_util::AllocateFile;
using singlejar_test_util::OutputFilePath;
using singlejar_test_util::VerifyZip;

using std::string;

#if !defined(DATA_DIR_TOP)
#define DATA_DIR_TOP
#endif

class OutputHugeJarTest : public ::testing::Test {
protected:
void SetUp() override { runfiles.reset(Runfiles::CreateForTest()); }

void CreateOutput(const string &out_path, const std::vector<string> &args) {
const char *option_list[100] = {"--output", out_path.c_str()};
int nargs = 2;
Expand All @@ -60,6 +59,7 @@ class OutputHugeJarTest : public ::testing::Test {

OutputJar output_jar_;
Options options_;
std::unique_ptr<Runfiles> runfiles;
};

TEST_F(OutputHugeJarTest, EntryAbove4G) {
Expand All @@ -70,8 +70,13 @@ TEST_F(OutputHugeJarTest, EntryAbove4G) {
ASSERT_TRUE(AllocateFile(launcher_path, 0x100000010));

string out_path = OutputFilePath("out.jar");
CreateOutput(out_path, {"--java_launcher", launcher_path, "--sources",
DATA_DIR_TOP "src/tools/singlejar/libtest1.jar"});
CreateOutput(
out_path,
{"--java_launcher", launcher_path, "--sources",
runfiles
->Rlocation(
"io_bazel/src/tools/singlejar/libtest1.jar")
.c_str()});
}

} // namespace
Loading

0 comments on commit 4b53e8f

Please sign in to comment.