Skip to content

Commit

Permalink
Fix #64: Support reading values containing line separators
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 20, 2024
1 parent 4d7faa6 commit d72acf5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 14 deletions.
22 changes: 22 additions & 0 deletions .github/actions/compare/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: compare
description: Compare two strings
inputs:
a:
required: true
description: A
b:
required: true
description: B
runs:
using: composite
steps:
- run: 'echo $a > /tmp/a'
shell: bash
env:
a: ${{ toJSON(inputs.a) }}
- run: 'echo $b > /tmp/b'
shell: bash
env:
b: ${{ toJSON(inputs.b) }}
- run: diff /tmp/a /tmp/b
shell: bash
84 changes: 76 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,103 @@ jobs:
- uses: actions/checkout@v4

- uses: ./src/test/resources/
id: success
id: simple
with:
file: src/test/resources/test.properties
property: name
- run: '[[ "Darth Vader" == "${{ steps.success.outputs.value }}" ]]'
- uses: ./.github/actions/compare
with:
a: Darth Vader
b: ${{ steps.simple.outputs.value }}

- uses: ./src/test/resources/
id: multiline
with:
file: src/test/resources/test.properties
property: affiliation
- uses: ./.github/actions/compare
with:
a: |-
Galactic
Empire
b: ${{ steps.multiline.outputs.value }}
- uses: ./src/test/resources/
id: default
id: simple-default
with:
file: src/test/resources/test.properties
property: version
default: 1.0.0
- run: '[[ "1.0.0" == "${{ steps.default.outputs.value }}" ]]'
- uses: ./.github/actions/compare
with:
a: 1.0.0
b: ${{ steps.simple-default.outputs.value }}

- uses: ./src/test/resources/
id: multiline-default
with:
file: src/test/resources/test.properties
property: version
default: |-
1
2
3
- uses: ./.github/actions/compare
with:
a: |-
1
2
3
b: ${{ steps.multiline-default.outputs.value }}
- uses: ./src/test/resources/
id: unexisting-file
with:
file: src/test/resources/unexisting.properties
property: name
default: Anakin Skywalker
- run: '[[ "Anakin Skywalker" == "${{ steps.unexisting-file.outputs.value }}" ]]'
- uses: ./.github/actions/compare
with:
a: Anakin Skywalker
b: ${{ steps.unexisting-file.outputs.value }}

- uses: ./src/test/resources/
id: all
with:
file: src/test/resources/test.properties
all: true
- run: '[[ "Darth Vader" == "${{ steps.all.outputs.name }}" ]]'
- run: '[[ "Sith Lord" == "${{ steps.all.outputs.occupation }}" ]]'
- run: '[[ "Luke Skywalker" == "${{ steps.all.outputs.son }}" ]]'
- uses: ./.github/actions/compare
with:
a: Darth Vader
b: ${{ steps.all.outputs.name }}
- uses: ./.github/actions/compare
with:
a: Sith Lord
b: ${{ steps.all.outputs.occupation }}
- uses: ./.github/actions/compare
with:
a: |-
Galactic
Empire
b: ${{ steps.all.outputs.affiliation }}
- uses: ./.github/actions/compare
with:
a: Luke Skywalker
b: ${{ steps.all.outputs.son }}
- uses: ./.github/actions/compare
with:
a: 1
b: ${{ steps.all.outputs.read-java-properties-delimiter }}
- uses: ./.github/actions/compare
with:
a: 2
b: ${{ steps.all.outputs.read-java-properties-delimiter-x }}
- uses: ./.github/actions/compare
with:
a: |-
3
4
b: ${{ steps.all.outputs.read-java-properties-delimiter-x-x }}
- uses: ./src/test/resources/
id: failure
Expand All @@ -51,6 +118,7 @@ jobs:
property: name
continue-on-error: true
- run: '[[ "failure" == "${{ steps.failure.outcome }}" ]]'

actions-typing:
name: Validate actions typing
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package me.madhead.github.actions.rjp

import java.io.FileInputStream
import java.lang.System.getenv
import java.lang.System.lineSeparator
import java.util.Properties
import kotlin.io.path.Path
import kotlin.io.path.appendText
import kotlin.io.path.useLines

private val newLine = System.lineSeparator()

fun main(args: Array<String>) {
val file = args[0]
Expand All @@ -21,14 +23,19 @@ fun main(args: Array<String>) {
properties.load(fis)

if ("true".equals(all, ignoreCase = true)) {
properties.entries.forEach { (key, value) ->
githubOutput.appendText("$key=$value${lineSeparator()}")
properties.every { key, value ->
githubOutput.appendText(packKeyValue(key, value))
}
} else {
properties.getProperty(property)?.let { value ->
githubOutput.appendText("value=$value${lineSeparator()}")
githubOutput.appendText(packKeyValue("value", value))
} ?: run {
githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")}${lineSeparator()}")
githubOutput.appendText(
packKeyValue(
"value",
default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")
)
)
}
}
}
Expand All @@ -38,8 +45,31 @@ fun main(args: Array<String>) {
if ("true".equals(all, ignoreCase = true)) {
throw e
} else {
githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw e}${lineSeparator()}")
githubOutput.appendText(packKeyValue("value", default.takeIf { it.isNotEmpty() } ?: throw e))
}
}

println("DEBUG")
githubOutput.useLines {
it.forEach { println(it) }
}
println("DEBUG")
}

private fun Properties.every(action: (String, String) -> Unit) {
for (element in this) action(element.key.toString(), element.value.toString())
}

private fun packKeyValue(key: String, value: String): String {
if (value.isMultiline()) {
var delimiter = "read-java-properties-delimiter"
while (value.contains(delimiter)) {
delimiter += "-x"
}
return "$key<<$delimiter$newLine$value$newLine$delimiter$newLine"
} else {
return "$key=$value$newLine"
}
}

private fun String.isMultiline(): Boolean = this.split(Regex("^(.*)$", RegexOption.MULTILINE)).isNotEmpty()
4 changes: 4 additions & 0 deletions src/test/resources/test.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
name=Darth Vader
occupation=Sith Lord
affiliation=Galactic\nEmpire
son=Luke \
Skywalker
read-java-properties-delimiter=1
read-java-properties-delimiter-x=2
read-java-properties-delimiter-x-x=3\n4

0 comments on commit d72acf5

Please sign in to comment.