Skip to content

Commit

Permalink
Windows compatitbility for the EndToEndWorkspace
Browse files Browse the repository at this point in the history
Summary: To be honest, I really don't like the implementation here, and would love to hear people's thoughts on an alternate interface so that every endtoend test doesn't need to think about converting build targets/managing the needed windows aspects.

Test Plan: Tested mostly against sandcastle local runs by changing the test target name, will switch the target name back before pushing (no more need to force testpilot to pick it up by that point)

Reviewed By: bobyangyf

fbshipit-source-id: 2d42ef1
  • Loading branch information
Keegan Parker authored and facebook-github-bot committed Mar 1, 2018
1 parent 79e6c9c commit b092ef7
Show file tree
Hide file tree
Showing 19 changed files with 510 additions and 164 deletions.
6 changes: 4 additions & 2 deletions test/com/facebook/buck/cxx/CxxErrorsIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.buck.cxx.toolchain.CxxPlatforms;
import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.WindowsUtils;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.util.environment.Platform;
Expand All @@ -39,15 +40,16 @@ public class CxxErrorsIntegrationTest {
@Rule public TemporaryPaths tmp = new TemporaryPaths();

private ProjectWorkspace workspace;
private WindowsUtils windowsUtils = new WindowsUtils();

@Before
public void setUp() throws IOException {
workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "errors", tmp);
workspace.setUp();
WindowsUtils.setUpWorkspace(workspace);
windowsUtils.setUpWorkspace(workspace);

if (Platform.detect() == Platform.WINDOWS) {
WindowsUtils.checkAssumptions();
windowsUtils.checkAssumptions();
}
}

Expand Down
10 changes: 7 additions & 3 deletions test/com/facebook/buck/cxx/WindowsCxxIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.WindowsUtils;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.util.ProcessExecutor;
Expand All @@ -42,13 +43,15 @@ public class WindowsCxxIntegrationTest {
private ProjectWorkspace workspace;


private WindowsUtils windowsUtils = new WindowsUtils();

@Before
public void setUp() throws IOException {
assumeTrue(Platform.detect() == Platform.WINDOWS);
WindowsUtils.checkAssumptions();
windowsUtils.checkAssumptions();
workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "win_x64", tmp);
workspace.setUp();
WindowsUtils.setUpWorkspace(workspace, "xplat");
windowsUtils.setUpWorkspace(workspace, "xplat");
}

