diff options
author | shedaniel <daniel@shedaniel.me> | 2021-10-26 20:08:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 13:08:17 +0100 |
commit | 5c190cc3ef99507bbb38525c0f6a24480a7ec65c (patch) | |
tree | 0fdfbd23d1f6e5d00e002cc25e5bb26fdad4e11e /src/test | |
parent | b7142ae468bf5b03a43ca366b5ffdccebd1f9330 (diff) | |
download | architectury-loom-5c190cc3ef99507bbb38525c0f6a24480a7ec65c.tar.gz architectury-loom-5c190cc3ef99507bbb38525c0f6a24480a7ec65c.tar.bz2 architectury-loom-5c190cc3ef99507bbb38525c0f6a24480a7ec65c.zip |
Use NIO instead of ZipUtil (#513)
* Use nio for zip utils
* Make tests work
* Please work
* Fix some issues with tests
* Fix more issues with tests
* NIOZipUtils -> ZipUtils
* Resolve Juuxel's reviews
* Use our own FS utils
* Improve error handling, add loom Pair
* Add Unit tests + fixes
Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/test')
4 files changed, 131 insertions, 8 deletions
diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/AccessWidenerTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/AccessWidenerTest.groovy index 6ffe74fb..87bd96a2 100644 --- a/src/test/groovy/net/fabricmc/loom/test/integration/AccessWidenerTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/integration/AccessWidenerTest.groovy @@ -25,7 +25,7 @@ package net.fabricmc.loom.test.integration import net.fabricmc.loom.test.util.GradleProjectTestTrait -import org.zeroturnaround.zip.ZipUtil +import net.fabricmc.loom.util.ZipUtils import spock.lang.Specification import spock.lang.Unroll @@ -54,7 +54,7 @@ class AccessWidenerTest extends Specification implements GradleProjectTestTrait def "transitive accesswidener (gradle #version)"() { setup: def gradle = gradleProject(project: "transitiveAccesswidener", version: version) - ZipUtil.pack(new File(gradle.projectDir, "dummyDependency"), new File(gradle.projectDir, "dummy.jar")) + ZipUtils.pack(new File(gradle.projectDir, "dummyDependency").toPath(), new File(gradle.projectDir, "dummy.jar").toPath()) when: def result = gradle.run(task: "build") diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/UnpickTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/UnpickTest.groovy index 2b285fb6..dbe37f99 100644 --- a/src/test/groovy/net/fabricmc/loom/test/integration/UnpickTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/integration/UnpickTest.groovy @@ -25,8 +25,7 @@ package net.fabricmc.loom.test.integration import net.fabricmc.loom.test.util.GradleProjectTestTrait - -import org.zeroturnaround.zip.ZipUtil +import net.fabricmc.loom.util.ZipUtils import spock.lang.Specification import java.nio.charset.StandardCharsets @@ -66,6 +65,6 @@ class UnpickTest extends Specification implements GradleProjectTestTrait { private static String getClassSource(GradleProject gradle, String classname, String mappings = MAPPINGS) { File sourcesJar = gradle.getGeneratedSources(mappings) - return new String(ZipUtil.unpackEntry(sourcesJar, classname), StandardCharsets.UTF_8) + return new String(ZipUtils.unpack(sourcesJar.toPath(), classname), StandardCharsets.UTF_8) } } diff --git a/src/test/groovy/net/fabricmc/loom/test/unit/ZipUtilsTest.groovy b/src/test/groovy/net/fabricmc/loom/test/unit/ZipUtilsTest.groovy new file mode 100644 index 00000000..921edff8 --- /dev/null +++ b/src/test/groovy/net/fabricmc/loom/test/unit/ZipUtilsTest.groovy @@ -0,0 +1,124 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2021 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.test.unit + +import net.fabricmc.loom.util.Pair +import net.fabricmc.loom.util.ZipUtils +import spock.lang.Specification + +import java.nio.charset.StandardCharsets +import java.nio.file.Files + +class ZipUtilsTest extends Specification { + def "pack"() { + given: + def dir = File.createTempDir() + def zip = File.createTempFile("loom-zip-test", ".zip").toPath() + new File(dir, "test.txt").text = "This is a test of packing" + + when: + ZipUtils.pack(dir.toPath(), zip) + + then: + Files.exists(zip) + ZipUtils.contains(zip, "test.txt") + !ZipUtils.contains(zip, "nope.txt") + new String( ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "This is a test of packing" + } + + def "transform string"() { + given: + def dir = File.createTempDir() + def zip = File.createTempFile("loom-zip-test", ".zip").toPath() + new File(dir, "test.txt").text = "This is a test of transforming" + + when: + ZipUtils.pack(dir.toPath(), zip) + def transformed = ZipUtils.transformString(zip, [ + new Pair<String, ZipUtils.UnsafeUnaryOperator<String>>("test.txt", new ZipUtils.UnsafeUnaryOperator<String>() { + @Override + String apply(String arg) throws IOException { + return arg.toUpperCase() + } + }) + ]) + + then: + transformed == 1 + ZipUtils.contains(zip, "test.txt") + new String( ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "THIS IS A TEST OF TRANSFORMING" + } + + def "replace string"() { + given: + def dir = File.createTempDir() + def zip = File.createTempFile("loom-zip-test", ".zip").toPath() + new File(dir, "test.txt").text = "This has not been replaced" + + when: + ZipUtils.pack(dir.toPath(), zip) + ZipUtils.replace(zip, "test.txt", "This has been replaced".bytes) + + then: + ZipUtils.contains(zip, "test.txt") + new String(ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "This has been replaced" + } + + def "add file"() { + given: + def dir = File.createTempDir() + def zip = File.createTempFile("loom-zip-test", ".zip").toPath() + new File(dir, "test.txt").text = "This is original" + + when: + ZipUtils.pack(dir.toPath(), zip) + ZipUtils.add(zip, "test2.txt", "This has been added".bytes) + + then: + ZipUtils.contains(zip, "test.txt") + ZipUtils.contains(zip, "test2.txt") + new String(ZipUtils.unpack(zip, "test.txt"), StandardCharsets.UTF_8) == "This is original" + new String(ZipUtils.unpack(zip, "test2.txt"), StandardCharsets.UTF_8) == "This has been added" + } + + def "unpack all"() { + given: + def input = File.createTempDir() + def output = File.createTempDir() + + def zip = File.createTempFile("loom-zip-test", ".zip").toPath() + new File(input, "test.txt").text = "This is a test of unpacking all" + + def outputFile = new File(output, "test.txt") + + when: + ZipUtils.pack(input.toPath(), zip) + ZipUtils.unpackAll(zip, output.toPath()) + + then: + outputFile.exists() + outputFile.text == "This is a test of unpacking all" + } +} diff --git a/src/test/groovy/net/fabricmc/loom/test/util/GradleProjectTestTrait.groovy b/src/test/groovy/net/fabricmc/loom/test/util/GradleProjectTestTrait.groovy index 1bb8b461..6380992e 100644 --- a/src/test/groovy/net/fabricmc/loom/test/util/GradleProjectTestTrait.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/util/GradleProjectTestTrait.groovy @@ -26,9 +26,9 @@ package net.fabricmc.loom.test.util import groovy.transform.Immutable import net.fabricmc.loom.test.LoomTestConstants +import net.fabricmc.loom.util.ZipUtils import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner -import org.zeroturnaround.zip.ZipUtil import spock.lang.Shared trait GradleProjectTestTrait { @@ -192,7 +192,7 @@ trait GradleProjectTestTrait { String getOutputZipEntry(String filename, String entryName) { def file = getOutputFile(filename) - def bytes = ZipUtil.unpackEntry(file, entryName) + def bytes = ZipUtils.unpackNullable(file.toPath(), entryName) if (bytes == null) { throw new FileNotFoundException("Could not find ${entryName} in ${entryName}") @@ -203,7 +203,7 @@ trait GradleProjectTestTrait { boolean hasOutputZipEntry(String filename, String entryName) { def file = getOutputFile(filename) - return ZipUtil.unpackEntry(file, entryName) != null + return ZipUtils.unpackNullable(file.toPath(), entryName) != null } File getGeneratedSources(String mappings) { |