awk incompatibility in tests
From Patrick DELAUNAY patrick.delaunay@foss.st.com
For information, it seems the new test "test_env_text" failed when the gawk is not installed on Ubuntu distribution,
or when /usr/bin/mawk is used as alternative (sudo update-alternatives --config awk)
The test result is
test/py/tests/test_env.py:556: in test_env_text check_script('''fred=123 test/py/tests/test_env.py:542: in check_script assert result == expect E assert '#define CONF...red=123\0"\n' == '#define CONF...nie=456\0"\n' E - #define CONFIG_EXTRA_ENV_TEXT "ernie=456\0fred=123\0" E ? ---------- E + #define CONFIG_EXTRA_ENV_TEXT "fred=123\0ernie=456\0" E ? ++++++++++ ------------------------------------------------------------ Captured stdout call ------------------------------------------------------------- +awk -f /u-boot/scripts/env2string.awk /tmp/tmp0zgiwrd9/infile #define CONFIG_EXTRA_ENV_TEXT "fred=123\0" +awk -f /u-boot/scripts/env2string.awk /tmp/tmpq6xej0ct/infile +awk -f /u-boot/scripts/env2string.awk /tmp/tmpyrn10apn/infile #define CONFIG_EXTRA_ENV_TEXT "ernie=456\0fred=123\0"
=> the env variables are sorted in alphabetic order in mawk output / in creation order in gawk ouput
I don't found solution to be POSIX compliant and to guarantee the output order
By default, when a for loop traverses an array, the order is undefined,
meaning that the awk implementation determines the order in which
the array is traversed. This order is usually based on the internal
implementation of arrays and will vary from one version of awk to the next.
References:
https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html
https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html ...
+END {
# Record the value of the variable now completed. If the variable is
# empty it is not set.
if (length(env) != 0) {
vars[var] = env
}
if (length(vars) != 0) {
printf("%s", "#define CONFIG_EXTRA_ENV_TEXT \"")
# Print out all the variables
ORDER is not guarantee here by awk for the loop on the "vars" array Here according the awk implementation the test result can be
'fred=123\0ernie=456\0' => creation order
'ernie=456\0fred=123\0' => alphabetic order
and perhaps other order for other awk ?
do you think you can have a solution with POSIX awk ? today I found only solution for gawk:
---------------------------- scripts/env2string.awk ---------------------------- index 1bfe9ed07a..f3215c369a 100644 @@ -81,7 +81,8 @@ END { if (do_output) { printf("%s", "#define CONFIG_EXTRA_ENV_TEXT "")
-
# Print out all the variables
-
# Print out all the variables by alphabetic order
-
PROCINFO["sorted_in"] = "@ind_str_asc" for (var in vars) { env = vars[var] print var "=" vars[var] "\\0"
But this GNU feature must be avoid, see commit 7acb3225 ("env: Avoid using GNU features in awk")
Regard