Skip to content

Commit

Permalink
Add test for nested dir and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ity committed Jul 18, 2018
1 parent 1b8d8b8 commit f7a8e6f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
28 changes: 15 additions & 13 deletions src/scala/org/pantsbuild/zinc/compiler/OutputUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ object OutputUtils {
* @param dir File handle containing the contents to sort
* @return sorted set of all paths within the `dir`
*/
def sort(dir:File): mutable.TreeSet[Path] = {
val sorted = new mutable.TreeSet[Path]()
def sort(dir:File): mutable.TreeSet[(Path, Boolean)] = {
val sorted = new mutable.TreeSet[(Path, Boolean)]()

val fileSortVisitor = new SimpleFileVisitor[Path]() {
override def preVisitDirectory(path: Path, attrs: BasicFileAttributes): FileVisitResult = {
sorted.add(path)
sorted.add(path, false)
FileVisitResult.CONTINUE
}

override def visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult = {
sorted.add(path)
sorted.add(path, true)
FileVisitResult.CONTINUE
}
}
Expand All @@ -39,7 +39,7 @@ object OutputUtils {
}

def relativize(base: String, path: Path): String = {
new File(base).toURI().relativize(new File(path.toString).toURI()).getPath()
new File(base.toString).toURI().relativize(new File(path.toString).toURI()).getPath()
}

/**
Expand All @@ -50,23 +50,25 @@ object OutputUtils {
* @param jarEntryTime time to be set for each JAR entry
*/
def createJar(
base: String, filePaths: mutable.TreeSet[Path], outputJarPath: Path, jarEntryTime: Long) {
base: String, filePaths: mutable.TreeSet[(Path, Boolean)], outputJarPath: Path, jarEntryTime: Long) {

val target = new JarOutputStream(Files.newOutputStream(outputJarPath))

def addToJar(source: Path, entryName: String): FileVisitResult = {
def addToJar(source: (Path, Boolean), entryName: String): FileVisitResult = {
val jarEntry = new JarEntry(entryName)
// setting jarEntry time to a fixed value for all entries within the jar so that jars are
// byte-for-byte reproducible.
jarEntry.setTime(jarEntryTime)

target.putNextEntry(jarEntry)
Files.copy(source, target)
if (source._2) {
Files.copy(source._1, target)
}
target.closeEntry()
FileVisitResult.CONTINUE
}

val pathToName = filePaths.zipWithIndex.map{case(k, v) => (k, relativize(base, k))}.toMap
val pathToName = filePaths.zipWithIndex.map{case(k, v) => (k, relativize(base, k._1))}.toMap
pathToName.map(e => addToJar(e._1, e._2))
target.close()
}
Expand All @@ -84,13 +86,13 @@ object OutputUtils {
}

/**
* Determines if a Class exists in a JAR provided.
* Determines if a file exists in a JAR provided.
*
* @param jarPath Absolute Path to the JAR being inspected
* @param clazz Name of the Class, the existence of which is to be inspected
* @param fileName Name of the file, the existence of which is to be inspected
* @return
*/
def existsClass(jarPath: Path, clazz: String): Boolean = {
def existsClass(jarPath: Path, fileName: String): Boolean = {
var jis: JarInputStream = null
var found = false
try {
Expand All @@ -100,7 +102,7 @@ object OutputUtils {
def findClass(entry: JarEntry): Boolean = entry match {
case null =>
false
case entry if entry.getName == clazz =>
case entry if entry.getName == fileName =>
true
case _ =>
findClass(jis.getNextJarEntry)
Expand Down
2 changes: 1 addition & 1 deletion tests/scala/org/pantsbuild/zinc/compiler/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

junit_tests(
Expand Down
30 changes: 23 additions & 7 deletions tests/scala/org/pantsbuild/zinc/compiler/JarCreationSpec.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.pantsbuild.zinc.compiler

import java.io.File
import java.nio.file.{Path, Paths}
import scala.collection.mutable

import sbt.io.IO

import java.io.File
import java.nio.file.{Files, Path, Paths}
import scala.collection.mutable
import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.junit.JUnitRunner
Expand All @@ -16,7 +15,7 @@ class JarCreationSpec extends WordSpec with MustMatchers {
"JarCreationWithoutClasses" should {
"succeed when input classes are not provided" in {
IO.withTemporaryDirectory { tempInputDir =>
val filePaths = new mutable.TreeSet[Path]()
val filePaths = new mutable.TreeSet[(Path, Boolean)]()

IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-empty-output.jar")
Expand All @@ -31,14 +30,31 @@ class JarCreationSpec extends WordSpec with MustMatchers {
"succeed when input classes are provided" in {
IO.withTemporaryDirectory { tempInputDir =>
val tempFile = File.createTempFile("Temp", ".class", tempInputDir)
val filePaths = mutable.TreeSet(tempFile.toPath)
val filePaths = mutable.TreeSet((tempFile.toPath, true))

IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-valid-output.jar")

OutputUtils.createJar(tempInputDir.toString, filePaths, jarOutputPath, System.currentTimeMillis())
OutputUtils.existsClass(jarOutputPath, tempFile.toString) must be(false)
OutputUtils.existsClass(jarOutputPath, tempFile.getName) must be(true)
OutputUtils.existsClass(jarOutputPath, OutputUtils.relativize(tempInputDir.toString, tempFile.toPath)) must be(true)
}
}
}
}

"JarCreationWithNestedClasses" should {
"succeed when nested input directory and classes are provided" in {
IO.withTemporaryDirectory { tempInputDir =>
val nestedTempDir = Files.createTempDirectory(tempInputDir.toPath, "tmp")
val nestedTempClass = File.createTempFile("NestedTemp", ".class", nestedTempDir.toFile)
val filePaths = mutable.TreeSet((nestedTempDir, false), (nestedTempClass.toPath, true))
IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-valid-output.jar")

OutputUtils.createJar(tempInputDir.toString, filePaths, jarOutputPath, System.currentTimeMillis())
OutputUtils.existsClass(jarOutputPath, OutputUtils.relativize(tempInputDir.toString, nestedTempDir)) must be(true)
OutputUtils.existsClass(jarOutputPath, OutputUtils.relativize(tempInputDir.toString, nestedTempClass.toPath)) must be(true)
}
}
}
Expand Down

0 comments on commit f7a8e6f

Please sign in to comment.