Skip to content

Commit

Permalink
JsBundleDescriptionTest: Group tests
Browse files Browse the repository at this point in the history
Summary:
This splits JsBundleDescriptionTest: into two separate test classes, one with a `Before` method and one without.
This makes debugging tests easier that don’t use the common setup

Reviewed By: jeanlauliac

fbshipit-source-id: b2d60e2
  • Loading branch information
davidaurelio authored and facebook-github-bot committed Jun 7, 2018
1 parent a22c998 commit ef10a39
Showing 1 changed file with 180 additions and 164 deletions.
344 changes: 180 additions & 164 deletions test/com/facebook/buck/js/JsBundleDescriptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;

import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.model.Flavor;
Expand All @@ -38,6 +39,7 @@
import com.facebook.buck.rules.macros.LocationMacro;
import com.facebook.buck.testutil.FakeFileHashCache;
import com.facebook.buck.util.RichStream;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.ImmutableMap;
import com.google.common.hash.HashCode;
import java.util.Collection;
Expand All @@ -47,190 +49,204 @@
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

@SuppressWarnings("PMD.TestClassWithoutTestCases")
@RunWith(Enclosed.class)
public class JsBundleDescriptionTest {
private static final BuildTarget bundleTarget = BuildTargetFactory.newInstance("//bundle:target");

private static final BuildTarget directDependencyTarget =
BuildTargetFactory.newInstance("//libs:direct");
private static final BuildTarget level2 = BuildTargetFactory.newInstance("//libs:level2");
private static final BuildTarget level1_1 = BuildTargetFactory.newInstance("//libs:level1.1");
private static final BuildTarget level1_2 = BuildTargetFactory.newInstance("//libs:level1.2");
private final BuildTarget bundleTarget = BuildTargetFactory.newInstance("//bundle:target");
public static class TestsWithSetup {

private JsTestScenario.Builder scenarioBuilder;
private JsTestScenario scenario;
private static final BuildTarget directDependencyTarget =
BuildTargetFactory.newInstance("//libs:direct");
private static final BuildTarget level2 = BuildTargetFactory.newInstance("//libs:level2");
private static final BuildTarget level1_1 = BuildTargetFactory.newInstance("//libs:level1.1");
private static final BuildTarget level1_2 = BuildTargetFactory.newInstance("//libs:level1.2");

static Collection<BuildTarget> allLibaryTargets(Flavor... flavors) {
return Stream.of(level2, level1_1, level1_2, directDependencyTarget)
.map(t -> t.withAppendedFlavors(flavors))
.collect(Collectors.toList());
}
private JsTestScenario.Builder scenarioBuilder;
private JsTestScenario scenario;

@Before
public void setUp() throws NoSuchBuildTargetException {
scenarioBuilder = JsTestScenario.builder();
scenarioBuilder
.library(level2)
.library(level1_1, level2)
.library(level1_2, level2)
.library(directDependencyTarget, level1_1, level1_2)
.bundleWithDeps(bundleTarget, directDependencyTarget);
scenario = scenarioBuilder.build();
}
static Collection<BuildTarget> allLibaryTargets(Flavor... flavors) {
return Stream.of(level2, level1_1, level1_2, directDependencyTarget)
.map(t -> t.withAppendedFlavors(flavors))
.collect(Collectors.toList());
}

@Test
public void testTransitiveLibraryDependencies() throws NoSuchBuildTargetException {
BuildRule jsBundle = scenario.graphBuilder.requireRule(bundleTarget);
assertThat(allLibaryTargets(), everyItem(in(dependencyTargets(jsBundle))));
}
@Before
public void setUp() throws NoSuchBuildTargetException {
scenarioBuilder = JsTestScenario.builder();
scenarioBuilder
.library(level2)
.library(level1_1, level2)
.library(level1_2, level2)
.library(directDependencyTarget, level1_1, level1_2)
.bundleWithDeps(bundleTarget, directDependencyTarget);
scenario = scenarioBuilder.build();
}

@Test
public void testTransitiveLibraryDependenciesWithFlavors() throws NoSuchBuildTargetException {
Flavor[] flavors = {JsFlavors.IOS, JsFlavors.RELEASE};
BuildRule jsBundle = scenario.graphBuilder.requireRule(bundleTarget.withFlavors(flavors));
assertThat(allLibaryTargets(flavors), everyItem(in(dependencyTargets(jsBundle))));
}
@Test
public void testTransitiveLibraryDependencies() throws NoSuchBuildTargetException {
BuildRule jsBundle = scenario.graphBuilder.requireRule(bundleTarget);
assertThat(allLibaryTargets(), everyItem(in(dependencyTargets(jsBundle))));
}

@Test
public void testFlavoredBundleDoesNotDependOnUnflavoredLibs() throws NoSuchBuildTargetException {
BuildRule jsBundle =
scenario.graphBuilder.requireRule(
bundleTarget.withFlavors(JsFlavors.IOS, JsFlavors.RELEASE));
assertThat(allLibaryTargets(), everyItem(not(in(dependencyTargets(jsBundle)))));
}
@Test
public void testTransitiveLibraryDependenciesWithFlavors() throws NoSuchBuildTargetException {
Flavor[] flavors = {JsFlavors.IOS, JsFlavors.RELEASE};
BuildRule jsBundle = scenario.graphBuilder.requireRule(bundleTarget.withFlavors(flavors));
assertThat(allLibaryTargets(flavors), everyItem(in(dependencyTargets(jsBundle))));
}

@Test
public void testFlavoredBundleWithoutReleaseFlavorDependOnFlavoredLibs()
throws NoSuchBuildTargetException {
Flavor[] flavors = {JsFlavors.IOS, JsFlavors.RAM_BUNDLE_INDEXED};
BuildRule jsBundle = scenario.graphBuilder.requireRule((bundleTarget.withFlavors(flavors)));
assertThat(allLibaryTargets(JsFlavors.IOS), everyItem(in(dependencyTargets(jsBundle))));
assertThat(allLibaryTargets(flavors), everyItem(not(in(dependencyTargets(jsBundle)))));
}
@Test
public void testFlavoredBundleDoesNotDependOnUnflavoredLibs()
throws NoSuchBuildTargetException {
BuildRule jsBundle =
scenario.graphBuilder.requireRule(
bundleTarget.withFlavors(JsFlavors.IOS, JsFlavors.RELEASE));
assertThat(allLibaryTargets(), everyItem(not(in(dependencyTargets(jsBundle)))));
}

@Test
public void testFlavoredReleaseBundleDoesNotPropagateRamBundleFlavors()
throws NoSuchBuildTargetException {
Flavor[] bundleFlavors = {JsFlavors.IOS, JsFlavors.RAM_BUNDLE_INDEXED, JsFlavors.RELEASE};
Flavor[] flavorsToBePropagated = {JsFlavors.IOS, JsFlavors.RELEASE};
BuildRule jsBundle =
scenario.graphBuilder.requireRule((bundleTarget.withFlavors(bundleFlavors)));
assertThat(allLibaryTargets(flavorsToBePropagated), everyItem(in(dependencyTargets(jsBundle))));
assertThat(allLibaryTargets(bundleFlavors), everyItem(not(in(dependencyTargets(jsBundle)))));
}
@Test
public void testFlavoredBundleWithoutReleaseFlavorDependOnFlavoredLibs()
throws NoSuchBuildTargetException {
Flavor[] flavors = {JsFlavors.IOS, JsFlavors.RAM_BUNDLE_INDEXED};
BuildRule jsBundle = scenario.graphBuilder.requireRule((bundleTarget.withFlavors(flavors)));
assertThat(allLibaryTargets(JsFlavors.IOS), everyItem(in(dependencyTargets(jsBundle))));
assertThat(allLibaryTargets(flavors), everyItem(not(in(dependencyTargets(jsBundle)))));
}

@Test
public void testFlavoredReleaseBundleDoesNotPropagateRamBundleFlavorsAndroid()
throws NoSuchBuildTargetException {
Flavor[] bundleFlavors = {JsFlavors.ANDROID, JsFlavors.RAM_BUNDLE_INDEXED, JsFlavors.RELEASE};
Flavor[] flavorsToBePropagated = {JsFlavors.ANDROID, JsFlavors.RELEASE};
BuildRule jsBundle =
scenario.graphBuilder.requireRule((bundleTarget.withFlavors(bundleFlavors)));
assertThat(allLibaryTargets(flavorsToBePropagated), everyItem(in(dependencyTargets(jsBundle))));
assertThat(allLibaryTargets(bundleFlavors), everyItem(not(in(dependencyTargets(jsBundle)))));
}
@Test
public void testFlavoredReleaseBundleDoesNotPropagateRamBundleFlavors()
throws NoSuchBuildTargetException {
Flavor[] bundleFlavors = {JsFlavors.IOS, JsFlavors.RAM_BUNDLE_INDEXED, JsFlavors.RELEASE};
Flavor[] flavorsToBePropagated = {JsFlavors.IOS, JsFlavors.RELEASE};
BuildRule jsBundle =
scenario.graphBuilder.requireRule((bundleTarget.withFlavors(bundleFlavors)));
assertThat(
allLibaryTargets(flavorsToBePropagated), everyItem(in(dependencyTargets(jsBundle))));
assertThat(allLibaryTargets(bundleFlavors), everyItem(not(in(dependencyTargets(jsBundle)))));
}

@Test
public void testTransitiveLibraryDependenciesWithFlavorsForAndroid()
throws NoSuchBuildTargetException {
Flavor[] flavors = {JsFlavors.ANDROID, JsFlavors.RELEASE};
BuildRule jsBundle = scenario.graphBuilder.requireRule(bundleTarget.withFlavors(flavors));
assertThat(allLibaryTargets(flavors), everyItem(in(dependencyTargets(jsBundle))));
}
@Test
public void testFlavoredReleaseBundleDoesNotPropagateRamBundleFlavorsAndroid()
throws NoSuchBuildTargetException {
Flavor[] bundleFlavors = {JsFlavors.ANDROID, JsFlavors.RAM_BUNDLE_INDEXED, JsFlavors.RELEASE};
Flavor[] flavorsToBePropagated = {JsFlavors.ANDROID, JsFlavors.RELEASE};
BuildRule jsBundle =
scenario.graphBuilder.requireRule((bundleTarget.withFlavors(bundleFlavors)));
assertThat(
allLibaryTargets(flavorsToBePropagated), everyItem(in(dependencyTargets(jsBundle))));
assertThat(allLibaryTargets(bundleFlavors), everyItem(not(in(dependencyTargets(jsBundle)))));
}

@Test
public void testSourceMapExport() throws NoSuchBuildTargetException {
BuildRule map =
scenario.graphBuilder.requireRule(
bundleTarget.withFlavors(JsFlavors.IOS, JsFlavors.SOURCE_MAP));
JsBundleOutputs bundle =
scenario.graphBuilder.getRuleWithType(
bundleTarget.withFlavors(JsFlavors.IOS), JsBundleOutputs.class);

DefaultSourcePathResolver pathResolver =
DefaultSourcePathResolver.from(new SourcePathRuleFinder(scenario.graphBuilder));
assertEquals(
pathResolver.getRelativePath(map.getSourcePathToOutput()),
pathResolver.getRelativePath(bundle.getSourcePathToSourceMap()));
}
@Test
public void testTransitiveLibraryDependenciesWithFlavorsForAndroid()
throws NoSuchBuildTargetException {
Flavor[] flavors = {JsFlavors.ANDROID, JsFlavors.RELEASE};
BuildRule jsBundle = scenario.graphBuilder.requireRule(bundleTarget.withFlavors(flavors));
assertThat(allLibaryTargets(flavors), everyItem(in(dependencyTargets(jsBundle))));
}

@Test
public void testJsLibraryInDeps() throws NoSuchBuildTargetException {
BuildTarget bundleTarget = BuildTargetFactory.newInstance("//the:bundle");
JsTestScenario testScenario =
JsTestScenario.builder(scenario)
.bundleWithDeps(bundleTarget, directDependencyTarget)
.build();
@Test
public void testSourceMapExport() throws NoSuchBuildTargetException {
BuildRule map =
scenario.graphBuilder.requireRule(
bundleTarget.withFlavors(JsFlavors.IOS, JsFlavors.SOURCE_MAP));
JsBundleOutputs bundle =
scenario.graphBuilder.getRuleWithType(
bundleTarget.withFlavors(JsFlavors.IOS), JsBundleOutputs.class);

BuildRule jsBundle = testScenario.graphBuilder.requireRule(bundleTarget);
assertThat(allLibaryTargets(), everyItem(in(dependencyTargets(jsBundle))));
}
DefaultSourcePathResolver pathResolver =
DefaultSourcePathResolver.from(new SourcePathRuleFinder(scenario.graphBuilder));
assertEquals(
pathResolver.getRelativePath(map.getSourcePathToOutput()),
pathResolver.getRelativePath(bundle.getSourcePathToSourceMap()));
}

@Test
public void testTransitiveDependenciesAcrossSubGraph() throws NoSuchBuildTargetException {
BuildTarget firstLevelA = BuildTargetFactory.newInstance("//:firstA");
BuildTarget firstLevelB = BuildTargetFactory.newInstance("//:firstB");
BuildTarget secondLevelA = BuildTargetFactory.newInstance("//:secondA");
BuildTarget secondLevelB = BuildTargetFactory.newInstance("//:secondB");
BuildTarget bundleTarget = BuildTargetFactory.newInstance("//the:bundle");

JsTestScenario.Builder builder = JsTestScenario.builder(scenario);
JsTestScenario testScenario =
builder
.appleLibraryWithDeps(firstLevelA, level1_1)
.library(secondLevelA)
.appleLibraryWithDeps(secondLevelB, level1_2, secondLevelA)
.appleLibraryWithDeps(firstLevelB, secondLevelB)
.bundleWithDeps(bundleTarget, firstLevelA, firstLevelB)
.build();

Flavor[] flavors = {JsFlavors.IOS, JsFlavors.RELEASE};
BuildRule jsBundle = testScenario.graphBuilder.requireRule(bundleTarget.withFlavors(flavors));
List<BuildTarget> expectedLibDeps =
Stream.of(level1_1, level1_2, level2, secondLevelA)
.map(t -> t.withAppendedFlavors(flavors))
.collect(Collectors.toList());

assertThat(expectedLibDeps, everyItem(in(dependencyTargets(jsBundle))));
}
@Test
public void testJsLibraryInDeps() throws NoSuchBuildTargetException {
BuildTarget bundleTarget = BuildTargetFactory.newInstance("//the:bundle");
JsTestScenario testScenario =
JsTestScenario.builder(scenario)
.bundleWithDeps(bundleTarget, directDependencyTarget)
.build();

BuildRule jsBundle = testScenario.graphBuilder.requireRule(bundleTarget);
assertThat(allLibaryTargets(), everyItem(in(dependencyTargets(jsBundle))));
}

@Test
public void testTransitiveDependenciesAcrossSubGraph() throws NoSuchBuildTargetException {
assumeTrue(Platform.detect() != Platform.WINDOWS);
BuildTarget firstLevelA = BuildTargetFactory.newInstance("//:firstA");
BuildTarget firstLevelB = BuildTargetFactory.newInstance("//:firstB");
BuildTarget secondLevelA = BuildTargetFactory.newInstance("//:secondA");
BuildTarget secondLevelB = BuildTargetFactory.newInstance("//:secondB");
BuildTarget bundleTarget = BuildTargetFactory.newInstance("//the:bundle");

JsTestScenario.Builder builder = JsTestScenario.builder(scenario);
JsTestScenario testScenario =
builder
.appleLibraryWithDeps(firstLevelA, level1_1)
.library(secondLevelA)
.appleLibraryWithDeps(secondLevelB, level1_2, secondLevelA)
.appleLibraryWithDeps(firstLevelB, secondLevelB)
.bundleWithDeps(bundleTarget, firstLevelA, firstLevelB)
.build();

Flavor[] flavors = {JsFlavors.IOS, JsFlavors.RELEASE};
BuildRule jsBundle = testScenario.graphBuilder.requireRule(bundleTarget.withFlavors(flavors));
List<BuildTarget> expectedLibDeps =
Stream.of(level1_1, level1_2, level2, secondLevelA)
.map(t -> t.withAppendedFlavors(flavors))
.collect(Collectors.toList());

@Test
public void rulesReferencedFromLocationMacrosInfluenceRuleKey() {
BuildTarget referencedTarget = BuildTargetFactory.newInstance("//:ref");
PathSourcePath referencedSource = FakeSourcePath.of("referenced/file");

JsTestScenario scenario =
JsTestScenario.builder().exportFile(referencedTarget, referencedSource).build();
JsBundle bundle =
scenario.createBundle(
"//:bundle",
builder -> builder.setExtraJson("[\"1 %s 2\"]", LocationMacro.of(referencedTarget)));

SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(scenario.graphBuilder);
SourcePathResolver pathResolver = DefaultSourcePathResolver.from(ruleFinder);

Function<HashCode, RuleKey> calc =
(refHash) ->
new TestDefaultRuleKeyFactory(
new FakeFileHashCache(
ImmutableMap.of(pathResolver.getAbsolutePath(referencedSource), refHash)),
pathResolver,
ruleFinder)
.build(bundle);

assertThat(
calc.apply(HashCode.fromInt(12345)), not(equalTo(calc.apply(HashCode.fromInt(67890)))));
assertThat(expectedLibDeps, everyItem(in(dependencyTargets(jsBundle))));
}
}

@Test
public void locationMacrosInExtraJsonAddBuildDeps() {
BuildTarget referencedTarget = BuildTargetFactory.newInstance("//:ref");
JsTestScenario scenario = JsTestScenario.builder().arbitraryRule(referencedTarget).build();
JsBundle bundle =
scenario.createBundle(
"//:bundle",
builder -> builder.setExtraJson("[\"%s\"]", LocationMacro.of(referencedTarget)));
assertThat(scenario.graphBuilder.getRule(referencedTarget), in(bundle.getBuildDeps()));
public static class TestsWithoutSetup {

@Test
public void rulesReferencedFromLocationMacrosInfluenceRuleKey() {
BuildTarget referencedTarget = BuildTargetFactory.newInstance("//:ref");
PathSourcePath referencedSource = FakeSourcePath.of("referenced/file");

JsTestScenario scenario =
JsTestScenario.builder().exportFile(referencedTarget, referencedSource).build();
JsBundle bundle =
scenario.createBundle(
"//:bundle",
builder -> builder.setExtraJson("[\"1 %s 2\"]", LocationMacro.of(referencedTarget)));

SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(scenario.graphBuilder);
SourcePathResolver pathResolver = DefaultSourcePathResolver.from(ruleFinder);

Function<HashCode, RuleKey> calc =
(refHash) ->
new TestDefaultRuleKeyFactory(
new FakeFileHashCache(
ImmutableMap.of(pathResolver.getAbsolutePath(referencedSource), refHash)),
pathResolver,
ruleFinder)
.build(bundle);

assertThat(
calc.apply(HashCode.fromInt(12345)), not(equalTo(calc.apply(HashCode.fromInt(67890)))));
}

@Test
public void locationMacrosInExtraJsonAddBuildDeps() {
BuildTarget referencedTarget = BuildTargetFactory.newInstance("//:ref");
JsTestScenario scenario = JsTestScenario.builder().arbitraryRule(referencedTarget).build();
JsBundle bundle =
scenario.createBundle(
"//:bundle",
builder -> builder.setExtraJson("[\"%s\"]", LocationMacro.of(referencedTarget)));
assertThat(scenario.graphBuilder.getRule(referencedTarget), in(bundle.getBuildDeps()));
}
}

private static Collection<BuildTarget> dependencyTargets(BuildRule rule) {
Expand Down

0 comments on commit ef10a39

Please sign in to comment.