Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Test postinst scripts #1193

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Test postinst scripts #1193

wants to merge 5 commits into from

Conversation

jpnurmi
Copy link
Contributor

@jpnurmi jpnurmi commented Oct 18, 2022

Based on the Bach unit testing framework for Bash - https://bach.sh/

@jpnurmi jpnurmi marked this pull request as draft October 18, 2022 11:03
@jpnurmi
Copy link
Contributor Author

jpnurmi commented Oct 18, 2022

Apparently, this worked locally only because I happened to have /target lying around for testing purposes. It turns out that Bach is unable to intercept I/O redirection. :(

@chaifeng
Copy link

Apparently, this worked locally only because I happened to have /target lying around for testing purposes. It turns out that Bach is unable to intercept I/O redirection. :(

Hi @jpnurmi

Thank you for using Bach Testing Framework. It's pity that Bach Testing Framework canot intercept I/O redirector due to the limitation of Bash. But we can use tee command.

Apply this patch, and all tests work as expected.

diff --git a/snap/local/postinst.d/10_override_desktop_settings b/snap/local/postinst.d/10_override_desktop_settings
index a6f693bc..446987a9 100755
--- a/snap/local/postinst.d/10_override_desktop_settings
+++ b/snap/local/postinst.d/10_override_desktop_settings
@@ -12,7 +12,7 @@ fi
 
 dconf_dump() {
     target_schema=$target_schemas/20_ubuntu-desktop-installer-$1.gschema.override
-    sudo -u $user dconf dump /org/gnome/desktop/$1/ > $target_schema
+    sudo -u $user dconf dump /org/gnome/desktop/$1/ | tee $target_schema
     # - [foo] -> [org.gnome.desktop.$1.foo]
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.$1.\1\]/g" $target_schema
     # - [/] -> [org.gnome.desktop.$1]
diff --git a/snap/local/postinst.d/test/override_desktop_settings.test.sh b/snap/local/postinst.d/test/override_desktop_settings.test.sh
index 76dd604d..9deeaae0 100755
--- a/snap/local/postinst.d/test/override_desktop_settings.test.sh
+++ b/snap/local/postinst.d/test/override_desktop_settings.test.sh
@@ -8,6 +8,7 @@ test-override-desktop-settings() {
     @mocktrue test -d /usr/share/glib-2.0/schemas
     @mocktrue test -d /target//usr/share/glib-2.0/schemas
     @mocktrue id ubuntu
+    @mock tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
     @ignore sed
 
     @run "$src/10_override_desktop_settings"
@@ -18,15 +19,19 @@ test-override-desktop-settings-assert() {
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/interface/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/input-sources/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/peripherals/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/wm/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
 
     glib-compile-schemas /target//usr/share/glib-2.0/schemas

@chaifeng
Copy link

I recommend not ignoring the sed command in that test case

diff --git a/snap/local/postinst.d/10_override_desktop_settings b/snap/local/postinst.d/10_override_desktop_settings
index a6f693bc..446987a9 100755
--- a/snap/local/postinst.d/10_override_desktop_settings
+++ b/snap/local/postinst.d/10_override_desktop_settings
@@ -12,7 +12,7 @@ fi
 
 dconf_dump() {
     target_schema=$target_schemas/20_ubuntu-desktop-installer-$1.gschema.override
-    sudo -u $user dconf dump /org/gnome/desktop/$1/ > $target_schema
+    sudo -u $user dconf dump /org/gnome/desktop/$1/ | tee $target_schema
     # - [foo] -> [org.gnome.desktop.$1.foo]
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.$1.\1\]/g" $target_schema
     # - [/] -> [org.gnome.desktop.$1]
diff --git a/snap/local/postinst.d/test/override_desktop_settings.test.sh b/snap/local/postinst.d/test/override_desktop_settings.test.sh
index 76dd604d..88923fb3 100755
--- a/snap/local/postinst.d/test/override_desktop_settings.test.sh
+++ b/snap/local/postinst.d/test/override_desktop_settings.test.sh
@@ -8,25 +8,39 @@ test-override-desktop-settings() {
     @mocktrue test -d /usr/share/glib-2.0/schemas
     @mocktrue test -d /target//usr/share/glib-2.0/schemas
     @mocktrue id ubuntu
-    @ignore sed
+    @mock tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
 
     @run "$src/10_override_desktop_settings"
 }
 
 test-override-desktop-settings-assert() {
     sudo -u ubuntu dconf dump /org/gnome/desktop/a11y/
+    sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.a11y.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
+    sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.a11y\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/interface/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
+    sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.interface.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
+    sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.interface\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/input-sources/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
+    sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.input-sources.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
+    sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.input-sources\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/peripherals/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
+    sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.peripherals.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
+    sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.peripherals\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/wm/
+    tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
+    sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.wm.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
+    sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.wm\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
     test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
 
     glib-compile-schemas /target//usr/share/glib-2.0/schemas

@jpnurmi
Copy link
Contributor Author

jpnurmi commented Oct 29, 2022

Hi @chaifeng, thanks for chiming in and taking the time to come up with suggestions! We're definitely going to give it a try. 👍

@jpnurmi jpnurmi reopened this Oct 29, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 29, 2022

Everyone contributing to this PR have now signed the CLA. Thanks!

@jpnurmi
Copy link
Contributor Author

jpnurmi commented Oct 29, 2022

@chaifeng Thank you so much, the tests are now passing in the CI! Is there any chance you could sign the CLA so we'll be able to accept the patch? If so, you can put my name in the "Canonical contact" field.

@chaifeng
Copy link

@chaifeng Thank you so much, the tests are now passing in the CI! Is there any chance you could sign the CLA so we'll be able to accept the patch? If so, you can put my name in the "Canonical contact" field.

Done!

Thank you.

@chaifeng
Copy link

Hi @jpnurmi

Could you re-run the cla-check again? I just noticed that I used a different email address on launchpad.net

@jpnurmi
Copy link
Contributor Author

jpnurmi commented Oct 30, 2022

It might take a day or two to get accepted and synced across the systems. I'll check back later!

@chaifeng
Copy link

chaifeng commented Nov 1, 2022

Hi @jpnurmi

I just released a new version of Bach Testing Framework. It supports mocking the left square bracket command [. The [ command will keep the original behavior if we don't mock it. Check the related test cases tests/bach-testing-framework.test.sh#L1629

With this version, we don't need to replace [ -d ... ] with test -d ...

So I checkout snap/local/postinst.d/10_* to the commit 117e140785aded7b11f05ddeaf717160cc9b0a43 and fix the test case override desktop settings

I'm not sure the correct way to update this PR, so post the patch again. Sorry ;-P

diff --git a/snap/local/postinst.d/10_copy_bluetooth_config b/snap/local/postinst.d/10_copy_bluetooth_config
index 4b12a68b..fef1ba07 100755
--- a/snap/local/postinst.d/10_copy_bluetooth_config
+++ b/snap/local/postinst.d/10_copy_bluetooth_config
@@ -5,7 +5,7 @@ target=/target
 source_bluetooth=/var/lib/bluetooth
 target_bluetooth=$target/$source_bluetooth
 
-if test -d $source_bluetooth; then
+if [ -d $source_bluetooth ]; then
     rm -rf $target_bluetooth
     cp -a $source_bluetooth $target_bluetooth
 fi
diff --git a/snap/local/postinst.d/10_copy_network_config b/snap/local/postinst.d/10_copy_network_config
index be6ccd35..b22f1aca 100755
--- a/snap/local/postinst.d/10_copy_network_config
+++ b/snap/local/postinst.d/10_copy_network_config
@@ -5,6 +5,6 @@ target=/target
 source_nm=/etc/NetworkManager/system-connections
 target_nm=$target/$source_nm
 
-if test -d $source_nm && test -d $target_nm; then
+if [ -d $source_nm ] && [ -d $target_nm ]; then
     cp -anr $source_nm/. $target_nm
 fi
diff --git a/snap/local/postinst.d/10_override_desktop_settings b/snap/local/postinst.d/10_override_desktop_settings
index 446987a9..2c03712f 100755
--- a/snap/local/postinst.d/10_override_desktop_settings
+++ b/snap/local/postinst.d/10_override_desktop_settings
@@ -6,7 +6,7 @@ target=/target
 source_schemas=/usr/share/glib-2.0/schemas
 target_schemas=$target/$source_schemas
 
-if ! test -d $source_schemas || ! test -d $target_schemas || ! id $user >/dev/null 2>&1; then
+if [ ! -d $source_schemas ] || [ ! -d $target_schemas ] || ! id $user >/dev/null 2>&1; then
     exit
 fi
 
@@ -17,7 +17,7 @@ dconf_dump() {
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.$1.\1\]/g" $target_schema
     # - [/] -> [org.gnome.desktop.$1]
     sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.$1\]/g" $target_schema
-    test -s $target_schema || rm $target_schema
+    [ -s $target_schema ] || rm $target_schema
 }
 
 dconf_dump a11y
diff --git a/snap/local/postinst.d/test/bach.sh b/snap/local/postinst.d/test/bach.sh
index f9e2e20f..9f6abd0a 100644
--- a/snap/local/postinst.d/test/bach.sh
+++ b/snap/local/postinst.d/test/bach.sh
@@ -208,7 +208,7 @@ function bach-run-tests() {
         if [[ "$1" != -* ]] && bach--is-function "$1"; then
             "$@"
         else
-            mockfunc="$(@generate_mock_function_name command "$@")"
+            declare mockfunc="$(@generate_mock_function_name command "$@")"
             if bach--is-function "${mockfunc}"; then
                 @debug "[BC-func]" "${mockfunc}" "$@"
                 "${mockfunc}" "$@"
@@ -241,6 +241,17 @@ function bach-run-tests() {
     }
     export -f xargs
 
+    function [() {
+        declare mockfunc="$(@generate_mock_function_name '[' "$@")"
+        if bach--is-function "${mockfunc}"; then
+            @debug "[LSB-func]" "${mockfunc}" "$@"
+            "${mockfunc}" "$@"
+        else
+            builtin '[' "$@"
+        fi
+    }
+    export -f '['
+
     if [[ "${BACH_ASSERT_IGNORE_COMMENT}" == true ]]; then
         BACH_ASSERT_DIFF_OPTS+=(-I "^${__bach_run_test__ignore_prefix}")
     fi
@@ -395,7 +406,7 @@ SCRIPT
         for (( mockfunc__SEQ=1; mockfunc__SEQ <= ${BACH_MOCK_FUNCTION_MAX_COUNT:-0}; ++mockfunc__SEQ )); do
             bach--is-function "${mockfunc}_${mockfunc__SEQ}" || break
         done
-        body="${mockfunc}_${mockfunc__SEQ}() {
+        body="function ${mockfunc}_${mockfunc__SEQ}() {
             # @mock ${name} ${cmd[@]} ===
             $func
         }; export -f ${mockfunc}_${mockfunc__SEQ}"
diff --git a/snap/local/postinst.d/test/copy_bluetooth_config.test.sh b/snap/local/postinst.d/test/copy_bluetooth_config.test.sh
index 63042851..c2bef459 100755
--- a/snap/local/postinst.d/test/copy_bluetooth_config.test.sh
+++ b/snap/local/postinst.d/test/copy_bluetooth_config.test.sh
@@ -5,7 +5,7 @@ source "bach.sh"
 src=$(realpath "$(dirname $0)/..")
 
 test-copy-bluetooth-config() {
-    @mocktrue test -d /var/lib/bluetooth
+    @mocktrue [ -d /var/lib/bluetooth ]
 
     @run "$src/10_copy_bluetooth_config"
 }
@@ -16,7 +16,7 @@ test-copy-bluetooth-config-assert() {
 }
 
 test-missing-bluetooth-config() {
-    @mockfalse test -d /var/lib/bluetooth
+    @mockfalse [ -d /var/lib/bluetooth ]
 
     @run "$src/10_copy_bluetooth_config"
 }
diff --git a/snap/local/postinst.d/test/copy_network_config.test.sh b/snap/local/postinst.d/test/copy_network_config.test.sh
index d5835d2e..5bba339c 100755
--- a/snap/local/postinst.d/test/copy_network_config.test.sh
+++ b/snap/local/postinst.d/test/copy_network_config.test.sh
@@ -5,8 +5,8 @@ source "bach.sh"
 src=$(realpath "$(dirname $0)/..")
 
 test-copy-network-config() {
-    @mocktrue test -d /etc/NetworkManager/system-connections
-    @mocktrue test -d /target//etc/NetworkManager/system-connections
+    @mocktrue [ -d /etc/NetworkManager/system-connections ]
+    @mocktrue [ -d /target//etc/NetworkManager/system-connections ]
 
     @run "$src/10_copy_network_config"
 }
@@ -16,8 +16,8 @@ test-copy-network-config-assert() {
 }
 
 test-missing-network-source-config() {
-    @mockfalse test -d /etc/NetworkManager/system-connections
-    @mocktrue test -d /target//etc/NetworkManager/system-connections
+    @mockfalse [ -d /etc/NetworkManager/system-connections ]
+    @mocktrue [ -d /target//etc/NetworkManager/system-connections ]
 
     @run "$src/10_copy_network_config"
 }
@@ -27,8 +27,8 @@ test-missing-network-source-config-assert() {
 }
 
 test-missing-network-target-config() {
-    @mocktrue test -d /etc/NetworkManager/system-connections
-    @mockfalse test -d /target//etc/NetworkManager/system-connections
+    @mocktrue [ -d /etc/NetworkManager/system-connections ]
+    @mockfalse [ -d /target//etc/NetworkManager/system-connections ]
 
     @run "$src/10_copy_network_config"
 }
diff --git a/snap/local/postinst.d/test/override_desktop_settings.test.sh b/snap/local/postinst.d/test/override_desktop_settings.test.sh
index 88923fb3..edad093d 100755
--- a/snap/local/postinst.d/test/override_desktop_settings.test.sh
+++ b/snap/local/postinst.d/test/override_desktop_settings.test.sh
@@ -5,11 +5,18 @@ source "bach.sh"
 src=$(realpath "$(dirname $0)/..")
 
 test-override-desktop-settings() {
-    @mocktrue test -d /usr/share/glib-2.0/schemas
-    @mocktrue test -d /target//usr/share/glib-2.0/schemas
+    @mockfalse [ ! -d /usr/share/glib-2.0/schemas ]
+    @mockfalse [ ! -d /target//usr/share/glib-2.0/schemas ]
     @mocktrue id ubuntu
     @mock tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
 
+    @mockfalse [ -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override ]
+    @mockfalse [ -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override ]
+    @mockfalse [ -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override ]
+    @mockfalse [ -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override ]
+
+    @mocktrue [ -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override ]
+
     @run "$src/10_override_desktop_settings"
 }
 
@@ -17,38 +24,38 @@ test-override-desktop-settings-assert() {
     sudo -u ubuntu dconf dump /org/gnome/desktop/a11y/
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.a11y.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
     sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.a11y\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
-    test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
+    rm /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-a11y.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/interface/
     tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.interface.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
     sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.interface\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
-    test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
+    rm /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-interface.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/input-sources/
     tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.input-sources.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
     sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.input-sources\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
-    test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
+    rm /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-input-sources.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/peripherals/
     tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.peripherals.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
     sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.peripherals\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
-    test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
+    rm /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-peripherals.gschema.override
 
     sudo -u ubuntu dconf dump /org/gnome/desktop/wm/
     tee /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
     sed -i -E "s/^\[([^/]*)\]/\[org.gnome.desktop.wm.\1\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
     sed -i -E "s/^\[\/\]$/\[org.gnome.desktop.wm\]/g" /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
-    test -s /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
+    # rm /target//usr/share/glib-2.0/schemas/20_ubuntu-desktop-installer-wm.gschema.override
 
     glib-compile-schemas /target//usr/share/glib-2.0/schemas
 }
 
 test-missing-source-schemas() {
-    @mockfalse test -d /usr/share/glib-2.0/schemas
-    @mocktrue test -d /target//usr/share/glib-2.0/schemas
+    @mockfalse [ -d /usr/share/glib-2.0/schemas ]
+    @mocktrue [ -d /target//usr/share/glib-2.0/schemas ]
     @mocktrue id ubuntu
 
     @run "$src/10_override_desktop_settings"
@@ -59,8 +66,8 @@ test-missing-source-schemas-assert() {
 }
 
 test-missing-target-schemas() {
-    @mocktrue test -d /usr/share/glib-2.0/schemas
-    @mockfalse test -d /target//usr/share/glib-2.0/schemas
+    @mocktrue [ -d /usr/share/glib-2.0/schemas ]
+    @mockfalse [ -d /target//usr/share/glib-2.0/schemas ]
     @mocktrue id ubuntu
 
     @run "$src/10_override_desktop_settings"

The latest Bach can mock `[`.
@jpnurmi jpnurmi marked this pull request as ready for review November 14, 2022 14:03
@jpnurmi
Copy link
Contributor Author

jpnurmi commented Nov 14, 2022

Thank you, @chaifeng! This is getting better and better. :)

I recommend not ignoring the sed command in that test case

I was originally hoping to:

  • mock dconf dump stdout to test with different input values
  • ignore detailed sed/regexp steps in between
  • verify that correctly transformed output values were written to the appropriate files

Do you think something like that would be doable?

@chaifeng
Copy link

chaifeng commented Nov 16, 2022

Hi @jpnurmi

Yes, it's doable, but test cases may not be stable. They may fail due to permission, network, file system, different versions of commands, etc. In this case, the sed command is going to modify a file that has an absolute path. It will definitely fail if the absolute path doesn't exist or we don't have permission to change it.

Without considering this absolute path problem, we can use a real sed command, for example:

test-sed() {
    function sed() { @real sed "$@"; }
    @mock dconf dump foobar === @stdout '[foobar]'

    dconf dump foobar | sed -E "s/^\[([^/]*)\]/\[org.gnome.desktop.FOOBAR.\1\]/g"
}

test-sed-assert() {
    @echo "[org.gnome.desktop.FOOBAR.foobar]"
}

In order to get the correct output values that will be written to the final files, there are two key inputs. One is the output of dconf dump, the another is the regular expression of the sed command.

For the output of dconf dump, we can assume it's stable. It's easy to notice it if there was a significant change to it.

For the sed regular expressions. If we manually test a regular expression from the command line, it works. It should also work in a script file. we don't have to verify if the same regular expression produces the same output every time.

If a command with specified parameters and options works on the command line, then it should work in the script as well. So from my point of view that the key point in writing script tests is should verify the behavior of our scripts. What is the command sequence in the test case? What parameters and options are passed to each command? etc.

Thank you.

@jpnurmi jpnurmi marked this pull request as draft February 10, 2023 10:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants