Skip to content

Commit

Permalink
⚡ (JVM): Improve the file forgery factory
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Dec 4, 2019
1 parent 67ee112 commit 6db9a4a
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#### `core`

- Add the nullable value forgeries

#### `jvm`

- Improve the File forgery factory

### 1.0.0-beta2 (2019/12/02)

Expand Down
2 changes: 1 addition & 1 deletion jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ dependencies {
testCompile(project(":junit5"))
}

commonConfig(0.85)
commonConfig(0.95)
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ class FileForgeryFactory :
return if (osName.contains("win")) {
aWindowsPath(forge)
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
aLinuxPath(forge)
aUnixPath(forge, LINUX_ROOTS)
} else if (osName.contains("mac")) {
aMacOsPath(forge)
aUnixPath(forge, MAC_ROOTS)
} else {
throw IllegalStateException("Unsupported OS path format for “$osName")
}
}

private fun aLinuxPath(forge: Forge): String {
private fun aUnixPath(forge: Forge, roots: List<String>): String {
val isAbsolute = forge.aBool()
val ancestorRoot = Array(forge.aTinyInt()) { ".." }.joinToString(UNIX_SEP) { it }
val roots = if (isAbsolute) LINUX_ROOTS else listOf(".", "..", ancestorRoot)
return aPath(
forge = forge,
separator = UNIX_SEP,
roots = roots,
roots = if (isAbsolute) roots else listOf(".", "..", ancestorRoot),
forbiddenChars = UNIX_FORBIDDEN_CHARS
)
}
Expand All @@ -54,18 +53,6 @@ class FileForgeryFactory :
)
}

private fun aMacOsPath(forge: Forge): String {
val isAbsolute = forge.aBool()
val ancestorRoot = Array(forge.aTinyInt()) { ".." }.joinToString(UNIX_SEP) { it }
val roots = if (isAbsolute) MAC_ROOTS else listOf(".", "..", ancestorRoot)
return aPath(
forge = forge,
separator = UNIX_SEP,
roots = roots,
forbiddenChars = UNIX_FORBIDDEN_CHARS
)
}

