From 224daec894886921015dd87b6adf4cbe3c80eb0a Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 30 Sep 2017 23:04:45 -0700 Subject: [PATCH 1/2] If a file is modified in a way that changes what the output files are, the up-to-date checking fails. --- .../ImageGrinderPluginSizeDependentTest.java | 57 ++++++++++++++++ src/test/resources/refresh-large.svg | 68 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/test/java/com/diffplug/gradle/imagegrinder/ImageGrinderPluginSizeDependentTest.java create mode 100644 src/test/resources/refresh-large.svg diff --git a/src/test/java/com/diffplug/gradle/imagegrinder/ImageGrinderPluginSizeDependentTest.java b/src/test/java/com/diffplug/gradle/imagegrinder/ImageGrinderPluginSizeDependentTest.java new file mode 100644 index 0000000..fa27896 --- /dev/null +++ b/src/test/java/com/diffplug/gradle/imagegrinder/ImageGrinderPluginSizeDependentTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 DiffPlug + * + * 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.diffplug.gradle.imagegrinder; + +import org.gradle.testkit.runner.TaskOutcome; +import org.junit.Test; + +public class ImageGrinderPluginSizeDependentTest extends GradleHarness { + @Test + public void testUpToDate() throws Exception { + write("build.gradle", + "plugins {", + " id 'com.diffplug.gradle.image-grinder'", + "}", + "imageGrinder {", + " eclipseSvg {", + " srcDir = file('src')", + " dstDir = file('dst')", + " bumpThisNumberWhenTheGrinderChanges = 1", + " grinder { img ->", + " if (img.size().width() <= 16) {", + " // hiDpi for small images", + " img.render('.png')", + " img.render('@2x.png', 2)", + " } else {", + " // but not for big images", + " img.render('.png')", + " }", + " }", + " }", + "}"); + // this image is 16x16, so it should be rendered with HiDPI + write("src/refresh.svg", readTestResource("refresh.svg")); + runAndAssert(TaskOutcome.SUCCESS); + runAndAssert(TaskOutcome.UP_TO_DATE); + assertFolderContent("dst").containsExactly("refresh.png", "refresh@2x.png"); + + // this image is large, so it should only have a 1x rendering + write("src/refresh.svg", readTestResource("refresh-large.svg")); + runAndAssert(TaskOutcome.SUCCESS); + runAndAssert(TaskOutcome.UP_TO_DATE); + assertFolderContent("dst").containsExactly("refresh.png"); + } +} diff --git a/src/test/resources/refresh-large.svg b/src/test/resources/refresh-large.svg new file mode 100644 index 0000000..1bd2cea --- /dev/null +++ b/src/test/resources/refresh-large.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + + + + + + + + + + + From d4e2209577c077652239d9bfd84eab4ed5238946 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 30 Sep 2017 23:05:10 -0700 Subject: [PATCH 2/2] Fixes the bug exposed by the previous commit. --- .../gradle/imagegrinder/ImageGrinderTask.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diffplug/gradle/imagegrinder/ImageGrinderTask.java b/src/main/java/com/diffplug/gradle/imagegrinder/ImageGrinderTask.java index 605666d..283e69a 100644 --- a/src/main/java/com/diffplug/gradle/imagegrinder/ImageGrinderTask.java +++ b/src/main/java/com/diffplug/gradle/imagegrinder/ImageGrinderTask.java @@ -109,6 +109,9 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception { } inputs.outOfDate(outOfDate -> { logger.info("outOfDate: " + Subpath.subpath(srcDir, outOfDate.getFile())); + if (outOfDate.isModified()) { + remove(outOfDate.getFile()); + } workerExecutor.submit(ProcessFile.class, workerConfig -> { workerConfig.setIsolationMode(IsolationMode.NONE); workerConfig.setParams(SerializableRef.create(ImageGrinderTask.this), outOfDate.getFile()); @@ -116,14 +119,18 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception { }); inputs.removed(removed -> { logger.info("removed: " + Subpath.subpath(srcDir, removed.getFile())); - synchronized (map) { - map.removeAll(removed.getFile()).forEach(getProject()::delete); - } + remove(removed.getFile()); }); workerExecutor.await(); writeToCache(cache); } + private void remove(File srcFile) { + synchronized (map) { + map.removeAll(srcFile).forEach(getProject()::delete); + } + } + @Internal public boolean debug = false;