-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved most of the code into sbtassembly.scalasig package.
Added test for EntryTable
- Loading branch information
1 parent
de199d3
commit e843a4f
Showing
8 changed files
with
103 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...g/pantsbuild/jarjar/ByteArrayReader.scala → ...btassembly/scalasig/ByteArrayReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ain/scala/org/pantsbuild/jarjar/Nat.scala → ...main/scala/sbtassembly/scalasig/Nat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package sbtassembly.scalasig | ||
|
||
import org.scalatest.OptionValues | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.reflect.ScalaSignature | ||
import scala.reflect.internal.pickling.ByteCodecs | ||
|
||
class Test | ||
|
||
abstract class Test2 { | ||
val test: Test | ||
} | ||
|
||
class EntryTableSpec extends AnyWordSpec with Matchers with OptionValues { | ||
|
||
"entry table" should { | ||
"parse annotation bytes" in { | ||
val encoded = classOf[Test2].getAnnotation(classOf[ScalaSignature]) | ||
val bytes = encoded.bytes().getBytes("UTF-8") | ||
|
||
val len = ByteCodecs.decode(bytes) | ||
val table = EntryTable.fromBytes(bytes.slice(0, len)) | ||
|
||
resolveName(table, "Test") shouldBe Some("sbtassembly.scalasig.Test") | ||
} | ||
|
||
"rename package" in { | ||
val encoded = classOf[Test2].getAnnotation(classOf[ScalaSignature]) | ||
val bytes = encoded.bytes().getBytes("UTF-8") | ||
|
||
val len = ByteCodecs.decode(bytes) | ||
val table = EntryTable.fromBytes(bytes.slice(0, len)) | ||
|
||
table.renameEntries { | ||
case pckg if pckg.startsWith("sbtassembly.scalasig") => Some("shaded.sbtassembly.scalasig") | ||
case _ => None | ||
} | ||
|
||
resolveName(table, "Test") shouldBe Some("shaded.sbtassembly.scalasig.Test") | ||
|
||
table.renameEntries { | ||
case pckg if pckg.startsWith("shaded.sbtassembly.scalasig") => Some("sbtassembly.scalasig.shadedtoo") | ||
case _ => None | ||
} | ||
|
||
resolveName(table, "Test") shouldBe Some("sbtassembly.scalasig.shadedtoo.Test") | ||
} | ||
|
||
"return same serialized bytes" in { | ||
val encoded = classOf[Test2].getAnnotation(classOf[ScalaSignature]) | ||
val bytes = encoded.bytes().getBytes("UTF-8") | ||
|
||
val len = ByteCodecs.decode(bytes) | ||
val decoded = bytes.slice(0, len) | ||
val table = EntryTable.fromBytes(decoded) | ||
|
||
val serialized = table.toBytes | ||
|
||
serialized.length shouldBe decoded.length +- 1 // Scala compiler (sometimes) adds an extra zero. We don't. | ||
EntryTable.fromBytes(serialized).toSeq shouldBe table.toSeq | ||
} | ||
} | ||
|
||
private def resolveName(entryTable: EntryTable, name: String): Option[String] = { | ||
val entries = entryTable.toSeq.zipWithIndex | ||
def findNameIndex: Option[Int] = entries.collectFirst { | ||
case (e: NameEntry, index) if e.name == name => index | ||
} | ||
|
||
def findRefEntry(nameIndex: Int): Option[RefEntry] = entries.collectFirst { | ||
case (e: RefEntry, _) if e.nameRef == nameIndex => e | ||
} | ||
|
||
for { | ||
index <- findNameIndex | ||
ref <- findRefEntry(index) | ||
resolved <- entryTable.resolveRef(ref) | ||
} yield resolved | ||
} | ||
} |