Skip to content

Commit

Permalink
Fix Python converage tests in ExternalTestRunnerIntegrationTest on Wi…
Browse files Browse the repository at this point in the history
…ndows

Summary:
- Re-using cxx configuration provided by `CxxToolchainUtilsForTests`
- Making tests working for both python2 and python3.

Reviewed By: bobyangyf

fbshipit-source-id: d64e47f4e0
  • Loading branch information
Sergey Tyurin authored and facebook-github-bot committed Dec 15, 2018
1 parent 8e78d1f commit 1dcc557
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 33 deletions.
71 changes: 47 additions & 24 deletions test/com/facebook/buck/cli/ExternalTestRunnerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package com.facebook.buck.cli;

import static com.facebook.buck.util.environment.Platform.WINDOWS;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeFalse;

import com.facebook.buck.cxx.CxxToolchainUtilsForTests;
import com.facebook.buck.io.file.MorePaths;
import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
Expand All @@ -46,17 +47,19 @@ public class ExternalTestRunnerIntegrationTest {
@Rule public TemporaryPaths tmp = new TemporaryPaths();

private ProjectWorkspace workspace;
private boolean isWindowsOs;

@Before
public void setUp() throws IOException {
workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "external_test_runner", tmp);
workspace.setUp();
isWindowsOs = Platform.detect().getType() == PlatformType.WINDOWS;
}

@Test
public void runPass() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test", "-c", "test.external_runner=" + workspace.getPath("test_runner.py"), "//:pass");
Expand All @@ -66,61 +69,82 @@ public void runPass() throws IOException {

@Test
public void runCoverage() throws IOException {
String externalTestRunner =
isWindowsOs ? "test_runner_coverage.bat" : "test_runner_coverage.py";
ProcessResult result =
workspace.runBuckCommand(
"test",
"-c",
"test.external_runner=" + workspace.getPath("test_runner_coverage.py"),
"test.external_runner=" + workspace.getPath(externalTestRunner),
"//dir:python-coverage");
result.assertSuccess();
String simplePath = MorePaths.pathWithPlatformSeparators("dir/simple.py").replace("\\", "\\\\");
String alsoSimplePath =
MorePaths.pathWithPlatformSeparators("dir/also_simple.py").replace("\\", "\\\\");
assertThat(
result.getStdout(),
result.getStdout().trim(),
is(
equalTo(
"[[0.0, [u'dir/simple.py']], "
+ "[0.75, [u'dir/also_simple.py', u'dir/simple.py']], "
+ "[1.0, [u'dir/also_simple.py']]]\n")));
endsWith(
String.format(
"[[0.0, ['%1$s']], " + "[0.75, ['%2$s', '%1$s']], " + "[1.0, ['%2$s']]]",
simplePath, alsoSimplePath))));
}

@Test
public void runPythonCxxAdditionalCoverage() throws IOException {
CxxToolchainUtilsForTests.configureCxxToolchains(workspace);
String externalTestRunner =
isWindowsOs ? "test_runner_additional_coverage.bat" : "test_runner_additional_coverage.py";
ProcessResult result =
workspace.runBuckCommand(
"test",
"-c",
"test.external_runner=" + workspace.getPath("test_runner_additional_coverage.py"),
"test.external_runner=" + workspace.getPath(externalTestRunner),
"//dir:python-cxx-additional-coverage");

result.assertSuccess();
assertTrue(result.getStdout().trim().endsWith("/buck-out/gen/dir/cpp_binary"));
if (isWindowsOs) {
assertTrue(result.getStdout().trim().endsWith("\\buck-out\\gen\\dir\\cpp_binary.exe"));
} else {
assertTrue(result.getStdout().trim().endsWith("/buck-out/gen/dir/cpp_binary"));
}
}

