[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.ContainerUbuntuHostProcessor
import org.domaindrivenarchitecture.provs.core.processors.RemoteProcessor import org.domaindrivenarchitecture.provs.core.processors.RemoteProcessor
import org.domaindrivenarchitecture.provs.core.tags.Api import org.domaindrivenarchitecture.provs.core.tags.Api
import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.fileContent
import java.io.File import java.io.File
import java.net.InetAddress 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. * 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( fun Prov.createFile(
fullyQualifiedFilename: String, fullyQualifiedFilename: String,
text: 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 { fun Prov.dirExists(dir: String, path: String? = null, sudo: Boolean = false): Boolean {
val effectivePath = if (path != null) path else val effectivePath = if (path != null) path else
(if (dir.startsWith(File.separator)) File.separator else "~" + File.separator) (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.Disabled
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import java.io.File
import java.net.UnknownHostException import java.net.UnknownHostException
internal class UtilsKtTest { 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 @Test
@Disabled // run manually after having updated user @Disabled // run manually after having updated user
fun test_remote() { fun test_remote() {

View file

@ -4,6 +4,7 @@ import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.io.File
internal class FilesystemKtTest { internal class FilesystemKtTest {
@ -139,7 +140,7 @@ internal class FilesystemKtTest {
// when // when
val file = "replaceTest" val file = "replaceTest"
val res1 = prov.createFile(file, "a\nb\nc\nd") val res1 = prov.createFile(file, "a\nb\nc\nd")
val res2 = prov.replaceTextInFile(file,"b", "hi\nho") val res2 = prov.replaceTextInFile(file, "b", "hi\nho")
val res3 = prov.fileContent(file).equals("a\nhi\nho\nc\nd") val res3 = prov.fileContent(file).equals("a\nhi\nho\nc\nd")
val res4 = prov.deleteFile(file) val res4 = prov.deleteFile(file)
@ -167,7 +168,7 @@ internal class FilesystemKtTest {
// then // then
assertTrue(res1.success) assertTrue(res1.success)
assertTrue(res2.success) assertTrue(res2.success)
assertEquals("a\nhi\nho\nc\nd",res3) assertEquals("a\nhi\nho\nc\nd", res3)
assertTrue(res4.success) assertTrue(res4.success)
} }
@ -191,4 +192,18 @@ internal class FilesystemKtTest {
assertEquals("a\nbananas\nhi\nc\nd", res3) assertEquals("a\nbananas\nhi\nc\nd", res3)
assertTrue(res4.success) 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)
}
} }