On Tue, Jul 30, 2019 at 11:27:35PM -0400, Jeff King wrote:

> That would perhaps be clearer if the "hashmap" tool actually did the
> sorting itself (so we'd sort _just_ the iteration, not the whole
> output). Something like this, though I'm on the fence about whether it
> is worth it:
> [...]

And here it is for reference with the matching change in test-oidmap,
and the adjustments necessary for the test scripts (from master, not
from my earlier patch). I think I prefer the simpler "just sort it all"
version I posted with the commit message.

The post-image in t0016 may be a new low: a command substitution in a DQ
string fed to echo, itself inside a command substitution in a here-doc.
Yuck. :)

---
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index aaf17b0ddf..9f6901666e 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -2,6 +2,7 @@
 #include "git-compat-util.h"
 #include "hashmap.h"
 #include "strbuf.h"
+#include "string-list.h"
 
 struct test_entry
 {
@@ -221,10 +222,18 @@ int cmd__hashmap(int argc, const char **argv)
 
                } else if (!strcmp("iterate", cmd)) {
 
+                       struct string_list sorted = STRING_LIST_INIT_NODUP;
+                       struct string_list_item *item;
                        struct hashmap_iter iter;
                        hashmap_iter_init(&map, &iter);
-                       while ((entry = hashmap_iter_next(&iter)))
-                               printf("%s %s\n", entry->key, get_value(entry));
+                       while ((entry = hashmap_iter_next(&iter))) {
+                               item = string_list_append(&sorted, entry->key);
+                               item->util = (void *)get_value(entry);
+                       }
+                       string_list_sort(&sorted);
+                       for_each_string_list_item(item, &sorted)
+                               printf("%s %s\n", item->string, (const char 
*)item->util);
+                       string_list_clear(&sorted, 0);
 
                } else if (!strcmp("size", cmd)) {
 
diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
index 0acf99931e..5fd059fe90 100644
--- a/t/helper/test-oidmap.c
+++ b/t/helper/test-oidmap.c
@@ -94,10 +94,19 @@ int cmd__oidmap(int argc, const char **argv)
 
                } else if (!strcmp("iterate", cmd)) {
 
+                       struct string_list sorted = STRING_LIST_INIT_DUP;
+                       struct string_list_item *item;
                        struct oidmap_iter iter;
                        oidmap_iter_init(&map, &iter);
-                       while ((entry = oidmap_iter_next(&iter)))
-                               printf("%s %s\n", 
oid_to_hex(&entry->entry.oid), entry->name);
+                       while ((entry = oidmap_iter_next(&iter))) {
+                               item = string_list_append(&sorted,
+                                                         
oid_to_hex(&entry->entry.oid));
+                               item->util = entry->name;
+                       }
+                       string_list_sort(&sorted);
+                       for_each_string_list_item(item, &sorted)
+                               printf("%s %s\n", item->string, (const char 
*)item->util);
+                       string_list_clear(&sorted, 0);
 
                } else {
 
diff --git a/t/t0011-hashmap.sh b/t/t0011-hashmap.sh
index 9c96b3e3b1..1adcd7762d 100755
--- a/t/t0011-hashmap.sh
+++ b/t/t0011-hashmap.sh
@@ -177,9 +177,9 @@ put fooBarFrotz value3
 iterate" "NULL
 NULL
 NULL
-key2 value2
+fooBarFrotz value3
 key1 value1
-fooBarFrotz value3"
+key2 value2"
 
 '
 
@@ -192,8 +192,8 @@ iterate" "NULL
 NULL
 NULL
 fooBarFrotz value3
-key2 value2
-key1 value1" ignorecase
+key1 value1
+key2 value2" ignorecase
 
 '
 
diff --git a/t/t0016-oidmap.sh b/t/t0016-oidmap.sh
index bbe719e950..abeaa64159 100755
--- a/t/t0016-oidmap.sh
+++ b/t/t0016-oidmap.sh
@@ -93,9 +93,14 @@ put three 3
 iterate" "NULL
 NULL
 NULL
-$(git rev-parse two) 2
-$(git rev-parse one) 1
-$(git rev-parse three) 3"
+$(
+       # sort to avoid relying on exact oids
+       {
+               echo "$(git rev-parse one) 1"
+               echo "$(git rev-parse two) 2"
+               echo "$(git rev-parse three) 3"
+       } | sort
+)"
 
 '
 

Reply via email to