@Test
Expand Down Expand Up @@ -128,7 +131,8 @@ public void simpleBinaryWithDll() throws IOException {
private ImmutableMap<String, String> getDevConsoleEnv(String vcvarsallBatArg)
throws IOException, InterruptedException {
workspace.writeContentsToPath(
"\"" + WindowsUtils.vcvarsallBat + "\" " + vcvarsallBatArg + " & set", "env.bat");
String.format("\"%s\" %s & set", windowsUtils.getVcvarsallbat(), vcvarsallBatArg),
"env.bat");
ProcessExecutor.Result envResult = workspace.runCommand("cmd", "/Q", "/c", "env.bat");
Optional<String> envOut = envResult.getStdout();
Assert.assertTrue(envOut.isPresent());
Expand Down
101 changes: 0 additions & 101 deletions test/com/facebook/buck/cxx/WindowsUtils.java

This file was deleted.

140 changes: 140 additions & 0 deletions test/com/facebook/buck/testutil/PlatformUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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.
*/

package com.facebook.buck.testutil;

import static org.junit.Assume.assumeTrue;

import com.facebook.buck.util.Escaper.Quoter;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* PlatformUtils exposes a consistent place to get potentially platform-specific configurations for
* testing
*/
public abstract class PlatformUtils {
private Quoter quoter;

protected PlatformUtils(Quoter quoter) {
this.quoter = quoter;
}

protected Optional<String> getClExe() {
return Optional.empty();
}

protected Optional<String> getLinkExe() {
return Optional.empty();
}

protected Optional<String> getLibExe() {
return Optional.empty();
}

protected String[] getWindowsIncludeDirs() {
return new String[] {};
}

protected String[] getWindowsLibDirs() {
return new String[] {};
}

private String replacementForConfig(Optional<String> config) {
if (config.isPresent()) {
return quoter.quote(config.get());
}
return "";
}

/** Replaces any placeholders in the given workspace with appropriate platform-specific configs */
public void setUpWorkspace(AbstractWorkspace workspace, String... cells) throws IOException {
for (int i = -1; i < cells.length; i++) {
String prefix = i == -1 ? "" : cells[i] + "/";
String buckconfig = prefix + ".buckconfig";
String buildDefs = prefix + "BUILD_DEFS";
workspace.replaceFileContents(buckconfig, "$CL_EXE$", replacementForConfig(getClExe()));
workspace.replaceFileContents(buckconfig, "$LIB_EXE$", replacementForConfig(getLibExe()));
workspace.replaceFileContents(buckconfig, "$LINK_EXE$", replacementForConfig(getLinkExe()));
workspace.replaceFileContents(
buildDefs,
"$WINDOWS_COMPILE_FLAGS$",
Arrays.stream(getWindowsIncludeDirs())
.map(s -> quoter.quote("/I" + s))
.collect(Collectors.joining(", ")));
workspace.replaceFileContents(
buildDefs,
"$WINDOWS_LINK_FLAGS$",
Arrays.stream(getWindowsLibDirs())
.map(s -> quoter.quote("/LIBPATH:" + s))
.collect(Collectors.joining(", ")));
}
}

/**
* Make sure that files we believe should exist do, if we don't, we shouldn't continue the test
* that likely relies on it.
*/
public void checkAssumptions() {
assumeTrue(
"cl.exe should exist",
!getClExe().isPresent() || Files.isExecutable(Paths.get(getClExe().get())));

assumeTrue(
"link.exe should exist",
!getLinkExe().isPresent() || Files.isExecutable(Paths.get(getLinkExe().get())));

assumeTrue(
"lib.exe should exist",
!getLibExe().isPresent() || Files.isExecutable(Paths.get(getLibExe().get())));

for (String includeDir : getWindowsIncludeDirs()) {
assumeTrue(
String.format("include dir %s should exist", includeDir),
Files.isDirectory(Paths.get(includeDir)));
}

for (String libDir : getWindowsLibDirs()) {
assumeTrue(
String.format("lib dir %s should exist", libDir), Files.isDirectory(Paths.get(libDir)));
}
}

/** Returns the flavor of build rules for the given platform */
public Optional<String> getFlavor() {
return Optional.empty();
}

/**
* Gets a base command builder for the given platform, where the utils adds in command necessary
* to launch buck in the given environment
*/
public abstract ImmutableList.Builder<String> getCommandBuilder();

/** Gets a PlatformUtils based on what Platform we're running tests in. */
public static PlatformUtils getForPlatform() {
if (Platform.detect() == Platform.WINDOWS) {
return new WindowsUtils();
}
return new UnixUtils();
}
}
42 changes: 42 additions & 0 deletions test/com/facebook/buck/testutil/UnixUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.
*/

package com.facebook.buck.testutil;

import com.facebook.buck.util.Escaper.Quoter;
import com.google.common.collect.ImmutableList;
import java.nio.file.FileSystems;

/** An implementation of {@link PlatformUtils} for Unix platforms (Mac OS, Linux) */
public class UnixUtils extends PlatformUtils {
private static final String BUCK_EXE =
FileSystems.getDefault()
.getPath("buck-out", "gen", "programs", "buck.pex")
.toAbsolutePath()
.toString();

public UnixUtils() {
super(Quoter.DOUBLE);
}

/** Returns a buck command builder for a unix platform, which runs buck through its .pex file */
@Override
public ImmutableList.Builder<String> getCommandBuilder() {
ImmutableList.Builder<String> commandBuilder = ImmutableList.<String>builder();
commandBuilder.add(BUCK_EXE);
return commandBuilder;
}
}
Loading

0 comments on commit b092ef7

Please sign in to comment.