Replace `createMock(OnDiskBuildInfo.class)` with `new FakeOnDiskBuildInfo()`. Summary: As promised, I am trying to address my abuse of mocks by replacing them with fakes. Test Plan: Sandcastle builds.
diff --git a/test/com/facebook/buck/java/AccumulateClassNamesTest.java b/test/com/facebook/buck/java/AccumulateClassNamesTest.java index 8354b73..87a7c83 100644 --- a/test/com/facebook/buck/java/AccumulateClassNamesTest.java +++ b/test/com/facebook/buck/java/AccumulateClassNamesTest.java
@@ -30,6 +30,7 @@ import com.facebook.buck.rules.BuildRuleType; import com.facebook.buck.rules.FakeAbstractBuildRuleBuilderParams; import com.facebook.buck.rules.FakeBuildableContext; +import com.facebook.buck.rules.FakeOnDiskBuildInfo; import com.facebook.buck.rules.OnDiskBuildInfo; import com.facebook.buck.rules.Sha1HashCode; import com.facebook.buck.step.ExecutionContext; @@ -37,7 +38,6 @@ import com.facebook.buck.step.TestExecutionContext; import com.facebook.buck.testutil.MoreAsserts; import com.facebook.buck.util.ProjectFilesystem; -import com.google.common.base.Optional; import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSortedMap; @@ -135,17 +135,17 @@ public void testInitializeFromDisk() throws IOException { BuildTarget buildTarget = new BuildTarget("//foo", "bar"); JavaLibraryRule javaRule = new FakeJavaLibraryRule(buildTarget); - AccumulateClassNames accumulateClassNames = new AccumulateClassNames(buildTarget, javaRule); - OnDiskBuildInfo onDiskBuildInfo = createMock(OnDiskBuildInfo.class); + ProjectFilesystem projectFilesystem = createMock(ProjectFilesystem.class); List<String> lines = ImmutableList.of( "com/example/Bar 087b7707a5f8e0a2adf5652e3cd2072d89a197dc", "com/example/Baz 62b1c2510840c0de55c13f66065a98a719be0f19", "com/example/Foo e4fccb7520b7795e632651323c63217c9f59f72a"); - expect(onDiskBuildInfo.getOutputFileContentsByLine(accumulateClassNames)).andReturn(lines); - expect(onDiskBuildInfo.getHash(AbiRule.ABI_KEY_ON_DISK_METADATA)) - .andReturn(Optional.of(new Sha1HashCode("f7d6d1efa11c8ceef36cc56b0ec6c3a20ddbf19f"))); + expect(projectFilesystem.readLines(Paths.get("buck-out/gen/foo/bar.classes.txt"))) + .andReturn(lines); + OnDiskBuildInfo onDiskBuildInfo = new FakeOnDiskBuildInfo(buildTarget, projectFilesystem) + .putMetadata(AbiRule.ABI_KEY_ON_DISK_METADATA, "f7d6d1efa11c8ceef36cc56b0ec6c3a20ddbf19f"); replayAll(); accumulateClassNames.initializeFromDisk(onDiskBuildInfo);
diff --git a/test/com/facebook/buck/rules/AbstractCachingBuildRuleTest.java b/test/com/facebook/buck/rules/AbstractCachingBuildRuleTest.java index 57f7ce4..fc17c2c 100644 --- a/test/com/facebook/buck/rules/AbstractCachingBuildRuleTest.java +++ b/test/com/facebook/buck/rules/AbstractCachingBuildRuleTest.java
@@ -165,8 +165,8 @@ expect(context.getProjectRoot()).andReturn(createMock(Path.class)); // Configure the OnDiskBuildInfo. - OnDiskBuildInfo onDiskBuildInfo = createMock(OnDiskBuildInfo.class); - expect(onDiskBuildInfo.getRuleKey()).andReturn(Optional.<RuleKey>absent()); + ProjectFilesystem projectFilesystem = createMock(ProjectFilesystem.class); + OnDiskBuildInfo onDiskBuildInfo = new FakeOnDiskBuildInfo(buildTarget, projectFilesystem); expect(context.createOnDiskBuildInfoFor(buildTarget)).andReturn(onDiskBuildInfo); // Configure the BuildInfoRecorder. @@ -248,19 +248,20 @@ /* ruleKeyWithoutDeps */ capture(new Capture<RuleKey>()))) .andReturn(buildInfoRecorder); + ProjectFilesystem projectFilesystem = createMock(ProjectFilesystem.class); // Populate the metadata that should be read from disk. - OnDiskBuildInfo onDiskBuildInfo = createMock(OnDiskBuildInfo.class); - // The RuleKey on disk should be different from the current RuleKey in memory, so reverse() it. - expect(onDiskBuildInfo.getRuleKey()).andReturn( - Optional.of(reverse(buildRule.getRuleKey()))); - // However, the RuleKey not including the deps in memory should be the same as the one on disk. - expect(onDiskBuildInfo.getRuleKeyWithoutDeps()).andReturn( - Optional.of(new RuleKey(TestAbstractCachingBuildRule.RULE_KEY_WITHOUT_DEPS_HASH))); - // Similarly, the ABI key for the deps in memory should be the same as the one on disk. - expect(onDiskBuildInfo.getHash(AbiRule.ABI_KEY_FOR_DEPS_ON_DISK_METADATA)).andReturn( - Optional.of(new Sha1HashCode(TestAbstractCachingBuildRule.ABI_KEY_FOR_DEPS_HASH))); - expect(onDiskBuildInfo.getValue(AbiRule.ABI_KEY_ON_DISK_METADATA)).andReturn( - Optional.of("At some point, this method call should go away.")); + OnDiskBuildInfo onDiskBuildInfo = new FakeOnDiskBuildInfo(buildTarget, projectFilesystem) + // The RuleKey on disk should be different from the current RuleKey in memory, so reverse() + // it. + .setRuleKey(reverse(buildRule.getRuleKey())) + // However, the RuleKey not including the deps in memory should be the same as the one on + // disk. + .setRuleKeyWithoutDeps(new RuleKey(TestAbstractCachingBuildRule.RULE_KEY_WITHOUT_DEPS_HASH)) + // Similarly, the ABI key for the deps in memory should be the same as the one on disk. + .putMetadata(AbiRule.ABI_KEY_FOR_DEPS_ON_DISK_METADATA, + TestAbstractCachingBuildRule.ABI_KEY_FOR_DEPS_HASH) + .putMetadata(AbiRule.ABI_KEY_ON_DISK_METADATA, + "At some point, this method call should go away."); // This metadata must be added to the buildInfoRecorder so that it is written as part of // writeMetadataToDisk().
diff --git a/test/com/facebook/buck/rules/BUCK b/test/com/facebook/buck/rules/BUCK index 5c72a7e..f8eefad 100644 --- a/test/com/facebook/buck/rules/BUCK +++ b/test/com/facebook/buck/rules/BUCK
@@ -7,6 +7,7 @@ 'FakeBuildableContext.java', 'FakeBuildRule.java', 'FakeBuildRuleParams.java', + 'FakeOnDiskBuildInfo.java', 'FakeRuleKeyBuilderFactory.java', 'FakeTestRule.java', 'NonCheckingBuildRuleFactoryParams.java',
diff --git a/test/com/facebook/buck/rules/FakeOnDiskBuildInfo.java b/test/com/facebook/buck/rules/FakeOnDiskBuildInfo.java new file mode 100644 index 0000000..99926ae --- /dev/null +++ b/test/com/facebook/buck/rules/FakeOnDiskBuildInfo.java
@@ -0,0 +1,70 @@ +/* + * Copyright 2013-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.rules; + +import com.facebook.buck.model.BuildTarget; +import com.facebook.buck.util.ProjectFilesystem; +import com.google.common.base.Optional; +import com.google.common.collect.Maps; + +import java.util.Map; + +import javax.annotation.Nullable; + +public class FakeOnDiskBuildInfo extends OnDiskBuildInfo { + + @Nullable private RuleKey ruleKey; + @Nullable private RuleKey ruleKeyWithoutDeps; + private Map<String, String> metadata = Maps.newHashMap(); + + public FakeOnDiskBuildInfo(BuildTarget target, ProjectFilesystem projectFilesystem) { + super(target, projectFilesystem); + } + + /** @return this */ + public FakeOnDiskBuildInfo setRuleKey(@Nullable RuleKey ruleKey) { + this.ruleKey = ruleKey; + return this; + } + + @Override + public Optional<RuleKey> getRuleKey() { + return Optional.fromNullable(ruleKey); + } + + /** @return this */ + public FakeOnDiskBuildInfo setRuleKeyWithoutDeps(@Nullable RuleKey ruleKeyWithoutDeps) { + this.ruleKeyWithoutDeps = ruleKeyWithoutDeps; + return this; + } + + @Override + public Optional<RuleKey> getRuleKeyWithoutDeps() { + return Optional.fromNullable(ruleKeyWithoutDeps); + } + + /** @return this */ + public FakeOnDiskBuildInfo putMetadata(String key, String value) { + this.metadata.put(key, value); + return this; + } + + @Override + public Optional<String> getValue(String key) { + return Optional.fromNullable(metadata.get(key)); + } +}