From adc12d85ba6ea1906622f07df2a0f59071005260 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Thu, 6 Jan 2022 19:00:36 +0100 Subject: [PATCH] [skip ci] add copyFileFromLocal --- .../provs/core/Utils.kt | 8 +++++++ .../ubuntu/filesystem/base/Filesystem.kt | 21 +++++++++++++++++++ .../provs/core/UtilsKtTest.kt | 7 +++++++ .../filesystem/base/FilesystemKtTest.kt | 19 +++++++++++++++-- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt index dd80e23..5c7e174 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt @@ -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. */ diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/Filesystem.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/Filesystem.kt index c20f57d..ca30a1d 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/Filesystem.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/Filesystem.kt @@ -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) diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt index 6afa4ee..698086e 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt @@ -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() { diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/FilesystemKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/FilesystemKtTest.kt index 3c78530..e38e4a1 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/FilesystemKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/filesystem/base/FilesystemKtTest.kt @@ -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 { @@ -139,7 +140,7 @@ internal class FilesystemKtTest { // when val file = "replaceTest" 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 res4 = prov.deleteFile(file) @@ -167,7 +168,7 @@ internal class FilesystemKtTest { // then assertTrue(res1.success) assertTrue(res2.success) - assertEquals("a\nhi\nho\nc\nd",res3) + assertEquals("a\nhi\nho\nc\nd", res3) assertTrue(res4.success) } @@ -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) + } } \ No newline at end of file