[v2,12/18] initscripts fkt: Ignore comments in readhash

Message ID 20240616160245.18865-13-jonatan.schlag@ipfire.org
State New
Headers
Series [v2,01/18] tests: Add bash lib |

Commit Message

Jonatan Schlag June 16, 2024, 4:02 p.m. UTC
  As '#Another Comment' is a valid key we test this change by checking if
the comments do not end up as keys in our array.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 src/initscripts/system/functions               | 5 +++++
 tests/src/initscripts/system/functions/data/1  | 3 +++
 tests/src/initscripts/system/functions/test.sh | 6 ++++++
 3 files changed, 14 insertions(+)
  

Patch

diff --git a/src/initscripts/system/functions b/src/initscripts/system/functions
index 3f01be9e0..6107463fc 100644
--- a/src/initscripts/system/functions
+++ b/src/initscripts/system/functions
@@ -906,6 +906,11 @@  readhash() {
 			continue
 		fi
 
+		# Skip Comments
+		if [[ ${line} =~ ^#.*$ ]]; then
+			continue
+		fi
+
 		local key="${line%=*}"
 		local val="${line#*=}"
 
diff --git a/tests/src/initscripts/system/functions/data/1 b/tests/src/initscripts/system/functions/data/1
index 8aca9422b..c75620b6b 100644
--- a/tests/src/initscripts/system/functions/data/1
+++ b/tests/src/initscripts/system/functions/data/1
@@ -5,6 +5,7 @@  GREEN_DRIVER=r8175
 RED_DEV=red0
 RED_MACADDR=00:c0:08:8a:a0:56
 RED_DRIVER=r8283
+# Another Comment
 BLUE_DEV='blue0 net0'
 BLUE_MACADDR=bc:30:7d:58:6b:e3
 BLUE_DRIVER=rt2800
@@ -15,3 +16,5 @@  RED_NETMASK=0.0.0.0
 RED_TYPE=PPPOE
 RED_NETADDRESS=0.0.0.0
 
+# Comment for testing  
+    # Comment for testing Comments with spaces before
diff --git a/tests/src/initscripts/system/functions/test.sh b/tests/src/initscripts/system/functions/test.sh
index 751be6884..915f098a0 100755
--- a/tests/src/initscripts/system/functions/test.sh
+++ b/tests/src/initscripts/system/functions/test.sh
@@ -15,5 +15,11 @@  readhash "CONFIG" "${SCRIPT_PATH}/data/1"
 test_value_in_array "CONFIG" "RED_DHCP_HOSTNAME" "ipfire"
 test_value_in_array "CONFIG" "BLUE_MACADDR" "bc:30:7d:58:6b:e3"
 
+# Test that comments are skipped
+# apparently the way we read the file strips the whitespace, so the key does not contain any whitespace either
+test_that_array_doesnt_have_key "CONFIG" "# Another Comment"
+test_that_array_doesnt_have_key "CONFIG" "# Comment for testing"
+test_that_array_doesnt_have_key "CONFIG" "# Comment for testing Comments with spaces before"
+
 test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1" readhash "CONFIG" "${SCRIPT_PATH}/data/1"
 test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2" readhash "CONFIG" "${SCRIPT_PATH}/data/1"