[skip ci] add copyFileFromLocal

This commit is contained in:
ansgarz 2022-01-06 19:00:36 +01:00
parent 9581b9460c
commit adc12d85ba
4 changed files with 53 additions and 2 deletions

View file

@ -5,6 +5,7 @@ import org.domaindrivenarchitecture.provs.core.processors.ContainerStartMode
import org.domaindrivenarchitecture.provs.core.processors.ContainerUbuntuHostProcessor
import org.domaindrivenarchitecture.provs.core.processors.RemoteProcessor
import org.domaindrivenarchitecture.provs.core.tags.Api
import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.fileContent
import java.io.File
import java.net.InetAddress
@ -94,6 +95,13 @@ fun getResourceAsText(path: String): String {
}
internal fun getLocalFileContent(fullyQualifiedLocalFileName: String, sudo: Boolean = false): String {
val content = local().fileContent(fullyQualifiedLocalFileName, sudo)
check(content != null, { "Could not retrieve content from file: $fullyQualifiedLocalFileName" })
return content
}
/**
* Returns default local Prov instance.
*/

View file

@ -26,6 +26,25 @@ fun Prov.createFileFromResource(
}
/**
* Copies a file from the local environment to the running Prov instance.
* In case the running ProvInstance is also local, it would copy from local to local.
*/
fun Prov.copyFileFromLocal(
fullyQualifiedFilename: String,
fullyQualifiedLocalFilename: String,
posixFilePermission: String? = null,
sudo: Boolean = false
): ProvResult = def {
createFile(
fullyQualifiedFilename,
getLocalFileContent(fullyQualifiedLocalFilename),
posixFilePermission,
sudo
)
}
fun Prov.createFile(
fullyQualifiedFilename: String,
text: String?,
@ -150,6 +169,8 @@ fun Prov.insertTextInFile(file: String, textBehindWhichToInsert: Regex, textToIn
}
// ============================= folder operations ==========================
fun Prov.dirExists(dir: String, path: String? = null, sudo: Boolean = false): Boolean {
val effectivePath = if (path != null) path else
(if (dir.startsWith(File.separator)) File.separator else "~" + File.separator)

View file

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.io.File
import java.net.UnknownHostException
internal class UtilsKtTest {
@ -54,6 +55,12 @@ internal class UtilsKtTest {
}
}
@Test
fun getLocalFileContent_successful() {
val resourcesDirectory = File("src/test/resources").absolutePath
assertEquals("resource text\n", getLocalFileContent("$resourcesDirectory/resource-test"))
}
@Test
@Disabled // run manually after having updated user
fun test_remote() {

View file

@ -4,6 +4,7 @@ import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.io.File
internal class FilesystemKtTest {
@ -191,4 +192,18 @@ internal class FilesystemKtTest {
assertEquals("a\nbananas\nhi\nc\nd", res3)
assertTrue(res4.success)
}
@Test
@ContainerTest
fun copyFileFromLocal_successfully() {
// given
val resourcesDirectory = File("src/test/resources").absolutePath
// when
defaultTestContainer().copyFileFromLocal("copiedFileFromLocal", "$resourcesDirectory/resource-test")
// then
val content = defaultTestContainer().fileContent( "copiedFileFromLocal")
assertEquals("resource text\n", content)
}
}