private fun aPath(
forge: Forge,
separator: String = File.separator,
Expand All @@ -76,11 +63,9 @@ class FileForgeryFactory :
val builder = StringBuilder()
var segments = 0

if (roots.isNotEmpty()) {
builder.append(forge.anElementFrom(roots))
.append(separator)
segments++
}
builder.append(forge.anElementFrom(roots))
.append(separator)
segments++

val isFile = forge.aBool()
val fileSize = if (isFile) forge.anInt(3, MAX_FILENAME_SIZE) else 0
Expand Down Expand Up @@ -137,15 +122,16 @@ class FileForgeryFactory :
private const val MAX_PATH_SIZE = 1024
private const val MAX_FILENAME_SIZE = 128

private val LINUX_ROOTS = listOf("", "/bin", "/boot", "/dev", "/dev/null", "/etc", "/home", "/lib", "/media",
internal val LINUX_ROOTS = listOf("/bin", "/boot", "/dev", "/dev/null", "/etc", "/home", "/lib", "/media",
"/mnt", "/opt", "/sbin", "/srv", "/tmp", "/usr", "/usr/bin", "/usr/lib", "/usr/share",
"/usr/local", "/usr/local/bin", "/var", "/var/lib", "/var/log", "/root", "/sys")
private val MAC_ROOTS = listOf("", "/Applications", "/Developer", "/Library", "/Network", "/System", "/Users",
internal val MAC_ROOTS = listOf("/Applications", "/Developer", "/Library", "/Network", "/System", "/Users",
"/Volumes", "/bin", "/dev", "/dev/null", "/etc", "/sbin", "/tmp", "/usr", "/usr/bin",
"/usr/lib", "/usr/share", "/usr/local", "/usr/local/bin", "/var", "/var/lib", "/var/log")
private val WINDOWS_ROOTS = listOf("A:", "C:", "D:", "C:\\Program Files", "C:\\Program Files (x86)",
"C:\\Program Files\\Common Files", "C:\\ProgramData", "C:\\Users", "C:\\Users\\Public", "C:\\Documents and Settings",
"C:\\Windows", "C:\\Windows\\System32")
internal val WINDOWS_ROOTS = listOf("C:", "C:\\Program Files", "C:\\Program Files (x86)",
"C:\\Program Files\\Common Files", "C:\\ProgramData", "C:\\Users", "C:\\Users\\Public",
"C:\\Documents and Settings", "C:\\Windows", "C:\\Windows\\System32",
"A:", "B:", "D:", "E:", "F:", "H:", "L:", "M:", "N:", "O:", "P:", "Q:", "U:", "Z:")

private const val UNIX_SEP = "/"
private const val WINDOWS_SEP = "\\"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package fr.xgouchet.elmyr.jvm.factories

import fr.xgouchet.elmyr.Forge
import fr.xgouchet.elmyr.annotation.Forgery
import fr.xgouchet.elmyr.junit5.ForgeConfiguration
import fr.xgouchet.elmyr.junit5.ForgeExtension
import fr.xgouchet.elmyr.jvm.JvmConfigurator
import java.io.File
import java.lang.IllegalStateException
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assumptions.assumeTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(ForgeExtension::class)
@ForgeConfiguration(JvmConfigurator::class)
class FileForgeryFactoryTest {

@Test
fun `factory fails on unknown OSs`(forge: Forge) {
val factory = FileForgeryFactory()

testFakingOsName("something") {
assertThrows<IllegalStateException> {
factory.getForgery(forge)
}
}
}

@Test
fun `forges different values`(
@Forgery file1: File,
Expand All @@ -21,4 +36,129 @@ class FileForgeryFactoryTest {
assertThat(file1.path)
.isNotEqualTo(file2.path)
}

@Test
fun `forge many Files`(@Forgery files: List<File>) {
// assuming we're on unix
assumeTrue(File.separatorChar == '/')
assumeTrue(files.size > 1)

val absolute = files.count { it.path.startsWith("/") }
val groups = files.groupBy { file ->
file.path.split(File.separatorChar).first { it.isNotEmpty() }
}

val atleastTwoGroups = groups.size > 1
val atLeastTwoTypes = (absolute > 0) && (absolute < files.size)

assertThat(atleastTwoGroups || atLeastTwoTypes)
}

@Test
fun `forge many Files (Linux)`(forge: Forge) {
// assuming we're on unix
assumeTrue(File.separatorChar == '/')

testFakingOsName("Linux") {
val files = forge.aList(64) { getForgery<File>() }

files.forEach {
if (it.path.startsWith('/')) {
val rootFolder = "/${it.path.split('/')[1]}"
assertThat(rootFolder)
.overridingErrorMessage(
"Expecting root folder $rootFolder to be a known Mac root folder"
)
.isIn(FileForgeryFactory.LINUX_ROOTS)
}
}
}
}

@Test
fun `forge many Files (Unix)`(forge: Forge) {
// assuming we're on unix
assumeTrue(File.separatorChar == '/')

testFakingOsName("Unix") {
val files = forge.aList(64) { getForgery<File>() }

files.forEach {
if (it.path.startsWith('/')) {
val rootFolder = "/${it.path.split('/')[1]}"
assertThat(rootFolder)
.overridingErrorMessage(
"Expecting root folder $rootFolder to be a known Mac root folder"
)
.isIn(FileForgeryFactory.LINUX_ROOTS)
}
}
}
}

@Test
fun `forge many Files (AIX)`(forge: Forge) {
// assuming we're on unix
assumeTrue(File.separatorChar == '/')

testFakingOsName("AIX") {
val files = forge.aList(64) { getForgery<File>() }

files.forEach {
if (it.path.startsWith('/')) {
val rootFolder = "/${it.path.split('/')[1]}"
assertThat(rootFolder)
.overridingErrorMessage(
"Expecting root folder $rootFolder to be a known Mac root folder"
)
.isIn(FileForgeryFactory.LINUX_ROOTS)
}
}
}
}

@Test
fun `forge many Files (Mac OS X)`(forge: Forge) {
// assuming we're on unix
assumeTrue(File.separatorChar == '/')

testFakingOsName("Mac OS X") {
val files = forge.aList(64) { getForgery<File>() }

files.forEach {
if (it.path.startsWith('/')) {
val rootFolder = "/${it.path.split('/')[1]}"
assertThat(rootFolder)
.overridingErrorMessage(
"Expecting root folder $rootFolder to be a known Mac root folder"
)
.isIn(FileForgeryFactory.MAC_ROOTS)
}
}
}
}

@Test
fun `forge many Files (Windows)`(forge: Forge) {
// assuming we're on unix
assumeTrue(File.separatorChar == '/')

testFakingOsName("Windows 10") {
val files = forge.aList(64) { getForgery<File>() }

files.forEach {
if (!it.path.startsWith('.')) {
assertThat(it.path)
.matches("[A-Z]:\\\\.*")
}
}
}
}

private fun testFakingOsName(osName: String, test: () -> Unit) {
val realOsName = System.getProperty("os.name")
System.setProperty("os.name", osName)
test()
System.setProperty("os.name", realOsName)
}
}

0 comments on commit 6db9a4a

Please sign in to comment.