-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write to stdout about what we did to each file. Add more tests (#10)
* Write to stdout about what we did to each file. Add more tests * Nits to safeCopyTo
- Loading branch information
Showing
6 changed files
with
302 additions
and
22 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
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 |
---|---|---|
@@ -1,8 +1,69 @@ | ||
package kales.cli | ||
|
||
import com.github.ajalt.clikt.core.UsageError | ||
import java.io.File | ||
import java.io.InputStream | ||
import java.nio.charset.Charset | ||
|
||
/** Like list() but returns empty list instead of null */ | ||
fun File.safeListFiles(): List<File> { | ||
return listFiles()?.asList() ?: emptyList() | ||
} | ||
} | ||
|
||
/** Print messages about file changes when writing text to it */ | ||
fun File.safeWriteText(text: String, charset: Charset = Charsets.UTF_8) { | ||
if (exists()) { | ||
if (readText(charset) != text) { | ||
printConflict() | ||
// TODO add the option for the user to abort, overwrite or merge with current file | ||
throw UsageError("Failed to write file since it already exists with different contents") | ||
} else { | ||
// Files are identical | ||
printIdentical() | ||
} | ||
} else { | ||
printCreated() | ||
writeText(text, charset) | ||
} | ||
} | ||
|
||
fun File.printCreated() { | ||
printStatus("create") | ||
} | ||
|
||
fun File.printSkipped() { | ||
printStatus("skip") | ||
} | ||
|
||
fun File.printIdentical() { | ||
printStatus("identical") | ||
} | ||
|
||
fun File.printConflict() { | ||
printStatus("conflict") | ||
} | ||
|
||
fun File.printStatus(status: String) { | ||
val relativePath = relativePathToWorkingDir() | ||
println(" $status $relativePath") | ||
} | ||
|
||
/** Copy streams and close at the end */ | ||
fun InputStream.safeCopyTo(destination: File): Long { | ||
if (!destination.exists() || destination.length() == 0L) { | ||
destination.printCreated() | ||
} else { | ||
destination.printSkipped() | ||
} | ||
use { input -> | ||
destination.outputStream().use { output -> | ||
return input.copyTo(output) | ||
} | ||
} | ||
} | ||
|
||
/** Returns a String with the path from this File relative to the current working dir (user.dir) */ | ||
fun File.relativePathToWorkingDir(): String { | ||
val workingDir = File(System.getProperty("user.dir")) | ||
return workingDir.toPath().relativize(absoluteFile.toPath()).toString() | ||
} |
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,95 @@ | ||
package kales.cli | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.PrintStream | ||
|
||
class CliTest { | ||
@get:Rule val tempDir = TemporaryFolder() | ||
private val originalOut = System.out | ||
private val outContent = ByteArrayOutputStream() | ||
|
||
@Before fun setUp() { | ||
System.setOut(PrintStream(outContent)) | ||
} | ||
|
||
@After fun tearDown() { | ||
System.setOut(originalOut) | ||
} | ||
|
||
@Test fun `test kales new with absolute path`() { | ||
New().parse(arrayOf(tempDir.root.absolutePath, "com.example")) | ||
val stdOut = outContent.toString() | ||
val buildGradleRelativePath = File(tempDir.root, "build.gradle").relativePathToWorkingDir() | ||
val gradleWrapperPropsRelativePath = File(tempDir.root, | ||
"gradle/wrapper/gradle-wrapper.properties").relativePathToWorkingDir() | ||
val gradleWrapperJarRelativePath = File(tempDir.root, | ||
"gradle/wrapper/gradle-wrapper.jar").relativePathToWorkingDir() | ||
val gradlewRelativePath = File(tempDir.root, "gradlew").relativePathToWorkingDir() | ||
assertThat(stdOut).isEqualTo(""" | ||
create $buildGradleRelativePath | ||
create $gradleWrapperPropsRelativePath | ||
create $gradleWrapperJarRelativePath | ||
create $gradlewRelativePath | ||
New Kales project successfully initialized at '${tempDir.root.absolutePath}'. | ||
Happy coding! | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test fun `test kales new with relative path`() { | ||
try { | ||
val workingDir = File(System.getProperty("user.dir")) | ||
New().parse(arrayOf("blah", "com.example")) | ||
val stdOut = outContent.toString() | ||
assertThat(stdOut).isEqualTo(""" | ||
create blah/build.gradle | ||
create blah/gradle/wrapper/gradle-wrapper.properties | ||
create blah/gradle/wrapper/gradle-wrapper.jar | ||
create blah/gradlew | ||
New Kales project successfully initialized at '${workingDir.absolutePath}/blah'. | ||
Happy coding! | ||
""".trimIndent()) | ||
} finally { | ||
File("blah").deleteRecursively() | ||
} | ||
} | ||
|
||
@Test fun `test identical files`() { | ||
try { | ||
val workingDir = File(System.getProperty("user.dir")) | ||
New().parse(arrayOf("blah", "com.example")) | ||
New().parse(arrayOf("blah", "com.example")) | ||
val stdOut = outContent.toString() | ||
assertThat(stdOut).isEqualTo(""" | ||
create blah/build.gradle | ||
create blah/gradle/wrapper/gradle-wrapper.properties | ||
create blah/gradle/wrapper/gradle-wrapper.jar | ||
create blah/gradlew | ||
New Kales project successfully initialized at '${workingDir.absolutePath}/blah'. | ||
Happy coding! | ||
identical blah/build.gradle | ||
identical blah/gradle/wrapper/gradle-wrapper.properties | ||
skip blah/gradle/wrapper/gradle-wrapper.jar | ||
skip blah/gradlew | ||
New Kales project successfully initialized at '${workingDir.absolutePath}/blah'. | ||
Happy coding! | ||
""".trimIndent()) | ||
} finally { | ||
File("blah").deleteRecursively() | ||
} | ||
} | ||
} |
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,4 @@ | ||
#!/usr/bin/env sh | ||
|
||
cp kales /usr/local/bin | ||
cp ../kales-cli/build/libs/kales-cli-0.0.1-SNAPSHOT-all.jar /usr/local/lib/kales.jar |
Oops, something went wrong.