@Test
public void runAdditionalCoverage() throws IOException {
CxxToolchainUtilsForTests.configureCxxToolchains(workspace);
String externalTestRunner =
isWindowsOs ? "test_runner_additional_coverage.bat" : "test_runner_additional_coverage.py";
ProcessResult result =
workspace.runBuckCommand(
"test",
"-c",
"test.external_runner=" + workspace.getPath("test_runner_additional_coverage.py"),
"test.external_runner=" + workspace.getPath(externalTestRunner),
"//dir:cpp_test");

result.assertSuccess();
assertTrue(result.getStdout().trim().endsWith("/buck-out/gen/dir/cpp_binary"));
if (isWindowsOs) {
assertTrue(result.getStdout().trim().endsWith("\\buck-out\\gen\\dir\\cpp_binary.exe"));
} else {
assertTrue(result.getStdout().trim().endsWith("/buck-out/gen/dir/cpp_binary"));
}
}

@Test
public void runFail() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test", "-c", "test.external_runner=" + workspace.getPath("test_runner.py"), "//:fail");
result.assertSuccess();
assertThat(result.getStderr(), Matchers.endsWith("TESTS FAILED!\n"));
assertThat(result.getStderr(), endsWith("TESTS FAILED!\n"));
}

@Test
public void extraArgs() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand All @@ -135,8 +159,7 @@ public void extraArgs() throws IOException {

@Test
public void runJavaTest() throws IOException {
String externalTestRunner =
Platform.detect().getType() == PlatformType.WINDOWS ? "test_runner.bat" : "test_runner.py";
String externalTestRunner = isWindowsOs ? "test_runner.bat" : "test_runner.py";
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand Down Expand Up @@ -170,7 +193,7 @@ public void runJavaTest() throws IOException {
@Test
public void numberOfJobsIsPassedToExternalRunner() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand All @@ -186,7 +209,7 @@ public void numberOfJobsIsPassedToExternalRunner() throws IOException {
@Test
public void numberOfJobsInExtraArgsIsPassedToExternalRunner() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand Down Expand Up @@ -215,7 +238,7 @@ public void numberOfJobsInExtraArgsIsPassedToExternalRunner() throws IOException
public void numberOfJobsInExtraArgsWithShortNotationIsPassedToExternalRunner()
throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand Down Expand Up @@ -243,7 +266,7 @@ public void numberOfJobsInExtraArgsWithShortNotationIsPassedToExternalRunner()
@Test
public void numberOfJobsWithUtilizationRatioAppliedIsPassedToExternalRunner() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand All @@ -261,7 +284,7 @@ public void numberOfJobsWithUtilizationRatioAppliedIsPassedToExternalRunner() th
@Test
public void numberOfJobsWithTestThreadsIsPassedToExternalRunner() throws IOException {
// sh_test doesn't support Windows
assumeThat(Platform.detect(), is(not(WINDOWS)));
assumeFalse(isWindowsOs);
ProcessResult result =
workspace.runBuckCommand(
"test",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python2 test_runner.py %*
python test_runner.py %*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python test_runner_additional_coverage.py %*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python test_runner_coverage.py %*
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
#!/usr/bin/python -u
# Copyright 2018-present Facebook, Inc.
#
# 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.

from __future__ import print_function

Expand All @@ -13,6 +26,19 @@
(options, args) = parser.parse_args()


def join_paths(paths):
return "[" + ", ".join(["'" + f.replace("\\", "\\\\") + "'" for f in paths]) + "]"


def convert_coverage_to_str(coverage):
return "[" + repr(coverage[0]) + ", " + join_paths(coverage[1]) + "]"


def convert_coverage_entries_to_str(coverage_entries):
return "[" + ", ".join([convert_coverage_to_str(c) for c in coverage_entries]) + "]"


with open(options.buck_test_info) as f:
test_infos = json.load(f)
print(test_infos[0]["needed_coverage"])
coverage = test_infos[0]["needed_coverage"]
print(convert_coverage_entries_to_str(coverage))
41 changes: 35 additions & 6 deletions test/com/facebook/buck/cxx/CxxToolchainUtilsForTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@
import com.facebook.buck.util.environment.Platform;
import com.facebook.buck.util.environment.PlatformType;
import com.facebook.buck.util.string.MoreStrings;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;

/** Helper class to setup cxx toolchains in a platform-independent way. */
public class CxxToolchainUtilsForTests {

private static final Path WINDOWS_CXX_TOOLCHAIN_LOCATION =
Paths.get("C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64");
Paths.get("C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC");

private static final Path WINDOWS_CXX_TOOLCHAIN_BIN_LOCATION =
WINDOWS_CXX_TOOLCHAIN_LOCATION.resolve("bin").resolve("amd64");

private static final Path WINDOWS_CXX_TOOLCHAIN_LIB_LOCATION =
WINDOWS_CXX_TOOLCHAIN_LOCATION.resolve("lib").resolve("amd64");

private static final Path WINDOWS_CXX_TOOLCHAIN_SDK_LIB_LOCATION =
Paths.get("C:/Program Files (x86)/Windows Kits/10/Lib");

private CxxToolchainUtilsForTests() {}

Expand All @@ -48,10 +59,11 @@ public static void configureCxxToolchains(ProjectWorkspace workspace) throws IOE
workspace.writeContentsToPath(config, ".buckconfig");
}

private static String getWindowsCxxConfig() {
Path cl = WINDOWS_CXX_TOOLCHAIN_LOCATION.resolve("cl.exe");
Path link = WINDOWS_CXX_TOOLCHAIN_LOCATION.resolve("link.exe");
Path lib = WINDOWS_CXX_TOOLCHAIN_LOCATION.resolve("lib.exe");
private static String getWindowsCxxConfig() throws IOException {
Path cl = WINDOWS_CXX_TOOLCHAIN_BIN_LOCATION.resolve("cl.exe");
Path link = WINDOWS_CXX_TOOLCHAIN_BIN_LOCATION.resolve("link.exe");
Path lib = WINDOWS_CXX_TOOLCHAIN_BIN_LOCATION.resolve("lib.exe");
Path libDir = findLastSubDir(WINDOWS_CXX_TOOLCHAIN_SDK_LIB_LOCATION);
return String.format(
MoreStrings.linesToText(
"[cxx]",
Expand All @@ -64,13 +76,30 @@ private static String getWindowsCxxConfig() {
" cxxpp=\"%1$s\"",
" cxxpp_type=windows",
" ld=\"%2$s\"",
" ldflags = \\",
" /LIBPATH:\"%4$s\" \\",
" /LIBPATH:\"%5$s\" \\",
" /LIBPATH:\"%6$s\" \\",
" /LIBPATH:\"%7$s\"",
" linker_platform=windows",
" ar=\"%3$s\"",
" archiver_platform=windows",
" ranlib=\"%3$s\""),
MorePaths.pathWithUnixSeparators(cl),
MorePaths.pathWithUnixSeparators(link),
MorePaths.pathWithUnixSeparators(lib));
MorePaths.pathWithUnixSeparators(lib),
MorePaths.pathWithUnixSeparators(WINDOWS_CXX_TOOLCHAIN_LIB_LOCATION),
MorePaths.pathWithUnixSeparators(libDir.resolve("km").resolve("x64")),
MorePaths.pathWithUnixSeparators(libDir.resolve("ucrt").resolve("x64")),
MorePaths.pathWithUnixSeparators(libDir.resolve("um").resolve("x64")));
}

private static Path findLastSubDir(Path path) throws IOException {
assumeTrue("Path " + path + "is not a directory", Files.isDirectory(path));
Path subdir = Iterables.getLast(Files.list(path).collect(Collectors.toList()), null);
assumeTrue(subdir != null);
assumeTrue("Path " + subdir + "is not a directory", Files.isDirectory(subdir));
return subdir;
}

private static String getPosixConfig() {
Expand Down
1 change: 0 additions & 1 deletion windows_failures.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
!com.facebook.buck.cli.DaemonIntegrationTest
!com.facebook.buck.cli.ExternalTestRunnerIntegrationTest
!com.facebook.buck.cxx.ArchiveStepIntegrationTest#thatGeneratedArchivesAreDeterministic
!com.facebook.buck.cxx.CxxBinaryDescriptionTest#binaryShouldLinkOwnRequiredLibraries
!com.facebook.buck.cxx.CxxBinaryDescriptionTest#binaryShouldLinkOwnRequiredLibraries.*
Expand Down

0 comments on commit 1dcc557

Please sign in to comment.