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

Refactor git_repository and new_git_repository rules implementations … #8264

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -224,6 +224,26 @@ public BuilderRunner bazel() {
workDir, binaryPath, getProcessTimeoutMillis(-1), commonEnv, executorService);
}

/**
* Runs external binary in the specified working directory. See {@link BuilderRunner}
*
* @param workingDirectory - working directory for running the binary
* @param processToRun - path to the binary to run
* @param arguments - arguments to pass to the binary
* @return ProcessResult - execution result
*/
public ProcessResult runBinary(Path workingDirectory, String processToRun, String... arguments)
throws Exception {
ProcessParameters parameters =
ProcessParameters.builder()
.setWorkingDirectory(workingDirectory.toFile())
.setName(processToRun)
.setTimeoutMillis(getProcessTimeoutMillis(getProcessTimeoutMillis(-1)))
.setArguments(arguments)
.build();
return new ProcessRunner(parameters, executorService).runSynchronously();
}

/**
* Take the value from environment variable and assert that it is a path, and the file or
* directory, specified by this path, exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,30 @@ java_test(
],
)

java_test(
name = "GitRepositoryBlackBoxTest",
timeout = "moderate",
srcs = [
"GitRepositoryBlackBoxTest.java",
"GitRepositoryHelper.java",
"RepoWithRuleWritingTextGenerator.java",
"WorkspaceTestUtils.java",
],
tags = ["black_box_test"],
deps = common_deps + [
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib:bazel-repository",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/test/java/com/google/devtools/build/lib:foundations_testutil",
],
)

test_suite(
name = "ws_black_box_tests",
tags = ["black_box_test"],
tests = [
"BazelEmbeddedSkylarkBlackBoxTest",
"GitRepositoryBlackBoxTest",
"RepoWithRuleWritingTextGeneratorTest",
"WorkspaceBlackBoxTest",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2019 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.

package com.google.devtools.build.lib.blackbox.tests.workspace;

import com.google.devtools.build.lib.blackbox.framework.BlackBoxTestContext;
import com.google.devtools.build.lib.blackbox.framework.BuilderRunner;
import com.google.devtools.build.lib.blackbox.framework.PathUtils;
import com.google.devtools.build.lib.blackbox.junit.AbstractBlackBoxTest;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;

/**
* Black box tests for git_repository/new_git_repository.
* On Windows, runs without MSYS {@link WorkspaceTestUtils#bazel}
*/
public class GitRepositoryBlackBoxTest extends AbstractBlackBoxTest {

private static final String HELLO_FROM_EXTERNAL_REPOSITORY = "Hello from GIT repository!";

@Test
public void testNewGitRepository() throws Exception {
Path repo = context().getTmpDir().resolve("ext_repo");
setupGitRepository(context(), repo);

String buildFileContent = String.format("%s\n%s", RepoWithRuleWritingTextGenerator.loadRule(""),
RepoWithRuleWritingTextGenerator.callRule("call_write_text", "out.txt",
HELLO_FROM_EXTERNAL_REPOSITORY));
context().write("WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"new_git_repository\")",
"new_git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
" tag='first',",
String.format(" build_file_content=\"\"\"%s\"\"\",", buildFileContent),
")");

// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:call_write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out.txt");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_EXTERNAL_REPOSITORY);
}

@Test
public void testCloneAtCommit() throws Exception {
Path repo = context().getTmpDir().resolve("ext_repo");
String commit = setupGitRepository(context(), repo);

String buildFileContent = String.format("%s\n%s", RepoWithRuleWritingTextGenerator.loadRule(""),
RepoWithRuleWritingTextGenerator.callRule("call_write_text", "out.txt",
HELLO_FROM_EXTERNAL_REPOSITORY));
context().write("WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"new_git_repository\")",
"new_git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
String.format(" commit='%s',", commit),
String.format(" build_file_content=\"\"\"%s\"\"\",", buildFileContent),
")");

// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:call_write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out.txt");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_EXTERNAL_REPOSITORY);
}

@Test
public void testCloneAtMaster() throws Exception {
Path repo = context().getTmpDir().resolve("ext_repo");
setupGitRepository(context(), repo);

String buildFileContent = String.format("%s\n%s", RepoWithRuleWritingTextGenerator.loadRule(""),
RepoWithRuleWritingTextGenerator.callRule("call_write_text", "out.txt",
HELLO_FROM_EXTERNAL_REPOSITORY));
context().write("WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"new_git_repository\")",
"new_git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
" branch='master',",
String.format(" build_file_content=\"\"\"%s\"\"\",", buildFileContent),
")");

// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:call_write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out.txt");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_EXTERNAL_REPOSITORY);
}

private static String setupGitRepository(BlackBoxTestContext context, Path repo) throws Exception {
PathUtils.deleteTree(repo);
Files.createDirectories(repo);
GitRepositoryHelper gitRepository = new GitRepositoryHelper(context, repo);
gitRepository.init();

RepoWithRuleWritingTextGenerator generator = new RepoWithRuleWritingTextGenerator(repo);
generator.withOutputText(HELLO_FROM_EXTERNAL_REPOSITORY)
.skipBuildFile()
.setupRepository();

gitRepository.addAll();
gitRepository.commit("Initial commit");
gitRepository.tag("first");
return gitRepository.getHead();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2019 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.

package com.google.devtools.build.lib.blackbox.tests.workspace;

import com.google.devtools.build.lib.blackbox.framework.BlackBoxTestContext;
import com.google.devtools.build.lib.blackbox.framework.ProcessResult;
import java.nio.file.Path;

/**
* Helper class for working with local git repository in tests.
*/
public class GitRepositoryHelper {
private final BlackBoxTestContext context;
private final Path root;

public GitRepositoryHelper(BlackBoxTestContext context, Path root) {
this.context = context;
this.root = root;
}

Path init() throws Exception {
runGit("init");
runGit("config", "user.email", "'me@example.com'");
runGit("config", "user.name", "'E X Ample'");
return root;
}

String addAll() throws Exception {
return runGit("add", ".");
}

String commit(String commitMessage) throws Exception {
return runGit("commit", "-m", commitMessage);
}

String tag(String tagName) throws Exception {
return runGit("tag", tagName);
}

String getHead() throws Exception {
return runGit("rev-parse", "--short", "HEAD");
}

private String runGit(String... arguments) throws Exception {
ProcessResult result = context.runBinary(root, "git", arguments);
return result.outString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class RepoWithRuleWritingTextGenerator {
private String target;
private String outputText;
private String outFile;
private boolean generateBuildFile;

/**
* Generator constructor
Expand All @@ -69,6 +70,7 @@ public class RepoWithRuleWritingTextGenerator {
this.target = TARGET;
this.outputText = HELLO;
this.outFile = OUT_FILE;
generateBuildFile = true;
}

/**
Expand Down Expand Up @@ -104,6 +106,16 @@ RepoWithRuleWritingTextGenerator withOutFile(String name) {
return this;
}

/**
* Specifies that BUILD file should not be generated
*
* @return this generator
*/
RepoWithRuleWritingTextGenerator skipBuildFile() {
generateBuildFile = false;
return this;
}

/**
* Generates the repository: WORKSPACE, BUILD, and helper.bzl files.
*
Expand All @@ -113,13 +125,15 @@ RepoWithRuleWritingTextGenerator withOutFile(String name) {
Path setupRepository() throws IOException {
Path workspace = PathUtils.writeFileInDir(root, "WORKSPACE");
PathUtils.writeFileInDir(root, HELPER_FILE, WRITE_TEXT_TO_FILE);
PathUtils.writeFileInDir(
root,
"BUILD",
"load(\"@bazel_tools//tools/build_defs/pkg:pkg.bzl\", \"pkg_tar\")",
loadRule(""),
callRule(target, outFile, outputText),
String.format("pkg_tar(name = \"%s\", srcs = glob([\"*\"]),)", getPkgTarTarget()));
if (generateBuildFile) {
PathUtils.writeFileInDir(
root,
"BUILD",
"load(\"@bazel_tools//tools/build_defs/pkg:pkg.bzl\", \"pkg_tar\")",
loadRule(""),
callRule(target, outFile, outputText),
String.format("pkg_tar(name = \"%s\", srcs = glob([\"*\"]),)", getPkgTarTarget()));
}
return workspace.getParent();
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/bazel/skylark_git_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ EOF

bazel fetch //planets:planet-info >& $TEST_log \
|| echo "Expect run to fail."
expect_log "error cloning"
expect_log "error running 'git fetch origin' while working with @pluto"
}

run_suite "skylark git_repository tests"
Loading