Skip to content

Commit

Permalink
Merge pull request #2907 from kageiit/buck_support
Browse files Browse the repository at this point in the history
Add support for the buck build system
  • Loading branch information
jongerrish committed Feb 18, 2017
2 parents 81637bc + f8180c9 commit e38f63a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
Expand Up @@ -10,6 +10,7 @@
import org.junit.runners.model.Statement;
import org.robolectric.annotation.Config;
import org.robolectric.internal.AndroidConfigurer;
import org.robolectric.internal.BuckManifestFactory;
import org.robolectric.internal.GradleManifestFactory;
import org.robolectric.internal.SandboxFactory;
import org.robolectric.internal.SandboxTestRunner;
Expand Down Expand Up @@ -353,7 +354,9 @@ protected SandboxTestRunner.HelperTestRunner getHelperTestRunner(Class bootstrap
protected ManifestFactory getManifestFactory(Config config) {
Class<?> buildConstants = config.constants();
//noinspection ConstantConditions
if (buildConstants != null && buildConstants != Void.class) {
if (BuckManifestFactory.isBuck()) {
return new BuckManifestFactory();
} else if (buildConstants != null && buildConstants != Void.class) {
return new GradleManifestFactory();
} else {
return new MavenManifestFactory();
Expand Down
@@ -0,0 +1,77 @@
package org.robolectric.internal;

import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.Fs;
import org.robolectric.res.FsFile;
import org.robolectric.res.ResourcePath;
import org.robolectric.util.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;

public class BuckManifestFactory implements ManifestFactory {

private static final String BUCK_ROBOLECTRIC_RES_DIRECTORIES = "buck.robolectric_res_directories";
private static final String BUCK_ROBOLECTRIC_ASSETS_DIRECTORIES = "buck.robolectric_assets_directories";
private static final String BUCK_ROBOLECTRIC_MANIFEST = "buck.robolectric_manifest";

@Override
public ManifestIdentifier identify(Config config) {
String buckManifest = System.getProperty(BUCK_ROBOLECTRIC_MANIFEST);
FsFile manifestFile = Fs.fileFromPath(buckManifest);
return new ManifestIdentifier(manifestFile, null, null, config.packageName(), null);
}

@Override
public AndroidManifest create(ManifestIdentifier manifestIdentifier) {
String buckResDirs = System.getProperty(BUCK_ROBOLECTRIC_RES_DIRECTORIES);
String buckAssetsDirs = System.getProperty(BUCK_ROBOLECTRIC_ASSETS_DIRECTORIES);
String packageName = manifestIdentifier.getPackageName();
FsFile manifestFile = manifestIdentifier.getManifestFile();

final List<String> buckResources = buckResDirs == null ? null :
Arrays.asList(buckResDirs.split(File.pathSeparator));
final List<String> buckAssets = buckAssetsDirs == null ? null :
Arrays.asList(buckAssetsDirs.split(File.pathSeparator));

final FsFile resDir = (buckResources == null || "".equals(buckResources)) ? null :
Fs.fileFromPath(buckResources.get(buckResources.size() - 1));
final FsFile assetsDir = (buckAssets == null || "".equals(buckAssets)) ? null :
Fs.fileFromPath(buckAssets.get(buckAssets.size() - 1));

Logger.debug("Robolectric assets directory: " + (assetsDir == null ? null : assetsDir.getPath()));
Logger.debug(" Robolectric res directory: " + (resDir == null ? null : resDir.getPath()));
Logger.debug(" Robolectric manifest path: " + manifestFile.getPath());
Logger.debug(" Robolectric package name: " + packageName);

return new AndroidManifest(manifestFile, resDir, assetsDir, packageName) {
@Override
public List<ResourcePath> getIncludedResourcePaths() {
Collection<ResourcePath> resourcePaths = new LinkedHashSet<>(); // Needs stable ordering and no duplicates
resourcePaths.add(super.getResourcePath());

// Add all the buck resource folders, no duplicates as they are being added to a set.
if (buckResources != null) {
ListIterator<String> it = buckResources.listIterator(buckResources.size());
while (it.hasPrevious()) {
resourcePaths.add(new ResourcePath(
getRClass(),
Fs.fileFromPath(it.previous()),
getAssetsDirectory()));
}
}
return new ArrayList<>(resourcePaths);
}
};
}

public static boolean isBuck() {
return System.getProperty(BUCK_ROBOLECTRIC_MANIFEST) != null;
}
}
Expand Up @@ -11,6 +11,7 @@
* <ul>
* <li>Maven</li>
* <li>Gradle</li>
* <li>Buck</li>
* </ul>
*/
public interface ManifestFactory {
Expand Down
@@ -0,0 +1,59 @@
package org.robolectric.internal;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.FileFsFile;
import org.robolectric.res.ResourcePath;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class BuckManifestFactoryTest {

private Config.Builder configBuilder;
private BuckManifestFactory buckManifestFactory;

@Before
public void setUp() throws Exception {
configBuilder = Config.Builder.defaults().setPackageName("com.robolectric.buck");
System.setProperty("buck.robolectric_manifest", "buck/AndroidManifest.xml");
System.setProperty("buck.robolectric_res_directories", "buck/res1:buck/res2");
System.setProperty("buck.robolectric_assets_directories", "buck/assets");
buckManifestFactory = new BuckManifestFactory();
}

@After
public void tearDown() {
System.clearProperty("buck.robolectric_manifest");
System.clearProperty("buck.robolectric_res_directories");
System.clearProperty("buck.robolectric_assets_directories");
}

@Test public void identify() throws Exception {
ManifestIdentifier manifestIdentifier = buckManifestFactory.identify(configBuilder.build());
assertThat(manifestIdentifier.getManifestFile())
.isEqualTo(FileFsFile.from("buck/AndroidManifest.xml"));
assertThat(manifestIdentifier.getPackageName())
.isEqualTo("com.robolectric.buck");
}

@Test public void multiple_res_dirs() throws Exception {
ManifestIdentifier manifestIdentifier = buckManifestFactory.identify(configBuilder.build());
AndroidManifest manifest = buckManifestFactory.create(manifestIdentifier);
assertThat(manifest.getResDirectory())
.isEqualTo(FileFsFile.from("buck/res2"));
assertThat(manifest.getAssetsDirectory())
.isEqualTo(FileFsFile.from("buck/assets"));

List<ResourcePath> resourcePathList = manifest.getIncludedResourcePaths();
assertThat(resourcePathList.size()).isEqualTo(2);
assertThat(resourcePathList).containsExactly(
new ResourcePath(manifest.getRClass(), FileFsFile.from("buck/res2"), FileFsFile.from("buck/assets")),
new ResourcePath(manifest.getRClass(), FileFsFile.from("buck/res1"), FileFsFile.from("buck/assets"))
);
}
}

0 comments on commit e38f63a

Please sign in to comment.